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/RdCost.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     RdCost.h
43
    \brief    RD cost computation classes (header)
44
*/
45
46
#pragma once
47
48
#include "CommonDef.h"
49
#include "Mv.h"
50
#include "Unit.h"
51
#include "Slice.h"
52
53
#include <math.h>
54
55
//! \ingroup CommonLib
56
//! \{
57
58
namespace vvenc {
59
  
60
#if defined(TARGET_SIMD_X86)  && ENABLE_SIMD_OPT_DIST
61
using namespace x86_simd;
62
#endif
63
#if defined(TARGET_SIMD_ARM)  && ENABLE_SIMD_OPT_DIST
64
using namespace arm_simd;
65
#endif
66
67
class DistParam;
68
69
// ====================================================================================================================
70
// Type definition
71
// ====================================================================================================================
72
73
// for function pointer
74
typedef Distortion( *FpDistFunc   )( const DistParam& );
75
typedef void      ( *FpDistFuncX5 )( const DistParam&, Distortion*, bool );
76
// The SADs of four reference positions. A result above maximumDistortionForEarlyExit may be a partial sum.
77
typedef void      ( *FpSadFourPositions )( const DistParam&, const Pel* const*, Distortion* );
78
79
// ====================================================================================================================
80
// Class definition
81
// ====================================================================================================================
82
83
/// distortion parameter class
84
class DistParam
85
{
86
public:
87
  CPelBuf               org;
88
  CPelBuf               cur;
89
  FpDistFunc            distFunc  = nullptr;
90
  FpDistFuncX5          dmvrSadX5 = nullptr;
91
  FpSadFourPositions    sadFourPositions = nullptr; // SADs of four reference positions; null where no kernel exists
92
#if ENABLE_MEASURE_SEARCH_SPACE
93
  FpDistFunc            xDistFunc = nullptr;
94
#endif
95
  int                   bitDepth    = 0;
96
  int                   subShift    = 0;
97
  ComponentID           compID      = MAX_NUM_COMP;
98
  bool                  applyWeight = false;     // whether weighted prediction is used or not
99
  Distortion            maximumDistortionForEarlyExit = MAX_DISTORTION; /// During cost calculations, if distortion exceeds this value, cost calculations may early-terminate.
100
  const WPScalingParam* wpCur   = nullptr;           // weighted prediction scaling parameters for current ref
101
  const CPelBuf*        orgLuma = nullptr;
102
103
  const Pel*            mask        = nullptr;
104
  int                   maskStride  = 0;
105
  int                   stepX       = 0;
106
  int                   maskStride2 = 0;
107
108
35.7k
  DistParam() = default;
109
110
  // org and cur have the same size and the kernels only read org's, so cur's width and height stay 0
111
  DistParam( const Pel* _org, int _orgStride, const Pel* _cur, int _curStride, int _width, int _height, FpDistFunc _distFunc, int _bitDepth, int _subShift, ComponentID _compID )
112
3.60M
    : distFunc(_distFunc), bitDepth(_bitDepth), subShift(_subShift), compID(_compID)
113
3.60M
  {
114
3.60M
    org.buf    = _org;
115
3.60M
    org.stride = _orgStride;
116
3.60M
    org.width  = _width;
117
3.60M
    org.height = _height;
118
3.60M
    cur.buf    = _cur;
119
3.60M
    cur.stride = _curStride;
120
3.60M
  }
121
};
122
123
/// RD cost computation class
124
class RdCost
125
{
126
public:
127
  Distortion ( *m_fxdWtdPredPtr )( const DistParam& dp, uint32_t fixedWeight );
128
  // the bits of getBitsMultiplePredsIBC( x0 + i * xStep, y ) for i = 0 ... count - 1, with the given predictors
129
  void       ( *m_bvBitsLineIBC )( const Mv* predictors, const int x0, const int xStep, const int count, const int y, const bool useIMV, unsigned* bits );
130
131
  // for distortion
132
  FpDistFunc              m_afpDistortFunc[2][DF_TOTAL_FUNCTIONS]; // [eDFunc]
133
  FpDistFuncX5            m_afpDistortFuncX5[2]; // [eDFunc]
134
  FpSadFourPositions      m_afpSadFourPositions[8] = {}; // [log2 of the width], SAD at bit depths up to 10 without weights
135
136
private:
137
  vvencCostMode           m_costMode;
138
  double                  m_distortionWeight[MAX_NUM_COMP]; // only chroma values are used.
139
  double                  m_dLambda;
140
  double                  m_dLambda_unadjusted; // TODO: check is necessary
141
  double                  m_DistScaleUnadjusted;
142
143
  std::vector<uint32_t>   m_lumaLevelToWeightPLUT;
144
145
  double                  m_chromaWeight;
146
  ChromaFormat            m_cf;
147
  double                  m_DistScale;
148
  double                  m_dLambdaMotionSAD;
149
150
  // for motion cost
151
  Mv                      m_mvPredictor;
152
  Mv                      m_bvPredictors[2];
153
  double                  m_motionLambda;
154
  // The motion search asks for the cost of a bit count at every position it tests. A multiplication and a conversion back take longer than this lookup.
155
  static const uint32_t   MOTION_COST_TABLE_SIZE = 128;
156
  Distortion              m_motionCost[MOTION_COST_TABLE_SIZE];
157
  int                     m_iCostScale;
158
  double                  m_dCostIBC;
159
public:
160
  RdCost();
161
  virtual ~RdCost();
162
163
  void          create( bool enableOpt = true );
164
165
#if defined(TARGET_SIMD_X86)  && ENABLE_SIMD_OPT_DIST
166
  void          initRdCostX86();
167
  template <X86_VEXT vext>
168
  void          _initRdCostX86();
169
#endif
170
171
#if defined(TARGET_SIMD_ARM)  && ENABLE_SIMD_OPT_DIST
172
  void initRdCostARM();
173
  template<ARM_VEXT vext>
174
  void _initRdCostARM();
175
#endif   // TARGET_SIMD_ARM
176
177
21.1k
  void          setDistortionWeight ( const ComponentID compID, const double distortionWeight ) { m_distortionWeight[compID] = distortionWeight; }
178
  void          setLambda           ( double dLambda, const BitDepths &bitDepths );
179
5.72k
  void          setCostMode         ( vvencCostMode m )                      { m_costMode = m; }
180
181
0
  double        getLambda           ( bool unadj = false )              { return unadj ? m_dLambda_unadjusted : m_dLambda; }
182
0
  double        getChromaWeight     ()                                  { return ((m_distortionWeight[COMP_Cb] + m_distortionWeight[COMP_Cr]) / 2.0); }
183
  double        calcRdCost          ( uint64_t fracBits, Distortion distortion, bool useUnadjustedLambda = true ) const
184
3.28M
  {
185
3.28M
    return ( useUnadjustedLambda ? m_DistScaleUnadjusted : m_DistScale ) * double( distortion ) + double( fracBits );
186
3.28M
  }
187
188
  void          setDistParam        ( DistParam &rcDP, const CPelBuf& org, const Pel* piRefY , int iRefStride, int bitDepth, ComponentID compID, int subShiftMode = 0, int useHadamard = 0 );
189
  DistParam     setDistParam        ( const CPelBuf& org, const CPelBuf& cur, int bitDepth, DFunc dfunc );
190
  DistParam     setDistParam        ( const Pel* pOrg, const Pel* piRefY, int iOrgStride, int iRefStride, int bitDepth, ComponentID compID, int width, int height, int subShift, bool isDMVR = false );
191
  void          setDistParamGeo     ( DistParam &rcDP, const CPelBuf& org, const Pel *piRefY, int iRefStride, const Pel *mask, int iMaskStride, int stepX, int iMaskStride2, int bitDepth, ComponentID compID );
192
193
60.0k
  double        getMotionLambda     ()                      const { return m_dLambdaMotionSAD; }
194
0
  void          selectMotionLambda  ()                            { if( m_motionLambda != getMotionLambda() ) { setMotionLambda( getMotionLambda() ); } }
195
107k
  void          setPredictor        ( const Mv& rcMv )            { m_mvPredictor = rcMv; }
196
56.9k
  void          setCostScale        ( int iCostScale )            { m_iCostScale = iCostScale; }
197
0
  Distortion    getCost             ( uint32_t b )          const { return b < MOTION_COST_TABLE_SIZE ? m_motionCost[b] : Distortion( m_motionLambda * b ); }
198
  // for motion cost
199
  static uint32_t    xGetExpGolombNumberOfBits( int iVal )
200
214k
  {
201
214k
    CHECKD( iVal == std::numeric_limits<int>::min(), "Wrong value" );
202
203
#if ENABLE_SIMD_OPT && defined( TARGET_SIMD_X86 )
204
    // the proper Log2 is not restricted to 0...MAX_CU_SIZE
205
    return 1 + ( Log2( iVal <= 0 ? ( unsigned( -iVal ) << 1 ) + 1 : unsigned( iVal << 1 ) ) << 1 );
206
#else
207
214k
    unsigned uiLength2 = 1, uiTemp2 = ( iVal <= 0 ) ? ( unsigned( -iVal ) << 1 ) + 1 : unsigned( iVal << 1 );
208
209
225k
    while( uiTemp2 > MAX_CU_SIZE )
210
10.4k
    {
211
10.4k
      uiLength2 += ( MAX_CU_DEPTH << 1 );
212
10.4k
      uiTemp2  >>=   MAX_CU_DEPTH;
213
10.4k
    }
214
215
214k
    return uiLength2 + ( Log2(uiTemp2) << 1 );
216
214k
#endif
217
214k
  }
218
0
  Distortion     getCostOfVectorWithPredictor( const int x, const int y, const unsigned imvShift )  { return getCost( getBitsOfVectorWithPredictor( x, y, imvShift ) ); }
219
107k
  uint32_t       getBitsOfVectorWithPredictor( const int x, const int y, const unsigned imvShift )  { return getBitsOfVectorHor( x, imvShift ) + getBitsOfVectorVer( y, imvShift ); }
220
  // the two halves of getBitsOfVectorWithPredictor: each depends on one component only
221
107k
  uint32_t       getBitsOfVectorHor( const int x, const unsigned imvShift ) const { return xGetExpGolombNumberOfBits( ( ( x * ( 1 << m_iCostScale ) ) - m_mvPredictor.hor ) >> imvShift ); }
222
107k
  uint32_t       getBitsOfVectorVer( const int y, const unsigned imvShift ) const { return xGetExpGolombNumberOfBits( ( ( y * ( 1 << m_iCostScale ) ) - m_mvPredictor.ver ) >> imvShift ); }
223
224
  void           saveUnadjustedLambda ();
225
  void           setMotionLambda      ( double motionLambda );
226
0
  void           setChromaFormat      ( ChromaFormat cf )   { m_cf = cf; }
227
  void           setPredictorsIBC     (Mv* pcMv)
228
30.0k
  {
229
90.1k
    for (int i = 0; i < 2; i++)
230
60.0k
    {
231
60.0k
      m_bvPredictors[i] = pcMv[i];
232
60.0k
    }
233
30.0k
  }
234
30.0k
  void           getMotionCostIBC(int add) { m_dCostIBC = m_dLambdaMotionSAD + add; }
235
  Distortion     getBvCostMultiplePredsIBC(int x, int y, bool useIMV);
236
1.44M
  Distortion     getBvCostIBC         ( const unsigned bits ) const { return Distortion( m_dCostIBC * bits ); }
237
  // the bits of the block vectors ( x0 + i * xStep, y ), i = 0 ... count - 1, as getBitsMultiplePredsIBC
238
  void           getBvBitsRowIBC      ( const int x0, const int xStep, const int count, const int y, const bool useIMV, unsigned* bits ) const
239
20.7k
  {
240
20.7k
    m_bvBitsLineIBC( m_bvPredictors, x0, xStep, count, y, useIMV, bits );
241
20.7k
  }
242
  // the bits of the block vectors ( x, y0 + i * yStep ): the estimate is symmetric in the two components, so this is
243
  // the row of the transposed vectors
244
  void           getBvBitsColumnIBC   ( const int x, const int y0, const int yStep, const int count, const bool useIMV, unsigned* bits ) const
245
17.9k
  {
246
17.9k
    const Mv transposed[2] = { Mv( m_bvPredictors[0].ver, m_bvPredictors[0].hor ), Mv( m_bvPredictors[1].ver, m_bvPredictors[1].hor ) };
247
17.9k
    m_bvBitsLineIBC( transposed, y0, yStep, count, x, useIMV, bits );
248
17.9k
  }
249
  
250
  static Distortion xGetSAD8          ( const DistParam& pcDtParam );
251
  static Distortion xGetSAD16         ( const DistParam& pcDtParam ); // needs to be public for xGetSAD_MxN_SIMD ( NOTE: they are all public in vvenc )
252
  static void       xGetSAD16X5       ( const DistParam& pcDtParam, Distortion* cost, bool isCalCentrePos ); // needs to be public for xGetSADX5_16xN_SIMD ( NOTE: they are all public in vvenc )
253
254
  static Distortion xGetSSE           ( const DistParam& pcDtParam );
255
  static Distortion xGetSSE4          ( const DistParam& pcDtParam );
256
  static Distortion xGetSSE8          ( const DistParam& pcDtParam );
257
  static Distortion xGetSSE16         ( const DistParam& pcDtParam );
258
  static Distortion xGetSSE32         ( const DistParam& pcDtParam );
259
  static Distortion xGetSSE64         ( const DistParam& pcDtParam );
260
  static Distortion xGetSSE128        ( const DistParam& pcDtParam );
261
262
263
  static Distortion xGetSAD           ( const DistParam& pcDtParam );
264
  static Distortion xGetSAD4          ( const DistParam& pcDtParam );
265
  static Distortion xGetSAD32         ( const DistParam& pcDtParam );
266
  static Distortion xGetSAD64         ( const DistParam& pcDtParam );
267
  static Distortion xGetSAD128        ( const DistParam& pcDtParam );
268
  static Distortion xGetSADwMask      ( const DistParam &pcDtParam );
269
  
270
  static void       xGetSAD8X5        ( const DistParam& pcDtParam, Distortion* cost, bool isCalCentrePos );
271
  
272
  static Distortion xCalcHADs2x2      ( const Pel* piOrg, const Pel* piCur, int iStrideOrg, int iStrideCur );
273
  static Distortion xGetHAD2SADs      ( const DistParam& pcDtParam );
274
  // stop: return the partial sum after a row of tiles once it exceeds maximumDistortionForEarlyExit (DF_HAD_STOP*)
275
  template<bool fastHad, bool stop = false>
276
  static Distortion xGetHADs          ( const DistParam& pcDtParam );
277
0
  static bool       xHadStops         ( Distortion sum, const DistParam& dp ) { return ( sum >> DISTORTION_PRECISION_ADJUSTMENT( dp.bitDepth ) ) > dp.maximumDistortionForEarlyExit; }
278
279
#if defined(TARGET_SIMD_X86)  && ENABLE_SIMD_OPT_DIST
280
281
  template<X86_VEXT vext>
282
  static Distortion xGetSSE_SIMD    ( const DistParam& pcDtParam );
283
  template<int iWidth, X86_VEXT vext>
284
  static Distortion xGetSSE_NxN_SIMD( const DistParam& pcDtParam );
285
286
  template<X86_VEXT vext>
287
  static Distortion xGetSAD_SIMD    ( const DistParam& pcDtParam );
288
  template<int iWidth, X86_VEXT vext>
289
  static Distortion xGetSAD_NxN_SIMD( const DistParam& pcDtParam );
290
291
  template <X86_VEXT vext>
292
  static void xGetSADX5_8xN_SIMD    ( const DistParam& rcDtParam, Distortion* cost, bool isCalCentrePos );
293
  template <X86_VEXT vext>
294
  static void xGetSADX5_16xN_SIMD_X86   ( const DistParam& rcDtParam, Distortion* cost, bool isCalCentrePos );
295
296
  template<X86_VEXT vext, bool fastHad, bool stop = false>
297
  static Distortion xGetHADs_SIMD   ( const DistParam& pcDtParam );
298
  template<X86_VEXT vext>
299
  static Distortion xGetHAD2SADs_SIMD( const DistParam &rcDtParam );
300
301
  template<X86_VEXT vext> 
302
  static Distortion xGetSADwMask_SIMD( const DistParam &pcDtParam );
303
#endif
304
305
  unsigned int   getBitsMultiplePredsIBC(int x, int y, bool useIMV);
306
307
  Distortion   getDistPart( const CPelBuf& org, const CPelBuf& cur, int bitDepth, const ComponentID compId, DFunc eDFunc );
308
309
  void initLumaLevelToWeightTable( int lumaBD );
310
};// END CLASS DEFINITION RdCost
311
312
} // namespace vvenc
313
314
//! \}
315