annotate source/encoder/ratecontrol.cpp @ 12695:f34b58f2f3de draft

Add real-time VBV fullness based QP tuning in VBV 2 pass This commit enables real-time VBV fullness based 2nd pass QP tuning. Experimental feature.Default disabled
author Kirithika <kirithika@multicorewareinc.com>
date Wed, 09 Sep 2020 21:45:55 +0530
parents f1c17b7ffb79
children 49a6e30c692e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
1 /*****************************************************************************
12596
2e84224f404a copyright: Update copyright in license header
Pradeep Ramachandran <pradeep@multicorewareinc.com>
parents: 12588
diff changeset
2 * Copyright (C) 2013-2020 MulticoreWare, Inc
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
3 *
3453
d1d0d90ec2f1 add rate control parameters to x265_param_t
sumalatha
parents: 3398
diff changeset
4 * Authors: Sumalatha Polureddy <sumalatha@multicorewareinc.com>
d1d0d90ec2f1 add rate control parameters to x265_param_t
sumalatha
parents: 3398
diff changeset
5 * Aarthi Priya Thirumalai <aarthi@multicorewareinc.com>
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
6 * Xun Xu, PPLive Corporation <xunxu@pptv.com>
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
7 *
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
11 * (at your option) any later version.
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
12 *
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
16 * GNU General Public License for more details.
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
17 *
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
21 *
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
22 * This program is also available under a commercial proprietary license.
6953
9c21227bb5bf change license email globally
Steve Borho <steve@borho.org>
parents: 6949
diff changeset
23 * For more information, contact us at license @ x265.com.
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
24 *****************************************************************************/
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
25
11233
5276fbdce4bd ratecontrol: silence warning
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 11216
diff changeset
26 #if _MSC_VER
5276fbdce4bd ratecontrol: silence warning
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 11216
diff changeset
27 #pragma warning(disable: 4127) // conditional expression is constant, yes I know
5276fbdce4bd ratecontrol: silence warning
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 11216
diff changeset
28 #endif
5276fbdce4bd ratecontrol: silence warning
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 11216
diff changeset
29
7154
6a11b4e683e1 replace TComPic with Frame class in common/frame.h
Steve Borho <steve@borho.org>
parents: 7134
diff changeset
30 #include "common.h"
6a11b4e683e1 replace TComPic with Frame class in common/frame.h
Steve Borho <steve@borho.org>
parents: 7134
diff changeset
31 #include "param.h"
6a11b4e683e1 replace TComPic with Frame class in common/frame.h
Steve Borho <steve@borho.org>
parents: 7134
diff changeset
32 #include "frame.h"
8711
fd95b6a592ee fix some header include loops
Steve Borho <steve@borho.org>
parents: 8613
diff changeset
33 #include "framedata.h"
8475
f3ede27baeee pull TComPicSym into common/ as FrameData
Steve Borho <steve@borho.org>
parents: 8474
diff changeset
34 #include "picyuv.h"
7154
6a11b4e683e1 replace TComPic with Frame class in common/frame.h
Steve Borho <steve@borho.org>
parents: 7134
diff changeset
35
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
36 #include "encoder.h"
3919
a027cdf5a6e6 ratecontrol: avoid lookahead overhead for CQP
Steve Borho <steve@borho.org>
parents: 3918
diff changeset
37 #include "slicetype.h"
3662
55d7edaa3e8e ratecontol: fix case sensitive includes
Steve Borho <steve@borho.org>
parents: 3656
diff changeset
38 #include "ratecontrol.h"
7083
9d43b41eb529 sei: first step towards simplifying SEI writing
Steve Borho <steve@borho.org>
parents: 7081
diff changeset
39 #include "sei.h"
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
40
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
41 #define BR_SHIFT 6
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
42 #define CPB_SHIFT 4
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
43
10588
52ca6e909590 cmake: introduce EXPORT_C_API build variable
Steve Borho <steve@borho.org>
parents: 10305
diff changeset
44 using namespace X265_NS;
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
45
6939
7cbe7e7a75c4 ratecontrol: improve visual quality and bitrate savings in ABR.
Aarthi Thirumalai
parents: 6904
diff changeset
46 /* Amortize the partial cost of I frames over the next N frames */
8909
11342c8376dd rc: limit bit amortization in ABR to longer sequences
Aarthi Thirumalai
parents: 8908
diff changeset
47
8234
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
48 const int RateControl::s_slidingWindowFrames = 20;
7126
ed85651a2840 rc: add 2 pass states in RateControl
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7096
diff changeset
49 const char *RateControl::s_defaultStatFileName = "x265_2pass.log";
6939
7cbe7e7a75c4 ratecontrol: improve visual quality and bitrate savings in ABR.
Aarthi Thirumalai
parents: 6904
diff changeset
50
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
51 namespace {
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
52 #define CMP_OPT_FIRST_PASS(opt, param_val)\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
53 {\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
54 bErr = 0;\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
55 p = strstr(opts, opt "=");\
12588
f0fe46ce379d Fix pass 2 encode failure (Issue #524)
Niranjan <niranjan@multicorewareinc.com>
parents: 12584
diff changeset
56 char* q = strstr(opts, "no-" opt " ");\
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
57 if (p && sscanf(p, opt "=%d" , &i) && param_val != i)\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
58 bErr = 1;\
8062
e6a80fb007e8 rc: bug fix for 2 pass when bframes = 0. fixes Issue #77
Aarthi Thirumalai
parents: 7923
diff changeset
59 else if (!param_val && !q && !p)\
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
60 bErr = 1;\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
61 else if (param_val && (q || !strstr(opts, opt)))\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
62 bErr = 1;\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
63 if (bErr)\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
64 {\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
65 x265_log(m_param, X265_LOG_ERROR, "different " opt " setting than first pass (%d vs %d)\n", param_val, i);\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
66 return false;\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
67 }\
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
68 }
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
69
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
70 inline int calcScale(uint32_t x)
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
71 {
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
72 static uint8_t lut[16] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
73 int y, z = (((x & 0xffff) - 1) >> 27) & 16;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
74 x >>= z;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
75 z += y = (((x & 0xff) - 1) >> 28) & 8;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
76 x >>= y;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
77 z += y = (((x & 0xf) - 1) >> 29) & 4;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
78 x >>= y;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
79 return z + lut[x&0xf];
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
80 }
6939
7cbe7e7a75c4 ratecontrol: improve visual quality and bitrate savings in ABR.
Aarthi Thirumalai
parents: 6904
diff changeset
81
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
82 inline int calcLength(uint32_t x)
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
83 {
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
84 static uint8_t lut[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
85 int y, z = (((x >> 16) - 1) >> 27) & 16;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
86 x >>= z ^ 16;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
87 z += y = ((x - 0x100) >> 28) & 8;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
88 x >>= y ^ 8;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
89 z += y = ((x - 0x10) >> 29) & 4;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
90 x >>= y ^ 4;
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
91 return z + lut[x];
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
92 }
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
93
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
94 inline char *strcatFilename(const char *input, const char *suffix)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
95 {
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
96 char *output = X265_MALLOC(char, strlen(input) + strlen(suffix) + 1);
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
97 if (!output)
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
98 {
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
99 x265_log(NULL, X265_LOG_ERROR, "unable to allocate memory for filename\n");
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
100 return NULL;
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
101 }
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
102 strcpy(output, input);
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
103 strcat(output, suffix);
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
104 return output;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
105 }
7545
f54d5368590a rc: nits
Steve Borho <steve@borho.org>
parents: 7544
diff changeset
106
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
107 inline double qScale2bits(RateControlEntry *rce, double qScale)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
108 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
109 if (qScale < 0.1)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
110 qScale = 0.1;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
111 return (rce->coeffBits + .1) * pow(rce->qScale / qScale, 1.1)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
112 + rce->mvBits * pow(X265_MAX(rce->qScale, 1) / X265_MAX(qScale, 1), 0.5)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
113 + rce->miscBits;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
114 }
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
115
7493
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
116 inline void copyRceData(RateControlEntry* rce, RateControlEntry* rce2Pass)
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
117 {
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
118 rce->coeffBits = rce2Pass->coeffBits;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
119 rce->mvBits = rce2Pass->mvBits;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
120 rce->miscBits = rce2Pass->miscBits;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
121 rce->iCuCount = rce2Pass->iCuCount;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
122 rce->pCuCount = rce2Pass->pCuCount;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
123 rce->skipCuCount = rce2Pass->skipCuCount;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
124 rce->keptAsRef = rce2Pass->keptAsRef;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
125 rce->qScale = rce2Pass->qScale;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
126 rce->newQScale = rce2Pass->newQScale;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
127 rce->expectedBits = rce2Pass->expectedBits;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
128 rce->expectedVbv = rce2Pass->expectedVbv;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
129 rce->blurredComplexity = rce2Pass->blurredComplexity;
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
130 rce->sliceType = rce2Pass->sliceType;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
131 rce->qpNoVbv = rce2Pass->qpNoVbv;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
132 rce->newQp = rce2Pass->newQp;
11216
9c797a591581 rc: fix rate factor values recorded in csv
Aarthi Priya Thirumalai <aarthi@multicorewareinc.com>
parents: 11215
diff changeset
133 rce->qRceq = rce2Pass->qRceq;
7493
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
134 }
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
135
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
136 } // end anonymous namespace
9191
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
137 /* Returns the zone for the current frame */
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
138 x265_zone* RateControl::getZone()
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
139 {
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
140 for (int i = m_param->rc.zoneCount - 1; i >= 0; i--)
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
141 {
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
142 x265_zone *z = &m_param->rc.zones[i];
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
143 if (m_framesDone + 1 >= z->startFrame && m_framesDone < z->endFrame)
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
144 return z;
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
145 }
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
146 return NULL;
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
147 }
92fb9eb8d1eb Support for tweaking rate control using zones
Adam Marcus <adampetermarcus@gmail.com>
parents: 9150
diff changeset
148
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
149 RateControl::RateControl(x265_param& p, Encoder *top)
3398
4085c992877a uncrustify, code cleanup , continue porting x264 rate control to x265.
Aarthi<aarthi@multicorewareinc.com>
parents: 3391
diff changeset
150 {
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
151 m_param = &p;
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
152 m_top = top;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
153 int lowresCuWidth = ((m_param->sourceWidth / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
7511
b85dbec30cc5 rc: bug fixes in 2 pass ABR
Aarthi Thirumalai
parents: 7510
diff changeset
154 int lowresCuHeight = ((m_param->sourceHeight / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
155 m_ncu = lowresCuWidth * lowresCuHeight;
6994
9f44990be4d4 rc: nit
Steve Borho <steve@borho.org>
parents: 6993
diff changeset
156
12454
3cd0b5ed0b91 new aq implementation
Ashok Kumar Mishra <ashok@multicorewareinc.com>
parents: 12445
diff changeset
157 m_qCompress = (m_param->rc.cuTree && !m_param->rc.hevcAq) ? 1 : m_param->rc.qCompress;
5440
9cfe20b782da cuTree: integrated CuTree into RateControl and Added b-references into RC
Gopu Govindaswamy <gopu@multicorewareinc.com>
parents: 5424
diff changeset
158
6424
0bd2465e3d4a Replaced Encoder* with x265_param* as member of several classes
David T Yuen <dtyx265@gmail.com>
parents: 6410
diff changeset
159 // validate for param->rc, maybe it is need to add a function like x265_parameters_valiate()
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
160 m_zoneBufferIdx = 0;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
161 m_residualFrames = 0;
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
162 m_partialResidualFrames = 0;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
163 m_residualCost = 0;
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
164 m_partialResidualCost = 0;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
165 m_rateFactorMaxIncrement = 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
166 m_rateFactorMaxDecrement = 0;
9438
2e25084cd441 rc: m_fps is double, do not use integer division
Steve Borho <steve@borho.org>
parents: 9437
diff changeset
167 m_fps = (double)m_param->fpsNum / m_param->fpsDenom;
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
168 m_startEndOrder.set(0);
7497
a2fd8a71de61 rc: prevent deadlock on encoder flush or close
Steve Borho <steve@borho.org>
parents: 7496
diff changeset
169 m_bTerminated = false;
a2fd8a71de61 rc: prevent deadlock on encoder flush or close
Steve Borho <steve@borho.org>
parents: 7496
diff changeset
170 m_finalFrameCount = 0;
7880
f0de8275ed4d rc: write I/i slice in stats file based on whether openGop is enabled or not.
Aarthi Thirumalai
parents: 7760
diff changeset
171 m_numEntries = 0;
10913
923bfe2a098b rc: adjust qp for scene transitions and fade ins to avoid quality loss with vbv
Aarthi Thirumalai
parents: 10850
diff changeset
172 m_isSceneTransition = false;
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
173 m_lastPredictorReset = 0;
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
174 m_avgPFrameQp = 0;
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
175 m_isFirstMiniGop = false;
12583
04db2bfee5d6 Enable Boundary Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12581
diff changeset
176 m_lastScenecut = -1;
04db2bfee5d6 Enable Boundary Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12581
diff changeset
177 m_lastScenecutAwareIFrame = -1;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
178 if (m_param->rc.rateControlMode == X265_RC_CRF)
4820
c51c35880df5 rc: add CRF ratecontrol
idxa<idxa@sina.com>
parents: 4817
diff changeset
179 {
7081
3a19a9fdb103 ratecontrol: [CHANGES OUTPUT for 10-bit CRF] Remove QP_BD_OFFSET from Ratecontrol
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 7080
diff changeset
180 m_param->rc.qp = (int)m_param->rc.rfConstant;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
181 m_param->rc.bitrate = 0;
4820
c51c35880df5 rc: add CRF ratecontrol
idxa<idxa@sina.com>
parents: 4817
diff changeset
182
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
183 double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
184 double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
185 m_rateFactorConstant = pow(baseCplx, 1 - m_qCompress) /
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
186 x265_qp2qScale(m_param->rc.rfConstant + mbtree_offset);
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
187 if (m_param->rc.rfConstantMax)
6238
fea3b6fa376b vbv: implement row wise vbvRateControl at each row diagonal
Aarthi Thirumalai
parents: 6237
diff changeset
188 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
189 m_rateFactorMaxIncrement = m_param->rc.rfConstantMax - m_param->rc.rfConstant;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
190 if (m_rateFactorMaxIncrement <= 0)
6238
fea3b6fa376b vbv: implement row wise vbvRateControl at each row diagonal
Aarthi Thirumalai
parents: 6237
diff changeset
191 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
192 x265_log(m_param, X265_LOG_WARNING, "CRF max must be greater than CRF\n");
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
193 m_rateFactorMaxIncrement = 0;
6238
fea3b6fa376b vbv: implement row wise vbvRateControl at each row diagonal
Aarthi Thirumalai
parents: 6237
diff changeset
194 }
fea3b6fa376b vbv: implement row wise vbvRateControl at each row diagonal
Aarthi Thirumalai
parents: 6237
diff changeset
195 }
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
196 if (m_param->rc.rfConstantMin)
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
197 m_rateFactorMaxDecrement = m_param->rc.rfConstant - m_param->rc.rfConstantMin;
4820
c51c35880df5 rc: add CRF ratecontrol
idxa<idxa@sina.com>
parents: 4817
diff changeset
198 }
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
199 m_isAbr = m_param->rc.rateControlMode != X265_RC_CQP && !m_param->rc.bStatRead;
11298
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
200 m_2pass = m_param->rc.rateControlMode != X265_RC_CQP && m_param->rc.bStatRead;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
201 m_bitrate = m_param->rc.bitrate * 1000;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
202 m_frameDuration = (double)m_param->fpsDenom / m_param->fpsNum;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
203 m_qp = m_param->rc.qp;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
204 m_lastRceq = 1; /* handles the cmplxrsum when the previous frame cost is zero */
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
205 m_shortTermCplxSum = 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
206 m_shortTermCplxCount = 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
207 m_lastNonBPictType = I_SLICE;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
208 m_isAbrReset = false;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
209 m_lastAbrResetPoc = -1;
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
210 m_statFileOut = NULL;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
211 m_cutreeStatFileOut = m_cutreeStatFileIn = NULL;
7496
3801142d080d rc: fix warnings in ratecontrol
Aarthi Thirumalai
parents: 7495
diff changeset
212 m_rce2Pass = NULL;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
213 m_encOrder = NULL;
9435
d7bf99b426e9 rc: fix to avoid false abr restarts
Aarthi Thirumalai
parents: 9397
diff changeset
214 m_lastBsliceSatdCost = 0;
11118
61396ea8096a rc: Remove the redundant calculation on slidingWindow
Ximing Cheng <ximingcheng@tencent.com>
parents: 11098
diff changeset
215 m_movingAvgSum = 0.0;
11430
b280e0c3ed87 rc: add option for encoding next gops
Divya Manivannan <divya@multicorewareinc.com>
parents: 11395
diff changeset
216 m_isNextGop = false;
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
217 m_relativeComplexity = NULL;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
218
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
219 // vbv initialization
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
220 m_param->rc.vbvBufferSize = x265_clip3(0, 2000000, m_param->rc.vbvBufferSize);
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
221 m_param->rc.vbvMaxBitrate = x265_clip3(0, 2000000, m_param->rc.vbvMaxBitrate);
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
222 m_param->rc.vbvBufferInit = x265_clip3(0.0, 2000000.0, m_param->rc.vbvBufferInit);
11900
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
223 m_param->vbvBufferEnd = x265_clip3(0.0, 2000000.0, m_param->vbvBufferEnd);
11958
2e684b80d90f rc: Fix inconsistency in --const-vbv (issue #381)
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11900
diff changeset
224 m_initVbv = false;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
225 m_singleFrameVbv = 0;
9083
ac94f35f4505 x265: remove rateTolerance from cli/param interface
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 9082
diff changeset
226 m_rateTolerance = 1.0;
ac94f35f4505 x265: remove rateTolerance from cli/param interface
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 9082
diff changeset
227
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
228 if (m_param->rc.vbvBufferSize)
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
229 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
230 if (m_param->rc.rateControlMode == X265_RC_CQP)
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
231 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
232 x265_log(m_param, X265_LOG_WARNING, "VBV is incompatible with constant QP, ignored.\n");
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
233 m_param->rc.vbvBufferSize = 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
234 m_param->rc.vbvMaxBitrate = 0;
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
235 }
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
236 else if (m_param->rc.vbvMaxBitrate == 0)
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
237 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
238 if (m_param->rc.rateControlMode == X265_RC_ABR)
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
239 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
240 x265_log(m_param, X265_LOG_WARNING, "VBV maxrate unspecified, assuming CBR\n");
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
241 m_param->rc.vbvMaxBitrate = m_param->rc.bitrate;
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
242 }
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
243 else
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
244 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
245 x265_log(m_param, X265_LOG_WARNING, "VBV bufsize set but maxrate unspecified, ignored\n");
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
246 m_param->rc.vbvBufferSize = 0;
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
247 }
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
248 }
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
249 else if (m_param->rc.vbvMaxBitrate < m_param->rc.bitrate &&
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
250 m_param->rc.rateControlMode == X265_RC_ABR)
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
251 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
252 x265_log(m_param, X265_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n");
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
253 m_param->rc.bitrate = m_param->rc.vbvMaxBitrate;
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
254 }
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
255 }
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
256 else if (m_param->rc.vbvMaxBitrate)
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
257 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
258 x265_log(m_param, X265_LOG_WARNING, "VBV maxrate specified, but no bufsize, ignored\n");
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
259 m_param->rc.vbvMaxBitrate = 0;
5399
8a90153de720 rc: initial work towards VBV rate control
Xun Xu, PPLive Corporation<xunxu@pptv.com>
parents: 5317
diff changeset
260 }
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
261 m_isVbv = m_param->rc.vbvMaxBitrate > 0 && m_param->rc.vbvBufferSize > 0;
11900
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
262 if (m_param->vbvBufferEnd && !m_isVbv)
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
263 {
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
264 x265_log(m_param, X265_LOG_WARNING, "vbv-end requires VBV parameters, ignored\n");
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
265 m_param->vbvBufferEnd = 0;
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
266 }
7085
b6f8e0ce8c81 sei: disable HRD with warning when vbv is off
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 7083
diff changeset
267 if (m_param->bEmitHRDSEI && !m_isVbv)
b6f8e0ce8c81 sei: disable HRD with warning when vbv is off
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 7083
diff changeset
268 {
b6f8e0ce8c81 sei: disable HRD with warning when vbv is off
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 7083
diff changeset
269 x265_log(m_param, X265_LOG_WARNING, "NAL HRD parameters require VBV parameters, ignored\n");
b6f8e0ce8c81 sei: disable HRD with warning when vbv is off
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 7083
diff changeset
270 m_param->bEmitHRDSEI = 0;
b6f8e0ce8c81 sei: disable HRD with warning when vbv is off
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 7083
diff changeset
271 }
12646
37916f420742 Enable Cbr in rate-control 2 pass
Kirithika <kirithika@multicorewareinc.com>
parents: 12645
diff changeset
272 m_isCbr = m_param->rc.rateControlMode == X265_RC_ABR && m_isVbv && m_param->rc.vbvMaxBitrate <= m_param->rc.bitrate;
9150
74bdb220e441 rc: fix bug in using strict-cbr option
Aarthi Thirumalai
parents: 9134
diff changeset
273 if (m_param->rc.bStrictCbr && !m_isCbr)
9079
da0f43681bca cli: remove tune cbr and introduce strict-cbr as a param option instead
Aarthi Thirumalai
parents: 9065
diff changeset
274 {
da0f43681bca cli: remove tune cbr and introduce strict-cbr as a param option instead
Aarthi Thirumalai
parents: 9065
diff changeset
275 x265_log(m_param, X265_LOG_WARNING, "strict CBR set without CBR mode, ignored\n");
da0f43681bca cli: remove tune cbr and introduce strict-cbr as a param option instead
Aarthi Thirumalai
parents: 9065
diff changeset
276 m_param->rc.bStrictCbr = 0;
da0f43681bca cli: remove tune cbr and introduce strict-cbr as a param option instead
Aarthi Thirumalai
parents: 9065
diff changeset
277 }
9083
ac94f35f4505 x265: remove rateTolerance from cli/param interface
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 9082
diff changeset
278 if(m_param->rc.bStrictCbr)
ac94f35f4505 x265: remove rateTolerance from cli/param interface
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 9082
diff changeset
279 m_rateTolerance = 0.7;
9082
321831bd539d ratecontrol: reorder strictCbr condition checks
Deepthi Nandakumar <deepthi@multicorewareinc.com>
parents: 9081
diff changeset
280
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
281 m_bframeBits = 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
282 m_leadingNoBSatd = 0;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
283 m_ipOffset = 6.0 * X265_LOG2(m_param->rc.ipFactor);
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
284 m_pbOffset = 6.0 * X265_LOG2(m_param->rc.pbFactor);
6940
58420e834424 rc: cleanups for clarity
Steve Borho <steve@borho.org>
parents: 6939
diff changeset
285
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
286 for (int i = 0; i < QP_MAX_MAX; i++)
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
287 m_qpToEncodedBits[i] = 0;
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
288
6940
58420e834424 rc: cleanups for clarity
Steve Borho <steve@borho.org>
parents: 6939
diff changeset
289 /* Adjust the first frame in order to stabilize the quality level compared to the rest */
6949
1c65cf8f068e rc: improve visual quality in high bit depth encodes.
Aarthi Thirumalai
parents: 6940
diff changeset
290 #define ABR_INIT_QP_MIN (24)
11512
3bacb3be4956 rc: change max value of ABR and disable the frame parallelism in first frame
Divya Manivannan <divya@multicorewareinc.com>
parents: 11508
diff changeset
291 #define ABR_INIT_QP_MAX (37)
11370
54d2625ae6a1 rc: fix tune grain to adapt to scene changes and preserve quality
Aarthi Thirumalai
parents: 11369
diff changeset
292 #define ABR_INIT_QP_GRAIN_MAX (33)
10913
923bfe2a098b rc: adjust qp for scene transitions and fade ins to avoid quality loss with vbv
Aarthi Thirumalai
parents: 10850
diff changeset
293 #define ABR_SCENECUT_INIT_QP_MIN (12)
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
294 #define CRF_INIT_QP (int)m_param->rc.rfConstant
3457
52a709ce88a8 ratecontrol: uncrustify
Steve Borho <steve@borho.org>
parents: 3453
diff changeset
295 for (int i = 0; i < 3; i++)
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
296 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
297 m_lastQScaleFor[i] = x265_qp2qScale(m_param->rc.rateControlMode == X265_RC_CRF ? CRF_INIT_QP : ABR_INIT_QP_MIN);
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
298 m_lmin[i] = x265_qp2qScale(m_param->rc.qpMin);
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
299 m_lmax[i] = x265_qp2qScale(m_param->rc.qpMax);
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
300 }
3457
52a709ce88a8 ratecontrol: uncrustify
Steve Borho <steve@borho.org>
parents: 3453
diff changeset
301
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
302 if (m_param->rc.rateControlMode == X265_RC_CQP)
3856
7bcd5530d55a ratecontrol: move CQP support into our code
Steve Borho <steve@borho.org>
parents: 3754
diff changeset
303 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
304 if (m_qp && !m_param->bLossless)
6730
07156a0e74e5 rc: if --qp 0 is specified, use zero quant for all slice types
Steve Borho <steve@borho.org>
parents: 6711
diff changeset
305 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
306 m_qpConstant[P_SLICE] = m_qp;
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
307 m_qpConstant[I_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp - m_ipOffset + 0.5));
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
308 m_qpConstant[B_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp + m_pbOffset + 0.5));
6730
07156a0e74e5 rc: if --qp 0 is specified, use zero quant for all slice types
Steve Borho <steve@borho.org>
parents: 6711
diff changeset
309 }
07156a0e74e5 rc: if --qp 0 is specified, use zero quant for all slice types
Steve Borho <steve@borho.org>
parents: 6711
diff changeset
310 else
07156a0e74e5 rc: if --qp 0 is specified, use zero quant for all slice types
Steve Borho <steve@borho.org>
parents: 6711
diff changeset
311 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
312 m_qpConstant[P_SLICE] = m_qpConstant[I_SLICE] = m_qpConstant[B_SLICE] = m_qp;
6730
07156a0e74e5 rc: if --qp 0 is specified, use zero quant for all slice types
Steve Borho <steve@borho.org>
parents: 6711
diff changeset
313 }
3856
7bcd5530d55a ratecontrol: move CQP support into our code
Steve Borho <steve@borho.org>
parents: 3754
diff changeset
314 }
7bcd5530d55a ratecontrol: move CQP support into our code
Steve Borho <steve@borho.org>
parents: 3754
diff changeset
315
10261
ddcf53cb6974 doc: correct --qpstep
Steve Borho <steve@borho.org>
parents: 10132
diff changeset
316 /* qpstep - value set as encoder specific */
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
317 m_lstep = pow(2, m_param->rc.qpStep / 6.0);
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
318
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
319 for (int i = 0; i < 2; i++)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
320 m_cuTreeStats.qpBuffer[i] = NULL;
3398
4085c992877a uncrustify, code cleanup , continue porting x264 rate control to x265.
Aarthi<aarthi@multicorewareinc.com>
parents: 3391
diff changeset
321 }
4085c992877a uncrustify, code cleanup , continue porting x264 rate control to x265.
Aarthi<aarthi@multicorewareinc.com>
parents: 3391
diff changeset
322
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
323 bool RateControl::init(const SPS& sps)
6272
ebaa34c8f651 rc: implement abr reset for single pass ABR + VBV
Aarthi Thirumalai
parents: 6270
diff changeset
324 {
11958
2e684b80d90f rc: Fix inconsistency in --const-vbv (issue #381)
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11900
diff changeset
325 if (m_isVbv && !m_initVbv)
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
326 {
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
327 /* We don't support changing the ABR bitrate right now,
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
328 * so if the stream starts as CBR, keep it CBR. */
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
329 if (m_param->rc.vbvBufferSize < (int)(m_param->rc.vbvMaxBitrate / m_fps))
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
330 {
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
331 m_param->rc.vbvBufferSize = (int)(m_param->rc.vbvMaxBitrate / m_fps);
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
332 x265_log(m_param, X265_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
333 m_param->rc.vbvBufferSize);
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
334 }
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
335 int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
336 int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
337
12633
10e354213df2 Add option to get global maxrate
Snehaa Giridharan <snehaa@multicorewareinc.com>
parents: 12632
diff changeset
338 if (m_param->bEmitHRDSEI && !m_param->decoderVbvMaxRate)
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
339 {
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
340 const HRDInfo* hrd = &sps.vuiParameters.hrdParameters;
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
341 vbvBufferSize = hrd->cpbSizeValue << (hrd->cpbSizeScale + CPB_SHIFT);
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
342 vbvMaxBitrate = hrd->bitRateValue << (hrd->bitRateScale + BR_SHIFT);
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
343 }
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
344 m_bufferRate = vbvMaxBitrate / m_fps;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
345 m_vbvMaxRate = vbvMaxBitrate;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
346 m_bufferSize = vbvBufferSize;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
347 m_singleFrameVbv = m_bufferRate * 1.1 > m_bufferSize;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
348
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
349 if (m_param->rc.vbvBufferInit > 1.)
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
350 m_param->rc.vbvBufferInit = x265_clip3(0.0, 1.0, m_param->rc.vbvBufferInit / m_param->rc.vbvBufferSize);
11900
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
351 if (m_param->vbvBufferEnd > 1.)
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
352 m_param->vbvBufferEnd = x265_clip3(0.0, 1.0, m_param->vbvBufferEnd / m_param->rc.vbvBufferSize);
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
353 if (m_param->vbvEndFrameAdjust > 1.)
8a121d8cc134 Add vbv-end to denote VBV emptiness after inserting all the frames into it.
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11883
diff changeset
354 m_param->vbvEndFrameAdjust = x265_clip3(0.0, 1.0, m_param->vbvEndFrameAdjust);
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
355 m_param->rc.vbvBufferInit = x265_clip3(0.0, 1.0, X265_MAX(m_param->rc.vbvBufferInit, m_bufferRate / m_bufferSize));
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
356 m_bufferFillFinal = m_bufferSize * m_param->rc.vbvBufferInit;
11599
a2d5144476be csv: add buffer fill per frame in csv
Divya Manivannan <divya@multicorewareinc.com>
parents: 11584
diff changeset
357 m_bufferFillActual = m_bufferFillFinal;
a2d5144476be csv: add buffer fill per frame in csv
Divya Manivannan <divya@multicorewareinc.com>
parents: 11584
diff changeset
358 m_bufferExcess = 0;
11958
2e684b80d90f rc: Fix inconsistency in --const-vbv (issue #381)
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11900
diff changeset
359 m_initVbv = true;
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
360 }
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
361
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
362 if (!m_param->bResetZoneConfig && (m_relativeComplexity == NULL))
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
363 {
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
364 m_relativeComplexity = X265_MALLOC(double, m_param->reconfigWindowSize);
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
365 if (m_relativeComplexity == NULL)
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
366 {
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
367 x265_log(m_param, X265_LOG_ERROR, "Failed to allocate memory for m_relativeComplexity\n");
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
368 return false;
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
369 }
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
370 }
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
371
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
372 m_totalBits = 0;
9081
22df66c01d88 rc: control bitrate overshoots during midframe encodes in strict-cbr
Aarthi Thirumalai
parents: 9080
diff changeset
373 m_encodedBits = 0;
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
374 m_framesDone = 0;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
375 m_residualCost = 0;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
376 m_partialResidualCost = 0;
9950
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
377 m_amortizeFraction = 0.85;
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
378 m_amortizeFrames = 75;
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
379 if (m_param->totalFrames && m_param->totalFrames <= 2 * m_fps && m_param->rc.bStrictCbr) /* Strict CBR segment encode */
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
380 {
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
381 m_amortizeFraction = 0.85;
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
382 m_amortizeFrames = m_param->totalFrames / 2;
bf98009eb155 rc: depreciate amortized frame and fraction by 1.1 at each I frame
Sreelakshmy V G <sreelakshmy@multicorewareinc.com>
parents: 9880
diff changeset
383 }
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
384
8234
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
385 for (int i = 0; i < s_slidingWindowFrames; i++)
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
386 {
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
387 m_satdCostWindow[i] = 0;
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
388 m_encodedBitsWindow[i] = 0;
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
389 }
4b7c473c3ef4 rc: use a sliding window to calculate moving avg SatdCost for ABR Reset logic
Aarthi Thirumalai
parents: 8198
diff changeset
390 m_sliderPos = 0;
9435
d7bf99b426e9 rc: fix to avoid false abr restarts
Aarthi Thirumalai
parents: 9397
diff changeset
391 m_isPatternPresent = false;
d7bf99b426e9 rc: fix to avoid false abr restarts
Aarthi Thirumalai
parents: 9397
diff changeset
392 m_numBframesInPattern = 0;
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
393
11370
54d2625ae6a1 rc: fix tune grain to adapt to scene changes and preserve quality
Aarthi Thirumalai
parents: 11369
diff changeset
394 m_isGrainEnabled = false;
54d2625ae6a1 rc: fix tune grain to adapt to scene changes and preserve quality
Aarthi Thirumalai
parents: 11369
diff changeset
395 if(m_param->rc.bEnableGrain) // tune for grainy content OR equal p-b frame sizes
12454
3cd0b5ed0b91 new aq implementation
Ashok Kumar Mishra <ashok@multicorewareinc.com>
parents: 12445
diff changeset
396 m_isGrainEnabled = true;
11370
54d2625ae6a1 rc: fix tune grain to adapt to scene changes and preserve quality
Aarthi Thirumalai
parents: 11369
diff changeset
397 for (int i = 0; i < 3; i++)
12454
3cd0b5ed0b91 new aq implementation
Ashok Kumar Mishra <ashok@multicorewareinc.com>
parents: 12445
diff changeset
398 m_lastQScaleFor[i] = x265_qp2qScale(m_param->rc.rateControlMode == X265_RC_CRF ? CRF_INIT_QP : ABR_INIT_QP_MIN);
11370
54d2625ae6a1 rc: fix tune grain to adapt to scene changes and preserve quality
Aarthi Thirumalai
parents: 11369
diff changeset
399 m_avgPFrameQp = 0 ;
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
400
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
401 /* 720p videos seem to be a good cutoff for cplxrSum */
12454
3cd0b5ed0b91 new aq implementation
Ashok Kumar Mishra <ashok@multicorewareinc.com>
parents: 12445
diff changeset
402 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;
3cd0b5ed0b91 new aq implementation
Ashok Kumar Mishra <ashok@multicorewareinc.com>
parents: 12445
diff changeset
403
7555
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
404 /* estimated ratio that produces a reasonable QP for the first I-frame */
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
405 m_cplxrSum = .01 * pow(7.0e5, m_qCompress) * pow(m_ncu, 0.5) * tuneCplxFactor;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
406 m_wantedBitsWindow = m_bitrate * m_frameDuration;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
407 m_accumPNorm = .01;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
408 m_accumPQp = (m_param->rc.rateControlMode == X265_RC_CRF ? CRF_INIT_QP : ABR_INIT_QP_MIN) * m_accumPNorm;
d3e2e487a488 rc: fix crashes in vbv with 2 pass
Aarthi Thirumalai
parents: 7545
diff changeset
409
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
410
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
411 /* Frame Predictors used in vbv */
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
412 initFramePredictors();
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
413 if (!m_statFileOut && (m_param->rc.bStatWrite || m_param->rc.bStatRead))
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
414 {
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
415 /* If the user hasn't defined the stat filename, use the default value */
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
416 const char *fileName = m_param->rc.statFileName;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
417 if (!fileName)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
418 fileName = s_defaultStatFileName;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
419 /* Load stat file and init 2pass algo */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
420 if (m_param->rc.bStatRead)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
421 {
7493
0461e091a7b5 rc: add 2 pass logic in rateEstimateQscale
Aarthi Thirumalai
parents: 7484
diff changeset
422 m_expectedBitsSum = 0;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
423 char *p, *statsIn, *statsBuf;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
424 /* read 1st pass stats */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
425 statsIn = statsBuf = x265_slurp_file(fileName);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
426 if (!statsBuf)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
427 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
428 if (m_param->rc.cuTree)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
429 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
430 char *tmpFile = strcatFilename(fileName, ".cutree");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
431 if (!tmpFile)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
432 return false;
11437
00ea3784bd36 Allows for Unicode filenames in Windows (output and stat files).
Ma0 <mateuszb@poczta.onet.pl>
parents: 11435
diff changeset
433 m_cutreeStatFileIn = x265_fopen(tmpFile, "rb");
7322
fd87a7e5b1e3 rc: nits
Steve Borho <steve@borho.org>
parents: 7319
diff changeset
434 X265_FREE(tmpFile);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
435 if (!m_cutreeStatFileIn)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
436 {
11437
00ea3784bd36 Allows for Unicode filenames in Windows (output and stat files).
Ma0 <mateuszb@poczta.onet.pl>
parents: 11435
diff changeset
437 x265_log_file(m_param, X265_LOG_ERROR, "can't open stats file %s.cutree\n", fileName);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
438 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
439 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
440 }
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
441
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
442 /* check whether 1st pass options were compatible with current options */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
443 if (strncmp(statsBuf, "#options:", 9))
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
444 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
445 x265_log(m_param, X265_LOG_ERROR,"options list in stats file not valid\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
446 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
447 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
448 {
11298
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
449 int i, j, m;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
450 uint32_t k , l;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
451 bool bErr = false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
452 char *opts = statsBuf;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
453 statsIn = strchr(statsBuf, '\n');
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
454 if (!statsIn)
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
455 {
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
456 x265_log(m_param, X265_LOG_ERROR, "Malformed stats file\n");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
457 return false;
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
458 }
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
459 *statsIn = '\0';
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
460 statsIn++;
11643
6737486707bf rc: change stat read options
Divya Manivannan <divya@multicorewareinc.com>
parents: 11628
diff changeset
461 if ((p = strstr(opts, " input-res=")) == 0 || sscanf(p, " input-res=%dx%d", &i, &j) != 2)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
462 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
463 x265_log(m_param, X265_LOG_ERROR, "Resolution specified in stats file not valid\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
464 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
465 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
466 if ((p = strstr(opts, " fps=")) == 0 || sscanf(p, " fps=%u/%u", &k, &l) != 2)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
467 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
468 x265_log(m_param, X265_LOG_ERROR, "fps specified in stats file not valid\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
469 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
470 }
11298
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
471 if (((p = strstr(opts, " vbv-maxrate=")) == 0 || sscanf(p, " vbv-maxrate=%d", &m) != 1) && m_param->rc.rateControlMode == X265_RC_CRF)
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
472 {
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
473 x265_log(m_param, X265_LOG_ERROR, "Constant rate-factor is incompatible with 2pass without vbv-maxrate in the previous pass\n");
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
474 return false;
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
475 }
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
476 if (k != m_param->fpsNum || l != m_param->fpsDenom)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
477 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
478 x265_log(m_param, X265_LOG_ERROR, "fps mismatch with 1st pass (%u/%u vs %u/%u)\n",
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
479 m_param->fpsNum, m_param->fpsDenom, k, l);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
480 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
481 }
11677
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
482 if (m_param->analysisMultiPassRefine)
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
483 {
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
484 p = strstr(opts, "ref=");
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
485 sscanf(p, "ref=%d", &i);
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
486 if (i > m_param->maxNumReferences)
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
487 {
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
488 x265_log(m_param, X265_LOG_ERROR, "maxNumReferences cannot be less than 1st pass (%d vs %d)\n",
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
489 i, m_param->maxNumReferences);
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
490 return false;
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
491 }
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
492 }
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
493 if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
494 {
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
495 p = strstr(opts, "ctu=");
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
496 sscanf(p, "ctu=%u", &k);
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
497 if (k != m_param->maxCUSize)
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
498 {
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
499 x265_log(m_param, X265_LOG_ERROR, "maxCUSize mismatch with 1st pass (%u vs %u)\n",
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
500 k, m_param->maxCUSize);
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
501 return false;
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
502 }
af10eaeb36cd Add pass 3 support for distortion refinement
Aruna Matheswaran
parents: 11659
diff changeset
503 }
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
504 CMP_OPT_FIRST_PASS("bitdepth", m_param->internalBitDepth);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
505 CMP_OPT_FIRST_PASS("weightp", m_param->bEnableWeightedPred);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
506 CMP_OPT_FIRST_PASS("bframes", m_param->bframes);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
507 CMP_OPT_FIRST_PASS("b-pyramid", m_param->bBPyramid);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
508 CMP_OPT_FIRST_PASS("open-gop", m_param->bOpenGOP);
11643
6737486707bf rc: change stat read options
Divya Manivannan <divya@multicorewareinc.com>
parents: 11628
diff changeset
509 CMP_OPT_FIRST_PASS(" keyint", m_param->keyframeMax);
8061
67ee212bbf78 rc: check for changes in scenecut input between multiple passes.
Aarthi Thirumalai
parents: 8060
diff changeset
510 CMP_OPT_FIRST_PASS("scenecut", m_param->scenecutThreshold);
11098
a7251c3e0ef8 typo: fix intra refresh for multipass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11092
diff changeset
511 CMP_OPT_FIRST_PASS("intra-refresh", m_param->bIntraRefresh);
12649
93ec18bf8595 Fix bug in frame-dup + multi pass
Akil Ayyappan R <akil@multicorewareinc.com>
parents: 12646
diff changeset
512 CMP_OPT_FIRST_PASS("frame-dup", m_param->bEnableFrameDuplication);
11628
b08109b3701e Add param check when store commonly RSP in SPS in 2-pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11624
diff changeset
513 if (m_param->bMultiPassOptRPS)
b08109b3701e Add param check when store commonly RSP in SPS in 2-pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11624
diff changeset
514 {
b08109b3701e Add param check when store commonly RSP in SPS in 2-pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11624
diff changeset
515 CMP_OPT_FIRST_PASS("multi-pass-opt-rps", m_param->bMultiPassOptRPS);
b08109b3701e Add param check when store commonly RSP in SPS in 2-pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11624
diff changeset
516 CMP_OPT_FIRST_PASS("repeat-headers", m_param->bRepeatHeaders);
b08109b3701e Add param check when store commonly RSP in SPS in 2-pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11624
diff changeset
517 CMP_OPT_FIRST_PASS("min-keyint", m_param->keyframeMin);
b08109b3701e Add param check when store commonly RSP in SPS in 2-pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11624
diff changeset
518 }
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
519
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
520 if ((p = strstr(opts, "b-adapt=")) != 0 && sscanf(p, "b-adapt=%d", &i) && i >= X265_B_ADAPT_NONE && i <= X265_B_ADAPT_TRELLIS)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
521 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
522 m_param->bFrameAdaptive = i;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
523 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
524 else if (m_param->bframes)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
525 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
526 x265_log(m_param, X265_LOG_ERROR, "b-adapt method specified in stats file not valid\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
527 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
528 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
529
7617
a9678988dda2 rc: fix crash in 2 pass for faster preset (ultrafast - faster)
Aarthi Thirumalai
parents: 7610
diff changeset
530 if ((p = strstr(opts, "rc-lookahead=")) != 0 && sscanf(p, "rc-lookahead=%d", &i))
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
531 m_param->lookaheadDepth = i;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
532 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
533 /* find number of pics */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
534 p = statsIn;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
535 int numEntries;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
536 for (numEntries = -1; p; numEntries++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
537 p = strchr(p + 1, ';');
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
538 if (!numEntries)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
539 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
540 x265_log(m_param, X265_LOG_ERROR, "empty stats file\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
541 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
542 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
543 m_numEntries = numEntries;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
544
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
545 if (m_param->totalFrames < m_numEntries && m_param->totalFrames > 0)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
546 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
547 x265_log(m_param, X265_LOG_WARNING, "2nd pass has fewer frames than 1st pass (%d vs %d)\n",
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
548 m_param->totalFrames, m_numEntries);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
549 }
12649
93ec18bf8595 Fix bug in frame-dup + multi pass
Akil Ayyappan R <akil@multicorewareinc.com>
parents: 12646
diff changeset
550 if (m_param->totalFrames > m_numEntries && !m_param->bEnableFrameDuplication)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
551 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
552 x265_log(m_param, X265_LOG_ERROR, "2nd pass has more frames than 1st pass (%d vs %d)\n",
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
553 m_param->totalFrames, m_numEntries);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
554 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
555 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
556
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
557 m_rce2Pass = X265_MALLOC(RateControlEntry, m_numEntries);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
558 if (!m_rce2Pass)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
559 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
560 x265_log(m_param, X265_LOG_ERROR, "Rce Entries for 2 pass cannot be allocated\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
561 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
562 }
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
563 m_encOrder = X265_MALLOC(int, m_numEntries);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
564 if (!m_encOrder)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
565 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
566 x265_log(m_param, X265_LOG_ERROR, "Encode order for 2 pass cannot be allocated\n");
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
567 return false;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
568 }
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
569 /* init all to skipped p frames */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
570 for (int i = 0; i < m_numEntries; i++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
571 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
572 RateControlEntry *rce = &m_rce2Pass[i];
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
573 rce->sliceType = P_SLICE;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
574 rce->qScale = rce->newQScale = x265_qp2qScale(20);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
575 rce->miscBits = m_ncu + 10;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
576 rce->newQp = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
577 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
578 /* read stats */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
579 p = statsIn;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
580 double totalQpAq = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
581 for (int i = 0; i < m_numEntries; i++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
582 {
12673
3a5b3583afb1 Improve: Scenecut Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12672
diff changeset
583 RateControlEntry *rce, *rcePocOrder;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
584 int frameNumber;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
585 int encodeOrder;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
586 char picType;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
587 int e;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
588 char *next;
11216
9c797a591581 rc: fix rate factor values recorded in csv
Aarthi Priya Thirumalai <aarthi@multicorewareinc.com>
parents: 11215
diff changeset
589 double qpRc, qpAq, qNoVbv, qRceq;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
590 next = strstr(p, ";");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
591 if (next)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
592 *next++ = 0;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
593 e = sscanf(p, " in:%d out:%d", &frameNumber, &encodeOrder);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
594 if (frameNumber < 0 || frameNumber >= m_numEntries)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
595 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
596 x265_log(m_param, X265_LOG_ERROR, "bad frame number (%d) at stats line %d\n", frameNumber, i);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
597 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
598 }
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
599 rce = &m_rce2Pass[encodeOrder];
12673
3a5b3583afb1 Improve: Scenecut Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12672
diff changeset
600 rcePocOrder = &m_rce2Pass[frameNumber];
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
601 m_encOrder[frameNumber] = encodeOrder;
11616
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
602 if (!m_param->bMultiPassOptRPS)
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
603 {
12673
3a5b3583afb1 Improve: Scenecut Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12672
diff changeset
604 int scenecut = 0;
3a5b3583afb1 Improve: Scenecut Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12672
diff changeset
605 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 sc:%d",
11616
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
606 &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
607 &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,
12673
3a5b3583afb1 Improve: Scenecut Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12672
diff changeset
608 &rce->skipCuCount, &scenecut);
3a5b3583afb1 Improve: Scenecut Aware Frame Quantizer Selection
Niranjan <niranjan@multicorewareinc.com>
parents: 12672
diff changeset
609 rcePocOrder->scenecut = scenecut != 0;
11616
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
610 }
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
611 else
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
612 {
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
613 char deltaPOC[128];
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
614 char bUsed[40];
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
615 memset(deltaPOC, 0, sizeof(deltaPOC));
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
616 memset(bUsed, 0, sizeof(bUsed));
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
617 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",
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
618 &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
619 &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
620 &rce->skipCuCount, &rce->rpsData.numberOfPictures, &rce->rpsData.numberOfNegativePictures, &rce->rpsData.numberOfPositivePictures, deltaPOC, bUsed);
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
621 splitdeltaPOC(deltaPOC, rce);
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
622 splitbUsed(bUsed, rce);
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
623 rce->rpsIdx = -1;
3f9fab5e334c Store commonly-used RPS in SPS in 2 pass mode.
ZhengWang <zheng@multicorewareinc.com>
parents: 11599
diff changeset
624 }
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
625 rce->keptAsRef = true;
11580
dc4893950b27 rc: fix non-IDR slicetype in multi-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11574
diff changeset
626 rce->isIdr = false;
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
627 if (picType == 'b' || picType == 'p')
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
628 rce->keptAsRef = false;
11580
dc4893950b27 rc: fix non-IDR slicetype in multi-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11574
diff changeset
629 if (picType == 'I')
dc4893950b27 rc: fix non-IDR slicetype in multi-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11574
diff changeset
630 rce->isIdr = true;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
631 if (picType == 'I' || picType == 'i')
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
632 rce->sliceType = I_SLICE;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
633 else if (picType == 'P' || picType == 'p')
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
634 rce->sliceType = P_SLICE;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
635 else if (picType == 'B' || picType == 'b')
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
636 rce->sliceType = B_SLICE;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
637 else
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
638 e = -1;
8036
139b41632c64 rc: use m_frameDuration instead of rce->frameDuration to derive complexity for each frame in 2nd pass.
Aarthi Thirumalai
parents: 7998
diff changeset
639 if (e < 10)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
640 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
641 x265_log(m_param, X265_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
642 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
643 }
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
644 rce->qScale = rce->newQScale = x265_qp2qScale(qpRc);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
645 totalQpAq += qpAq;
11167
e90c2d74de88 rc: record one more field in 1st pass stats
Aarthi Thirumalai
parents: 11147
diff changeset
646 rce->qpNoVbv = qNoVbv;
e90c2d74de88 rc: record one more field in 1st pass stats
Aarthi Thirumalai
parents: 11147
diff changeset
647 rce->qpaRc = qpRc;
e90c2d74de88 rc: record one more field in 1st pass stats
Aarthi Thirumalai
parents: 11147
diff changeset
648 rce->qpAq = qpAq;
11216
9c797a591581 rc: fix rate factor values recorded in csv
Aarthi Priya Thirumalai <aarthi@multicorewareinc.com>
parents: 11215
diff changeset
649 rce->qRceq = qRceq;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
650 p = next;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
651 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
652 X265_FREE(statsBuf);
11298
822782933427 rc: modify the condition for enabling 2pass in CRF mode
Divya Manivannan <divya@multicorewareinc.com>
parents: 11278
diff changeset
653 if (m_param->rc.rateControlMode != X265_RC_CQP)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
654 {
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
655 m_start = 0;
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
656 m_isQpModified = true;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
657 if (!initPass2())
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
658 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
659 } /* else we're using constant quant, so no need to run the bitrate allocation */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
660 }
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
661 /* Open output file */
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
662 /* If input and output files are the same, output to a temp file
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
663 * and move it to the real name only when it's complete */
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
664 if (m_param->rc.bStatWrite)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
665 {
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
666 char *p, *statFileTmpname;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
667 statFileTmpname = strcatFilename(fileName, ".temp");
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
668 if (!statFileTmpname)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
669 return false;
11437
00ea3784bd36 Allows for Unicode filenames in Windows (output and stat files).
Ma0 <mateuszb@poczta.onet.pl>
parents: 11435
diff changeset
670 m_statFileOut = x265_fopen(statFileTmpname, "wb");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
671 X265_FREE(statFileTmpname);
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
672 if (!m_statFileOut)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
673 {
11437
00ea3784bd36 Allows for Unicode filenames in Windows (output and stat files).
Ma0 <mateuszb@poczta.onet.pl>
parents: 11435
diff changeset
674 x265_log_file(m_param, X265_LOG_ERROR, "can't open stats file %s.temp\n", fileName);
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
675 return false;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
676 }
11652
5d95fbd53ca3 Fix source width and height in info
Divya Manivannan <divya@multicorewareinc.com>
parents: 11643
diff changeset
677 p = x265_param2string(m_param, sps.conformanceWindow.rightOffset, sps.conformanceWindow.bottomOffset);
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
678 if (p)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
679 fprintf(m_statFileOut, "#options: %s\n", p);
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
680 X265_FREE(p);
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
681 if (m_param->rc.cuTree && !m_param->rc.bStatRead)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
682 {
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
683 statFileTmpname = strcatFilename(fileName, ".cutree.temp");
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
684 if (!statFileTmpname)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
685 return false;
11437
00ea3784bd36 Allows for Unicode filenames in Windows (output and stat files).
Ma0 <mateuszb@poczta.onet.pl>
parents: 11435
diff changeset
686 m_cutreeStatFileOut = x265_fopen(statFileTmpname, "wb");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
687 X265_FREE(statFileTmpname);
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
688 if (!m_cutreeStatFileOut)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
689 {
11437
00ea3784bd36 Allows for Unicode filenames in Windows (output and stat files).
Ma0 <mateuszb@poczta.onet.pl>
parents: 11435
diff changeset
690 x265_log_file(m_param, X265_LOG_ERROR, "can't open mbtree stats file %s.cutree.temp\n", fileName);
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
691 return false;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
692 }
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
693 }
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
694 }
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
695 if (m_param->rc.cuTree)
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
696 {
11537
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
697 if (m_param->rc.qgSize == 8)
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
698 {
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
699 m_cuTreeStats.qpBuffer[0] = X265_MALLOC(uint16_t, m_ncu * 4 * sizeof(uint16_t));
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
700 if (m_param->bBPyramid && m_param->rc.bStatRead)
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
701 m_cuTreeStats.qpBuffer[1] = X265_MALLOC(uint16_t, m_ncu * 4 * sizeof(uint16_t));
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
702 }
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
703 else
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
704 {
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
705 m_cuTreeStats.qpBuffer[0] = X265_MALLOC(uint16_t, m_ncu * sizeof(uint16_t));
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
706 if (m_param->bBPyramid && m_param->rc.bStatRead)
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
707 m_cuTreeStats.qpBuffer[1] = X265_MALLOC(uint16_t, m_ncu * sizeof(uint16_t));
1cb84267d514 rc: add support for qg-size 8
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11525
diff changeset
708 }
7128
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
709 m_cuTreeStats.qpBufPos = -1;
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
710 }
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
711 }
60178ece9879 rc: initialize the 2 pass states in rc
Aarthi Thirumalai<aarthi@multicorewareinc.com>
parents: 7126
diff changeset
712 return true;
6272
ebaa34c8f651 rc: implement abr reset for single pass ABR + VBV
Aarthi Thirumalai
parents: 6270
diff changeset
713 }
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
714
11734
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
715 void RateControl::reconfigureRC()
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
716 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
717 if (m_isVbv)
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
718 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
719 m_param->rc.vbvBufferSize = x265_clip3(0, 2000000, m_param->rc.vbvBufferSize);
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
720 m_param->rc.vbvMaxBitrate = x265_clip3(0, 2000000, m_param->rc.vbvMaxBitrate);
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
721 if (m_param->reconfigWindowSize)
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
722 m_param->rc.vbvMaxBitrate = (int)(m_param->rc.vbvMaxBitrate * (double)(m_fps / m_param->reconfigWindowSize));
11734
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
723 if (m_param->rc.vbvMaxBitrate < m_param->rc.bitrate &&
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
724 m_param->rc.rateControlMode == X265_RC_ABR)
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
725 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
726 x265_log(m_param, X265_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n");
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
727 m_param->rc.bitrate = m_param->rc.vbvMaxBitrate;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
728 }
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
729
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
730 if (m_param->rc.vbvBufferSize < (int)(m_param->rc.vbvMaxBitrate / m_fps))
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
731 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
732 m_param->rc.vbvBufferSize = (int)(m_param->rc.vbvMaxBitrate / m_fps);
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
733 x265_log(m_param, X265_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
734 m_param->rc.vbvBufferSize);
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
735 }
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
736 int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
737 int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
738 m_bufferRate = vbvMaxBitrate / m_fps;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
739 m_vbvMaxRate = vbvMaxBitrate;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
740 m_bufferSize = vbvBufferSize;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
741 m_singleFrameVbv = m_bufferRate * 1.1 > m_bufferSize;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
742 }
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
743 if (m_param->rc.rateControlMode == X265_RC_CRF)
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
744 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
745 #define CRF_INIT_QP (int)m_param->rc.rfConstant
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
746 m_param->rc.bitrate = 0;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
747 double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
748 double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
749 m_rateFactorConstant = pow(baseCplx, 1 - m_qCompress) /
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
750 x265_qp2qScale(m_param->rc.rfConstant + mbtree_offset);
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
751 if (m_param->rc.rfConstantMax)
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
752 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
753 m_rateFactorMaxIncrement = m_param->rc.rfConstantMax - m_param->rc.rfConstant;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
754 if (m_rateFactorMaxIncrement <= 0)
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
755 {
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
756 x265_log(m_param, X265_LOG_WARNING, "CRF max must be greater than CRF\n");
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
757 m_rateFactorMaxIncrement = 0;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
758 }
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
759 }
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
760 if (m_param->rc.rfConstantMin)
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
761 m_rateFactorMaxDecrement = m_param->rc.rfConstant - m_param->rc.rfConstantMin;
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
762 }
12445
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
763 if (m_param->rc.rateControlMode == X265_RC_CQP)
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
764 {
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
765 m_qp = m_param->rc.qp;
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
766 if (m_qp && !m_param->bLossless)
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
767 {
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
768 m_qpConstant[P_SLICE] = m_qp;
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
769 m_qpConstant[I_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp - m_ipOffset + 0.5));
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
770 m_qpConstant[B_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp + m_pbOffset + 0.5));
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
771 }
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
772 else
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
773 {
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
774 m_qpConstant[P_SLICE] = m_qpConstant[I_SLICE] = m_qpConstant[B_SLICE] = m_qp;
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
775 }
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
776 }
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
777 m_bitrate = (double)m_param->rc.bitrate * 1000;
11734
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
778 }
3c26e29120aa Add dynamic rate-control reconfiguration
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 11730
diff changeset
779
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
780 void RateControl::initHRD(SPS& sps)
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
781 {
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
782 int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
783 int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
784
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
785 // Init HRD
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
786 HRDInfo* hrd = &sps.vuiParameters.hrdParameters;
7383
d9921a247139 hrd: remove set/get methods of TComHRD, remove unused members
Steve Borho <steve@borho.org>
parents: 7381
diff changeset
787 hrd->cbrFlag = m_isCbr;
12633
10e354213df2 Add option to get global maxrate
Snehaa Giridharan <snehaa@multicorewareinc.com>
parents: 12632
diff changeset
788 if (m_param->reconfigWindowSize) {
12631
6cb4a24acac3 zone: Enable strict VBV conformance for zone encode as per requirement
Snehaa Giridharan <snehaa@multicorewareinc.com>
parents: 12596
diff changeset
789 hrd->cbrFlag = 0;
12633
10e354213df2 Add option to get global maxrate
Snehaa Giridharan <snehaa@multicorewareinc.com>
parents: 12632
diff changeset
790 vbvMaxBitrate = m_param->decoderVbvMaxRate * 1000;
10e354213df2 Add option to get global maxrate
Snehaa Giridharan <snehaa@multicorewareinc.com>
parents: 12632
diff changeset
791 }
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
792
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
793 // normalize HRD size and rate to the value / scale notation
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
794 hrd->bitRateScale = x265_clip3(0, 15, calcScale(vbvMaxBitrate) - BR_SHIFT);
7384
3bc86d0fbdc7 hrd: remove *Minus1 from member names and semantics, remove m_ prefixes
Steve Borho <steve@borho.org>
parents: 7383
diff changeset
795 hrd->bitRateValue = (vbvMaxBitrate >> (hrd->bitRateScale + BR_SHIFT));
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
796
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
797 hrd->cpbSizeScale = x265_clip3(0, 15, calcScale(vbvBufferSize) - CPB_SHIFT);
7384
3bc86d0fbdc7 hrd: remove *Minus1 from member names and semantics, remove m_ prefixes
Steve Borho <steve@borho.org>
parents: 7383
diff changeset
798 hrd->cpbSizeValue = (vbvBufferSize >> (hrd->cpbSizeScale + CPB_SHIFT));
3bc86d0fbdc7 hrd: remove *Minus1 from member names and semantics, remove m_ prefixes
Steve Borho <steve@borho.org>
parents: 7383
diff changeset
799 int bitRateUnscale = hrd->bitRateValue << (hrd->bitRateScale + BR_SHIFT);
3bc86d0fbdc7 hrd: remove *Minus1 from member names and semantics, remove m_ prefixes
Steve Borho <steve@borho.org>
parents: 7383
diff changeset
800 int cpbSizeUnscale = hrd->cpbSizeValue << (hrd->cpbSizeScale + CPB_SHIFT);
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
801
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
802 // arbitrary
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
803 #define MAX_DURATION 0.5
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
804
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
805 TimingInfo *time = &sps.vuiParameters.timingInfo;
7378
9b807ca6313a slice: simplify TimingInfo to a struct
Steve Borho <steve@borho.org>
parents: 7359
diff changeset
806 int maxCpbOutputDelay = (int)(X265_MIN(m_param->keyframeMax * MAX_DURATION * time->timeScale / time->numUnitsInTick, INT_MAX));
9437
1f1bc3fda16c rc: pass param and sps by reference, no change
Steve Borho <steve@borho.org>
parents: 9435
diff changeset
807 int maxDpbOutputDelay = (int)(sps.maxDecPicBuffering * MAX_DURATION * time->timeScale / time->numUnitsInTick);
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
808 int maxDelay = (int)(90000.0 * cpbSizeUnscale / bitRateUnscale + 0.5);
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
809
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
810 hrd->initialCpbRemovalDelayLength = 2 + x265_clip3(4, 22, 32 - calcLength(maxDelay));
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
811 hrd->cpbRemovalDelayLength = x265_clip3(4, 31, 32 - calcLength(maxCpbOutputDelay));
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
812 hrd->dpbOutputDelayLength = x265_clip3(4, 31, 32 - calcLength(maxDpbOutputDelay));
6967
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
813
f9f553c8bd6a SEI: Insert buffering period and picture timing SEI messages
Kavitha Sampath <kavitha@multicorewareinc.com>
parents: 6953
diff changeset
814 #undef MAX_DURATION
6272
ebaa34c8f651 rc: implement abr reset for single pass ABR + VBV
Aarthi Thirumalai
parents: 6270
diff changeset
815 }
11883
667bbf65185e vbv hanging issue; fix for multiple slices
Ashok Kumar Mishra <ashok@multicorewareinc.com>
parents: 11852
diff changeset
816
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
817 bool RateControl::analyseABR2Pass(uint64_t allAvailableBits)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
818 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
819 double rateFactor, stepMult;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
820 double qBlur = m_param->rc.qblur;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
821 double cplxBlur = m_param->rc.complexityBlur;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
822 const int filterSize = (int)(qBlur * 4) | 1;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
823 double expectedBits;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
824 double *qScale, *blurredQscale;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
825 double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
8036
139b41632c64 rc: use m_frameDuration instead of rce->frameDuration to derive complexity for each frame in 2nd pass.
Aarthi Thirumalai
parents: 7998
diff changeset
826 double clippedDuration = CLIP_DURATION(m_frameDuration) / BASE_FRAME_DURATION;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
827 /* Blur complexities, to reduce local fluctuation of QP.
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
828 * We don't blur the QPs directly, because then one very simple frame
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
829 * could drag down the QP of a nearby complex frame and give it more
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
830 * bits than intended. */
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
831 for (int i = 0; i < m_numEntries; i++)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
832 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
833 double weightSum = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
834 double cplxSum = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
835 double weight = 1.0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
836 double gaussianWeight;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
837 /* weighted average of cplx of future frames */
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
838 for (int j = 1; j < cplxBlur * 2 && j < m_numEntries - i; j++)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
839 {
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
840 int index = i+j;
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
841 RateControlEntry *rcj = &m_rce2Pass[index];
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
842 weight *= 1 - pow(rcj->iCuCount / m_ncu, 2);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
843 if (weight < 0.0001)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
844 break;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
845 gaussianWeight = weight * exp(-j * j / 200.0);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
846 weightSum += gaussianWeight;
8036
139b41632c64 rc: use m_frameDuration instead of rce->frameDuration to derive complexity for each frame in 2nd pass.
Aarthi Thirumalai
parents: 7998
diff changeset
847 cplxSum += gaussianWeight * (qScale2bits(rcj, 1) - rcj->miscBits) / clippedDuration;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
848 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
849 /* weighted average of cplx of past frames */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
850 weight = 1.0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
851 for (int j = 0; j <= cplxBlur * 2 && j <= i; j++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
852 {
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
853 int index = i-j;
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
854 RateControlEntry *rcj = &m_rce2Pass[index];
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
855 gaussianWeight = weight * exp(-j * j / 200.0);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
856 weightSum += gaussianWeight;
8036
139b41632c64 rc: use m_frameDuration instead of rce->frameDuration to derive complexity for each frame in 2nd pass.
Aarthi Thirumalai
parents: 7998
diff changeset
857 cplxSum += gaussianWeight * (qScale2bits(rcj, 1) - rcj->miscBits) / clippedDuration;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
858 weight *= 1 - pow(rcj->iCuCount / m_ncu, 2);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
859 if (weight < .0001)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
860 break;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
861 }
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
862 m_rce2Pass[i].blurredComplexity= cplxSum / weightSum;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
863 }
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
864 CHECKED_MALLOC(qScale, double, m_numEntries);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
865 if (filterSize > 1)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
866 {
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
867 CHECKED_MALLOC(blurredQscale, double, m_numEntries);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
868 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
869 else
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
870 blurredQscale = qScale;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
871
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
872 /* Search for a factor which, when multiplied by the RCEQ values from
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
873 * each frame, adds up to the desired total size.
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
874 * There is no exact closed-form solution because of VBV constraints and
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
875 * because qscale2bits is not invertible, but we can start with the simple
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
876 * approximation of scaling the 1st pass by the ratio of bitrates.
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
877 * The search range is probably overkill, but speed doesn't matter here. */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
878 expectedBits = 1;
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
879 for (int i = 0; i < m_numEntries; i++)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
880 {
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
881 RateControlEntry* rce = &m_rce2Pass[i];
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
882 double q = getQScale(rce, 1.0);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
883 expectedBits += qScale2bits(rce, q);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
884 m_lastQScaleFor[rce->sliceType] = q;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
885 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
886 stepMult = allAvailableBits / expectedBits;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
887
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
888 rateFactor = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
889 for (double step = 1E4 * stepMult; step > 1E-7 * stepMult; step *= 0.5)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
890 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
891 expectedBits = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
892 rateFactor += step;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
893
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
894 m_lastNonBPictType = -1;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
895 m_lastAccumPNorm = 1;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
896 m_accumPNorm = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
897
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
898 m_lastQScaleFor[0] = m_lastQScaleFor[1] =
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
899 m_lastQScaleFor[2] = pow(baseCplx, 1 - m_qCompress) / rateFactor;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
900
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
901 /* find qscale */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
902 for (int i = 0; i < m_numEntries; i++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
903 {
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
904 RateControlEntry *rce = &m_rce2Pass[i];
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
905 qScale[i] = getQScale(rce, rateFactor);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
906 m_lastQScaleFor[rce->sliceType] = qScale[i];
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
907 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
908
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
909 /* fixed I/B qscale relative to P */
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
910 for (int i = 0; i < m_numEntries; i++)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
911 {
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
912 qScale[i] = getDiffLimitedQScale(&m_rce2Pass[i], qScale[i]);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
913 X265_CHECK(qScale[i] >= 0, "qScale became negative\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
914 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
915
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
916 /* smooth curve */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
917 if (filterSize > 1)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
918 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
919 X265_CHECK(filterSize % 2 == 1, "filterSize not an odd number\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
920 for (int i = 0; i < m_numEntries; i++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
921 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
922 double q = 0.0, sum = 0.0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
923 for (int j = 0; j < filterSize; j++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
924 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
925 int idx = i + j - filterSize / 2;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
926 double d = idx - i;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
927 double coeff = qBlur == 0 ? 1.0 : exp(-d * d / (qBlur * qBlur));
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
928 if (idx < 0 || idx >= m_numEntries)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
929 continue;
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
930 if (m_rce2Pass[i].sliceType != m_rce2Pass[idx].sliceType)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
931 continue;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
932 q += qScale[idx] * coeff;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
933 sum += coeff;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
934 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
935 blurredQscale[i] = q / sum;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
936 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
937 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
938
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
939 /* find expected bits */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
940 for (int i = 0; i < m_numEntries; i++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
941 {
12645
eca79c288012 Fix the RC Pass2 ABR
Kirithika <kirithika@multicorewareinc.com>
parents: 12633
diff changeset
942 RateControlEntry *rce = &m_rce2Pass[i];
8165
8740d938dbb7 rc: apply maxAU size restrictions while encoding each frame
Aarthi Thirumalai
parents: 8114
diff changeset
943 rce->newQScale = clipQscale(NULL, rce, blurredQscale[i]); // check if needed
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
944 X265_CHECK(rce->newQScale >= 0, "new Qscale is negative\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
945 expectedBits += qScale2bits(rce, rce->newQScale);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
946 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
947
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
948 if (expectedBits > allAvailableBits)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
949 rateFactor -= step;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
950 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
951
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
952 X265_FREE(qScale);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
953 if (filterSize > 1)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
954 X265_FREE(blurredQscale);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
955 if (m_isVbv)
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
956 if (!vbv2Pass(allAvailableBits, m_numEntries - 1, 0))
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
957 return false;
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
958 expectedBits = countExpectedBits(0, m_numEntries - 1);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
959 if (fabs(expectedBits / allAvailableBits - 1.0) > 0.01)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
960 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
961 double avgq = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
962 for (int i = 0; i < m_numEntries; i++)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
963 avgq += m_rce2Pass[i].newQScale;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
964 avgq = x265_qScale2qp(avgq / m_numEntries);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
965
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
966 if (expectedBits > allAvailableBits || !m_isVbv)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
967 x265_log(m_param, X265_LOG_WARNING, "Error: 2pass curve failed to converge\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
968 x265_log(m_param, X265_LOG_WARNING, "target: %.2f kbit/s, expected: %.2f kbit/s, avg QP: %.4f\n",
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
969 (double)m_param->rc.bitrate,
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
970 expectedBits * m_fps / (m_numEntries * 1000.),
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
971 avgq);
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
972 if (expectedBits < allAvailableBits && avgq < m_param->rc.qpMin + 2)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
973 {
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
974 if (m_param->rc.qpMin > 0)
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
975 x265_log(m_param, X265_LOG_WARNING, "try reducing target bitrate or reducing qp_min (currently %d)\n", m_param->rc.qpMin);
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
976 else
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
977 x265_log(m_param, X265_LOG_WARNING, "try reducing target bitrate\n");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
978 }
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
979 else if (expectedBits > allAvailableBits && avgq > m_param->rc.qpMax - 2)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
980 {
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
981 if (m_param->rc.qpMax < QP_MAX_MAX)
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
982 x265_log(m_param, X265_LOG_WARNING, "try increasing target bitrate or increasing qp_max (currently %d)\n", m_param->rc.qpMax);
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
983 else
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
984 x265_log(m_param, X265_LOG_WARNING, "try increasing target bitrate\n");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
985 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
986 else if (!(m_2pass && m_isVbv))
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
987 x265_log(m_param, X265_LOG_WARNING, "internal error\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
988 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
989
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
990 return true;
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
991
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
992 fail:
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
993 x265_log(m_param, X265_LOG_WARNING, "two-pass ABR initialization failed\n");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
994 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
995 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
996
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
997 bool RateControl::initPass2()
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
998 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
999 uint64_t allConstBits = 0, allCodedBits = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1000 uint64_t allAvailableBits = uint64_t(m_param->rc.bitrate * 1000. * m_numEntries * m_frameDuration);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1001 int startIndex, framesCount, endIndex;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1002 int fps = X265_MIN(m_param->keyframeMax, (int)(m_fps + 0.5));
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1003 startIndex = endIndex = framesCount = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1004 int diffQp = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1005 double targetBits = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1006 double expectedBits = 0;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1007 for (startIndex = m_start, endIndex = m_start; endIndex < m_numEntries; endIndex++)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1008 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1009 allConstBits += m_rce2Pass[endIndex].miscBits;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1010 allCodedBits += m_rce2Pass[endIndex].coeffBits + m_rce2Pass[endIndex].mvBits;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1011 if (m_param->rc.rateControlMode == X265_RC_CRF)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1012 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1013 framesCount = endIndex - startIndex + 1;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1014 diffQp += int (m_rce2Pass[endIndex].qpaRc - m_rce2Pass[endIndex].qpNoVbv);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1015 if (framesCount > fps)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1016 diffQp -= int (m_rce2Pass[endIndex - fps].qpaRc - m_rce2Pass[endIndex - fps].qpNoVbv);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1017 if (framesCount >= fps)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1018 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1019 if (diffQp >= 1)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1020 {
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1021 if (!m_isQpModified && endIndex > fps)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1022 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1023 double factor = 2;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1024 double step = 0;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1025 if (endIndex + fps >= m_numEntries)
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1026 {
11375
b6d8e66e7f71 rc: change reencode position for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11374
diff changeset
1027 m_start = endIndex - (endIndex % fps);
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1028 return true;
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1029 }
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1030 for (int start = endIndex + 1; start <= endIndex + fps && start < m_numEntries; start++)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1031 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1032 RateControlEntry *rce = &m_rce2Pass[start];
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1033 targetBits += qScale2bits(rce, x265_qp2qScale(rce->qpNoVbv));
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1034 expectedBits += qScale2bits(rce, rce->qScale);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1035 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1036 if (expectedBits < 0.95 * targetBits)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1037 {
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1038 m_isQpModified = true;
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1039 m_isGopReEncoded = true;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1040 while (endIndex + fps < m_numEntries)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1041 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1042 step = pow(2, factor / 6.0);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1043 expectedBits = 0;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1044 for (int start = endIndex + 1; start <= endIndex + fps; start++)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1045 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1046 RateControlEntry *rce = &m_rce2Pass[start];
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1047 rce->newQScale = rce->qScale / step;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1048 X265_CHECK(rce->newQScale >= 0, "new Qscale is negative\n");
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1049 expectedBits += qScale2bits(rce, rce->newQScale);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1050 rce->newQp = x265_qScale2qp(rce->newQScale);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1051 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1052 if (expectedBits >= targetBits && step > 1)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1053 factor *= 0.90;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1054 else
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1055 break;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1056 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1057
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1058 if (m_isVbv && endIndex + fps < m_numEntries)
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1059 if (!vbv2Pass((uint64_t)targetBits, endIndex + fps, endIndex + 1))
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1060 return false;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1061
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1062 targetBits = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1063 expectedBits = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1064
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1065 for (int start = endIndex - fps + 1; start <= endIndex; start++)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1066 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1067 RateControlEntry *rce = &m_rce2Pass[start];
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1068 targetBits += qScale2bits(rce, x265_qp2qScale(rce->qpNoVbv));
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1069 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1070 while (1)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1071 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1072 step = pow(2, factor / 6.0);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1073 expectedBits = 0;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1074 for (int start = endIndex - fps + 1; start <= endIndex; start++)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1075 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1076 RateControlEntry *rce = &m_rce2Pass[start];
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1077 rce->newQScale = rce->qScale * step;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1078 X265_CHECK(rce->newQScale >= 0, "new Qscale is negative\n");
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1079 expectedBits += qScale2bits(rce, rce->newQScale);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1080 rce->newQp = x265_qScale2qp(rce->newQScale);
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1081 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1082 if (expectedBits > targetBits && step > 1)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1083 factor *= 1.1;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1084 else
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1085 break;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1086 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1087 if (m_isVbv)
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1088 if (!vbv2Pass((uint64_t)targetBits, endIndex, endIndex - fps + 1))
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1089 return false;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1090 diffQp = 0;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1091 m_reencode = endIndex - fps + 1;
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1092 endIndex = endIndex + fps;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1093 startIndex = endIndex + 1;
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1094 m_start = startIndex;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1095 targetBits = expectedBits = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1096 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1097 else
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1098 targetBits = expectedBits = 0;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1099 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1100 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1101 else
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1102 m_isQpModified = false;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1103 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1104 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1105 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1106
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1107 if (m_param->rc.rateControlMode == X265_RC_ABR)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1108 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1109 if (allAvailableBits < allConstBits)
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1110 {
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1111 x265_log(m_param, X265_LOG_ERROR, "requested bitrate is too low. estimated minimum is %d kbps\n",
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1112 (int)(allConstBits * m_fps / framesCount * 1000.));
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1113 return false;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1114 }
11330
1efdce65fd9f rc: fix ABR 2-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11313
diff changeset
1115 if (!analyseABR2Pass(allAvailableBits))
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1116 return false;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1117 }
11313
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1118
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1119 m_start = X265_MAX(m_start, endIndex - fps);
f44b6adbffd3 rc: fix GOP reencode logic for cappedvbr
Divya Manivannan <divya@multicorewareinc.com>
parents: 11298
diff changeset
1120
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1121 return true;
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1122 }
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1123
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1124 bool RateControl::vbv2Pass(uint64_t allAvailableBits, int endPos, int startPos)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1125 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1126 /* for each interval of bufferFull .. underflow, uniformly increase the qp of all
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1127 * frames in the interval until either buffer is full at some intermediate frame or the
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1128 * last frame in the interval no longer underflows. Recompute intervals and repeat.
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1129 * Then do the converse to put bits back into overflow areas until target size is met */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1130
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1131 double *fills;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1132 double expectedBits = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1133 double adjustment;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1134 double prevBits = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1135 int t0, t1;
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
1136 double qScaleMin = x265_qp2qScale(m_param->rc.qpMin);
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
1137 double qScaleMax = x265_qp2qScale(m_param->rc.qpMax);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1138 int iterations = 0 , adjMin, adjMax;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1139 CHECKED_MALLOC(fills, double, m_numEntries + 1);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1140 fills++;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1141
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1142 /* adjust overall stream size */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1143 do
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1144 {
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1145 iterations++;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1146 prevBits = expectedBits;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1147
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1148 if (expectedBits)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1149 { /* not first iteration */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1150 adjustment = X265_MAX(X265_MIN(expectedBits / allAvailableBits, 0.999), 0.9);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1151 fills[-1] = m_bufferSize * m_param->rc.vbvBufferInit;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1152 t0 = startPos;
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1153 /* fix overflows */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1154 adjMin = 1;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1155 while (adjMin && findUnderflow(fills, &t0, &t1, 1, endPos))
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1156 {
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
1157 adjMin = fixUnderflow(t0, t1, adjustment, qScaleMin, qScaleMax);
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1158 t0 = t1;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1159 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1160 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1161
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1162 fills[-1] = m_bufferSize * (1. - m_param->rc.vbvBufferInit);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1163 t0 = 0;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1164 /* fix underflows -- should be done after overflow, as we'd better undersize target than underflowing VBV */
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1165 adjMax = 1;
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1166 while (adjMax && findUnderflow(fills, &t0, &t1, 0, endPos))
11525
98a948623fdc rc: add qpmin and qpmax options
Gopi Satykrishna Akisetty <gopi.satykrishna@multicorewareinc.com>
parents: 11518
diff changeset
1167 adjMax = fixUnderflow(t0, t1, 1.001, qScaleMin, qScaleMax);
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1168 expectedBits = countExpectedBits(startPos, endPos);
7495
2f87f3c24b4a rc: generate cutree offsets for 2nd pass from stats
Aarthi Thirumalai
parents: 7494
diff changeset
1169 }
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1170 while ((expectedBits < .995 * allAvailableBits) && ((int64_t)(expectedBits+.5) > (int64_t)(prevBits+.5)) && !(m_param->rc.rateControlMode == X265_RC_CRF));
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1171 if (!adjMax)
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1172 x265_log(m_param, X265_LOG_WARNING, "vbv-maxrate issue, qpmax or vbv-maxrate too low\n");
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1173 /* store expected vbv filling values for tracking when encoding */
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1174 for (int i = startPos; i <= endPos; i++)
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1175 m_rce2Pass[i].expectedVbv = m_bufferSize - fills[i];
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1176 X265_FREE(fills - 1);
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1177 return true;
7545
f54d5368590a rc: nits
Steve Borho <steve@borho.org>
parents: 7544
diff changeset
1178
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1179 fail:
7544
650c2d3c5194 rc: instrument all error conditions in initPass2()
Steve Borho <steve@borho.org>
parents: 7530
diff changeset
1180 x265_log(m_param, X265_LOG_ERROR, "malloc failure in two-pass VBV init\n");
7319
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1181 return false;
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1182 }
03164c7ddcbb rc: init RC data for second pass in a multi pass encode
Aarthi Thirumalai
parents: 7237
diff changeset
1183
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1184 /* In 2pass, force the same frame types as in the 1st pass */
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1185 int RateControl::rateControlSliceType(int frameNum)
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1186 {
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1187 if (m_param->rc.bStatRead)
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1188 {
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1189 if (frameNum >= m_numEntries)
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1190 {
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1191 /* We could try to initialize everything required for ABR and
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1192 * adaptive B-frames, but that would be complicated.
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1193 * So just calculate the average QP used so far. */
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1194 m_param->rc.qp = (m_accumPQp < 1) ? ABR_INIT_QP_MAX : (int)(m_accumPQp + 0.5);
9015
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
1195 m_qpConstant[P_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, m_param->rc.qp);
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
1196 m_qpConstant[I_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_param->rc.qp - m_ipOffset + 0.5));
9b553540a49b common: unify clip templates, no output changes
Steve Borho <steve@borho.org>
parents: 8951
diff changeset
1197 m_qpConstant[B_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_param->rc.qp + m_pbOffset + 0.5));
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1198
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1199 x265_log(m_param, X265_LOG_ERROR, "2nd pass has more frames than 1st pass (%d)\n", m_numEntries);
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1200 x265_log(m_param, X265_LOG_ERROR, "continuing anyway, at constant QP=%d\n", m_param->rc.qp);
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1201 if (m_param->bFrameAdaptive)
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1202 x265_log(m_param, X265_LOG_ERROR, "disabling adaptive B-frames\n");
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1203
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1204 m_isAbr = 0;
7610
0308c9e187be rc,weight: nits
Steve Borho <steve@borho.org>
parents: 7609
diff changeset
1205 m_2pass = 0;
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1206 m_param->rc.rateControlMode = X265_RC_CQP;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1207 m_param->rc.bStatRead = 0;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1208 m_param->bFrameAdaptive = 0;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1209 m_param->scenecutThreshold = 0;
12584
ddf9734816a8 Histogram Based Scene Cut Detection.
Srikanth Kurapati <srikanth.kurapati@multicorewareinc.com>
parents: 12583
diff changeset
1210 m_param->bHistBasedSceneCut = 0;
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1211 m_param->rc.cuTree = 0;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1212 if (m_param->bframes > 1)
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1213 m_param->bframes = 1;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1214 return X265_TYPE_AUTO;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1215 }
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1216 int index = m_encOrder[frameNum];
11580
dc4893950b27 rc: fix non-IDR slicetype in multi-pass
Divya Manivannan <divya@multicorewareinc.com>
parents: 11574
diff changeset
1217 int frameType = m_rce2Pass[index].sliceType == I_SLICE ? (m_rce2Pass[index].isIdr ? X265_TYPE_IDR : X265_TYPE_I)
11215
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1218 : m_rce2Pass[index].sliceType == P_SLICE ? X265_TYPE_P
73cc1cd4c7fc rc: implement 2 pass CRF, when vbv is enabled.
Divya Manivannan <divya@multicorewareinc.com>
parents: 11167
diff changeset
1219 : (m_rce2Pass[index].sliceType == B_SLICE && m_rce2Pass[index].keptAsRef ? X265_TYPE_BREF : X265_TYPE_B);
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1220 return frameType;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1221 }
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1222 else
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1223 return X265_TYPE_AUTO;
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1224 }
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1225
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1226 void RateControl::initFramePredictors()
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1227 {
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1228 /* Frame Predictors used in vbv */
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1229 for (int i = 0; i < 4; i++)
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1230 {
11147
b3b931b0e6cd rc: change the predictors update algorithm
Aarthi Thirumalai
parents: 11146
diff changeset
1231 m_pred[i].coeffMin = 1.0 / 4;
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1232 m_pred[i].coeff = 1.0;
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1233 m_pred[i].count = 1.0;
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1234 m_pred[i].decay = 0.5;
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1235 m_pred[i].offset = 0.0;
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1236 }
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1237 m_pred[0].coeff = m_pred[3].coeff = 0.75;
11147
b3b931b0e6cd rc: change the predictors update algorithm
Aarthi Thirumalai
parents: 11146
diff changeset
1238 m_pred[0].coeffMin = m_pred[3].coeffMin = 0.75 / 4;
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
1239 if (m_isGrainEnabled) // when tuned for grain
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1240 {
11147
b3b931b0e6cd rc: change the predictors update algorithm
Aarthi Thirumalai
parents: 11146
diff changeset
1241 m_pred[1].coeffMin = 0.75 / 4;
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1242 m_pred[1].coeff = 0.75;
11369
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
1243 m_pred[0].coeff = m_pred[3].coeff = 0.75;
305a1272a412 rc: fix Rate Control for grainy content
Aarthi Thirumalai
parents: 11358
diff changeset
1244 m_pred[0].coeffMin = m_pred[3].coeffMin = 0.75 / 4;
11046
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1245 }
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1246 }
e27327f5da35 rc: don't update predictors from previous frames, after reset at scenecut.
Aarthi Thirumalai
parents: 11045
diff changeset
1247
8275
89c388e280e4 yuv: bring Yuv, PicYuv classes into common/
Steve Borho <steve@borho.org>
parents: 8234
diff changeset
1248 int RateControl::rateControlStart(Frame* curFrame, RateControlEntry* rce, Encoder* enc)
3370
9974f57f56a1 add (incomplete) ratecontrol.cpp and ratecontrol.h
sumalatha
parents:
diff changeset
1249 {
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1250 int orderValue = m_startEndOrder.get();
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1251 int startOrdinal = rce->encodeOrder * 2;
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1252
7497
a2fd8a71de61 rc: prevent deadlock on encoder flush or close
Steve Borho <steve@borho.org>
parents: 7496
diff changeset
1253 while (orderValue < startOrdinal && !m_bTerminated)
9880
e81e94e4748b rc: nit
Steve Borho <steve@borho.org>
parents: 9810
diff changeset
1254 orderValue = m_startEndOrder.waitForChange(orderValue);
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1255
8275
89c388e280e4 yuv: bring Yuv, PicYuv classes into common/
Steve Borho <steve@borho.org>
parents: 8234
diff changeset
1256 if (!curFrame)
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1257 {
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1258 // faked rateControlStart calls when the encoder is flushing
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1259 m_startEndOrder.incr();
7605
71c023799f3d frameencoder: re-order compressFrame() to avoid dependency problem with HRD
Steve Borho <steve@borho.org>
parents: 7555
diff changeset
1260 return 0;
7484
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1261 }
0a8ecd8a6cf9 rc: update ratecontrol stats in every frame, avoid frame parallelism lag in abr
Santhoshini Sekar <santhoshini@multicorewareinc.com>
parents: 7482
diff changeset
1262
8494
16cf099c8a78 frame: move rate control data to FrameData, cleanup variable names
Steve Borho <steve@borho.org>
parents: 8475
diff changeset
1263 FrameData& curEncData = *curFrame->m_encData;
16cf099c8a78 frame: move rate control data to FrameData, cleanup variable names
Steve Borho <steve@borho.org>
parents: 8475
diff changeset
1264 m_curSlice = curEncData.m_slice;
7451
880285f10294 slice: remove get/set methods for m_sliceType
Steve Borho <steve@borho.org>
parents: 7446
diff changeset
1265 m_sliceType = m_curSlice->m_sliceType;
7001
d3b1d2d3f104 rc: convert RateControl to a class
Steve Borho <steve@borho.org>
parents: 6998
diff changeset
1266 rce->sliceType = m_sliceType;
10131
1b15d6129041 rc: separate frame bits predictor objects for BRef and B frames
Aarthi Thirumalai
parents: 10065
diff changeset
1267 if (!m_2pass)
1b15d6129041 rc: separate frame bits predictor objects for BRef and B frames
Aarthi Thirumalai
parents: 10065
diff changeset
1268 rce->keptAsRef = IS_REFERENCED(curFrame);
10160
2884fb779578 rc: add helper function to decide the VBV predictor type for each frame
Aarthi Thirumalai
parents: 10132
diff changeset
1269 m_predType = getPredictorType(curFrame->m_lowres.sliceType, m_sliceType);
7494
1b2fbf3208ca rc: fix sliceType of 2nd pass from prev pass stats
Aarthi Thirumalai
parents: 7493
diff changeset
1270 rce->poc = m_curSlice->m_poc;
12445
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
1271
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1272 if (!m_param->bResetZoneConfig && (rce->encodeOrder % m_param->reconfigWindowSize == 0))
12445
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
1273 {
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1274 int index = m_zoneBufferIdx % m_param->rc.zonefileCount;
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1275 int read = m_top->zoneReadCount[index].get();
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1276 int write = m_top->zoneWriteCount[index].get();
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1277 if (write <= read)
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1278 write = m_top->zoneWriteCount[index].waitForChange(write);
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1279 m_zoneBufferIdx++;
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1280
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1281 for (int i = 0; i < m_param->rc.zonefileCount; i++)
12445
2db4cd096099 zone: reconfigure rate control parameters
Pooja Venkatesan <pooja@multicorewareinc.com>
parents: 12423
diff changeset
1282 {
12581
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1283 if (m_param->rc.zones[i].startFrame == rce->encodeOrder)
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1284 {
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1285 m_param->rc.bitrate = m_param->rc.zones[i].zoneParam->rc.bitrate;
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1286 m_param->rc.vbvMaxBitrate = m_param->rc.zones[i].zoneParam->rc.vbvMaxBitrate;
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1287 memcpy(m_relativeComplexity, m_param->rc.zones[i].relativeComplexity, sizeof(double) * m_param->reconfigWindowSize);
5297824e605c zone: tune base QP to achieve zone target
Aruna Matheswaran <aruna@multicorewareinc.com>
parents: 12578
diff changeset
1288 reconfigureRC();
12631
6cb4a24acac3 zone: Enable strict VBV conformance for zone encode as per requirement
Snehaa Giridharan <snehaa@multicorewareinc.com>
parents: 12596
diff changeset
1289 m_isCbr = 1; /* Always vbvmaxrate == bitrate here*/
12581