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.cpp
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
43
44
/** \file     Quant.cpp
45
    \brief    transform and quantization class
46
*/
47
48
#include "Quant.h"
49
#include "UnitTools.h"
50
#include "ContextModelling.h"
51
#include "CodingStructure.h"
52
#include "dtrace_buffer.h"
53
54
#include <stdlib.h>
55
#include <memory.h>
56
57
//! \ingroup CommonLib
58
//! \{
59
60
namespace vvenc {
61
62
// ====================================================================================================================
63
// Constants
64
// ====================================================================================================================
65
66
67
// ====================================================================================================================
68
// QpParam constructor
69
// ====================================================================================================================
70
 
71
QpParam::QpParam(const TransformUnit& tu, const ComponentID &compID, const bool allowACTQpoffset)
72
2.64M
{
73
2.64M
  const ChannelType chType = toChannelType( compID );
74
2.64M
  const SPS        &sps    = *tu.cu->cs->sps;
75
2.64M
  const int     qpBdOffset = sps.qpBDOffset[chType];
76
2.64M
  const bool useJQP        = isChroma( compID ) && abs( TU::getICTMode( tu ) ) == 2;
77
2.64M
  const ComponentID jCbCr  = useJQP ? COMP_JOINT_CbCr : compID;
78
  
79
2.64M
        int chromaQpOffset = 0;
80
81
2.64M
  if( isChroma( compID ) )
82
2.45M
  {
83
2.45M
    const PPS &pps  = *tu.cu->slice->pps;
84
2.45M
    chromaQpOffset  = pps.chromaQpOffset              [jCbCr];
85
2.45M
    chromaQpOffset += tu.cu->slice->sliceChromaQpDelta[jCbCr];
86
2.45M
    chromaQpOffset += pps.getChromaQpOffsetListEntry( tu.cu->chromaQpAdj ).u.offset[int( jCbCr ) - 1];
87
2.45M
  }
88
  
89
2.64M
  int baseQp;
90
2.64M
  int qpy        = tu.cu->qp;
91
  //bool skip      = tu.mtsIdx[compID] == MTS_SKIP;
92
93
2.64M
  if( isLuma( compID ) )
94
190k
  {
95
190k
    baseQp = tu.cu->qp + qpBdOffset;
96
190k
  }
97
2.45M
  else
98
2.45M
  {
99
2.45M
    int qpi = Clip3( -qpBdOffset, MAX_QP, qpy );
100
2.45M
    baseQp  = sps.chromaQpMappingTable.getMappedChromaQpValue( jCbCr, qpi );
101
2.45M
    baseQp  = Clip3( -qpBdOffset, MAX_QP, baseQp + chromaQpOffset ) + qpBdOffset;
102
2.45M
  }
103
104
2.64M
  if( allowACTQpoffset && tu.cu->colorTransform )
105
0
  {
106
0
    baseQp += DELTA_QP_ACT[jCbCr];
107
0
  }
108
109
2.64M
  baseQp = Clip3( 0, MAX_QP + qpBdOffset, baseQp );
110
111
  //if( !skip )
112
2.64M
  {
113
2.64M
    Qps [0] = baseQp;
114
2.64M
    pers[0] = baseQp / 6;
115
2.64M
    rems[0] = baseQp % 6;
116
2.64M
  }
117
  //else
118
2.64M
  {
119
2.64M
    int internalMinusInputBitDepth = sps.internalMinusInputBitDepth[chType];
120
2.64M
    int baseQpTS           = std::max( baseQp, 4 + 6 * internalMinusInputBitDepth );
121
122
2.64M
    Qps [1] = baseQpTS;
123
2.64M
    pers[1] = baseQpTS / 6;
124
2.64M
    rems[1] = baseQpTS % 6;
125
2.64M
  }
126
2.64M
}
127
128
129
// ====================================================================================================================
130
// Quant class member functions
131
// ====================================================================================================================
132
static void QuantCore(const TransformUnit tu, const ComponentID compID, const CCoeffBuf& piCoef,CoeffSigBuf piQCoef,TCoeff &uiAbsSum, int &lastScanPos,TCoeff *deltaU,const int defaultQuantisationCoefficient,const int iQBits,const int64_t iAdd,const TCoeff entropyCodingMinimum,const TCoeff entropyCodingMaximum,const bool signHiding, const TCoeff m_thrVal)
133
6.51k
{
134
6.51k
  CoeffCodingContext cctx( tu, compID, signHiding );
135
136
6.51k
  const CompArea &rect      = tu.blocks[compID];
137
6.51k
  const uint32_t uiWidth    = rect.width;
138
6.51k
  const uint32_t uiHeight   = rect.height;
139
140
  /* for 422 chroma blocks, the effective scaling applied during transformation is not a power of 2, hence it cannot be
141
  * implemented as a bit-shift (the quantised result will be sqrt(2) * larger than required). Alternatively, adjust the
142
  * uiLog2TrSize applied in iTransformShift, such that the result is 1/sqrt(2) the required result (i.e. smaller)
143
  * Then a QP+3 (sqrt(2)) or QP-3 (1/sqrt(2)) method could be used to get the required result
144
  */
145
146
6.51k
  const uint32_t log2CGSize         = cctx.log2CGSize();
147
148
6.51k
  uiAbsSum = 0;
149
150
6.51k
  const int iCGSize   = 1 << log2CGSize;
151
152
6.51k
  const uint32_t lfnstIdx = tu.cu->lfnstIdx;
153
6.51k
  const int iCGNum   = lfnstIdx > 0 ? 1 : std::min<int>(JVET_C0024_ZERO_OUT_TH, uiWidth) * std::min<int>(JVET_C0024_ZERO_OUT_TH, uiHeight) >> cctx.log2CGSize();
154
6.51k
  int       iScanPos = ( iCGNum << log2CGSize ) - 1;
155
156
6.51k
  if( lfnstIdx > 0 && ( ( uiWidth == 4 && uiHeight == 4 ) || ( uiWidth == 8 && uiHeight == 8 ) ) )
157
0
  {
158
0
    iScanPos = 7;
159
0
  }
160
161
  // Find first non-zero coeff
162
37.5k
  for( ; iScanPos > 0; iScanPos-- )
163
35.8k
  {
164
35.8k
    uint32_t uiBlkPos = cctx.blockPos( iScanPos );
165
35.8k
    if( piCoef.buf[uiBlkPos] )
166
4.81k
      break;
167
35.8k
  }
168
169
  //////////////////////////////////////////////////////////////////////////
170
  //  Loop over sub-sets (coefficient groups)
171
  //////////////////////////////////////////////////////////////////////////
172
  
173
6.51k
  TCoeff thres = 0, useThres = 0;
174
  
175
6.51k
  if( iQBits )
176
6.51k
    thres = TCoeff( ( int64_t( m_thrVal ) << ( iQBits - 1 ) ) );
177
0
  else
178
0
    thres = TCoeff( ( int64_t( m_thrVal >> 1 ) << iQBits ) );
179
180
6.51k
  useThres = thres / ( defaultQuantisationCoefficient << 2 );
181
182
6.51k
  const bool is4x4sbb = log2CGSize == 4 && cctx.log2CGWidth() == 2;
183
184
6.51k
  int subSetId = iScanPos >> log2CGSize;
185
7.45k
  for( ; subSetId >= 1; subSetId-- )
186
938
  {
187
938
    if( is4x4sbb && iScanPos >= 16 )
188
0
    {
189
0
      int  iScanPosinCG = iScanPos & ( iCGSize - 1 );
190
0
      bool allSmaller   = true;
191
192
0
      for( int xScanPosinCG = iScanPosinCG, xScanPos = iScanPos; allSmaller && xScanPosinCG >= 0; xScanPosinCG--, xScanPos-- )
193
0
      {
194
0
        const uint32_t uiBlkPos = cctx.blockPos( xScanPos );
195
0
        allSmaller &= abs( piCoef.buf[uiBlkPos] ) <= useThres;
196
0
      }
197
198
0
      if( allSmaller )
199
0
      {
200
0
        iScanPos    -= iScanPosinCG + 1;
201
0
        continue;
202
0
      }
203
0
      else
204
0
      {
205
0
        break;
206
0
      }
207
0
    }
208
938
  }
209
210
6.51k
  const int qBits8 = iQBits - 8;
211
6.51k
  piQCoef.memset( 0 );
212
213
100k
  for( int currPos = 0; currPos <= iScanPos; currPos++ )
214
93.8k
  {
215
93.8k
    const int uiBlockPos  = cctx.blockPos( currPos );
216
93.8k
    const TCoeff iLevel   = piCoef.buf[uiBlockPos];
217
93.8k
    const TCoeff iSign    = (iLevel < 0 ? -1: 1);
218
219
93.8k
    const int64_t  tmpLevel = (int64_t)abs(iLevel) * defaultQuantisationCoefficient;
220
93.8k
    const TCoeff quantisedMagnitude = TCoeff((tmpLevel + iAdd ) >> iQBits);
221
93.8k
    deltaU[uiBlockPos] = (TCoeff)((tmpLevel - ((int64_t)quantisedMagnitude<<iQBits) )>> qBits8);
222
223
93.8k
    uiAbsSum += quantisedMagnitude;
224
93.8k
    const TCoeff quantisedCoefficient = quantisedMagnitude * iSign;
225
226
93.8k
    piQCoef.buf[uiBlockPos] = Clip3<TCoeff>( entropyCodingMinimum, entropyCodingMaximum, quantisedCoefficient );
227
93.8k
  } // for n
228
229
6.51k
  lastScanPos = iScanPos;
230
6.51k
}
231
232
static void DeQuantCore(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)
233
56.9k
{
234
56.9k
  const int inputMinimum = -(inputMaximum+1);
235
56.9k
  const TCoeff transformMinimum = -(transformMaximum+1);
236
56.9k
  if (rightShift>0)
237
46.7k
  {
238
46.7k
    const Intermediate_Int iAdd = (Intermediate_Int) 1 << (rightShift - 1);
239
453k
    for( int y = 0, n = 0; y <= maxY; y++)
240
407k
    {
241
4.42M
      for( int x = 0; x <= maxX; x++, n++ )
242
4.01M
      {
243
4.01M
        const TCoeff           clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[x + y * piQCfStride]));
244
4.01M
        Intermediate_Int iCoeffQ   = (Intermediate_Int(clipQCoef) * scale + iAdd) >> rightShift;
245
4.01M
        piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
246
4.01M
      }
247
407k
    }
248
46.7k
  }
249
10.2k
  else  // rightshift <0
250
10.2k
  {
251
10.2k
    int leftShift = -rightShift;
252
124k
    for( int y = 0, n = 0; y <= maxY; y++)
253
113k
    {
254
1.39M
      for( int x = 0; x <= maxX; x++, n++ )
255
1.27M
      {
256
1.27M
        const TCoeff           clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[x + y * piQCfStride]));
257
1.27M
        const Intermediate_Int iCoeffQ   = (Intermediate_Int(clipQCoef) * scale) * (1 << leftShift);
258
1.27M
        piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
259
1.27M
      }
260
113k
    }
261
10.2k
  }
262
56.9k
}
263
264
static bool needRdoqCore( const TCoeff* pCoeff, size_t numCoeff, int quantCoeff, int64_t offset, int shift )
265
0
{
266
0
  for( int uiBlockPos = 0; uiBlockPos < numCoeff; uiBlockPos++ )
267
0
  {
268
0
    const TCoeff   iLevel = pCoeff[uiBlockPos];
269
0
    const int64_t  tmpLevel = ( int64_t ) std::abs( iLevel ) * quantCoeff;
270
0
    const TCoeff quantisedMagnitude = TCoeff( ( tmpLevel + offset ) >> shift );
271
272
0
    if( quantisedMagnitude != 0 )
273
0
    {
274
0
      return true;
275
0
    }
276
0
  } // for n
277
0
  return false;
278
0
}
279
280
281
void Quant::fwdResDPCMCore( TCoeffSig* coef, const ptrdiff_t stride, const int width, const int height, const bool vertical )
282
6.51k
{
283
6.51k
  if( !vertical )
284
0
  {
285
0
    for( int y = 0; y < height; y++ )
286
0
    {
287
0
      for( int x = width - 1; x > 0; x-- )
288
0
      {
289
0
        coef[x] -= coef[x - 1];
290
0
      }
291
0
      coef += stride;
292
0
    }
293
0
  }
294
6.51k
  else
295
6.51k
  {
296
6.51k
    coef += stride * (height - 1);
297
13.0k
    for( int y = 0; y < height - 1; y++ )
298
6.51k
    {
299
68.9k
      for ( int x = 0; x < width; x++ )
300
62.4k
      {
301
62.4k
        coef[x] -= coef[x - stride];
302
62.4k
      }
303
6.51k
      coef -= stride;
304
6.51k
    }
305
6.51k
  }
306
6.51k
}
307
308
void Quant::invResDPCMVerCore( const TCoeffSig* coef, const ptrdiff_t coefStride, TCoeffSig* dst, const ptrdiff_t dstStride, const int width, const int height )
309
56.4k
{
310
56.4k
  const TCoeff inputMinimum = std::numeric_limits<TCoeffSig>::min();
311
56.4k
  const TCoeff inputMaximum = std::numeric_limits<TCoeffSig>::max();
312
313
599k
  for( int x = 0; x < width; x++ )
314
543k
  {
315
543k
    dst[x] = coef[x];
316
543k
  }
317
513k
  for( int y = 0; y < height - 1; y++ )
318
457k
  {
319
5.08M
    for( int x = 0; x < width; x++ )
320
4.63M
    {
321
4.63M
      dst[dstStride + x] = Clip3(inputMinimum, inputMaximum, TCoeff( dst[x] ) + TCoeff( coef[coefStride + x] ));
322
4.63M
    }
323
457k
    coef += coefStride;
324
457k
    dst += dstStride;
325
457k
  }
326
56.4k
}
327
328
void Quant::findFirstPosCore( int& firstTestPos, const TCoeff* tCoeff, const ScanElement* scan, int /*width*/, int /*log2SbbWidth*/, int /*log2SbbHeight*/,
329
                              int th, bool zeroOut, int zeroOutWidth, int zeroOutHeight )
330
2.25M
{
331
259M
  for( ; firstTestPos >= 0; firstTestPos-- )
332
258M
  {
333
258M
    if( zeroOut && ( scan[firstTestPos].x >= zeroOutWidth || scan[firstTestPos].y >= zeroOutHeight ) )
334
0
    {
335
0
      continue;
336
0
    }
337
258M
    if( abs( tCoeff[scan[firstTestPos].idx] ) > th )
338
941k
    {
339
941k
      break;
340
941k
    }
341
258M
  }
342
2.25M
}
343
344
bool Quant::allZeroLevels4x4Core( const TCoeff* src, ptrdiff_t stride, int quantScale, int zeroLimit )
345
29.1k
{
346
76.7k
  for( int y = 0; y < 4; y++, src += stride )
347
64.8k
  {
348
255k
    for( int x = 0; x < 4; x++ )
349
207k
    {
350
207k
      CHECKD( int64_t( abs( src[x] ) ) * quantScale > std::numeric_limits<int32_t>::max(), "the product must fit 32 bits" );
351
207k
      if( abs( src[x] ) * quantScale >= zeroLimit )
352
17.2k
      {
353
17.2k
        return false;
354
17.2k
      }
355
207k
    }
356
64.8k
  }
357
11.8k
  return true;
358
29.1k
}
359
360
5.72k
Quant::Quant( const Quant* other, bool useScalingLists, bool enableOpt ) : m_RDOQ( 0 ), m_useRDOQTS( false ), m_dLambda( 0.0 )
361
5.72k
{
362
5.72k
  xInitScalingList( other, useScalingLists );
363
5.72k
  xFindFirstPos     = findFirstPosCore;
364
5.72k
  xAllZeroLevels4x4 = allZeroLevels4x4Core;
365
5.72k
  xDeQuant       = DeQuantCore;
366
5.72k
  xQuant         = QuantCore;
367
5.72k
  xNeedRdoq      = needRdoqCore;
368
5.72k
  xFwdResDPCM    = fwdResDPCMCore;
369
5.72k
  xInvResDPCMVer = invResDPCMVerCore;
370
5.72k
  if( enableOpt )
371
5.72k
  {
372
#if defined( TARGET_SIMD_X86 ) && ENABLE_SIMD_OPT_QUANT
373
    initQuantX86();
374
#endif
375
#if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_OPT_QUANT
376
    initQuantARM();
377
#endif
378
5.72k
  }
379
5.72k
}
380
381
Quant::~Quant()
382
5.72k
{
383
5.72k
  xDestroyScalingList();
384
5.72k
}
385
386
void Quant::xInvResDPCM( const TransformUnit& tu, const ComponentID compID, CoeffSigBuf& dstBuf ) const
387
56.9k
{
388
56.9k
  const CompArea&    rect   = tu.blocks[compID];
389
56.9k
  const int          wdt    = rect.width;
390
56.9k
  const int          hgt    = rect.height;
391
56.9k
  const CCoeffSigBuf coeffs = tu.getCoeffs(compID);
392
393
56.9k
  const int      maxLog2TrDynamicRange = tu.cs->sps->getMaxLog2TrDynamicRange();
394
56.9k
  const TCoeff   inputMinimum          = -(1 << maxLog2TrDynamicRange);
395
56.9k
  const TCoeff   inputMaximum          =  (1 << maxLog2TrDynamicRange) - 1;
396
397
56.9k
  const TCoeffSig* coef = &coeffs.buf[0];
398
56.9k
        TCoeffSig* dst  = &dstBuf.buf[0];
399
400
56.9k
  if ( tu.cu->bdpcmM[toChannelType(compID)] == 1)
401
574
  {
402
    // a running sum along each row, clipped at every step: sequential
403
8.07k
    for( int y = 0; y < hgt; y++ )
404
7.49k
    {
405
7.49k
      dst[0] = coef[0];
406
119k
      for( int x = 1; x < wdt; x++ )
407
112k
      {
408
112k
        dst[x] = Clip3(inputMinimum, inputMaximum, TCoeff( dst[x - 1] ) + TCoeff( coef[x] ));
409
112k
      }
410
7.49k
      coef += coeffs.stride;
411
7.49k
      dst += dstBuf.stride;
412
7.49k
    }
413
574
  }
414
56.4k
  else
415
56.4k
  {
416
56.4k
    CHECKD( maxLog2TrDynamicRange != 15, "the vertical inverse clips to the 16-bit range" );
417
56.4k
    xInvResDPCMVer( coef, coeffs.stride, dst, dstBuf.stride, wdt, hgt );
418
56.4k
  }
419
56.9k
}
420
421
// To minimize the distortion only. No rate is considered.
422
void Quant::xSignBitHidingHDQ( TCoeffSig* pQCoef, const TCoeff* pCoef, TCoeff* deltaU, const CoeffCodingContext& cctx, int& lastScanPos, const int maxLog2TrDynamicRange )
423
0
{
424
0
  const uint32_t groupSize = 1 << cctx.log2CGSize();
425
426
0
  const TCoeff entropyCodingMinimum = -(1 << maxLog2TrDynamicRange);
427
0
  const TCoeff entropyCodingMaximum =  (1 << maxLog2TrDynamicRange) - 1;
428
429
0
  int lastCG = -1;
430
0
  int absSum = 0 ;
431
0
  int n ;
432
433
0
  for( int subSet = lastScanPos >> cctx.log2CGSize(); subSet >= 0; subSet-- )
434
0
  {
435
0
    int  subPos = subSet << cctx.log2CGSize();
436
0
    int  firstNZPosInCG=groupSize , lastNZPosInCG=-1 ;
437
0
    absSum = 0 ;
438
439
0
    for(n = groupSize-1; n >= 0; --n )
440
0
    {
441
0
      if( pQCoef[ cctx.blockPos( n + subPos ) ] )
442
0
      {
443
0
        lastNZPosInCG = n;
444
0
        break;
445
0
      }
446
0
    }
447
448
0
    for(n = 0; n <groupSize; n++ )
449
0
    {
450
0
      if( pQCoef[ cctx.blockPos( n + subPos ) ] )
451
0
      {
452
0
        firstNZPosInCG = n;
453
0
        break;
454
0
      }
455
0
    }
456
457
0
    for(n = firstNZPosInCG; n <=lastNZPosInCG; n++ )
458
0
    {
459
0
      absSum += int(pQCoef[ cctx.blockPos( n + subPos ) ]);
460
0
    }
461
462
0
    if(lastNZPosInCG>=0 && lastCG==-1)
463
0
    {
464
0
      lastCG = 1 ;
465
0
    }
466
467
0
    if( lastNZPosInCG-firstNZPosInCG>=SBH_THRESHOLD )
468
0
    {
469
0
      uint32_t signbit = (pQCoef[cctx.blockPos(subPos+firstNZPosInCG)]>0?0:1) ;
470
0
      if( signbit!=(absSum&0x1) )  //compare signbit with sum_parity
471
0
      {
472
0
        TCoeff curCost    = std::numeric_limits<TCoeff>::max();
473
0
        TCoeff minCostInc = std::numeric_limits<TCoeff>::max();
474
0
        int minPos =-1, finalChange=0, curChange=0, minScanPos = -1;
475
476
0
        for( n = (lastCG==1?lastNZPosInCG:groupSize-1) ; n >= 0; --n )
477
0
        {
478
0
          uint32_t blkPos   = cctx.blockPos( n+subPos );
479
0
          if(pQCoef[ blkPos ] != 0 )
480
0
          {
481
0
            if(deltaU[blkPos]>0)
482
0
            {
483
0
              curCost = - deltaU[blkPos];
484
0
              curChange=1 ;
485
0
            }
486
0
            else
487
0
            {
488
              //curChange =-1;
489
0
              if(n==firstNZPosInCG && abs(pQCoef[blkPos])==1)
490
0
              {
491
0
                curCost = std::numeric_limits<TCoeff>::max();
492
0
              }
493
0
              else
494
0
              {
495
0
                curCost = deltaU[blkPos];
496
0
                curChange =-1;
497
0
              }
498
0
            }
499
0
          }
500
0
          else
501
0
          {
502
0
            if(n<firstNZPosInCG)
503
0
            {
504
0
              uint32_t thisSignBit = (pCoef[blkPos]>=0?0:1);
505
0
              if(thisSignBit != signbit )
506
0
              {
507
0
                curCost = std::numeric_limits<TCoeff>::max();
508
0
              }
509
0
              else
510
0
              {
511
0
                curCost = - (deltaU[blkPos])  ;
512
0
                curChange = 1 ;
513
0
              }
514
0
            }
515
0
            else
516
0
            {
517
0
              curCost = - (deltaU[blkPos])  ;
518
0
              curChange = 1 ;
519
0
            }
520
0
          }
521
522
0
          if( curCost<minCostInc)
523
0
          {
524
0
            minCostInc = curCost ;
525
0
            finalChange = curChange ;
526
0
            minPos = blkPos;
527
0
            minScanPos = n + subPos;
528
0
          }
529
0
        } //CG loop
530
531
0
        if(pQCoef[minPos] == entropyCodingMaximum || pQCoef[minPos] == entropyCodingMinimum)
532
0
        {
533
0
          finalChange = -1;
534
0
        }
535
536
0
        if(pCoef[minPos]>=0)
537
0
        {
538
0
          pQCoef[minPos] += finalChange ;
539
0
        }
540
0
        else
541
0
        {
542
0
          pQCoef[minPos] -= finalChange ;
543
0
        }
544
545
        // if changing lastScanPos element to 0, move the pointer to the new lastScanPos element
546
0
        if( minScanPos == lastScanPos && pQCoef[minPos] == 0 )
547
0
        {
548
0
          for( ; lastScanPos >= 0 && pQCoef[cctx.blockPos( lastScanPos )] == 0; lastScanPos-- );
549
0
        }
550
0
        else if( minScanPos > lastScanPos && pQCoef[minPos] != 0 )
551
0
        {
552
0
          lastScanPos = minPos;
553
0
        }
554
0
      } // Hide
555
0
    }
556
0
    if(lastCG==1)
557
0
    {
558
0
      lastCG=0 ;
559
0
    }
560
0
  } // TU loop
561
562
0
  return;
563
0
}
564
565
void Quant::dequant(const TransformUnit& tu,
566
                          CoeffBuf&      dstCoeff,
567
                    const ComponentID    compID,
568
                    const QpParam&       cQP)
569
56.9k
{
570
56.9k
  const SPS       *sps                  = tu.cs->sps;
571
56.9k
  const CompArea  &area                 = tu.blocks[compID];
572
56.9k
  const uint32_t  uiWidth               = area.width;
573
56.9k
  const uint32_t  uiHeight              = area.height;
574
56.9k
  TCoeff *const   piCoef                = dstCoeff.buf;
575
56.9k
  const uint32_t  numSamplesInBlock     = uiWidth * uiHeight;
576
56.9k
  const int       maxLog2TrDynamicRange = sps->getMaxLog2TrDynamicRange();
577
56.9k
  const TCoeff    transformMinimum      = -(1 << maxLog2TrDynamicRange);
578
56.9k
  const TCoeff    transformMaximum      =  (1 << maxLog2TrDynamicRange) - 1;
579
56.9k
  const bool      isTransformSkip       = tu.mtsIdx[compID] == MTS_SKIP;
580
56.9k
  const bool      isLfnstApplied        = tu.cu->lfnstIdx > 0 && (CU::isSepTree(*tu.cu) ? true : isLuma(compID));
581
56.9k
  const bool      enableScalingLists    = getUseScalingList(uiWidth, uiHeight, isTransformSkip, isLfnstApplied);
582
56.9k
  const int       scalingListType       = getScalingListType(tu.cu->predMode, compID);
583
56.9k
  const int       channelBitDepth       = sps->bitDepths[toChannelType(compID)];
584
585
56.9k
  const TCoeffSig *coef     = tu.getCoeffs( compID ).buf;
586
56.9k
  const ptrdiff_t  piStride = tu.getCoeffs( compID ).stride;
587
588
56.9k
  if( tu.cu->bdpcmM[toChannelType( compID )] )
589
56.9k
  {
590
56.9k
    CoeffSigBuf coefBuf( m_tmpBdpcm, uiWidth, uiHeight );
591
56.9k
    xInvResDPCM( tu, compID, coefBuf );
592
56.9k
    coef      = m_tmpBdpcm;
593
56.9k
  }
594
595
56.9k
  const TCoeffSig  *const piQCoef = coef;
596
56.9k
  CHECK(scalingListType >= SCALING_LIST_NUM, "Invalid scaling list");
597
598
  // Represents scaling through forward transform
599
56.9k
  const int  originalTransformShift = getTransformShift(channelBitDepth, area.size(), maxLog2TrDynamicRange);
600
56.9k
  const bool needSqrtAdjustment     = TU::needsSqrt2Scale( tu, compID );
601
56.9k
  const int  iTransformShift        = originalTransformShift + (needSqrtAdjustment?-1:0);
602
603
56.9k
  const int QP_per = cQP.per(isTransformSkip);
604
56.9k
  const int QP_rem = cQP.rem(isTransformSkip);
605
606
56.9k
  const int  rightShift = (IQUANT_SHIFT - ((isTransformSkip ? 0 : iTransformShift) + QP_per)) + (enableScalingLists ? LOG2_SCALING_LIST_NEUTRAL_VALUE : 0);
607
608
56.9k
  if(enableScalingLists)
609
0
  {
610
    //from the dequantization equation:
611
    //iCoeffQ                         = ((Intermediate_Int(clipQCoef) * piDequantCoef[deQuantIdx]) + iAdd ) >> rightShift
612
    //(sizeof(Intermediate_Int) * 8)  =              inputBitDepth    +    dequantCoefBits                   - rightShift
613
0
    const uint32_t             dequantCoefBits     = 1 + IQUANT_SHIFT + SCALING_LIST_BITS;
614
0
    const uint32_t             targetInputBitDepth = std::min<uint32_t>((maxLog2TrDynamicRange + 1), (((sizeof(Intermediate_Int) * 8) + rightShift) - dequantCoefBits));
615
616
0
    const Intermediate_Int inputMinimum        = -(1 << (targetInputBitDepth - 1));
617
0
    const Intermediate_Int inputMaximum        =  (1 << (targetInputBitDepth - 1)) - 1;
618
619
0
    const uint32_t uiLog2TrWidth  = Log2(uiWidth);
620
0
    const uint32_t uiLog2TrHeight = Log2(uiHeight);
621
0
    int* piDequantCoef            = getDequantCoeff(scalingListType, QP_rem, uiLog2TrWidth, uiLog2TrHeight);
622
623
0
    if(rightShift > 0)
624
0
    {
625
0
      const Intermediate_Int iAdd = (Intermediate_Int) 1 << (rightShift - 1);
626
0
      for( int n = 0; n < numSamplesInBlock; n++ )
627
0
      {
628
0
        const TCoeff           clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[n]));
629
0
        const Intermediate_Int iCoeffQ   = ((Intermediate_Int(clipQCoef) * piDequantCoef[n]) + iAdd ) >> rightShift;
630
0
        piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
631
0
      }
632
0
    }
633
0
    else
634
0
    {
635
0
      const int leftShift = -rightShift;
636
0
      for( int n = 0; n < numSamplesInBlock; n++ )
637
0
      {
638
0
        const TCoeff           clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[n]));
639
0
        const Intermediate_Int iCoeffQ   = (Intermediate_Int(clipQCoef) * piDequantCoef[n]) << leftShift;
640
0
        piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
641
0
      }
642
0
    }
643
0
  }
644
56.9k
  else
645
56.9k
  {
646
56.9k
    const int scale     = g_invQuantScales[needSqrtAdjustment?1:0][QP_rem];
647
56.9k
    const int scaleBits = ( IQUANT_SHIFT + 1 );
648
    //from the dequantisation equation:
649
    //iCoeffQ                         = Intermediate_Int((int64_t(clipQCoef) * scale + iAdd) >> rightShift);
650
    //(sizeof(Intermediate_Int) * 8)  =                    inputBitDepth   + scaleBits      - rightShift
651
56.9k
    const uint32_t             targetInputBitDepth = std::min<uint32_t>((maxLog2TrDynamicRange + 1), (((sizeof(Intermediate_Int) * 8) + rightShift) - scaleBits));
652
56.9k
    const Intermediate_Int inputMaximum        =  (1 << (targetInputBitDepth - 1)) - 1;
653
56.9k
    xDeQuant(uiWidth-1,uiHeight-1,scale,piQCoef,piStride,piCoef,rightShift,inputMaximum,transformMaximum);
654
56.9k
  }
655
56.9k
}
656
657
void Quant::init( int rdoq, bool bUseRDOQTS, int thrVal, int sbbSkipThrVal )
658
5.72k
{
659
660
  // TODO: pass to init() a single variable containing (quantization) flags,
661
  //       instead of variables that don't have to do with this class
662
663
5.72k
  m_RDOQ             = rdoq;
664
5.72k
  m_useRDOQTS        = bUseRDOQTS;
665
5.72k
  m_thrVal           = thrVal;
666
5.72k
  m_sbbSkipThrVal    = sbbSkipThrVal;
667
5.72k
}
668
669
/** set flat matrix value to quantized coefficient
670
 */
671
void Quant::setFlatScalingList(const int maxLog2TrDynamicRange[MAX_NUM_CH], const BitDepths &bitDepths )
672
5.72k
{
673
5.72k
  if( !m_scalingListEnabled ) return;
674
675
0
  const int minimumQp = 0;
676
0
  const int maximumQp = SCALING_LIST_REM_NUM;
677
678
0
  for(uint32_t sizeX = 0; sizeX < SCALING_LIST_SIZE_NUM; sizeX++)
679
0
  {
680
0
    for(uint32_t sizeY = 0; sizeY < SCALING_LIST_SIZE_NUM; sizeY++)
681
0
    {
682
0
      for(uint32_t list = 0; list < SCALING_LIST_NUM; list++)
683
0
      {
684
0
        for(int qp = minimumQp; qp < maximumQp; qp++)
685
0
        {
686
0
          xSetFlatScalingList( list, sizeX, sizeY, qp );
687
0
        }
688
0
      }
689
0
    }
690
0
  }
691
0
}
692
693
/** set flat matrix value to quantized coefficient
694
 * \param list List ID
695
 * \param size size index
696
 * \param qp Quantization parameter
697
 * \param format chroma format
698
 */
699
void Quant::xSetFlatScalingList(uint32_t list, uint32_t sizeX, uint32_t sizeY, int qp )
700
0
{
701
0
  uint32_t i,num = g_scalingListSizeX[sizeX]*g_scalingListSizeX[sizeY];
702
0
  int *quantcoeff;
703
0
  int *dequantcoeff;
704
705
0
  const bool blockIsNotPowerOf4 = ((Log2(g_scalingListSizeX[sizeX] * g_scalingListSizeX[sizeY])) & 1) == 1;
706
0
  int quantScales    = g_quantScales   [blockIsNotPowerOf4?1:0][qp];
707
0
  int invQuantScales = g_invQuantScales[blockIsNotPowerOf4?1:0][qp] << 4;
708
709
0
  quantcoeff   = getQuantCoeff(list, qp, sizeX, sizeY);
710
0
  dequantcoeff = getDequantCoeff(list, qp, sizeX, sizeY);
711
712
0
  for(i=0;i<num;i++)
713
0
  {
714
0
    *quantcoeff++ = quantScales;
715
0
    *dequantcoeff++ = invQuantScales;
716
0
  }
717
0
}
718
719
720
/** initialization process of scaling list array
721
 */
722
void Quant::xInitScalingList( const Quant* other, bool useScalingLists )
723
5.72k
{
724
5.72k
  m_isScalingListOwner = other == nullptr;
725
5.72k
  m_scalingListEnabled = useScalingLists;
726
727
45.8k
  for(uint32_t sizeIdX = 0; sizeIdX < SCALING_LIST_SIZE_NUM; sizeIdX++)
728
40.0k
  {
729
320k
    for(uint32_t sizeIdY = 0; sizeIdY < SCALING_LIST_SIZE_NUM; sizeIdY++)
730
280k
    {
731
1.96M
      for(uint32_t qp = 0; qp < SCALING_LIST_REM_NUM; qp++)
732
1.68M
      {
733
11.7M
        for(uint32_t listId = 0; listId < SCALING_LIST_NUM; listId++)
734
10.1M
        {
735
10.1M
          if( m_isScalingListOwner )
736
2.52M
          {
737
2.52M
            const size_t scalingListSize = g_scalingListSizeX[sizeIdX] * g_scalingListSizeX[sizeIdY];
738
739
2.52M
            m_quantCoef   [sizeIdX][sizeIdY][listId][qp] = useScalingLists ? new int[scalingListSize] : nullptr;
740
2.52M
            m_dequantCoef [sizeIdX][sizeIdY][listId][qp] = useScalingLists ? new int[scalingListSize] : nullptr;
741
2.52M
          }
742
7.57M
          else
743
7.57M
          {
744
7.57M
            m_quantCoef   [sizeIdX][sizeIdY][listId][qp] = other->m_quantCoef   [sizeIdX][sizeIdY][listId][qp];
745
7.57M
            m_dequantCoef [sizeIdX][sizeIdY][listId][qp] = other->m_dequantCoef [sizeIdX][sizeIdY][listId][qp];
746
7.57M
          }
747
10.1M
        } // listID loop
748
1.68M
      }
749
280k
    }
750
40.0k
  }
751
5.72k
}
752
753
/** destroy quantization matrix array
754
 */
755
void Quant::xDestroyScalingList()
756
5.72k
{
757
5.72k
  if( !m_isScalingListOwner ) return;
758
759
11.4k
  for(uint32_t sizeIdX = 0; sizeIdX < SCALING_LIST_SIZE_NUM; sizeIdX++)
760
10.0k
  {
761
80.1k
    for(uint32_t sizeIdY = 0; sizeIdY < SCALING_LIST_SIZE_NUM; sizeIdY++)
762
70.1k
    {
763
491k
      for(uint32_t listId = 0; listId < SCALING_LIST_NUM; listId++)
764
421k
      {
765
2.94M
        for(uint32_t qp = 0; qp < SCALING_LIST_REM_NUM; qp++)
766
2.52M
        {
767
2.52M
          if(m_quantCoef[sizeIdX][sizeIdY][listId][qp])
768
0
          {
769
0
            delete [] m_quantCoef[sizeIdX][sizeIdY][listId][qp];
770
0
          }
771
2.52M
          if(m_dequantCoef[sizeIdX][sizeIdY][listId][qp])
772
0
          {
773
0
            delete [] m_dequantCoef[sizeIdX][sizeIdY][listId][qp];
774
0
          }
775
2.52M
        }
776
421k
      }
777
70.1k
    }
778
10.0k
  }
779
1.43k
}
780
781
void Quant::quant(TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, TCoeff &uiAbsSum, const QpParam& cQP, const Ctx& ctx)
782
6.51k
{
783
6.51k
  const SPS &sps            = *tu.cs->sps;
784
6.51k
  const CompArea& rect      = tu.blocks[compID];
785
6.51k
  const uint32_t uiWidth    = rect.width;
786
6.51k
  const uint32_t uiHeight   = rect.height;
787
6.51k
  const int channelBitDepth = sps.bitDepths[toChannelType(compID)];
788
789
6.51k
  const CCoeffBuf&  piCoef  = pSrc;
790
6.51k
        CoeffSigBuf piQCoef = tu.getCoeffs(compID);
791
792
6.51k
  const bool useTransformSkip = tu.mtsIdx[compID] == MTS_SKIP;
793
6.51k
  const int  maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange();
794
795
6.51k
  {
796
6.51k
    CoeffCodingContext cctx(tu, compID, tu.cs->slice->signDataHidingEnabled);
797
798
6.51k
    const TCoeff entropyCodingMinimum = -(1 << maxLog2TrDynamicRange);
799
6.51k
    const TCoeff entropyCodingMaximum =  (1 << maxLog2TrDynamicRange) - 1;
800
801
6.51k
    TCoeff deltaU[MAX_TB_SIZEY * MAX_TB_SIZEY];
802
6.51k
    int scalingListType           = getScalingListType(tu.cu->predMode, compID);
803
6.51k
    CHECK(scalingListType >= SCALING_LIST_NUM, "Invalid scaling list");
804
6.51k
    const uint32_t uiLog2TrWidth  = Log2(uiWidth);
805
6.51k
    const uint32_t uiLog2TrHeight = Log2(uiHeight);
806
6.51k
    int *piQuantCoeff             = getQuantCoeff(scalingListType, cQP.rem(useTransformSkip), uiLog2TrWidth, uiLog2TrHeight);
807
808
6.51k
    const bool isLfnstApplied     = tu.cu->lfnstIdx > 0 && (CU::isSepTree(*tu.cu) ? true : isLuma(compID));
809
6.51k
    const bool enableScalingLists = getUseScalingList(uiWidth, uiHeight, useTransformSkip, isLfnstApplied);
810
811
    // for blocks that where width*height != 4^N, the effective scaling applied during transformation cannot be
812
    // compensated by a bit-shift (the quantised result will be sqrt(2) * larger than required).
813
    // The quantScale table and shift is used to compensate for this.
814
6.51k
    const bool needSqrtAdjustment= TU::needsSqrt2Scale( tu, compID );
815
6.51k
    const int defaultQuantisationCoefficient    = g_quantScales[needSqrtAdjustment?1:0][cQP.rem(useTransformSkip)];
816
6.51k
    const int iTransformShift = getTransformShift(channelBitDepth, rect.size(), maxLog2TrDynamicRange) + ( needSqrtAdjustment?-1:0);
817
818
6.51k
    const int iQBits = QUANT_SHIFT + cQP.per(useTransformSkip) + (useTransformSkip ? 0 : iTransformShift);
819
    // QBits will be OK for any internal bit depth as the reduction in transform shift is balanced by an increase in Qp_per due to QpBDOffset
820
821
6.51k
    const int64_t iAdd = int64_t(tu.cs->slice->isIRAP() ? 171 : 85) << int64_t(iQBits - 9);
822
6.51k
    const int qBits8 = iQBits - 8;
823
824
6.51k
    int lastScanPos = -1;
825
826
6.51k
    if (!enableScalingLists)
827
6.51k
      xQuant(tu,compID,piCoef,piQCoef,uiAbsSum,lastScanPos,deltaU,defaultQuantisationCoefficient,iQBits,iAdd,entropyCodingMinimum,entropyCodingMaximum,cctx.signHiding(),m_thrVal);
828
0
    else
829
0
    {
830
0
      const uint32_t lfnstIdx = tu.cu->lfnstIdx;
831
0
      const int maxNumberOfCoeffs = lfnstIdx > 0 ? ( ( ( uiWidth == 4 && uiHeight == 4 ) || ( uiWidth == 8 && uiHeight == 8 ) ) ? 8 : 16 ) : piQCoef.area();
832
833
0
      piQCoef.memset( 0 );
834
0
      for (int uiScanPos = 0; uiScanPos < maxNumberOfCoeffs; uiScanPos++ )
835
0
      {
836
0
        const int uiBlockPos  = cctx.blockPos( uiScanPos );
837
0
        const TCoeff iLevel   = piCoef.buf[uiBlockPos];
838
0
        const TCoeff iSign    = (iLevel < 0 ? -1: 1);
839
840
0
        const int64_t  tmpLevel = (int64_t)abs(iLevel) * (enableScalingLists ? piQuantCoeff[uiBlockPos] : defaultQuantisationCoefficient);
841
0
        const TCoeff quantisedMagnitude = TCoeff((tmpLevel + iAdd ) >> iQBits);
842
0
        deltaU[uiBlockPos] = (TCoeff)((tmpLevel - ((int64_t)quantisedMagnitude<<iQBits) )>> qBits8);
843
844
0
        uiAbsSum += quantisedMagnitude;
845
0
        const TCoeff quantisedCoefficient = quantisedMagnitude * iSign;
846
847
0
        piQCoef.buf[uiBlockPos] = Clip3<TCoeff>( entropyCodingMinimum, entropyCodingMaximum, quantisedCoefficient );
848
0
      } // for n
849
0
    }
850
6.51k
    if (tu.cu->bdpcmM[toChannelType(compID)])
851
6.51k
    {
852
6.51k
      CoeffSigBuf coeffs = tu.getCoeffs( compID );
853
6.51k
      xFwdResDPCM( coeffs.buf, coeffs.stride, uiWidth, uiHeight, tu.cu->bdpcmM[toChannelType( compID )] != 1 );
854
6.51k
    }
855
856
6.51k
    if( uiAbsSum )
857
3.90k
    {
858
7.81k
      for( int scanPos = lastScanPos; scanPos >= 0; scanPos-- )
859
7.81k
      {
860
7.81k
        unsigned blkPos = cctx.blockPos( scanPos );
861
7.81k
        if( piQCoef.buf[blkPos] )
862
3.90k
        {
863
3.90k
          lastScanPos = scanPos;
864
3.90k
          break;
865
3.90k
        }
866
7.81k
      }
867
868
3.90k
      if( cctx.signHiding() )
869
0
      {
870
0
        if( uiAbsSum >= 2 ) //this prevents TUs with only one coefficient of value 1 from being tested
871
0
        {
872
0
          xSignBitHidingHDQ( piQCoef.buf, piCoef.buf, deltaU, cctx, lastScanPos, maxLog2TrDynamicRange );
873
0
        }
874
0
      }
875
3.90k
    }
876
877
6.51k
    tu.lastPos[compID] = lastScanPos;
878
6.51k
  } //if RDOQ
879
  //return;
880
6.51k
}
881
882
bool Quant::xNeedRDOQ(TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, const QpParam& cQP)
883
0
{
884
0
  const SPS &sps            = *tu.cs->sps;
885
0
  const CompArea& rect      = tu.blocks[compID];
886
0
  const uint32_t uiWidth    = rect.width;
887
0
  const uint32_t uiHeight   = rect.height;
888
0
  const uint32_t efHeight   = std::min<unsigned>( uiHeight, JVET_C0024_ZERO_OUT_TH );
889
0
  const uint32_t efArea     = uiWidth * efHeight;
890
0
  const int channelBitDepth = sps.bitDepths[toChannelType(compID)];
891
0
  const CCoeffBuf piCoef    = pSrc;
892
893
0
  const bool useTransformSkip      = tu.mtsIdx[compID] == MTS_SKIP;
894
0
  const int  maxLog2TrDynamicRange = sps.getMaxLog2TrDynamicRange();
895
896
0
  const int scalingListType     = getScalingListType( tu.cu->predMode, compID );
897
0
  CHECK( scalingListType >= SCALING_LIST_NUM, "Invalid scaling list" );
898
899
0
  const bool        isDq        = tu.cs->slice->depQuantEnabled && !useTransformSkip;
900
0
  const int         qpDQ        = isDq ? cQP.Qp( false ) + 1 : cQP.Qp( useTransformSkip );
901
0
  const int         qpPer       = isDq ? qpDQ / 6 : cQP.per( useTransformSkip );
902
0
  const int         qpRem       = isDq ? qpDQ - 6 * qpPer : cQP.rem( useTransformSkip );
903
904
0
  const uint32_t uiLog2TrWidth  = Log2( uiWidth );
905
0
  const uint32_t uiLog2TrHeight = Log2( uiHeight );
906
0
  int *piQuantCoeff             = getQuantCoeff( scalingListType, qpRem, uiLog2TrWidth, uiLog2TrHeight );
907
908
0
  const bool isLfnstApplied     = tu.cu->lfnstIdx > 0 && ( CU::isSepTree( *tu.cu ) ? true : isLuma( compID ) );
909
0
  const bool enableScalingLists = getUseScalingList( uiWidth, uiHeight, ( useTransformSkip != 0 ), isLfnstApplied );
910
911
0
  const bool needSqrtAdjustment = TU::needsSqrt2Scale( tu, compID );
912
0
  const int defaultQuantisationCoefficient
913
0
                                = g_quantScales[needSqrtAdjustment?1:0][qpRem];
914
0
  const int iTransformShift     = getTransformShift( channelBitDepth, rect.size(), maxLog2TrDynamicRange ) + ( needSqrtAdjustment ? -1 : 0 );
915
916
917
0
  const int iQBits              = QUANT_SHIFT + qpPer + iTransformShift;
918
919
  // QBits will be OK for any internal bit depth as the reduction in transform shift is balanced by an increase in Qp_per due to QpBDOffset
920
  // iAdd is different from the iAdd used in normal quantization
921
0
  const int64_t iAdd = int64_t( compID == COMP_Y ? 171 : 256 ) << ( iQBits - 9 );
922
923
0
  if( !enableScalingLists )
924
0
    return xNeedRdoq( piCoef.buf, efArea, defaultQuantisationCoefficient, iAdd, iQBits );
925
926
0
  for( int uiBlockPos = 0; uiBlockPos < efArea; uiBlockPos++ )
927
0
  {
928
0
    const TCoeff   iLevel           = piCoef.buf[uiBlockPos];
929
0
    const int64_t  tmpLevel         = ( int64_t ) std::abs( iLevel ) * piQuantCoeff[uiBlockPos];
930
0
    const TCoeff quantisedMagnitude = TCoeff( ( tmpLevel + iAdd ) >> iQBits );
931
932
0
    if( quantisedMagnitude != 0 )
933
0
    {
934
0
      return true;
935
0
    }
936
0
  } // for n
937
0
  return false;
938
0
}
939
940
} // namespace vvenc
941
942
//! \}
943