Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvdec/source/Lib/CommonLib/Reshape.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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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
/** \file     Reshape.cpp
44
    \brief    common reshaper class
45
*/
46
#include "Reshape.h"
47
#include <stdio.h>
48
#include <string.h>
49
#include <math.h>
50
#include <UnitTools.h>
51
#include "CommonLib/TimeProfiler.h"
52
53
namespace vvdec
54
{
55
56
// ====================================================================================================================
57
// Constructor / destructor / create / destroy
58
// ====================================================================================================================
59
60
Reshape::Reshape()
61
37.8k
{
62
37.8k
  m_invLUT = nullptr;
63
37.8k
  m_lumaBD=0;
64
37.8k
  m_chromaScale = 1 << CSCALE_FP_PREC;
65
37.8k
  m_vpduX = -1;
66
37.8k
  m_vpduY = -1;
67
37.8k
}
68
69
Reshape::~Reshape()
70
37.8k
{
71
37.8k
  destroy();
72
37.8k
}
73
74
void  Reshape::createDec(int bitDepth)
75
0
{
76
  //sanity check
77
0
  if (m_lumaBD != bitDepth)
78
0
  {
79
0
    if (m_invLUT )
80
0
    {
81
0
      xFree( m_invLUT );
82
0
      m_invLUT = nullptr;
83
0
    }
84
0
  }
85
0
  m_lumaBD = bitDepth;
86
0
  m_reshapeLUTSize = 1 << m_lumaBD;
87
0
  m_initCW = m_reshapeLUTSize / PIC_CODE_CW_BINS;
88
0
  if( !m_invLUT )
89
0
  {
90
0
    m_invLUT = ( Pel* ) xMalloc( Pel, m_reshapeLUTSize + 1 );
91
0
    memset( m_invLUT, 0, ( m_reshapeLUTSize + 1 ) * sizeof( Pel ) );
92
0
  }
93
0
  if (m_binCW.empty())
94
0
    m_binCW.resize(PIC_CODE_CW_BINS, 0);
95
0
  if (m_inputPivot.empty())
96
0
    m_inputPivot.resize(PIC_CODE_CW_BINS + 1, 0);
97
0
  if (m_fwdScaleCoef.empty())
98
0
    m_fwdScaleCoef.resize(PIC_CODE_CW_BINS, 1 << FP_PREC);
99
0
  if (m_invScaleCoef.empty())
100
0
    m_invScaleCoef.resize(PIC_CODE_CW_BINS, 1 << FP_PREC);
101
0
  if (m_reshapePivot.empty())
102
0
    m_reshapePivot.resize(PIC_CODE_CW_BINS + 1, 0);
103
0
  if (m_chromaAdjHelpLUT.empty())
104
0
    m_chromaAdjHelpLUT.resize(PIC_CODE_CW_BINS, 1<<CSCALE_FP_PREC);
105
0
}
106
107
void  Reshape::destroy()
108
37.8k
{
109
37.8k
  xFree( m_invLUT );
110
37.8k
  m_invLUT = nullptr;
111
37.8k
}
112
113
void  Reshape::initSlice( int nalUnitLayerId, const PicHeader& picHeader, const VPS* vps )
114
0
{
115
0
  if( picHeader.getLmcsEnabledFlag() )
116
0
  {
117
0
    if( nalUnitLayerId != picHeader.getLmcsAPS()->getLayerId() )
118
0
    {
119
0
      for (int i = 0; vps && i < vps->getNumOutputLayerSets(); i++ )
120
0
      {
121
0
        bool isCurrLayerInOls = false;
122
0
        bool isRefLayerInOls = false;
123
0
        for( int j = vps->getNumLayersInOls(i) - 1; j >= 0; j-- )
124
0
        {
125
0
          if( vps->getLayerIdInOls(i, j) == nalUnitLayerId )
126
0
          {
127
0
            isCurrLayerInOls = true;
128
0
          }
129
0
          if( vps->getLayerIdInOls(i, j) == picHeader.getLmcsAPS()->getLayerId() )
130
0
          {
131
0
            isRefLayerInOls = true;
132
0
          }
133
0
        }
134
0
        CHECK( isCurrLayerInOls && !isRefLayerInOls, "When VCL NAl unit in layer A refers to APS in layer B, all OLS that contains layer A shall also contains layer B" );
135
0
      }
136
0
    }
137
138
0
    const SliceReshapeInfo& sInfo = picHeader.getLmcsAPS()->getReshaperAPSInfo();
139
0
    m_sliceReshapeInfo.sliceReshaperEnableFlag       = true;
140
0
    m_sliceReshapeInfo.sliceReshaperModelPresentFlag = true;
141
0
    m_sliceReshapeInfo.enableChromaAdj               = picHeader.getLmcsChromaResidualScaleFlag();
142
0
    m_sliceReshapeInfo.reshaperModelMaxBinIdx        = sInfo.reshaperModelMaxBinIdx;
143
0
    m_sliceReshapeInfo.reshaperModelMinBinIdx        = sInfo.reshaperModelMinBinIdx;
144
0
    m_sliceReshapeInfo.maxNbitsNeededDeltaCW         = sInfo.maxNbitsNeededDeltaCW;
145
0
    m_sliceReshapeInfo.chrResScalingOffset           = sInfo.chrResScalingOffset;
146
0
    memcpy( m_sliceReshapeInfo.reshaperModelBinCWDelta, sInfo.reshaperModelBinCWDelta, sizeof( int ) * ( PIC_CODE_CW_BINS ) );
147
0
    constructReshaper();
148
0
  }
149
0
  else
150
0
  {
151
0
    m_sliceReshapeInfo.sliceReshaperEnableFlag       = false;
152
0
    m_sliceReshapeInfo.enableChromaAdj               = false;
153
0
    m_sliceReshapeInfo.sliceReshaperModelPresentFlag = false;
154
0
  }
155
0
  m_vpduX = -1;
156
0
  m_vpduY = -1;
157
0
}
158
159
bool Reshape::getCTUFlag( const Slice& slice ) const
160
0
{
161
0
  if( (slice.getSliceType() == I_SLICE) && m_sliceReshapeInfo.sliceReshaperEnableFlag )
162
0
  {
163
0
    return false;
164
0
  }
165
0
  else
166
0
  {
167
0
    return m_sliceReshapeInfo.sliceReshaperEnableFlag;
168
0
  }
169
0
}
170
171
172
173
/** compute chroma residuce scale for TU
174
* \param average luma pred of TU
175
* \return chroma residue scale
176
*/
177
int  Reshape::calculateChromaAdj(Pel avgLuma) const
178
0
{
179
0
  int iAdj = m_chromaAdjHelpLUT[getPWLIdxInv(avgLuma)];
180
0
  return(iAdj);
181
0
}
182
183
/** compute chroma residuce scale for TU
184
* \param average luma pred of TU
185
* \return chroma residue scale
186
*/
187
int  Reshape::calculateChromaAdjVpduNei(TransformUnit &tu, const Position pos)
188
0
{
189
0
  CodingStructure &cs = *tu.cu->cs;
190
0
  int xPos = pos.x;
191
0
  int yPos = pos.y;
192
0
  int ctuSize = cs.sps->getCTUSize();
193
0
  int numNeighbor = std::min(64, ctuSize);
194
0
  int numNeighborLog = getLog2(numNeighbor);
195
0
  if (ctuSize == 128)
196
0
  {
197
0
    xPos &= ~63;
198
0
    yPos &= ~63;
199
0
  }
200
0
  else
201
0
  {
202
0
    xPos &= ~( ctuSize - 1 );
203
0
    yPos &= ~( ctuSize - 1 );
204
0
  }
205
206
0
  if( isVPDUprocessed( xPos, yPos ) )
207
0
  {
208
0
    return getChromaScale();
209
0
  }
210
0
  else
211
0
  {
212
0
    setVPDULoc(xPos, yPos);
213
0
    Position topLeft(xPos, yPos);
214
0
    CodingUnit *topLeftLuma;
215
0
    const CodingUnit *cuAbove, *cuLeft;
216
217
0
    topLeftLuma = cs.getCU( topLeft, CHANNEL_TYPE_LUMA );
218
0
    cuAbove     = cs.getCURestricted( topLeftLuma->lumaPos().offset( 0, -1 ), *topLeftLuma, CHANNEL_TYPE_LUMA, topLeftLuma->ly() == yPos ? topLeftLuma : topLeftLuma->above );
219
0
    cuLeft      = cs.getCURestricted( topLeftLuma->lumaPos().offset( -1, 0 ), *topLeftLuma, CHANNEL_TYPE_LUMA, topLeftLuma->lx() == xPos ? topLeftLuma : topLeftLuma->left  );
220
221
0
    xPos = topLeftLuma->lumaPos().x;
222
0
    yPos = topLeftLuma->lumaPos().y;
223
224
0
    CompArea lumaArea = CompArea(COMPONENT_Y, topLeftLuma->lumaPos(), topLeftLuma->lumaSize());
225
0
    PelBuf piRecoY = cs.picture->getRecoBuf(lumaArea);
226
0
    ptrdiff_t strideY = piRecoY.stride;
227
0
    int chromaScale = (1 << CSCALE_FP_PREC);
228
0
    int lumaValue = -1;
229
230
0
    Pel* recSrc0 = piRecoY.bufAt(0, 0);
231
0
    const uint32_t picH = tu.cu->cs->picture->lheight();
232
0
    const uint32_t picW = tu.cu->cs->picture->lwidth();
233
0
    const Pel   valueDC = 1 << (tu.cu->sps->getBitDepth() - 1);
234
0
    int32_t recLuma = 0;
235
0
    int pelnum = 0;
236
0
    if (cuLeft != nullptr)
237
0
    {
238
0
      for (int i = 0; i < numNeighbor; i++)
239
0
      {
240
0
        int k = (yPos + i) >= picH ? (picH - yPos - 1) : i;
241
0
        recLuma += recSrc0[-1 + k * strideY];
242
0
        pelnum++;
243
0
      }
244
0
    }
245
0
    if (cuAbove != nullptr)
246
0
    {
247
0
      for (int i = 0; i < numNeighbor; i++)
248
0
      {
249
0
        int k = (xPos + i) >= picW ? (picW - xPos - 1) : i;
250
0
        recLuma += recSrc0[-strideY + k];
251
0
        pelnum++;
252
0
      }
253
0
    }
254
0
    if (pelnum == numNeighbor)
255
0
    {
256
0
      lumaValue = (recLuma + (1 << (numNeighborLog - 1))) >> numNeighborLog;
257
0
    }
258
0
    else if (pelnum == (numNeighbor << 1))
259
0
    {
260
0
      lumaValue = (recLuma + (1 << numNeighborLog)) >> (numNeighborLog + 1);
261
0
    }
262
0
    else
263
0
    {
264
0
      CHECK(pelnum != 0, "");
265
0
      lumaValue = valueDC;
266
0
    }
267
0
    chromaScale = calculateChromaAdj(lumaValue);
268
0
    setChromaScale(chromaScale);
269
0
    return(chromaScale);
270
0
  }
271
0
}
272
/** find inx of PWL for inverse mapping
273
* \param average luma pred of TU
274
* \return idx of PWL for inverse mapping
275
*/
276
int Reshape::getPWLIdxInv(int lumaVal) const
277
0
{
278
0
  int idxS = 0;
279
0
  for (idxS = m_sliceReshapeInfo.reshaperModelMinBinIdx; (idxS <= m_sliceReshapeInfo.reshaperModelMaxBinIdx); idxS++)
280
0
  {
281
0
    if (lumaVal < m_reshapePivot[idxS + 1])     break;
282
0
  }
283
0
  return std::min(idxS, PIC_CODE_CW_BINS-1);
284
0
}
285
286
/**
287
-copy Slice reshaper info structure
288
\param   tInfo describing the target Slice reshaper info structure
289
\param   sInfo describing the source Slice reshaper info structure
290
*/
291
void Reshape::copySliceReshaperInfo(SliceReshapeInfo& tInfo, SliceReshapeInfo& sInfo)
292
0
{
293
0
  tInfo.sliceReshaperModelPresentFlag = sInfo.sliceReshaperModelPresentFlag;
294
0
  if (sInfo.sliceReshaperModelPresentFlag)
295
0
  {
296
0
    tInfo.reshaperModelMaxBinIdx = sInfo.reshaperModelMaxBinIdx;
297
0
    tInfo.reshaperModelMinBinIdx = sInfo.reshaperModelMinBinIdx;
298
0
    memcpy(tInfo.reshaperModelBinCWDelta, sInfo.reshaperModelBinCWDelta, sizeof(int)*(PIC_CODE_CW_BINS));
299
0
    tInfo.maxNbitsNeededDeltaCW = sInfo.maxNbitsNeededDeltaCW;
300
0
    tInfo.chrResScalingOffset = sInfo.chrResScalingOffset;
301
0
  }
302
0
  tInfo.sliceReshaperEnableFlag = sInfo.sliceReshaperEnableFlag;
303
0
  if (sInfo.sliceReshaperEnableFlag)
304
0
    tInfo.enableChromaAdj = sInfo.enableChromaAdj;
305
0
  else
306
0
    tInfo.enableChromaAdj = 0;
307
0
}
308
309
/** Construct reshaper from syntax
310
* \param void
311
* \return void
312
*/
313
void Reshape::constructReshaper()
314
0
{
315
0
  int pwlFwdLUTsize = PIC_CODE_CW_BINS;
316
0
  int pwlFwdBinLen = m_reshapeLUTSize / PIC_CODE_CW_BINS;
317
318
0
  for (int i = 0; i < m_sliceReshapeInfo.reshaperModelMinBinIdx; i++)
319
0
    m_binCW[i] = 0;
320
0
  for (int i = m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1; i < PIC_CODE_CW_BINS; i++)
321
0
    m_binCW[i] = 0;
322
0
  for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
323
0
    m_binCW[i] = (uint16_t)(m_sliceReshapeInfo.reshaperModelBinCWDelta[i] + (int)m_initCW);
324
325
0
  unsigned int sumCW = 0;
326
0
  for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
327
0
  {
328
0
    CHECK_READ_RANGE( m_binCW[i], m_initCW >> 3, ( m_initCW << 3 ) - 1, //
329
0
                      "The value of lmcsCW[ i ] shall be in the range of OrgCW >> 3 to ( OrgCW << 3 ) - 1, inclusive." );
330
0
    CHECK_READ_RANGE( m_binCW[i] + m_sliceReshapeInfo.chrResScalingOffset, ( m_initCW >> 3 ), ( m_initCW << 3 ) - 1,
331
0
                      "lmcsCW[ i ] + lmcsDeltaCrs shall be in the range of OrgCW >> 3 to ( OrgCW << 3 ) - 1, inclusive." );
332
0
    sumCW += m_binCW[i];
333
0
  }
334
0
  CHECK( sumCW > ( 1 << m_lumaBD ) - 1,
335
0
         "It is a requirement of bitstream conformance that the following condition is true: sum( lmcsCW[0..15] ) <= ( 1 << BitDepth ) − 1" );
336
337
0
  for (int i = 0; i < pwlFwdLUTsize; i++)
338
0
  {
339
0
    m_reshapePivot[i + 1] = m_reshapePivot[i] + m_binCW[i];
340
0
    m_inputPivot[i + 1] = m_inputPivot[i] + m_initCW;
341
0
    m_fwdScaleCoef[i] = ((int32_t)m_binCW[i] * (1 << FP_PREC) + (1 << (getLog2(pwlFwdBinLen) - 1))) >> getLog2(pwlFwdBinLen);
342
0
    if (m_binCW[i] == 0)
343
0
    {
344
0
      m_invScaleCoef[i] = 0;
345
0
      m_chromaAdjHelpLUT[i] = 1 << CSCALE_FP_PREC;
346
0
    }
347
0
    else
348
0
    {
349
0
      CHECK( m_initCW * (1 << FP_PREC) / m_binCW[i] > (1 << 15) - 1, "Inverse scale coeff doesn't fit in a short!" );
350
0
      m_invScaleCoef[i] = (int32_t)(m_initCW * (1 << FP_PREC) / m_binCW[i]);
351
0
      m_chromaAdjHelpLUT[i] = (int32_t)(m_initCW * (1 << FP_PREC) / ( m_binCW[i] + m_sliceReshapeInfo.chrResScalingOffset ) );
352
0
    }
353
0
  }
354
0
  for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++)
355
0
  {
356
0
    if( m_reshapePivot[i] % ( 1 << ( m_lumaBD - 5 ) ) != 0 )
357
0
    {
358
0
      CHECK( ( m_reshapePivot[i] >> ( m_lumaBD - 5 ) ) == ( m_reshapePivot[i + 1] >> ( m_lumaBD - 5 ) ),
359
0
             "When the value of LmcsPivot[i] is not a multiple of 1 << ( BitDepth - 5 ), "
360
0
             "the value of( LmcsPivot[i] >> ( BitDepth - 5 ) ) shall not be equal to the value of( LmcsPivot[i + 1] >> ( BitDepth - 5 ) )." );
361
0
    }
362
0
  }
363
364
0
  for (int lumaSample = 0; lumaSample < m_reshapeLUTSize; lumaSample++)
365
0
  {
366
0
    int idxYInv = getPWLIdxInv(lumaSample);
367
0
    int invSample = m_inputPivot[idxYInv] + ((m_invScaleCoef[idxYInv] * (lumaSample - m_reshapePivot[idxYInv]) + (1 << (FP_PREC - 1))) >> FP_PREC);
368
0
    m_invLUT[lumaSample] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)(invSample));
369
0
  }
370
0
}
371
372
void Reshape::rspCtuBcw( CodingStructure& cs, int col, int ln ) const
373
0
{
374
0
  if( !cs.sps->getUseReshaper() || !m_sliceReshapeInfo.sliceReshaperEnableFlag )
375
0
  {
376
0
    return;
377
0
  }
378
379
0
  const Slice* slice = cs.getCtuData( col, ln ).slice;
380
0
  if( !slice->getLmcsEnabledFlag() )
381
0
  {
382
0
    return;
383
0
  }
384
385
0
  PROFILER_SCOPE_AND_STAGE_EXT( 1, g_timeProfiler, P_RESHAPER, cs, CH_L );
386
387
0
  const PreCalcValues& pcv = *cs.pcv;
388
389
0
  int xPos = pcv.maxCUWidth * col;
390
0
  int lw = std::min( pcv.lumaWidth - xPos, pcv.maxCUWidth );
391
392
0
  int yPos = ln * pcv.maxCUHeight;
393
0
  int lh = std::min( pcv.lumaHeight - yPos, pcv.maxCUHeight );
394
395
0
  PelBuf picYuvRec = cs.getRecoBuf( COMPONENT_Y ).subBuf( Position( xPos, yPos ), Size( lw, lh ) );
396
397
0
#if 1
398
0
  if( g_pelBufOP.rspBcw )
399
0
    g_pelBufOP.rspBcw( picYuvRec.buf, picYuvRec.stride, picYuvRec.width, picYuvRec.height, m_lumaBD, m_sliceReshapeInfo.reshaperModelMinBinIdx, m_sliceReshapeInfo.reshaperModelMaxBinIdx, m_reshapePivot.data(), m_invScaleCoef.data(), m_inputPivot.data() );
400
0
  else
401
0
#endif
402
0
    g_pelBufOP.applyLut( picYuvRec.buf, picYuvRec.stride, picYuvRec.width, picYuvRec.height, m_invLUT );
403
0
}
404
405
void Reshape::rspBufFwd( PelBuf& buf ) const
406
0
{
407
0
  g_pelBufOP.rspFwd( buf.buf, buf.stride, buf.width, buf.height, m_lumaBD, m_initCW, m_reshapePivot.data(), m_fwdScaleCoef.data(), m_inputPivot.data() );
408
0
}
409
410
}