Update changelog.
[deb_x265.git] / source / encoder / entropy.h
... / ...
CommitLineData
1/*****************************************************************************
2* Copyright (C) 2013 x265 project
3*
4* Authors: Steve Borho <steve@borho.org>
5*
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.
10*
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.
15*
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.
19*
20* This program is also available under a commercial proprietary license.
21* For more information, contact us at license @ x265.com.
22*****************************************************************************/
23
24#ifndef X265_ENTROPY_H
25#define X265_ENTROPY_H
26
27#include "common.h"
28#include "bitstream.h"
29#include "frame.h"
30#include "contexts.h"
31#include "slice.h"
32
33namespace x265 {
34// private namespace
35
36struct SaoCtuParam;
37struct EstBitsSbac;
38class CUData;
39struct CUGeom;
40class ScalingList;
41
42enum SplitType
43{
44 DONT_SPLIT = 0,
45 VERTICAL_SPLIT = 1,
46 QUAD_SPLIT = 2,
47 NUMBER_OF_SPLIT_MODES = 3
48};
49
50struct TURecurse
51{
52 uint32_t section;
53 uint32_t splitMode;
54 uint32_t absPartIdxTURelCU;
55 uint32_t absPartIdxStep;
56
57 TURecurse(SplitType splitType, uint32_t _absPartIdxStep, uint32_t _absPartIdxTU)
58 {
59 static const uint32_t partIdxStepShift[NUMBER_OF_SPLIT_MODES] = { 0, 1, 2 };
60 section = 0;
61 absPartIdxTURelCU = _absPartIdxTU;
62 splitMode = (uint32_t)splitType;
63 absPartIdxStep = _absPartIdxStep >> partIdxStepShift[splitMode];
64 }
65
66 bool isNextSection()
67 {
68 if (splitMode == DONT_SPLIT)
69 {
70 section++;
71 return false;
72 }
73 else
74 {
75 absPartIdxTURelCU += absPartIdxStep;
76
77 section++;
78 return section < (uint32_t)(1 << splitMode);
79 }
80 }
81
82 bool isLastSection() const
83 {
84 return (section + 1) >= (uint32_t)(1 << splitMode);
85 }
86};
87
88struct EstBitsSbac
89{
90 int significantCoeffGroupBits[NUM_SIG_CG_FLAG_CTX][2];
91 int significantBits[NUM_SIG_FLAG_CTX][2];
92 int lastXBits[10];
93 int lastYBits[10];
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];
98};
99
100class Entropy : public SyntaxElementWriter
101{
102public:
103
104 uint64_t m_pad;
105 uint8_t m_contextState[160]; // MAX_OFF_CTX_MOD + padding
106
107 /* CABAC state */
108 uint32_t m_low;
109 uint32_t m_range;
110 uint32_t m_bufferedByte;
111 int m_numBufferedBytes;
112 int m_bitsLeft;
113 uint64_t m_fracBits;
114 EstBitsSbac m_estBitsSbac;
115
116 Entropy();
117
118 void setBitstream(Bitstream* p) { m_bitIf = p; }
119
120 uint32_t getNumberOfWrittenBits()
121 {
122 X265_CHECK(!m_bitIf, "bit counting mode expected\n");
123 return (uint32_t)(m_fracBits >> 15);
124 }
125
126#if CHECKED_BUILD || _DEBUG
127 bool m_valid;
128 void markInvalid() { m_valid = false; }
129 void markValid() { m_valid = true; }
130#else
131 void markValid() { }
132#endif
133 void zeroFract() { m_fracBits = 0; }
134 void resetBits();
135 void resetEntropy(const Slice& slice);
136
137 // SBAC RD
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);
143
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);
150
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(); }
155
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]); }
159
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);
167
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);
179
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);
184
185 // RDO functions
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;
191
192private:
193
194 /* CABAC private methods */
195 void start();
196 void finish();
197
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);
202
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);
205
206 void writeOut();
207
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);
212
213 void codeProfileTier(const ProfileTierLevel& ptl);
214 void codeScalingList(const ScalingList&);
215 void codeScalingList(const ScalingList& scalingList, uint32_t sizeId, uint32_t listId);
216
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);
223
224 void codeSaoMaxUvlc(uint32_t code, uint32_t maxSymbol);
225
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);
229
230 struct CoeffCodeState
231 {
232 uint32_t bakAbsPartIdx;
233 uint32_t bakChromaOffset;
234 uint32_t bakAbsPartIdxCU;
235 };
236
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]);
240
241 void copyFrom(const Entropy& src);
242 void copyContextsFrom(const Entropy& src);
243};
244}
245
246#endif // ifndef X265_ENTROPY_H