1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Sumalatha Polureddy <sumalatha@multicorewareinc.com>
5 * Aarthi Priya Thirumalai <aarthi@multicorewareinc.com>
6 * Xun Xu, PPLive Corporation <xunxu@pptv.com>
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.
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.
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.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at license @ x265.com.
24 *****************************************************************************/
26 #ifndef X265_RATECONTROL_H
27 #define X265_RATECONTROL_H
38 class SEIBufferingPeriod
;
39 #define BASE_FRAME_DURATION 0.04
41 /* Arbitrary limitations as a sanity check. */
42 #define MAX_FRAME_DURATION 1.00
43 #define MIN_FRAME_DURATION 0.01
45 #define CLIP_DURATION(f) Clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f)
47 /* Current frame stats for 2 pass */
50 int mvBits
; /* MV bits (MV+Ref+Block Type) */
51 int coeffBits
; /* Texture bits (DCT coefs) */
58 /* CU type counts stored as percentage */
77 double cpbRemovalTime
;
80 struct RateControlEntry
82 int64_t lastSatd
; /* Contains the picture cost of the previous frame, required for resetAbr and VBV */
87 int64_t leadingNoBSatd
;
88 bool bLastMiniGopBFrame
;
89 double blurredComplexity
;
93 double frameSizePlanned
; /* frame Size decided by RateCotrol before encoding the frame */
97 int64_t rowTotalBits
; /* update cplxrsum and totalbits at the end of 2 rows */
100 double frameDuration
;
101 double clippedDuration
;
102 Predictor rowPreds
[3][2];
103 Predictor
* rowPred
[2];
104 double frameSizeEstimated
; /* hold frameSize, updated from cu level vbv rc */
105 double frameSizeMaximum
; /* max frame Size according to minCR restrictions and level of the video */
107 SEIPictureTiming
*picTimingSEI
;
108 HRDTiming
*hrdTiming
;
109 /* Required in 2-pass rate control */
121 uint64_t expectedBits
; /* total expected bits up to the current frame (current one excluded) */
129 Slice
* m_curSlice
; /* all info about the current frame */
130 SliceType m_sliceType
; /* Current frame type */
131 int m_ncu
; /* number of CUs in a frame */
132 int m_qp
; /* updated qp for current frame */
137 bool m_singleFrameVbv
;
140 int m_lastAbrResetPoc
;
142 double m_frameDuration
; /* current frame duration in seconds */
144 double m_rateFactorConstant
;
146 double m_bufferFillFinal
; /* real buffer as of the last finished frame */
147 double m_bufferFill
; /* planned buffer, if all in-progress frames hit their bit budget */
148 double m_bufferRate
; /* # of bits added to buffer_fill after each frame */
149 double m_vbvMaxRate
; /* in kbps */
150 double m_rateFactorMaxIncrement
; /* Don't allow RF above (CRF + this value). */
151 double m_rateFactorMaxDecrement
; /* don't allow RF below (this value). */
154 Predictor m_predBfromP
;
156 int m_leadingBframes
;
157 int64_t m_bframeBits
;
158 int64_t m_currentSatd
;
163 int m_lastNonBPictType
;
164 int64_t m_leadingNoBSatd
;
166 double m_cplxrSum
; /* sum of bits*qscale/rceq */
167 double m_wantedBitsWindow
; /* target bitrate * window */
168 double m_accumPQp
; /* for determining I-frame quant */
170 double m_lastQScaleFor
[3]; /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */
172 double m_shortTermCplxSum
;
173 double m_shortTermCplxCount
;
176 int64_t m_totalBits
; /* total bits used for already encoded frames */
177 int m_framesDone
; /* # of frames passed through RateCotrol already */
179 int64_t m_satdCostWindow
[50];
181 int64_t m_encodedBitsWindow
[50];
182 /* a common variable on which rateControlStart, rateControlEnd and rateControUpdateStats waits to
183 * sync the calls to these functions. For example
194 ThreadSafeInteger m_startEndOrder
;
195 int m_finalFrameCount
; /* set when encoder begins flushing */
196 bool m_bTerminated
; /* set true when encoder is closing */
199 SEIBufferingPeriod m_bufPeriodSEI
;
200 double m_nominalRemovalTime
;
201 double m_prevCpbFinalAT
;
206 FILE* m_cutreeStatFileOut
;
207 FILE* m_cutreeStatFileIn
;
209 RateControlEntry
*m_rce2Pass
;
210 double m_lastAccumPNorm
;
211 int64_t m_predictedBits
;
212 double m_expectedBitsSum
; /* sum of qscale2bits after rceq, ratefactor, and overflow, only includes finished frames */
215 uint16_t *qpBuffer
[2]; /* Global buffers for converting MB-tree quantizer data. */
216 int qpBufPos
; /* In order to handle pyramid reordering, QP buffer acts as a stack.
217 * This value is the current position (0 or 1). */
220 RateControl(x265_param
*p
);
221 void setFinalFrameCount(int count
);
222 void terminate(); /* un-block all waiting functions so encoder may close */
225 // to be called for each curFrame to process RateControl and set QP
226 int rateControlStart(Frame
* curFrame
, RateControlEntry
* rce
, Encoder
* enc
);
227 void calcAdaptiveQuantFrame(Frame
*curFrame
);
228 void rateControlUpdateStats(RateControlEntry
* rce
);
229 int rateControlEnd(Frame
* curFrame
, int64_t bits
, RateControlEntry
* rce
, FrameStats
* stats
);
230 int rowDiagonalVbvRateControl(Frame
* curFrame
, uint32_t row
, RateControlEntry
* rce
, double& qpVbv
);
231 void hrdFullness(SEIBufferingPeriod
* sei
);
232 bool init(const SPS
* sps
);
233 void initHRD(SPS
* sps
);
234 int rateControlSliceType(int frameNum
);
235 bool cuTreeReadFor2Pass(Frame
* curFrame
);
236 double tuneAbrQScaleFromFeedback(double qScale
);
240 static const int s_slidingWindowFrames
;
241 static const char *s_defaultStatFileName
;
243 int m_residualFrames
;
244 int m_partialResidualFrames
;
246 int m_partialResidualCost
;
247 int m_amortizeFrames
;
248 double m_amortizeFraction
;
250 double getQScale(RateControlEntry
*rce
, double rateFactor
);
251 double rateEstimateQscale(Frame
* pic
, RateControlEntry
*rce
); // main logic for calculating QP based on ABR
252 void accumPQpUpdate();
253 uint32_t acEnergyCu(Frame
* pic
, uint32_t block_x
, uint32_t block_y
);
255 void updateVbv(int64_t bits
, RateControlEntry
* rce
);
256 void updatePredictor(Predictor
*p
, double q
, double var
, double bits
);
257 double clipQscale(Frame
* pic
, RateControlEntry
* rce
, double q
);
258 void updateVbvPlan(Encoder
* enc
);
259 double predictSize(Predictor
*p
, double q
, double var
);
260 void checkAndResetABR(RateControlEntry
* rce
, bool isFrameDone
);
261 double predictRowsSizeSum(Frame
* pic
, RateControlEntry
* rce
, double qpm
, int32_t& encodedBits
);
263 double getDiffLimitedQScale(RateControlEntry
*rce
, double q
);
264 double countExpectedBits();
265 bool vbv2Pass(uint64_t allAvailableBits
);
266 bool findUnderflow(double *fills
, int *t0
, int *t1
, int over
);
267 bool fixUnderflow(int t0
, int t1
, double adjustment
, double qscaleMin
, double qscaleMax
);
270 #endif // ifndef X265_RATECONTROL_H