Coverage Report

Created: 2026-06-10 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/EncoderLib/EncPicture.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     EncPicture.cpp
45
    \brief    encode picture
46
*/
47
48
#include "EncPicture.h"
49
#include "EncGOP.h"
50
#include "CommonLib/UnitTools.h"
51
#include "CommonLib/CommonDef.h"
52
#include "CommonLib/dtrace_buffer.h"
53
#include "CommonLib/dtrace_codingstruct.h"
54
55
//! \ingroup EncoderLib
56
//! \{
57
58
namespace vvenc {
59
60
#ifdef TRACE_ENABLE_ITT
61
static __itt_string_handle* itt_handle_start = __itt_string_handle_create( "PicEnc" );
62
static __itt_domain* itt_domain_picEncoder   = __itt_domain_create( "PictureEncoder" );
63
#endif
64
65
// ---------------------------------------------------------------------------------------------------------------------
66
67
void EncPicture::init( const VVEncCfg& encCfg,
68
                       std::vector<int>* const globalCtuQpVector,
69
                       const SPS& sps,
70
                       const PPS& pps,
71
                       RateCtrl& rateCtrl,
72
                       NoMallocThreadPool* threadPool )
73
4.44k
{
74
4.44k
  m_pcEncCfg = &encCfg;
75
76
4.44k
  if( encCfg.m_alf || encCfg.m_ccalf )
77
4.44k
    m_ALF       .init( encCfg, pps, m_CABACEstimator, m_CtxCache, threadPool );
78
79
4.44k
  m_SliceEncoder.init( encCfg, sps, pps, globalCtuQpVector, m_LoopFilter, m_ALF, rateCtrl, threadPool, &m_ctuTasksDoneCounter );
80
4.44k
  m_pcRateCtrl = &rateCtrl;
81
4.44k
}
82
83
84
void EncPicture::compressPicture( Picture& pic, EncGOP& gopEncoder )
85
1.11k
{
86
1.11k
  PROFILER_SCOPE_TOP_LEVEL_EXT( 1, g_timeProfiler, P_TOP_LEVEL, pic.cs );
87
1.11k
  ITT_TASKSTART( itt_domain_picEncoder, itt_handle_start );
88
89
1.11k
  pic.encTime.startTimer();
90
91
1.11k
  pic.createTempBuffers( pic.cs->pcv->maxCUSize );
92
1.11k
  pic.cs->createCoeffs();
93
1.11k
  pic.cs->createTempBuffers( true );
94
1.11k
  pic.cs->initStructData( MAX_INT, false, nullptr );
95
96
1.11k
  if( pic.useLMCS && m_pcEncCfg->m_reshapeSignalType == RESHAPE_SIGNAL_PQ && m_pcEncCfg->m_alf )
97
0
  {
98
0
    const double *weights = gopEncoder.getReshaper().getlumaLevelToWeightPLUT();
99
0
    auto& vec = m_ALF.getLumaLevelWeightTable();
100
0
    const size_t numEl = size_t( 1 ) << m_pcEncCfg->m_internalBitDepth[0];
101
102
0
    vec.resize( numEl );
103
0
    std::copy( weights, weights + numEl, vec.begin() );
104
105
0
    m_ALF.setAlfWSSD( 1 );
106
0
  }
107
1.11k
  else
108
1.11k
  {
109
1.11k
    m_ALF.setAlfWSSD( 0 );
110
1.11k
  }
111
112
  // compress picture
113
1.11k
  xInitPicEncoder( pic );
114
115
  // compress current slice
116
1.11k
  pic.cs->slice = pic.slices[0];
117
1.11k
  std::fill( pic.ctuSlice.begin(), pic.ctuSlice.end(), pic.slices[0] );
118
1.11k
  m_SliceEncoder.compressSlice( &pic );
119
120
1.11k
  ITT_TASKEND( itt_domain_picEncoder, itt_handle_start );
121
1.11k
}
122
123
void EncPicture::finalizePicture( Picture& pic )
124
1.11k
{
125
1.11k
  PROFILER_SCOPE_TOP_LEVEL_EXT( 1, g_timeProfiler, P_TOP_LEVEL, pic.cs );
126
1.11k
  CodingStructure& cs = *(pic.cs);
127
1.11k
  Slice* slice        = pic.slices[0];
128
  // ALF
129
1.11k
  if( slice->sps->alfEnabled )
130
1.11k
  {
131
#ifdef TRACE_ENABLE_ITT
132
    std::stringstream ss;
133
    ss << "ALF_post_" << slice->poc;
134
    __itt_string_handle* itt_handle_post = __itt_string_handle_create( ss.str().c_str() );
135
#endif
136
1.11k
    pic.picApsMap.setApsIdStart( m_ALF.getApsIdStart() );
137
138
1.11k
    cs.slice->ccAlfFilterParam      = m_ALF.getCcAlfFilterParam();
139
1.11k
    cs.slice->ccAlfFilterControl[0] = m_ALF.getCcAlfControlIdc(COMP_Cb);
140
1.11k
    cs.slice->ccAlfFilterControl[1] = m_ALF.getCcAlfControlIdc(COMP_Cr);
141
142
1.11k
    DTRACE( g_trace_ctx, D_CRC, "ALF" );
143
1.11k
    DTRACE_CRC( g_trace_ctx, D_CRC, cs, cs.getRecoBuf() );
144
1.11k
    DTRACE_PIC_COMP( D_REC_CB_LUMA_ALF,   cs, cs.getRecoBuf(), COMP_Y  );
145
1.11k
    DTRACE_PIC_COMP( D_REC_CB_CHROMA_ALF, cs, cs.getRecoBuf(), COMP_Cb );
146
1.11k
    DTRACE_PIC_COMP( D_REC_CB_CHROMA_ALF, cs, cs.getRecoBuf(), COMP_Cr );
147
1.11k
  }
148
149
  // write picture
150
1.11k
  DTRACE_UPDATE( g_trace_ctx, std::make_pair( "bsfinal", 1 ) );
151
1.11k
  xWriteSliceData( pic );
152
1.11k
  DTRACE_UPDATE( g_trace_ctx, std::make_pair( "bsfinal", 0 ) );
153
154
1.11k
  xCalcDistortion( pic, *slice->sps );
155
156
  // finalize
157
1.11k
  if ( m_pcEncCfg->m_useAMaxBT )
158
0
  {
159
0
    pic.picBlkStat.storeBlkSize( pic );
160
0
  }
161
162
  // copy ALF APSs to global list
163
1.11k
  CHECK( m_pcEncCfg->m_alf && m_pcEncCfg->m_alfTempPred && !pic.picApsGlobal, "Missing APS from top level" );
164
1.11k
  if( pic.picApsGlobal )
165
1.11k
  {
166
1.11k
    CHECK( pic.picApsGlobal->poc != pic.poc, "Global APS POC must be consistent with picture poc" );
167
1.11k
    const ParameterSetMap<APS>& src = pic.picApsMap;
168
1.11k
    ParameterSetMap<APS>&       dst = pic.picApsGlobal->apsMap;
169
9.99k
    for( int i = 0; i < ALF_CTB_MAX_NUM_APS; i++ )
170
8.88k
    {
171
8.88k
      const int apsMapIdx = ( i << NUM_APS_TYPE_LEN ) + ALF_APS;
172
8.88k
      const APS* srcAPS = src.getPS( apsMapIdx );
173
8.88k
      if( srcAPS )
174
0
      {
175
0
        APS* dstAPS = dst.allocatePS( apsMapIdx );
176
0
        *dstAPS = *srcAPS;
177
0
      }
178
8.88k
    }
179
1.11k
    dst.setApsIdStart( src.getApsIdStart() );
180
1.11k
  }
181
182
  // cleanup
183
1.11k
  pic.cs->releaseIntermediateData();
184
1.11k
  pic.cs->destroyTempBuffers();
185
1.11k
  pic.cs->destroyCoeffs();
186
1.11k
  pic.destroyTempBuffers();
187
188
1.11k
  pic.encTime.stopTimer();
189
1.11k
}
190
191
uint64_t findDistortionPlane( const CPelBuf& pic0, const CPelBuf& pic1, uint32_t rshift )
192
3.33k
{
193
3.33k
  uint64_t uiTotalDiff;
194
3.33k
  const  Pel*  pSrc0 = pic0.bufAt(0, 0);
195
3.33k
  const  Pel*  pSrc1 = pic1.bufAt(0, 0);
196
197
3.33k
  CHECK(pic0.width  != pic1.width , "Unspecified error");
198
3.33k
  CHECK(pic0.height != pic1.height, "Unspecified error");
199
200
3.33k
  if( rshift > 0 )
201
0
  {
202
0
    uiTotalDiff = 0;
203
0
    for (int y = 0; y < pic0.height; y++)
204
0
    {
205
0
      for (int x = 0; x < pic0.width; x++)
206
0
      {
207
0
        Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
208
0
        uiTotalDiff += uint64_t((iTemp * iTemp) >> rshift);
209
0
      }
210
0
      pSrc0 += pic0.stride;
211
0
      pSrc1 += pic1.stride;
212
0
    }
213
0
  }
214
3.33k
  else
215
3.33k
  {
216
3.33k
    uiTotalDiff = 0;
217
338k
    for (int y = 0; y < pic0.height; y++)
218
335k
    {
219
40.3M
      for (int x = 0; x < pic0.width; x++)
220
40.0M
      {
221
40.0M
        Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
222
40.0M
        uiTotalDiff += uint64_t(iTemp * iTemp);
223
40.0M
      }
224
335k
      pSrc0 += pic0.stride;
225
335k
      pSrc1 += pic1.stride;
226
335k
    }
227
3.33k
  }
228
229
3.33k
  return uiTotalDiff;
230
3.33k
}
231
232
void EncPicture::xCalcDistortion( Picture& pic, const SPS& sps )
233
1.11k
{
234
1.11k
  const CPelUnitBuf cPicD = pic.getRecoBuf();
235
1.11k
  const CPelUnitBuf org   = pic.getOrigBuf();
236
1.11k
  const ChromaFormat formatD = cPicD.chromaFormat;
237
1.11k
  const ChromaFormat format  = sps.chromaFormatIdc;
238
239
4.44k
  for (int comp = 0; comp < getNumberValidComponents(formatD); comp++)
240
3.33k
  {
241
3.33k
    const ComponentID compID = ComponentID(comp);
242
3.33k
    const CPelBuf&    p = cPicD.get(compID);
243
3.33k
    const CPelBuf&    o = org.get(compID);
244
245
3.33k
    CHECK(!( p.width  == o.width), "Unspecified error");
246
3.33k
    CHECK(!( p.height == o.height), "Unspecified error");
247
248
3.33k
    const uint32_t   width  = p.width  - (m_pcEncCfg->m_aiPad[ 0 ] >> getComponentScaleX(compID, format));
249
3.33k
    const uint32_t   height = p.height - (m_pcEncCfg->m_aiPad[ 1 ] >> getComponentScaleY(compID, format));
250
251
    // create new buffers with correct dimensions
252
3.33k
    const CPelBuf recPB(p.bufAt(0, 0), p.stride, width, height);
253
3.33k
    const CPelBuf orgPB(o.bufAt(0, 0), o.stride, width, height);
254
3.33k
    const uint32_t    bitDepth = sps.bitDepths[toChannelType(compID)];
255
    //pic.ssd[comp] = findDistortionPlane(recPB, orgPB, 0);
256
3.33k
    const uint64_t uiSSDtemp = findDistortionPlane(recPB, orgPB, 0);
257
3.33k
    const uint32_t maxval = 255 << (bitDepth - 8);
258
3.33k
    const uint32_t size   = width * height;
259
3.33k
    const double fRefValue = (double)maxval * maxval * size;
260
3.33k
    pic.psnr[comp] = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : MAX_DOUBLE;
261
3.33k
    pic.mse [comp] = (double)uiSSDtemp / size;
262
3.33k
  }
263
1.11k
}
264
265
void EncPicture::xInitPicEncoder( Picture& pic )
266
1.11k
{
267
1.11k
  Slice* slice = pic.cs->slice;
268
269
1.11k
  CHECK( slice != pic.slices[0], "Slice pointers don't match!" );
270
271
1.11k
  m_SliceEncoder.initPic( &pic );
272
273
1.11k
  xInitSliceColFromL0Flag( slice );
274
1.11k
  xInitSliceCheckLDC     ( slice );
275
276
1.11k
  if( slice->sps->alfEnabled )
277
1.11k
  {
278
2.22k
    for (int s = 0; s < (int)pic.slices.size(); s++)
279
1.11k
    {
280
1.11k
      pic.slices[s]->alfEnabled[COMP_Y] = false;
281
1.11k
    }
282
1.11k
  }
283
1.11k
}
284
285
286
void EncPicture::xInitSliceColFromL0Flag( Slice* slice ) const
287
1.11k
{
288
1.11k
  if( m_pcEncCfg->m_rprRASLtoolSwitch )
289
0
  {
290
0
    return;
291
0
  }
292
293
1.11k
  if ( slice->sliceType == VVENC_B_SLICE )
294
0
  {
295
0
    const int refIdx = 0; // Zero always assumed
296
0
    const Picture* refPicL0 = slice->getRefPic( REF_PIC_LIST_0, refIdx );
297
0
    const Picture* refPicL1 = slice->getRefPic( REF_PIC_LIST_1, refIdx );
298
0
    slice->colFromL0Flag = ( refPicL0->slices[ 0 ]->sliceQp > refPicL1->slices[ 0 ]->sliceQp );
299
0
  }
300
1.11k
}
301
302
303
void EncPicture::xInitSliceCheckLDC( Slice* slice ) const
304
1.11k
{
305
1.11k
  if ( slice->sliceType == VVENC_B_SLICE )
306
0
  {
307
0
    bool bLowDelay = true;
308
0
    int  iCurrPOC  = slice->poc;
309
0
    int  iRefIdx   = 0;
310
311
0
    for ( iRefIdx = 0; iRefIdx < slice->numRefIdx[ REF_PIC_LIST_0 ] && bLowDelay; iRefIdx++ )
312
0
    {
313
0
      if ( slice->getRefPic( REF_PIC_LIST_0, iRefIdx )->getPOC() > iCurrPOC )
314
0
      {
315
0
        bLowDelay = false;
316
0
      }
317
0
    }
318
0
    for ( iRefIdx = 0; iRefIdx < slice->numRefIdx[ REF_PIC_LIST_1 ] && bLowDelay; iRefIdx++ )
319
0
    {
320
0
      if ( slice->getRefPic( REF_PIC_LIST_1, iRefIdx )->getPOC() > iCurrPOC )
321
0
      {
322
0
        bLowDelay = false;
323
0
      }
324
0
    }
325
326
0
    slice->checkLDC = bLowDelay;
327
0
  }
328
1.11k
  else
329
1.11k
  {
330
1.11k
    slice->checkLDC = true;
331
1.11k
  }
332
1.11k
}
333
334
void EncPicture::xWriteSliceData( Picture& pic )
335
1.11k
{
336
1.11k
  const int numSlices = (int)pic.slices.size();
337
338
1.11k
  pic.sliceDataStreams.clear();
339
1.11k
  pic.sliceDataStreams.resize( numSlices );
340
1.11k
  pic.sliceDataNumBins = 0;
341
342
2.22k
  for ( int i = 0; i < numSlices; i++ )
343
1.11k
  {
344
    // set current slice
345
1.11k
    pic.cs->slice = pic.slices[i];
346
1.11k
    m_SliceEncoder.encodeSliceData( &pic );
347
1.11k
  }
348
1.11k
}
349
350
} // namespace vvenc
351
352
//! \}
353