Mercurial > hg > forks > libbpg
comparison x265/source/encoder/ratecontrol.h @ 0:772086c29cc7
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 16 Nov 2016 11:16:33 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:772086c29cc7 |
---|---|
1 /***************************************************************************** | |
2 * Copyright (C) 2013 x265 project | |
3 * | |
4 * Authors: Sumalatha Polureddy <sumalatha@multicorewareinc.com> | |
5 * Aarthi Priya Thirumalai <aarthi@multicorewareinc.com> | |
6 * Xun Xu, PPLive Corporation <xunxu@pptv.com> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. | |
21 * | |
22 * This program is also available under a commercial proprietary license. | |
23 * For more information, contact us at license @ x265.com. | |
24 *****************************************************************************/ | |
25 | |
26 #ifndef X265_RATECONTROL_H | |
27 #define X265_RATECONTROL_H | |
28 | |
29 #include "common.h" | |
30 #include "sei.h" | |
31 | |
32 namespace X265_NS { | |
33 // encoder namespace | |
34 | |
35 class Encoder; | |
36 class Frame; | |
37 class SEIBufferingPeriod; | |
38 struct SPS; | |
39 #define BASE_FRAME_DURATION 0.04 | |
40 | |
41 /* Arbitrary limitations as a sanity check. */ | |
42 #define MAX_FRAME_DURATION 1.00 | |
43 #define MIN_FRAME_DURATION 0.01 | |
44 | |
45 #define MIN_AMORTIZE_FRAME 10 | |
46 #define MIN_AMORTIZE_FRACTION 0.2 | |
47 #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f) | |
48 | |
49 struct Predictor | |
50 { | |
51 double coeff; | |
52 double count; | |
53 double decay; | |
54 double offset; | |
55 }; | |
56 | |
57 struct HRDTiming | |
58 { | |
59 double cpbInitialAT; | |
60 double cpbFinalAT; | |
61 double dpbOutputTime; | |
62 double cpbRemovalTime; | |
63 }; | |
64 | |
65 struct RateControlEntry | |
66 { | |
67 Predictor rowPreds[3][2]; | |
68 Predictor* rowPred[2]; | |
69 | |
70 int64_t lastSatd; /* Contains the picture cost of the previous frame, required for resetAbr and VBV */ | |
71 int64_t leadingNoBSatd; | |
72 int64_t rowTotalBits; /* update cplxrsum and totalbits at the end of 2 rows */ | |
73 double blurredComplexity; | |
74 double qpaRc; | |
75 double qpAq; | |
76 double qRceq; | |
77 double frameSizePlanned; /* frame Size decided by RateCotrol before encoding the frame */ | |
78 double bufferRate; | |
79 double movingAvgSum; | |
80 double rowCplxrSum; | |
81 double qpNoVbv; | |
82 double bufferFill; | |
83 double frameDuration; | |
84 double clippedDuration; | |
85 double frameSizeEstimated; /* hold frameSize, updated from cu level vbv rc */ | |
86 double frameSizeMaximum; /* max frame Size according to minCR restrictions and level of the video */ | |
87 int sliceType; | |
88 int bframes; | |
89 int poc; | |
90 int encodeOrder; | |
91 bool bLastMiniGopBFrame; | |
92 bool isActive; | |
93 double amortizeFrames; | |
94 double amortizeFraction; | |
95 /* Required in 2-pass rate control */ | |
96 uint64_t expectedBits; /* total expected bits up to the current frame (current one excluded) */ | |
97 double iCuCount; | |
98 double pCuCount; | |
99 double skipCuCount; | |
100 double expectedVbv; | |
101 double qScale; | |
102 double newQScale; | |
103 double newQp; | |
104 int mvBits; | |
105 int miscBits; | |
106 int coeffBits; | |
107 bool keptAsRef; | |
108 | |
109 SEIPictureTiming *picTimingSEI; | |
110 HRDTiming *hrdTiming; | |
111 }; | |
112 | |
113 class RateControl | |
114 { | |
115 public: | |
116 | |
117 x265_param* m_param; | |
118 Slice* m_curSlice; /* all info about the current frame */ | |
119 SliceType m_sliceType; /* Current frame type */ | |
120 int m_ncu; /* number of CUs in a frame */ | |
121 int m_qp; /* updated qp for current frame */ | |
122 | |
123 bool m_isAbr; | |
124 bool m_isVbv; | |
125 bool m_isCbr; | |
126 bool m_singleFrameVbv; | |
127 | |
128 bool m_isAbrReset; | |
129 int m_lastAbrResetPoc; | |
130 | |
131 double m_rateTolerance; | |
132 double m_frameDuration; /* current frame duration in seconds */ | |
133 double m_bitrate; | |
134 double m_rateFactorConstant; | |
135 double m_bufferSize; | |
136 double m_bufferFillFinal; /* real buffer as of the last finished frame */ | |
137 double m_bufferFill; /* planned buffer, if all in-progress frames hit their bit budget */ | |
138 double m_bufferRate; /* # of bits added to buffer_fill after each frame */ | |
139 double m_vbvMaxRate; /* in kbps */ | |
140 double m_rateFactorMaxIncrement; /* Don't allow RF above (CRF + this value). */ | |
141 double m_rateFactorMaxDecrement; /* don't allow RF below (this value). */ | |
142 | |
143 Predictor m_pred[4]; /* Slice predictors to preidct bits for each Slice type - I,P,Bref and B */ | |
144 int64_t m_leadingNoBSatd; | |
145 int m_predType; /* Type of slice predictors to be used - depends on the slice type */ | |
146 double m_ipOffset; | |
147 double m_pbOffset; | |
148 int64_t m_bframeBits; | |
149 int64_t m_currentSatd; | |
150 int m_qpConstant[3]; | |
151 int m_lastNonBPictType; | |
152 int m_framesDone; /* # of frames passed through RateCotrol already */ | |
153 | |
154 double m_cplxrSum; /* sum of bits*qscale/rceq */ | |
155 double m_wantedBitsWindow; /* target bitrate * window */ | |
156 double m_accumPQp; /* for determining I-frame quant */ | |
157 double m_accumPNorm; | |
158 double m_lastQScaleFor[3]; /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */ | |
159 double m_lstep; | |
160 double m_shortTermCplxSum; | |
161 double m_shortTermCplxCount; | |
162 double m_lastRceq; | |
163 double m_qCompress; | |
164 int64_t m_totalBits; /* total bits used for already encoded frames (after ammortization) */ | |
165 int64_t m_encodedBits; /* bits used for encoded frames (without ammortization) */ | |
166 double m_fps; | |
167 int64_t m_satdCostWindow[50]; | |
168 int64_t m_encodedBitsWindow[50]; | |
169 int m_sliderPos; | |
170 | |
171 /* To detect a pattern of low detailed static frames in single pass ABR using satdcosts */ | |
172 int64_t m_lastBsliceSatdCost; | |
173 int m_numBframesInPattern; | |
174 bool m_isPatternPresent; | |
175 bool m_isSceneTransition; | |
176 | |
177 /* a common variable on which rateControlStart, rateControlEnd and rateControUpdateStats waits to | |
178 * sync the calls to these functions. For example | |
179 * -F2: | |
180 * rceStart 10 | |
181 * rceUpdate 10 | |
182 * rceEnd 9 | |
183 * rceStart 11 | |
184 * rceUpdate 11 | |
185 * rceEnd 10 | |
186 * rceStart 12 | |
187 * rceUpdate 12 | |
188 * rceEnd 11 */ | |
189 ThreadSafeInteger m_startEndOrder; | |
190 int m_finalFrameCount; /* set when encoder begins flushing */ | |
191 bool m_bTerminated; /* set true when encoder is closing */ | |
192 | |
193 /* hrd stuff */ | |
194 SEIBufferingPeriod m_bufPeriodSEI; | |
195 double m_nominalRemovalTime; | |
196 double m_prevCpbFinalAT; | |
197 | |
198 /* 2 pass */ | |
199 bool m_2pass; | |
200 int m_numEntries; | |
201 FILE* m_statFileOut; | |
202 FILE* m_cutreeStatFileOut; | |
203 FILE* m_cutreeStatFileIn; | |
204 double m_lastAccumPNorm; | |
205 double m_expectedBitsSum; /* sum of qscale2bits after rceq, ratefactor, and overflow, only includes finished frames */ | |
206 int64_t m_predictedBits; | |
207 RateControlEntry* m_rce2Pass; | |
208 | |
209 struct | |
210 { | |
211 uint16_t *qpBuffer[2]; /* Global buffers for converting MB-tree quantizer data. */ | |
212 int qpBufPos; /* In order to handle pyramid reordering, QP buffer acts as a stack. | |
213 * This value is the current position (0 or 1). */ | |
214 } m_cuTreeStats; | |
215 | |
216 RateControl(x265_param& p); | |
217 bool init(const SPS& sps); | |
218 void initHRD(SPS& sps); | |
219 | |
220 void setFinalFrameCount(int count); | |
221 void terminate(); /* un-block all waiting functions so encoder may close */ | |
222 void destroy(); | |
223 | |
224 // to be called for each curFrame to process RateControl and set QP | |
225 int rateControlStart(Frame* curFrame, RateControlEntry* rce, Encoder* enc); | |
226 void rateControlUpdateStats(RateControlEntry* rce); | |
227 int rateControlEnd(Frame* curFrame, int64_t bits, RateControlEntry* rce); | |
228 int rowDiagonalVbvRateControl(Frame* curFrame, uint32_t row, RateControlEntry* rce, double& qpVbv); | |
229 int rateControlSliceType(int frameNum); | |
230 bool cuTreeReadFor2Pass(Frame* curFrame); | |
231 void hrdFullness(SEIBufferingPeriod* sei); | |
232 int writeRateControlFrameStats(Frame* curFrame, RateControlEntry* rce); | |
233 protected: | |
234 | |
235 static const int s_slidingWindowFrames; | |
236 static const char* s_defaultStatFileName; | |
237 | |
238 double m_amortizeFraction; | |
239 int m_amortizeFrames; | |
240 int m_residualFrames; | |
241 int m_partialResidualFrames; | |
242 int m_residualCost; | |
243 int m_partialResidualCost; | |
244 | |
245 x265_zone* getZone(); | |
246 double getQScale(RateControlEntry *rce, double rateFactor); | |
247 double rateEstimateQscale(Frame* pic, RateControlEntry *rce); // main logic for calculating QP based on ABR | |
248 double tuneAbrQScaleFromFeedback(double qScale); | |
249 void accumPQpUpdate(); | |
250 | |
251 int getPredictorType(int lowresSliceType, int sliceType); | |
252 void updateVbv(int64_t bits, RateControlEntry* rce); | |
253 void updatePredictor(Predictor *p, double q, double var, double bits); | |
254 double clipQscale(Frame* pic, RateControlEntry* rce, double q); | |
255 void updateVbvPlan(Encoder* enc); | |
256 double predictSize(Predictor *p, double q, double var); | |
257 void checkAndResetABR(RateControlEntry* rce, bool isFrameDone); | |
258 double predictRowsSizeSum(Frame* pic, RateControlEntry* rce, double qpm, int32_t& encodedBits); | |
259 bool initPass2(); | |
260 double getDiffLimitedQScale(RateControlEntry *rce, double q); | |
261 double countExpectedBits(); | |
262 bool vbv2Pass(uint64_t allAvailableBits); | |
263 bool findUnderflow(double *fills, int *t0, int *t1, int over); | |
264 bool fixUnderflow(int t0, int t1, double adjustment, double qscaleMin, double qscaleMax); | |
265 }; | |
266 } | |
267 #endif // ifndef X265_RATECONTROL_H |