/src/vvenc/source/Lib/EncoderLib/CABACWriter.h
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | /** \file CABACWriter.h |
43 | | * \brief Writer for low level syntax |
44 | | */ |
45 | | |
46 | | #pragma once |
47 | | |
48 | | #include "CommonLib/BitStream.h" |
49 | | #include "CommonLib/ContextModelling.h" |
50 | | #include "BinEncoder.h" |
51 | | |
52 | | //! \ingroup EncoderLib |
53 | | //! \{ |
54 | | |
55 | | namespace vvenc { |
56 | | |
57 | | class EncCu; |
58 | | class CABACWriter : public DeriveCtx |
59 | | { |
60 | | public: |
61 | 0 | CABACWriter(BinEncIf& binEncoder) : m_BinEncoder(binEncoder), m_Bitstream(0) { m_TestCtx = m_BinEncoder.getCtx(); } |
62 | 0 | virtual ~CABACWriter() {} |
63 | | |
64 | | public: |
65 | 0 | DeriveCtx& getDeriveCtx () { return *this; } |
66 | | void initCtxModels ( const Slice& slice ); |
67 | | SliceType getCtxInitId ( const Slice& slice ); |
68 | 0 | void initBitstream ( OutputBitstream* bitstream ) { m_Bitstream = bitstream; m_BinEncoder.init( m_Bitstream ); } |
69 | | |
70 | 0 | const Ctx& getCtx () const { return m_BinEncoder.getCtx(); } |
71 | 0 | Ctx& getCtx () { return m_BinEncoder.getCtx(); } |
72 | | |
73 | 0 | void start () { m_BinEncoder.start(); } |
74 | 0 | void resetBits () { m_BinEncoder.resetBits(); } |
75 | 0 | uint64_t getEstFracBits () const { return m_BinEncoder.getEstFracBits(); } |
76 | 0 | uint32_t getNumBins () { return m_BinEncoder.getNumBins(); } |
77 | 0 | bool isEncoding () { return m_BinEncoder.isEncoding(); } |
78 | | |
79 | | public: |
80 | | // slice segment data (clause 7.3.8.1) |
81 | | void end_of_slice (); |
82 | | |
83 | | // coding tree unit (clause 7.3.8.2) |
84 | | void coding_tree_unit ( CodingStructure& cs, const UnitArea& area, int (&qps)[2], unsigned ctuRsAddr, bool skipSao = false, bool skipAlf = false ); |
85 | | |
86 | | // sao (clause 7.3.8.3) |
87 | | void sao ( const Slice& slice, unsigned ctuRsAddr ); |
88 | | void sao_block_pars ( const SAOBlkParam& saoPars, const BitDepths& bitDepths, const bool* sliceEnabled, bool leftMergeAvail, bool aboveMergeAvail, bool onlyEstMergeInfo ); |
89 | | void sao_offset_pars ( const SAOOffset& ctbPars, ComponentID compID, bool sliceEnabled, int bitDepth ); |
90 | | // coding (quad)tree (clause 7.3.8.4) |
91 | | void coding_tree ( const CodingStructure& cs, Partitioner& pm, CUCtx& cuCtx, Partitioner* pPartitionerChroma = nullptr, CUCtx* pCuCtxChroma = nullptr); |
92 | | void split_cu_mode ( const PartSplit split, const CodingStructure& cs, Partitioner& pm ); |
93 | | void mode_constraint ( const PartSplit split, const CodingStructure& cs, Partitioner& pm, const ModeType modeType ); |
94 | | |
95 | | // coding unit (clause 7.3.8.5) |
96 | | void coding_unit ( const CodingUnit& cu, Partitioner& pm, CUCtx& cuCtx ); |
97 | | void cu_skip_flag ( const CodingUnit& cu ); |
98 | | void pred_mode ( const CodingUnit& cu ); |
99 | | void bdpcm_mode ( const CodingUnit& cu, const ComponentID compID ); |
100 | | |
101 | | void cu_pred_data ( const CodingUnit& cu ); |
102 | | void cu_bcw_flag ( const CodingUnit& cu ); |
103 | | void extend_ref_line ( const CodingUnit& cu ); |
104 | | void intra_luma_pred_modes ( const CodingUnit& cu ); |
105 | | void intra_luma_pred_mode ( const CodingUnit& cu, const unsigned *mpmLst = nullptr ); |
106 | | void intra_chroma_pred_modes ( const CodingUnit& cu ); |
107 | | void intra_chroma_lmc_mode ( const CodingUnit& cu ); |
108 | | void intra_chroma_pred_mode ( const CodingUnit& cu ); |
109 | | void cu_residual ( const CodingUnit& cu, Partitioner& pm, CUCtx& cuCtx ); |
110 | | void rqt_root_cbf ( const CodingUnit& cu ); |
111 | | void adaptive_color_transform ( const CodingUnit& cu); |
112 | | void sbt_mode ( const CodingUnit& cu ); |
113 | | void end_of_ctu ( const CodingUnit& cu, CUCtx& cuCtx ); |
114 | | void mip_flag ( const CodingUnit& cu ); |
115 | | void mip_pred_modes ( const CodingUnit& cu ); |
116 | | void mip_pred_mode ( const CodingUnit& cu ); |
117 | | void cu_palette_info ( const CodingUnit& cu, ComponentID compBegin, uint32_t numComp, CUCtx& cuCtx); |
118 | | void cuPaletteSubblockInfo ( const CodingUnit& cu, ComponentID compBegin, uint32_t numComp, int subSetId, uint32_t& prevRunPos, unsigned& prevRunType ); |
119 | | // prediction unit (clause 7.3.8.6) |
120 | | void prediction_unit ( const CodingUnit& cu ); |
121 | | void merge_flag ( const CodingUnit& cu ); |
122 | | void merge_data ( const CodingUnit& cu ); |
123 | | void affine_flag ( const CodingUnit& cu ); |
124 | | void subblock_merge_flag ( const CodingUnit& cu ); |
125 | | void merge_idx ( const CodingUnit& cu ); |
126 | | void mmvd_merge_idx ( const CodingUnit& cu); |
127 | | void imv_mode ( const CodingUnit& cu ); |
128 | | void affine_amvr_mode ( const CodingUnit& cu ); |
129 | | void inter_pred_idc ( const CodingUnit& cu ); |
130 | | void ref_idx ( const CodingUnit& cu, RefPicList eRefList ); |
131 | | void mvp_flag ( const CodingUnit& cu, RefPicList eRefList ); |
132 | | |
133 | | void ciip_flag ( const CodingUnit& cu ); |
134 | | void smvd_mode ( const CodingUnit& cu ); |
135 | | |
136 | | // transform tree (clause 7.3.8.8) |
137 | | void transform_tree ( const CodingStructure& cs, Partitioner& pm, CUCtx& cuCtx, const PartSplit ispType = TU_NO_ISP, const int subTuIdx = -1 ); |
138 | | void cbf_comp ( const CodingUnit& cu, bool cbf, const CompArea& area, unsigned depth, const bool prevCbf = false, const bool useISP = false ); |
139 | | |
140 | | // mvd coding (clause 7.3.8.9) |
141 | | void mvd_coding ( const Mv &rMvd, int8_t imv ); |
142 | | // transform unit (clause 7.3.8.10) |
143 | | void transform_unit ( const TransformUnit& tu, CUCtx& cuCtx, Partitioner& pm, const int subTuCounter = -1 ); |
144 | | void cu_qp_delta ( const CodingUnit& cu, int predQP, const int8_t qp ); |
145 | | void cu_chroma_qp_offset ( const CodingUnit& cu ); |
146 | | |
147 | | // residual coding (clause 7.3.8.11) |
148 | | void residual_coding ( const TransformUnit& tu, ComponentID compID, CUCtx* cuCtx = nullptr ); |
149 | | void ts_flag ( const TransformUnit& tu, ComponentID compID ); |
150 | | void mts_idx ( const CodingUnit& cu, CUCtx* cuCtx ); |
151 | | void residual_lfnst_mode ( const CodingUnit& cu, CUCtx& cuCtx ); |
152 | | void isp_mode ( const CodingUnit& cu ); |
153 | | void last_sig_coeff ( CoeffCodingContext& cctx, const TransformUnit& tu, ComponentID compID ); |
154 | | void residual_coding_subblock ( CoeffCodingContext& cctx, const TCoeffSig* coeff, const int stateTransTable, int& state ); |
155 | | void residual_codingTS ( const TransformUnit& tu, ComponentID compID ); |
156 | | void residual_coding_subblockTS( CoeffCodingContext& cctx, const TCoeffSig* coeff ); |
157 | | void joint_cb_cr ( const TransformUnit& tu, const int cbfMask ); |
158 | | |
159 | | |
160 | | void codeAlfCtuEnabled ( CodingStructure& cs, ChannelType channel, AlfParam* alfParam, const int numCtus ); |
161 | | void codeAlfCtuEnabled ( CodingStructure& cs, ComponentID compID, AlfParam* alfParam, const int numCtus ); |
162 | | void codeAlfCtuEnabledFlag ( CodingStructure& cs, uint32_t ctuRsAddr, const int compIdx ); |
163 | | void codeAlfCtuFilterIndex ( CodingStructure& cs, uint32_t ctuRsAddr ); |
164 | | |
165 | | void codeAlfCtuAlternatives ( CodingStructure& cs, ChannelType channel, AlfParam* alfParam, const int numCtus ); |
166 | | void codeAlfCtuAlternatives ( CodingStructure& cs, ComponentID compID, AlfParam* alfParam, const int numCtus ); |
167 | | void codeAlfCtuAlternative ( CodingStructure& cs, uint32_t ctuRsAddr, const int compIdx, const AlfParam* alfParam = NULL ); |
168 | | void codeCcAlfFilterControlIdc ( uint8_t idcVal, CodingStructure &cs, const ComponentID compID, const int curIdx, |
169 | | const uint8_t *filterControlIdc, Position lumaPos, const int filterCount); |
170 | | |
171 | | private: |
172 | | void unary_max_symbol ( unsigned symbol, unsigned ctxId0, unsigned ctxIdN, unsigned maxSymbol ); |
173 | | void unary_max_eqprob ( unsigned symbol, unsigned maxSymbol ); |
174 | | void exp_golomb_eqprob ( unsigned symbol, unsigned count ); |
175 | | |
176 | | void xWriteTruncBinCode ( uint32_t uiSymbol, uint32_t uiMaxSymbol); |
177 | | private: |
178 | | BinEncIf& m_BinEncoder; |
179 | | OutputBitstream* m_Bitstream; |
180 | | Ctx m_TestCtx; |
181 | | const ScanElement* m_scanOrder; |
182 | | |
183 | | Partitioner m_partitioner[2]; |
184 | | CtxTpl m_tplBuf[MAX_TB_SIZEY * MAX_TB_SIZEY]; |
185 | | }; |
186 | | |
187 | | |
188 | | } // namespace vvenc |
189 | | |
190 | | //! \} |
191 | | |