Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/Quant.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     Quant.h
43
    \brief    base quantization class (header)
44
*/
45
46
#pragma once
47
48
#include "CommonDef.h"
49
#include "Unit.h"
50
#include "Contexts.h"
51
#include "ContextModelling.h"
52
#include "UnitPartitioner.h"
53
54
//! \ingroup CommonLib
55
//! \{
56
57
namespace vvenc {
58
59
#if defined(TARGET_SIMD_X86)  && ENABLE_SIMD_OPT_QUANT
60
using namespace x86_simd;
61
#endif
62
63
// ====================================================================================================================
64
// Constants
65
// ====================================================================================================================
66
67
#define QP_BITS                 15
68
69
static inline int getTransformShift(const int channelBitDepth, const Size size, const int maxLog2TrDynamicRange)
70
31.6M
{
71
31.6M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
31.6M
}
Unexecuted instantiation: vvencimpl.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: EncLib.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: CuTree.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: EncGOP.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: EncPicture.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: EncSlice.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: InterSearch.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: LoopFilter.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Quant.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
63.4k
{
71
63.4k
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
63.4k
}
TrQuant.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
20.1k
{
71
20.1k
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
20.1k
}
Unexecuted instantiation: EncCu.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: EncModeCtrl.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Unexecuted instantiation: IntraSearch.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
DepQuant.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
1.14M
{
71
1.14M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
1.14M
}
QuantRDOQ2.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
20.2M
{
71
20.2M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
20.2M
}
Unexecuted instantiation: DecCu.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
QuantRDOQ.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
10.2M
{
71
10.2M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
10.2M
}
73
74
static inline int getScalingListType(const PredMode predMode, const ComponentID compID)
75
177k
{
76
177k
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
177k
}
Unexecuted instantiation: vvencimpl.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: EncLib.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: CuTree.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: EncGOP.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: EncPicture.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: EncSlice.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: InterSearch.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: LoopFilter.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Quant.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Line
Count
Source
75
63.4k
{
76
63.4k
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
63.4k
}
Unexecuted instantiation: TrQuant.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: EncCu.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: EncModeCtrl.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: IntraSearch.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: DepQuant.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: QuantRDOQ2.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Unexecuted instantiation: DecCu.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
QuantRDOQ.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Line
Count
Source
75
113k
{
76
113k
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
113k
}
78
79
// ====================================================================================================================
80
// Class definition
81
// ====================================================================================================================
82
struct TrQuantParams
83
{
84
  int     rightShift;
85
  int     qScale;
86
};
87
88
/// QP struct
89
class QpParam
90
{
91
public:
92
  short  Qps[2];
93
  int8_t pers[2];
94
  int8_t rems[2];
95
96
public:
97
  QpParam(const TransformUnit& tu, const ComponentID &compID, const bool allowACTQpoffset = true);
98
  
99
3.74M
  int Qp ( const bool ts ) const { return Qps [ts?1:0]; }
100
287k
  int per( const bool ts ) const { return pers[ts?1:0]; }
101
408k
  int rem( const bool ts ) const { return rems[ts?1:0]; }
102
}; // END STRUCT DEFINITION QpParam
103
104
/// transform and quantization class
105
class Quant
106
{
107
public:
108
  Quant( const Quant* other, bool useScalingLists, bool enableOpt = true );
109
  virtual ~Quant();
110
111
  // initialize class
112
  virtual void init( int rdoq, bool useRDOQTS, int thrVal, int sbbSkipThrVal );
113
  // The all-zero sub-block limit of RDOQ2 and DQ on |coeff| * qScale: sbbSkipThrVal (SbbSkipThr, 6 fractional bits) half
114
  // quantization steps of 1 << halfStepShift each (0: off). Exact for halfStepShift >= 6 (the >> 6 drops only zero bits);
115
  // clamped to 32 bits, which no |coeff| * qScale reaches.
116
  static int sbbSkipLimit( int sbbSkipThrVal, int halfStepShift )
117
223k
  {
118
223k
    CHECKD( halfStepShift < 6 || halfStepShift > 40, "the sub-block skip limit assumes 6 <= halfStepShift <= 40" );
119
223k
    return int( std::min<int64_t>( ( int64_t( sbbSkipThrVal ) << halfStepShift ) >> 6, std::numeric_limits<int>::max() ) );
120
223k
  }
121
122
public:
123
124
49.4k
  void  setLambdas                ( const double lambdas[MAX_NUM_COMP] )   { for (uint32_t component = 0; component < MAX_NUM_COMP; component++) m_lambdas[component] = lambdas[component]; }
125
2.37M
  void  selectLambda              ( const ComponentID compId )             { m_dLambda = m_lambdas[ compId ]; }
126
0
  void  getLambdas                ( double (&lambdas)[MAX_NUM_COMP]) const { for (uint32_t component = 0; component < MAX_NUM_COMP; component++) lambdas[component] = m_lambdas[component]; }
127
1.71M
  void  scaleLambda               ( const double scale)                    { m_dLambda *= scale;}
128
129
6.51k
  int*  getQuantCoeff             ( uint32_t list, int qp, uint32_t sizeX, uint32_t sizeY ) { return m_quantCoef            [sizeX][sizeY][list][qp]; };  //!< get Quant Coefficent
130
0
  int*  getDequantCoeff           ( uint32_t list, int qp, uint32_t sizeX, uint32_t sizeY ) { return m_dequantCoef          [sizeX][sizeY][list][qp]; };  //!< get DeQuant Coefficent
131
132
  bool  getUseScalingList         ( const uint32_t width, const uint32_t height, const bool isTransformSkip, const bool lfnstApplied )
133
63.4k
  {
134
63.4k
    return (m_scalingListEnabled && !isTransformSkip && (!lfnstApplied));
135
63.4k
  };
136
23.4M
  bool getScalingListEnabled      ()   { return m_scalingListEnabled; }
137
  virtual void setFlatScalingList ( const int maxLog2TrDynamicRange[MAX_NUM_CH], const BitDepths &bitDepths );
138
139
  // quantization
140
  virtual void quant              ( TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, TCoeff &uiAbsSum, const QpParam& cQP, const Ctx& ctx );
141
  // de-quantization
142
  virtual void dequant            ( const TransformUnit& tu, CoeffBuf& dstCoeff, const ComponentID compID, const QpParam& cQP );
143
144
protected:
145
  bool    xNeedRDOQ               ( TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, const QpParam& cQP );
146
private:
147
  void    xInitScalingList        ( const Quant* other, bool useScalingLists );
148
  void    xDestroyScalingList     ();
149
  void    xSetFlatScalingList     ( uint32_t list, uint32_t sizeX, uint32_t sizeY, int qp );
150
  void    xSignBitHidingHDQ       ( TCoeffSig* pQCoef, const TCoeff* pCoef, TCoeff* deltaU, const CoeffCodingContext& cctx, int &lastPos, const int maxLog2TrDynamicRange);
151
  void    xInvResDPCM             ( const TransformUnit& tu, const ComponentID compID, CoeffSigBuf& dstBuf ) const;
152
153
#if defined(TARGET_SIMD_X86)  && ENABLE_SIMD_OPT_QUANT
154
  void initQuantX86();
155
  template <X86_VEXT vext>
156
  void _initQuantX86();
157
#endif
158
#if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_OPT_QUANT
159
  void initQuantARM();
160
  template <ARM_VEXT vext>
161
  void _initQuantARM();
162
#endif
163
164
public:
165
  // The highest scan position at or below firstTestPos whose coefficient's magnitude exceeds th (-1 if none), skipping the
166
  // positions in the zero-out region (zeroOut: x >= zeroOutWidth or y >= zeroOutHeight). scan: the block's scan, width its
167
  // stride. With 4x4 sub-blocks (log2SbbWidth == log2SbbHeight == 2) firstTestPos must be the last position of a sub-block
168
  // (the SIMD versions test whole sub-blocks).
169
  void    ( *xFindFirstPos )      ( int& firstTestPos, const TCoeff* tCoeff, const ScanElement* scan, int width, int log2SbbWidth, int log2SbbHeight,
170
                                    int th, bool zeroOut, int zeroOutWidth, int zeroOutHeight );
171
  static void findFirstPosCore    ( int& firstTestPos, const TCoeff* tCoeff, const ScanElement* scan, int width, int log2SbbWidth, int log2SbbHeight,
172
                                    int th, bool zeroOut, int zeroOutWidth, int zeroOutHeight );
173
  // Whether all 16 coefficients of a 4x4 block (rows of stride) quantize to level 0: |c| * quantScale < zeroLimit. The
174
  // products must fit 32 bits.
175
  bool    ( *xAllZeroLevels4x4 )  ( const TCoeff* src, ptrdiff_t stride, int quantScale, int zeroLimit );
176
  static bool allZeroLevels4x4Core( const TCoeff* src, ptrdiff_t stride, int quantScale, int zeroLimit );
177
  bool    ( *xNeedRdoq )          ( const TCoeff* pCoeff, size_t numCoeff, int quantCoeff, int64_t offset, int shift );
178
  // the BDPCM residual in place: each coefficient minus its left (horizontal) or upper (vertical) neighbour
179
  void    ( *xFwdResDPCM )        ( TCoeffSig* coef, const ptrdiff_t stride, const int width, const int height, const bool vertical );
180
  // the inverse vertical BDPCM residual: the column sums of coef, each clipped to the 16-bit coefficient range
181
  void    ( *xInvResDPCMVer )     ( const TCoeffSig* coef, const ptrdiff_t coefStride, TCoeffSig* dst, const ptrdiff_t dstStride, const int width, const int height );
182
  // the C versions of xFwdResDPCM and xInvResDPCMVer, also used by the SIMD versions for widths that are not a multiple of 4
183
  static void fwdResDPCMCore       ( TCoeffSig* coef, const ptrdiff_t stride, const int width, const int height, const bool vertical );
184
  static void invResDPCMVerCore    ( const TCoeffSig* coef, const ptrdiff_t coefStride, TCoeffSig* dst, const ptrdiff_t dstStride, const int width, const int height );
185
  void    ( *xDeQuant )           ( const int maxX, const int maxY, const int scale, const TCoeffSig* const piQCoef, const size_t piQCfStride, TCoeff* const piCoef, const int rightShift, const int inputMaximum, const TCoeff transformMaximum );
186
  // deltaU is only populated up to lastScanPos, and only when signHiding is true
187
  void    ( *xQuant )             ( const TransformUnit tu, const ComponentID compID,
188
                                    const CCoeffBuf& piCoef, CoeffSigBuf piQCoef,
189
                                    TCoeff& uiAbsSum, int& lastScanPos, TCoeff* deltaU,
190
                                    const int defaultQuantisationCoefficient, const int iQBits, const int64_t iAdd,
191
                                    const TCoeff entropyCodingMinimum, const TCoeff entropyCodingMaximum,
192
                                    const bool signHiding,
193
                                    const TCoeff m_thrVal );
194
195
protected:
196
  int      m_RDOQ;
197
  bool     m_useRDOQTS;
198
  double   m_dLambda;
199
  TCoeffSig m_tmpBdpcm[1 << ( MAX_TB_LOG2_SIZEY << 1 )];
200
  int      m_thrVal;
201
  int      m_sbbSkipThrVal;   // the all-zero sub-block limit of RDOQ2 and DQ in half quantization steps, 6 fractional bits (0: off)
202
203
private:
204
  double   m_lambdas[MAX_NUM_COMP];
205
  bool     m_scalingListEnabled;
206
  bool     m_isScalingListOwner;
207
208
  int      *m_quantCoef            [SCALING_LIST_SIZE_NUM][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of quantization matrix coefficient 4x4
209
  int      *m_dequantCoef          [SCALING_LIST_SIZE_NUM][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of dequantization matrix coefficient 4x4
210
};// END CLASS DEFINITION Quant
211
212
} // namespace vvenc
213
214
//! \}
215