Coverage Report

Created: 2026-10-28 06:18

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
35.1M
{
71
35.1M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
35.1M
}
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
66.6k
{
71
66.6k
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
66.6k
}
TrQuant.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
21.0k
{
71
21.0k
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
21.0k
}
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
3.36M
{
71
3.36M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
3.36M
}
QuantRDOQ2.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
21.0M
{
71
21.0M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
21.0M
}
Unexecuted instantiation: DecCu.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
QuantRDOQ.cpp:vvenc::getTransformShift(int, vvenc::Size, int)
Line
Count
Source
70
10.6M
{
71
10.6M
  return maxLog2TrDynamicRange - channelBitDepth - ( ( Log2(size.width) + Log2(size.height) ) >> 1 );
72
10.6M
}
73
74
static inline int getScalingListType(const PredMode predMode, const ComponentID compID)
75
3.55M
{
76
3.55M
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
3.55M
}
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
66.6k
{
76
66.6k
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
66.6k
}
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)
DepQuant.cpp:vvenc::getScalingListType(vvenc::PredMode, vvenc::ComponentID)
Line
Count
Source
75
3.36M
{
76
3.36M
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
3.36M
}
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
119k
{
76
119k
  return ((predMode == MODE_INTRA) ? 0 : MAX_NUM_COMP) + compID;
77
119k
}
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
10.2M
  int Qp ( const bool ts ) const { return Qps [ts?1:0]; }
100
302k
  int per( const bool ts ) const { return pers[ts?1:0]; }
101
429k
  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 = 0, bool useRDOQTS = false, int thrVal = 8 );
113
114
public:
115
116
55.6k
  void  setLambdas                ( const double lambdas[MAX_NUM_COMP] )   { for (uint32_t component = 0; component < MAX_NUM_COMP; component++) m_lambdas[component] = lambdas[component]; }
117
2.52M
  void  selectLambda              ( const ComponentID compId )             { m_dLambda = m_lambdas[ compId ]; }
118
0
  void  getLambdas                ( double (&lambdas)[MAX_NUM_COMP]) const { for (uint32_t component = 0; component < MAX_NUM_COMP; component++) lambdas[component] = m_lambdas[component]; }
119
1.82M
  void  scaleLambda               ( const double scale)                    { m_dLambda *= scale;}
120
121
2.40M
  int*  getQuantCoeff             ( uint32_t list, int qp, uint32_t sizeX, uint32_t sizeY ) { return m_quantCoef            [sizeX][sizeY][list][qp]; };  //!< get Quant Coefficent
122
971k
  int*  getDequantCoeff           ( uint32_t list, int qp, uint32_t sizeX, uint32_t sizeY ) { return m_dequantCoef          [sizeX][sizeY][list][qp]; };  //!< get DeQuant Coefficent
123
124
  bool  getUseScalingList         ( const uint32_t width, const uint32_t height, const bool isTransformSkip, const bool lfnstApplied )
125
3.43M
  {
126
3.43M
    return (m_scalingListEnabled && !isTransformSkip && (!lfnstApplied));
127
3.43M
  };
128
21.0M
  bool getScalingListEnabled      ()   { return m_scalingListEnabled; }
129
  virtual void setFlatScalingList ( const int maxLog2TrDynamicRange[MAX_NUM_CH], const BitDepths &bitDepths );
130
131
  // quantization
132
  virtual void quant              ( TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, TCoeff &uiAbsSum, const QpParam& cQP, const Ctx& ctx );
133
  // de-quantization
134
  virtual void dequant            ( const TransformUnit& tu, CoeffBuf& dstCoeff, const ComponentID compID, const QpParam& cQP );
135
136
protected:
137
  bool    xNeedRDOQ               ( TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, const QpParam& cQP );
138
private:
139
  void    xInitScalingList        ( const Quant* other, bool useScalingLists );
140
  void    xDestroyScalingList     ();
141
  void    xSetFlatScalingList     ( uint32_t list, uint32_t sizeX, uint32_t sizeY, int qp );
142
  void    xSignBitHidingHDQ       ( TCoeffSig* pQCoef, const TCoeff* pCoef, TCoeff* deltaU, const CoeffCodingContext& cctx, int &lastPos, const int maxLog2TrDynamicRange);
143
  void    xInvResDPCM             ( const TransformUnit& tu, const ComponentID compID, CoeffSigBuf& dstBuf ) const;
144
  void    ( *xQuant )             ( const TransformUnit tu, const ComponentID compID,
145
                                    const CCoeffBuf& piCoef, CoeffSigBuf piQCoef,
146
                                    TCoeff& uiAbsSum, int& lastScanPos, TCoeff* deltaU,
147
                                    const int defaultQuantisationCoefficient, const int iQBits, const int64_t iAdd,
148
                                    const TCoeff entropyCodingMinimum, const TCoeff entropyCodingMaximum,
149
                                    const bool signHiding,
150
                                    const TCoeff m_thrVal );
151
152
#if defined(TARGET_SIMD_X86)  && ENABLE_SIMD_OPT_QUANT
153
  void initQuantX86();
154
  template <X86_VEXT vext>
155
  void _initQuantX86();
156
#endif
157
#if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_OPT_QUANT
158
  void initQuantARM();
159
  template <ARM_VEXT vext>
160
  void _initQuantARM();
161
#endif
162
163
public:
164
  bool    ( *xNeedRdoq )          ( const TCoeff* pCoeff, size_t numCoeff, int quantCoeff, int64_t offset, int shift );
165
  // the BDPCM residual in place: each coefficient minus its left (horizontal) or upper (vertical) neighbour
166
  void    ( *xFwdResDPCM )        ( TCoeffSig* coef, const ptrdiff_t stride, const int width, const int height, const bool vertical );
167
  // the inverse vertical BDPCM residual: the column sums of coef, each clipped to the 16-bit coefficient range
168
  void    ( *xInvResDPCMVer )     ( const TCoeffSig* coef, const ptrdiff_t coefStride, TCoeffSig* dst, const ptrdiff_t dstStride, const int width, const int height );
169
  // the C versions of xFwdResDPCM and xInvResDPCMVer, also used by the SIMD versions for widths that are not a multiple of 4
170
  static void fwdResDPCMCore       ( TCoeffSig* coef, const ptrdiff_t stride, const int width, const int height, const bool vertical );
171
  static void invResDPCMVerCore    ( const TCoeffSig* coef, const ptrdiff_t coefStride, TCoeffSig* dst, const ptrdiff_t dstStride, const int width, const int height );
172
  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 );
173
174
protected:
175
  int      m_RDOQ;
176
  bool     m_useRDOQTS;
177
  double   m_dLambda;
178
  TCoeffSig m_tmpBdpcm[1 << ( MAX_TB_LOG2_SIZEY << 1 )];
179
  int      m_thrVal;
180
181
private:
182
  double   m_lambdas[MAX_NUM_COMP];
183
  bool     m_scalingListEnabled;
184
  bool     m_isScalingListOwner;
185
186
  int      *m_quantCoef            [SCALING_LIST_SIZE_NUM][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of quantization matrix coefficient 4x4
187
  int      *m_dequantCoef          [SCALING_LIST_SIZE_NUM][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of dequantization matrix coefficient 4x4
188
};// END CLASS DEFINITION Quant
189
190
} // namespace vvenc
191
192
//! \}
193