| 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 { |
| 33 | // encoder namespace |
| 34 | |
| 35 | class Encoder; |
| 36 | class Frame; |
| 37 | struct SPS; |
| 38 | class SEIBufferingPeriod; |
| 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 CLIP_DURATION(f) Clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f) |
| 46 | |
| 47 | /* Current frame stats for 2 pass */ |
| 48 | struct FrameStats |
| 49 | { |
| 50 | int mvBits; /* MV bits (MV+Ref+Block Type) */ |
| 51 | int coeffBits; /* Texture bits (DCT coefs) */ |
| 52 | int miscBits; |
| 53 | |
| 54 | int iCuCnt; |
| 55 | int pCuCnt; |
| 56 | int skipCuCnt; |
| 57 | |
| 58 | /* CU type counts stored as percentage */ |
| 59 | double percentIntra; |
| 60 | double percentInter; |
| 61 | double percentSkip; |
| 62 | }; |
| 63 | |
| 64 | struct Predictor |
| 65 | { |
| 66 | double coeff; |
| 67 | double count; |
| 68 | double decay; |
| 69 | double offset; |
| 70 | }; |
| 71 | |
| 72 | struct HRDTiming |
| 73 | { |
| 74 | double cpbInitialAT; |
| 75 | double cpbFinalAT; |
| 76 | double dpbOutputTime; |
| 77 | double cpbRemovalTime; |
| 78 | }; |
| 79 | |
| 80 | struct RateControlEntry |
| 81 | { |
| 82 | int64_t lastSatd; /* Contains the picture cost of the previous frame, required for resetAbr and VBV */ |
| 83 | int sliceType; |
| 84 | int bframes; |
| 85 | int poc; |
| 86 | int encodeOrder; |
| 87 | int64_t leadingNoBSatd; |
| 88 | bool bLastMiniGopBFrame; |
| 89 | double blurredComplexity; |
| 90 | double qpaRc; |
| 91 | double qpAq; |
| 92 | double qRceq; |
| 93 | double frameSizePlanned; /* frame Size decided by RateCotrol before encoding the frame */ |
| 94 | double bufferRate; |
| 95 | double movingAvgSum; |
| 96 | double rowCplxrSum; |
| 97 | int64_t rowTotalBits; /* update cplxrsum and totalbits at the end of 2 rows */ |
| 98 | double qpNoVbv; |
| 99 | double bufferFill; |
| 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 */ |
| 106 | bool isActive; |
| 107 | SEIPictureTiming *picTimingSEI; |
| 108 | HRDTiming *hrdTiming; |
| 109 | /* Required in 2-pass rate control */ |
| 110 | double iCuCount; |
| 111 | double pCuCount; |
| 112 | double skipCuCount; |
| 113 | bool keptAsRef; |
| 114 | double expectedVbv; |
| 115 | double qScale; |
| 116 | double newQScale; |
| 117 | double newQp; |
| 118 | int mvBits; |
| 119 | int miscBits; |
| 120 | int coeffBits; |
| 121 | uint64_t expectedBits; /* total expected bits up to the current frame (current one excluded) */ |
| 122 | }; |
| 123 | |
| 124 | class RateControl |
| 125 | { |
| 126 | public: |
| 127 | |
| 128 | x265_param* m_param; |
| 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 */ |
| 133 | |
| 134 | bool m_isAbr; |
| 135 | bool m_isVbv; |
| 136 | bool m_isCbr; |
| 137 | bool m_singleFrameVbv; |
| 138 | |
| 139 | bool m_isAbrReset; |
| 140 | int m_lastAbrResetPoc; |
| 141 | |
| 142 | double m_frameDuration; /* current frame duration in seconds */ |
| 143 | double m_bitrate; |
| 144 | double m_rateFactorConstant; |
| 145 | double m_bufferSize; |
| 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). */ |
| 152 | |
| 153 | Predictor m_pred[5]; |
| 154 | Predictor m_predBfromP; |
| 155 | |
| 156 | int m_leadingBframes; |
| 157 | int64_t m_bframeBits; |
| 158 | int64_t m_currentSatd; |
| 159 | int m_qpConstant[3]; |
| 160 | double m_ipOffset; |
| 161 | double m_pbOffset; |
| 162 | |
| 163 | int m_lastNonBPictType; |
| 164 | int64_t m_leadingNoBSatd; |
| 165 | |
| 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 */ |
| 169 | double m_accumPNorm; |
| 170 | double m_lastQScaleFor[3]; /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */ |
| 171 | double m_lstep; |
| 172 | double m_shortTermCplxSum; |
| 173 | double m_shortTermCplxCount; |
| 174 | double m_lastRceq; |
| 175 | double m_qCompress; |
| 176 | int64_t m_totalBits; /* total bits used for already encoded frames */ |
| 177 | int m_framesDone; /* # of frames passed through RateCotrol already */ |
| 178 | double m_fps; |
| 179 | int64_t m_satdCostWindow[50]; |
| 180 | int m_sliderPos; |
| 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 |
| 184 | * -F2: |
| 185 | * rceStart 10 |
| 186 | * rceUpdate 10 |
| 187 | * rceEnd 9 |
| 188 | * rceStart 11 |
| 189 | * rceUpdate 11 |
| 190 | * rceEnd 10 |
| 191 | * rceStart 12 |
| 192 | * rceUpdate 12 |
| 193 | * rceEnd 11 */ |
| 194 | ThreadSafeInteger m_startEndOrder; |
| 195 | int m_finalFrameCount; /* set when encoder begins flushing */ |
| 196 | bool m_bTerminated; /* set true when encoder is closing */ |
| 197 | |
| 198 | /* hrd stuff */ |
| 199 | SEIBufferingPeriod m_bufPeriodSEI; |
| 200 | double m_nominalRemovalTime; |
| 201 | double m_prevCpbFinalAT; |
| 202 | |
| 203 | /* 2 pass */ |
| 204 | bool m_2pass; |
| 205 | FILE* m_statFileOut; |
| 206 | FILE* m_cutreeStatFileOut; |
| 207 | FILE* m_cutreeStatFileIn; |
| 208 | int m_numEntries; |
| 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 */ |
| 213 | struct |
| 214 | { |
| 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). */ |
| 218 | } m_cuTreeStats; |
| 219 | |
| 220 | RateControl(x265_param *p); |
| 221 | void setFinalFrameCount(int count); |
| 222 | void terminate(); /* un-block all waiting functions so encoder may close */ |
| 223 | void destroy(); |
| 224 | |
| 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); |
| 237 | |
| 238 | protected: |
| 239 | |
| 240 | static const int s_slidingWindowFrames; |
| 241 | static const char *s_defaultStatFileName; |
| 242 | |
| 243 | int m_residualFrames; |
| 244 | int m_partialResidualFrames; |
| 245 | int m_residualCost; |
| 246 | int m_partialResidualCost; |
| 247 | int m_amortizeFrames; |
| 248 | double m_amortizeFraction; |
| 249 | |
| 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); |
| 254 | |
| 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); |
| 262 | bool initPass2(); |
| 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); |
| 268 | }; |
| 269 | } |
| 270 | #endif // ifndef X265_RATECONTROL_H |