Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
42.1k
  CABACWriter(BinEncIf& binEncoder)   : m_BinEncoder(binEncoder), m_Bitstream(0)   { m_TestCtx = m_BinEncoder.getCtx(); }  
62
42.1k
  virtual ~CABACWriter() {}
63
64
public:
65
318k
  DeriveCtx&  getDeriveCtx              ()                                                    { return *this; }
66
  void        initCtxModels             ( const Slice&                  slice );
67
  SliceType   getCtxInitId              ( const Slice&                  slice );
68
4.65k
  void        initBitstream             ( OutputBitstream*              bitstream )           { m_Bitstream = bitstream; m_BinEncoder.init( m_Bitstream ); }
69
70
0
  const Ctx&  getCtx                    ()                                            const   { return m_BinEncoder.getCtx();  }
71
8.24M
  Ctx&        getCtx                    ()                                                    { return m_BinEncoder.getCtx();  }
72
73
0
  void        start                     ()                                                    { m_BinEncoder.start(); }
74
5.38M
  void        resetBits                 ()                                                    { m_BinEncoder.resetBits(); }
75
  // an entry of setTuResiBits' array whose residual transform_unit codes as usual
76
  static constexpr uint64_t TU_RESI_BITS_UNKNOWN = std::numeric_limits<uint64_t>::max();
77
  // from now on transform_unit counts these bits for the residuals (per component), without coding them. When luma's
78
  // entry is known, it also adds lumaResiFlags to its CUCtx: the flags luma's residual_coding set when its bits were
79
  // counted (needed where MTS or LFNST follow). nullptr ends it.
80
  void        setTuResiBits             ( const uint64_t* tuResiBits, const CUCtx* lumaResiFlags = nullptr )
81
250k
  {
82
250k
    m_tuResiBits      = tuResiBits;
83
250k
    m_tuLumaResiFlags = lumaResiFlags;
84
250k
  }
85
2.43k
  void        addEstFracBits            ( uint64_t fracBits )                                 { static_cast<BitEstimatorBase&>( m_BinEncoder ).addEstFracBits( fracBits ); }
86
5.41M
  uint64_t    getEstFracBits            ()                                            const   { return m_BinEncoder.getEstFracBits(); }
87
1.43k
  uint32_t    getNumBins                ()                                                    { return m_BinEncoder.getNumBins(); }
88
0
  bool        isEncoding                ()                                                    { return m_BinEncoder.isEncoding(); }
89
90
public:
91
  // slice segment data (clause 7.3.8.1)
92
  void        end_of_slice              ();
93
94
  // coding tree unit (clause 7.3.8.2)
95
  void        coding_tree_unit          ( CodingStructure&              cs,       const UnitArea&   area,       int (&qps)[2],  unsigned ctuRsAddr,  bool skipSao = false, bool skipAlf = false );
96
97
  // sao (clause 7.3.8.3)
98
  void        sao                       ( const Slice&                  slice,    unsigned          ctuRsAddr );
99
  void        sao_block_pars            ( const SAOBlkParam&            saoPars,  const BitDepths&  bitDepths,  const bool* sliceEnabled, bool leftMergeAvail, bool aboveMergeAvail, bool onlyEstMergeInfo );
100
  void        sao_offset_pars           ( const SAOOffset&              ctbPars,  ComponentID       compID,     bool sliceEnabled,  int bitDepth );
101
  // coding (quad)tree (clause 7.3.8.4)
102
  void        coding_tree               ( const CodingStructure&        cs,       Partitioner&      pm,         CUCtx& cuCtx, Partitioner* pPartitionerChroma = nullptr, CUCtx* pCuCtxChroma = nullptr);
103
  void        split_cu_mode             ( const PartSplit               split,    const CodingStructure& cs,    Partitioner& pm );
104
  void        mode_constraint           ( const PartSplit               split,    const CodingStructure& cs,    Partitioner& pm,    const ModeType modeType );
105
106
  // coding unit (clause 7.3.8.5)
107
  void        coding_unit               ( const CodingUnit&             cu,       Partitioner&      pm,         CUCtx& cuCtx );
108
  void        cu_skip_flag              ( const CodingUnit&             cu );
109
  void        pred_mode                 ( const CodingUnit&             cu );
110
  void        bdpcm_mode                ( const CodingUnit&             cu,       const ComponentID compID );
111
112
  void        cu_pred_data              ( const CodingUnit&             cu );
113
  void        cu_bcw_flag               ( const CodingUnit&             cu );
114
  void        extend_ref_line           ( const CodingUnit&             cu );
115
  void        intra_luma_pred_modes     ( const CodingUnit&             cu );
116
  void        intra_luma_pred_mode      ( const CodingUnit&             cu,       const unsigned *mpmLst = nullptr );
117
  void        intra_chroma_pred_modes   ( const CodingUnit&             cu );
118
  void        intra_chroma_lmc_mode     ( const CodingUnit&             cu );
119
  void        intra_chroma_pred_mode    ( const CodingUnit&             cu );
120
  void        cu_residual               ( const CodingUnit&             cu,       Partitioner&      pm,         CUCtx& cuCtx );
121
  void        rqt_root_cbf              ( const CodingUnit&             cu );
122
  void        adaptive_color_transform  ( const CodingUnit&             cu);
123
  void        sbt_mode                  ( const CodingUnit&             cu );
124
  void        end_of_ctu                ( const CodingUnit&             cu,       CUCtx&            cuCtx );
125
  void        mip_flag                  ( const CodingUnit&             cu );
126
  void        mip_pred_modes            ( const CodingUnit&             cu );
127
  void        mip_pred_mode             ( const CodingUnit&             cu );
128
  void        cu_palette_info           ( const CodingUnit&             cu,       ComponentID       compBegin,     uint32_t numComp,          CUCtx&       cuCtx);
129
  void        cuPaletteSubblockInfo     ( const CodingUnit&             cu,       ComponentID       compBegin,     uint32_t numComp,          int subSetId,               uint32_t& prevRunPos,        unsigned& prevRunType );
130
  // prediction unit (clause 7.3.8.6)
131
  void        prediction_unit           ( const CodingUnit&             cu );
132
  void        merge_flag                ( const CodingUnit&             cu );
133
  void        merge_data                ( const CodingUnit&             cu );
134
  void        affine_flag               ( const CodingUnit&             cu );
135
  void        subblock_merge_flag       ( const CodingUnit&             cu );
136
  void        merge_idx                 ( const CodingUnit&             cu );
137
  void        mmvd_merge_idx            ( const CodingUnit&             cu);
138
  void        imv_mode                  ( const CodingUnit&             cu );
139
  void        affine_amvr_mode          ( const CodingUnit&             cu );
140
  void        inter_pred_idc            ( const CodingUnit&             cu );
141
  void        ref_idx                   ( const CodingUnit&             cu,       RefPicList        eRefList );
142
  void        mvp_flag                  ( const CodingUnit&             cu,       RefPicList        eRefList );
143
144
  void        ciip_flag                 ( const CodingUnit&             cu );
145
  void        smvd_mode                 ( const CodingUnit&             cu );
146
147
  // transform tree (clause 7.3.8.8)
148
  void        transform_tree            ( const CodingStructure&        cs,       Partitioner&      pm,     CUCtx& cuCtx,                         const PartSplit ispType = TU_NO_ISP, const int subTuIdx = -1 );
149
  void        cbf_comp                  ( const CodingUnit&             cu,       bool              cbf,    const CompArea& area, unsigned depth, const bool prevCbf = false, const bool useISP = false );
150
151
  // mvd coding (clause 7.3.8.9)
152
  void        mvd_coding                ( const Mv &rMvd, int8_t imv );
153
  // transform unit (clause 7.3.8.10)
154
  void        transform_unit            ( const TransformUnit&          tu,       CUCtx&            cuCtx,  Partitioner& pm,       const int subTuCounter = -1 );
155
  void        cu_qp_delta               ( const CodingUnit&             cu,       int               predQP, const int8_t qp );
156
  void        cu_chroma_qp_offset       ( const CodingUnit&             cu );
157
158
  // residual coding (clause 7.3.8.11)
159
  void        residual_coding           ( const TransformUnit&          tu,       ComponentID       compID, CUCtx* cuCtx = nullptr );
160
  void        ts_flag                   ( const TransformUnit&          tu,       ComponentID       compID );
161
  void        mts_idx                   ( const CodingUnit&             cu,       CUCtx*            cuCtx  );
162
  void        residual_lfnst_mode       ( const CodingUnit&             cu,       CUCtx&            cuCtx );
163
  void        isp_mode                  ( const CodingUnit&             cu );
164
  void        last_sig_coeff            ( CoeffCodingContext&           cctx,     const TransformUnit& tu, ComponentID       compID );
165
  void        residual_coding_subblock  ( CoeffCodingContext&           cctx,     const TCoeffSig*  coeff, const int stateTransTable, int& state );
166
  void        residual_codingTS         ( const TransformUnit&          tu,       ComponentID       compID );
167
  void        residual_coding_subblockTS( CoeffCodingContext&           cctx,     const TCoeffSig*  coeff  );
168
  void        joint_cb_cr               ( const TransformUnit&          tu,       const int cbfMask );
169
170
171
  void        codeAlfCtuEnabled          ( CodingStructure& cs, ChannelType channel, AlfParam* alfParam, const int numCtus );
172
  void        codeAlfCtuEnabled          ( CodingStructure& cs, ComponentID compID, AlfParam* alfParam, const int numCtus );
173
  void        codeAlfCtuEnabledFlag      ( CodingStructure& cs, uint32_t ctuRsAddr, const int compIdx );
174
  void        codeAlfCtuFilterIndex      ( CodingStructure& cs, uint32_t ctuRsAddr );
175
176
  void        codeAlfCtuAlternatives     ( CodingStructure& cs, ChannelType channel, AlfParam* alfParam, const int numCtus );
177
  void        codeAlfCtuAlternatives     ( CodingStructure& cs, ComponentID compID, AlfParam* alfParam, const int numCtus );
178
  void        codeAlfCtuAlternative      ( CodingStructure& cs, uint32_t ctuRsAddr, const int compIdx, const AlfParam* alfParam = NULL );
179
  void        codeCcAlfFilterControlIdc  ( uint8_t idcVal, CodingStructure &cs, const ComponentID compID, const int curIdx,
180
                                           const uint8_t *filterControlIdc, Position lumaPos, const int filterCount);
181
182
private:
183
  void        unary_max_symbol           ( unsigned symbol, unsigned ctxId0, unsigned ctxIdN, unsigned maxSymbol );
184
  void        unary_max_eqprob           ( unsigned symbol,                                   unsigned maxSymbol );
185
  void        exp_golomb_eqprob          ( unsigned symbol, unsigned count );
186
187
  void        xWriteTruncBinCode         ( uint32_t uiSymbol, uint32_t uiMaxSymbol);
188
private:
189
  const uint64_t*    m_tuResiBits = nullptr;
190
  const CUCtx*       m_tuLumaResiFlags = nullptr;
191
  BinEncIf&          m_BinEncoder;
192
  OutputBitstream*   m_Bitstream;
193
  Ctx                m_TestCtx;
194
  const ScanElement* m_scanOrder;
195
196
  Partitioner        m_partitioner[2];
197
  CtxTpl             m_tplBuf[CTX_TPL_BUF_SIZE];
198
 };
199
200
201
} // namespace vvenc
202
203
//! \}
204