1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
24 #ifndef X265_ENTROPY_H
25 #define X265_ENTROPY_H
28 #include "bitstream.h"
47 NUMBER_OF_SPLIT_MODES
= 3
54 uint32_t absPartIdxTURelCU
;
55 uint32_t absPartIdxStep
;
57 TURecurse(SplitType splitType
, uint32_t _absPartIdxStep
, uint32_t _absPartIdxTU
)
59 static const uint32_t partIdxStepShift
[NUMBER_OF_SPLIT_MODES
] = { 0, 1, 2 };
61 absPartIdxTURelCU
= _absPartIdxTU
;
62 splitMode
= (uint32_t)splitType
;
63 absPartIdxStep
= _absPartIdxStep
>> partIdxStepShift
[splitMode
];
68 if (splitMode
== DONT_SPLIT
)
75 absPartIdxTURelCU
+= absPartIdxStep
;
78 return section
< (uint32_t)(1 << splitMode
);
82 bool isLastSection() const
84 return (section
+ 1) >= (uint32_t)(1 << splitMode
);
90 int significantCoeffGroupBits
[NUM_SIG_CG_FLAG_CTX
][2];
91 int significantBits
[NUM_SIG_FLAG_CTX
][2];
94 int greaterOneBits
[NUM_ONE_FLAG_CTX
][2];
95 int levelAbsBits
[NUM_ABS_FLAG_CTX
][2];
96 int blockCbpBits
[NUM_QT_CBF_CTX
][2];
97 int blockRootCbpBits
[2];
100 class Entropy
: public SyntaxElementWriter
105 uint8_t m_contextState
[160]; // MAX_OFF_CTX_MOD + padding
110 uint32_t m_bufferedByte
;
111 int m_numBufferedBytes
;
114 EstBitsSbac m_estBitsSbac
;
118 void setBitstream(Bitstream
* p
) { m_bitIf
= p
; }
120 uint32_t getNumberOfWrittenBits()
122 X265_CHECK(!m_bitIf
, "bit counting mode expected\n");
123 return (uint32_t)(m_fracBits
>> 15);
126 #if CHECKED_BUILD || _DEBUG
128 void markInvalid() { m_valid
= false; }
129 void markValid() { m_valid
= true; }
133 void zeroFract() { m_fracBits
= 0; }
135 void resetEntropy(const Slice
& slice
);
138 void load(const Entropy
& src
) { copyFrom(src
); }
139 void store(Entropy
& dest
) const { dest
.copyFrom(*this); }
140 void loadContexts(const Entropy
& src
) { copyContextsFrom(src
); }
141 void loadIntraDirModeLuma(const Entropy
& src
);
142 void copyState(const Entropy
& other
);
144 void codeVPS(const VPS
& vps
);
145 void codeSPS(const SPS
& sps
, const ScalingList
& scalingList
, const ProfileTierLevel
& ptl
);
146 void codePPS(const PPS
& pps
);
147 void codeVUI(const VUI
& vui
);
148 void codeAUD(const Slice
& slice
);
149 void codeHrdParameters(const HRDInfo
& hrd
);
151 void codeSliceHeader(const Slice
& slice
, FrameData
& encData
);
152 void codeSliceHeaderWPPEntryPoints(const Slice
& slice
, const uint32_t *substreamSizes
, uint32_t maxOffset
);
153 void codeShortTermRefPicSet(const RPS
& rps
);
154 void finishSlice() { encodeBinTrm(1); finish(); dynamic_cast<Bitstream
*>(m_bitIf
)->writeByteAlignment(); }
156 void encodeCTU(const CUData
& cu
, const CUGeom
& cuGeom
);
157 void codeSaoOffset(const SaoCtuParam
& ctuParam
, int plane
);
158 void codeSaoMerge(uint32_t code
) { encodeBin(code
, m_contextState
[OFF_SAO_MERGE_FLAG_CTX
]); }
160 void codeCUTransquantBypassFlag(uint32_t symbol
);
161 void codeSkipFlag(const CUData
& cu
, uint32_t absPartIdx
);
162 void codeMergeFlag(const CUData
& cu
, uint32_t absPartIdx
);
163 void codeMergeIndex(const CUData
& cu
, uint32_t absPartIdx
);
164 void codeSplitFlag(const CUData
& cu
, uint32_t absPartIdx
, uint32_t depth
);
165 void codeMVPIdx(uint32_t symbol
);
166 void codeMvd(const CUData
& cu
, uint32_t absPartIdx
, int list
);
168 void codePartSize(const CUData
& cu
, uint32_t absPartIdx
, uint32_t depth
);
169 void codePredMode(int predMode
);
170 void codePredInfo(const CUData
& cu
, uint32_t absPartIdx
);
171 void codeTransformSubdivFlag(uint32_t symbol
, uint32_t ctx
);
172 void codeQtCbf(const CUData
& cu
, uint32_t absPartIdx
, uint32_t absPartIdxStep
, uint32_t width
, uint32_t height
, TextType ttype
, uint32_t trDepth
, bool lowestLevel
);
173 void codeQtCbf(const CUData
& cu
, uint32_t absPartIdx
, TextType ttype
, uint32_t trDepth
);
174 void codeQtCbf(uint32_t cbf
, TextType ttype
, uint32_t trDepth
);
175 void codeQtCbfZero(TextType ttype
, uint32_t trDepth
);
176 void codeQtRootCbfZero();
177 void codeCoeff(const CUData
& cu
, uint32_t absPartIdx
, uint32_t depth
, bool& bCodeDQP
, uint32_t depthRange
[2]);
178 void codeCoeffNxN(const CUData
& cu
, const coeff_t
* coef
, uint32_t absPartIdx
, uint32_t log2TrSize
, TextType ttype
);
180 uint32_t bitsIntraModeNonMPM() const;
181 uint32_t bitsIntraModeMPM(const uint32_t preds
[3], uint32_t dir
) const;
182 void codeIntraDirLumaAng(const CUData
& cu
, uint32_t absPartIdx
, bool isMultiple
);
183 void codeIntraDirChroma(const CUData
& cu
, uint32_t absPartIdx
, uint32_t *chromaDirMode
);
186 void estBit(EstBitsSbac
& estBitsSbac
, uint32_t log2TrSize
, bool bIsLuma
) const;
187 void estCBFBit(EstBitsSbac
& estBitsSbac
) const;
188 void estSignificantCoeffGroupMapBit(EstBitsSbac
& estBitsSbac
, bool bIsLuma
) const;
189 void estSignificantMapBit(EstBitsSbac
& estBitsSbac
, uint32_t log2TrSize
, bool bIsLuma
) const;
190 void estSignificantCoefficientsBit(EstBitsSbac
& estBitsSbac
, bool bIsLuma
) const;
194 /* CABAC private methods */
198 void encodeBin(uint32_t binValue
, uint8_t& ctxModel
);
199 void encodeBinEP(uint32_t binValue
);
200 void encodeBinsEP(uint32_t binValues
, int numBins
);
201 void encodeBinTrm(uint32_t binValue
);
203 void encodeCU(const CUData
& cu
, const CUGeom
&cuGeom
, uint32_t absPartIdx
, uint32_t depth
, bool& bEncodeDQP
);
204 void finishCU(const CUData
& cu
, uint32_t absPartIdx
, uint32_t depth
);
208 /* SBac private methods */
209 void writeUnaryMaxSymbol(uint32_t symbol
, uint8_t* scmModel
, int offset
, uint32_t maxSymbol
);
210 void writeEpExGolomb(uint32_t symbol
, uint32_t count
);
211 void writeCoefRemainExGolomb(uint32_t symbol
, const uint32_t absGoRice
);
213 void codeProfileTier(const ProfileTierLevel
& ptl
);
214 void codeScalingList(const ScalingList
&);
215 void codeScalingList(const ScalingList
& scalingList
, uint32_t sizeId
, uint32_t listId
);
217 void codePredWeightTable(const Slice
& slice
);
218 void codeInterDir(const CUData
& cu
, uint32_t absPartIdx
);
219 void codePUWise(const CUData
& cu
, uint32_t absPartIdx
);
220 void codeQtRootCbf(uint32_t cbf
);
221 void codeRefFrmIdxPU(const CUData
& cu
, uint32_t absPartIdx
, int list
);
222 void codeRefFrmIdx(const CUData
& cu
, uint32_t absPartIdx
, int list
);
224 void codeSaoMaxUvlc(uint32_t code
, uint32_t maxSymbol
);
226 void codeDeltaQP(const CUData
& cu
, uint32_t absPartIdx
);
227 void codeLastSignificantXY(uint32_t posx
, uint32_t posy
, uint32_t log2TrSize
, bool bIsLuma
, uint32_t scanIdx
);
228 void codeTransformSkipFlags(const CUData
& cu
, uint32_t absPartIdx
, uint32_t trSize
, TextType ttype
);
230 struct CoeffCodeState
232 uint32_t bakAbsPartIdx
;
233 uint32_t bakChromaOffset
;
234 uint32_t bakAbsPartIdxCU
;
237 void encodeTransform(const CUData
& cu
, CoeffCodeState
& state
, uint32_t offsetLumaOffset
, uint32_t offsetChroma
,
238 uint32_t absPartIdx
, uint32_t absPartIdxStep
, uint32_t depth
, uint32_t log2TrSize
, uint32_t trIdx
,
239 bool& bCodeDQP
, uint32_t depthRange
[2]);
241 void copyFrom(const Entropy
& src
);
242 void copyContextsFrom(const Entropy
& src
);
246 #endif // ifndef X265_ENTROPY_H