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/TrQuant.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     TrQuant.h
43
    \brief    transform and 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
#include "Quant.h"
54
#include "DepQuant.h"
55
56
//! \ingroup CommonLib
57
//! \{
58
59
namespace vvenc {
60
61
#if defined(TARGET_SIMD_X86)  && (ENABLE_SIMD_OPT_QUANT || ENABLE_SIMD_TRAFO)
62
using namespace x86_simd;
63
#endif
64
65
typedef void FwdTrans(const TCoeff*, TCoeff*, int, int, int, int);
66
typedef void InvTrans(const TCoeff*, TCoeff*, int, int, int, int, const TCoeff, const TCoeff);
67
68
// ====================================================================================================================
69
// Class definition
70
// ====================================================================================================================
71
typedef std::pair<int, bool> TrMode;   // a transform type (mtsIdx) and whether to test it
72
typedef std::pair<int, int>  TrCost;
73
// Fixed-capacity lists, because a search builds them for every block: the transform types to try and their costs.
74
typedef static_vector<TrMode, NUM_TRAFO_MODES_MTS> TrModeList;
75
typedef static_vector<TrCost, NUM_TRAFO_MODES_MTS> TrCostList;
76
77
// The primary transform an LFNST candidate works on, for the transforms of one TU with the same residual
78
enum LfnstPrimary
79
{
80
  LFNST_PRIMARY_NEW,    // transform the residual
81
  LFNST_PRIMARY_KEEP,   // transform the residual, and keep what an LFNST of this TU will take from it
82
  LFNST_PRIMARY_KNOWN   // an earlier call on this TU kept it: the residual isn't read
83
};
84
85
/// transform and quantization class
86
class TrQuant
87
{
88
public:
89
  explicit TrQuant( bool enableOpt = true );
90
  ~TrQuant();
91
92
  // initialize class
93
  void init(
94
             const Quant* otherQuant,
95
             const int  rdoq,
96
             const bool bUseRDOQTS,
97
             const bool scalingListsEnabled,
98
             const bool bEnc,
99
             const int  thrValue,
100
             const int  sbbSkipThrVal       // the all-zero sub-block limit of RDOQ2 and DQ, in half quantization steps with 6 fractional bits
101
           );
102
103
public:
104
  void invTransformNxN    ( TransformUnit& tu, const ComponentID compID, PelBuf& pResi, const QpParam& cQPs);
105
  void transformNxN       ( TransformUnit& tu, const ComponentID compID, const QpParam& cQP, TCoeff &uiAbsSum, const Ctx& ctx, const bool loadTr = false, const LfnstPrimary lfnstPrimary = LFNST_PRIMARY_NEW);
106
  void checktransformsNxN ( TransformUnit& tu, TrModeList *trModes, const int maxCand, const ComponentID compID = COMP_Y);
107
  // Called with true before transforming the residuals of one block with several LFNST indices, which leave these residuals as
108
  // they are, and with false afterwards. Forgets what was kept.
109
  void keepPrimaryCoeffs  ( const bool keep );
110
111
  void                        invTransformICT     ( const TransformUnit& tu, PelBuf& resCb, PelBuf& resCr );
112
  std::pair<int64_t,int64_t>  fwdTransformICT     ( const TransformUnit& tu, const PelBuf& resCb, const PelBuf& resCr, PelBuf& resC1, PelBuf& resC2, int jointCbCr = -1 );
113
  std::vector<int>            selectICTCandidates ( const TransformUnit& tu, CompStorage* resCb, CompStorage* resCr );
114
115
12.3k
  void   setLambdas  ( const double lambdas[MAX_NUM_COMP] )   { m_quant->setLambdas( lambdas ); }
116
2.37M
  void   selectLambda( const ComponentID compIdx )            { m_quant->selectLambda( compIdx ); }
117
0
  void   getLambdas  ( double (&lambdas)[MAX_NUM_COMP]) const { m_quant->getLambdas( lambdas ); }
118
1.71M
  void   scaleLambda ( const double scale)                    { m_quant->scaleLambda(scale);}
119
120
10.0k
  DepQuant* getQuant ()                                       { return m_quant; }
121
122
  // joint Cb-Cr residual transforms, indexed by the ICT mode -3..3 (public for the unit test)
123
  static const int maxAbsIctMode = 3;
124
  void                      (**m_invICT)(PelBuf&,PelBuf&);
125
  std::pair<int64_t,int64_t>(**m_fwdICT)(const PelBuf&,const PelBuf&,PelBuf&,PelBuf&);
126
127
  // low frequency non-separable transform kernels (public for the unit test)
128
  void (*m_fwdLfnstNxN)( int* src, int* dst, const uint32_t mode, const uint32_t index, const uint32_t size, int zeroOutSize );
129
  void (*m_invLfnstNxN)( int* src, int* dst, const uint32_t mode, const uint32_t index, const uint32_t size, int zeroOutSize );
130
131
protected:
132
  TCoeff*   m_plTempCoeff;
133
  bool      m_bEnc;
134
  bool      m_scalingListEnabled;
135
  TCoeff*   m_blk;
136
  TCoeff*   m_tmp;
137
138
private:
139
  DepQuant* m_quant;          //!< Quantizer
140
  TCoeff    m_tempInMatrix[48];
141
  TCoeff    m_lfnstFwdIn[48];     // what the forward LFNST takes from the primary transform; only xFwdLfnst writes it
142
  TCoeff    m_tempOutMatrix[48];
143
  TCoeff   *m_mtsCoeffs[NUM_TRAFO_MODES_MTS];
144
145
  // The coefficients LFNST reads from a primary transform, and the transform they came from
146
  struct KeptPrimaryCoeffs
147
  {
148
    TCoeff coeffs[8 * 8];
149
    int    trTypeHor;
150
    int    trTypeVer;
151
    bool   valid;
152
  };
153
  KeptPrimaryCoeffs m_keptPrimary[MAX_NUM_COMP + 3];   // per component, then per joint Cb-Cr mode 1 to 3
154
  bool              m_keepPrimary = false;
155
156
  void                      (*m_invICTMem[1+2*maxAbsIctMode])(PelBuf&,PelBuf&);
157
  std::pair<int64_t,int64_t>(*m_fwdICTMem[1+2*maxAbsIctMode])(const PelBuf&,const PelBuf&,PelBuf&,PelBuf&);
158
159
  uint32_t xGetLFNSTIntraMode( const Area& tuArea, const uint32_t dirMode );
160
  bool     xGetTransposeFlag(uint32_t intraMode);
161
  void     xFwdLfnst    ( const TransformUnit &tu, const ComponentID compID, const bool loadTr, const LfnstPrimary lfnstPrimary );
162
  void     xInvLfnst    ( const TransformUnit &tu, const ComponentID compID);
163
  void     xSetTrTypes  ( const TransformUnit& tu, const ComponentID compID, const int width, const int height, int &trTypeHor, int &trTypeVer );
164
165
  // forward Transform
166
  void xT               (const TransformUnit& tu, const ComponentID compID, const CPelBuf& resi, CoeffBuf& dstCoeff, const int width, const int height);
167
  void xTOrKept         (const TransformUnit& tu, const ComponentID compID, const CPelBuf& resi, CoeffBuf& dstCoeff, const int width, const int height);
168
169
  // quantization
170
  void xQuant           (TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, TCoeff &uiAbsSum, const QpParam& cQP, const Ctx& ctx);
171
172
  // dequantization
173
  void xDeQuant( const TransformUnit& tu,
174
                       CoeffBuf      &dstCoeff,
175
                 const ComponentID   &compID,
176
                 const QpParam       &cQP      );
177
178
  // inverse transform
179
  void xIT     ( const TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pCoeff, PelBuf& pResidual );
180
  // skipping Transform
181
  void xTransformSkip(const TransformUnit& tu, const ComponentID& compID, const CPelBuf& resi, TCoeff* psCoeff);
182
  // inverse skipping transform
183
  void xITransformSkip(const CCoeffBuf& plCoef, PelBuf& pResidual, const TransformUnit& tu, const ComponentID component);
184
  
185
#if defined(TARGET_SIMD_X86)  && (ENABLE_SIMD_TRAFO )
186
  template<X86_VEXT vext>
187
  void _initTrQuantX86();
188
  void initTrQuantX86();
189
#endif
190
#if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_TRAFO
191
  template<ARM_VEXT vext>
192
  void _initTrQuantARM();
193
  void initTrQuantARM();
194
#endif
195
};// END CLASS DEFINITION TrQuant
196
197
} // namespace vvenc
198
199
//! \}
200