Coverage Report

Created: 2026-07-16 06:32

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.86k
{
74
4.86k
  m_pcEncCfg = &encCfg;
75
76
4.86k
  if( encCfg.m_alf || encCfg.m_ccalf )
77
4.86k
    m_ALF       .init( encCfg, pps, m_CABACEstimator, m_CtxCache, threadPool );
78
79
4.86k
  m_SliceEncoder.init( encCfg, sps, pps, globalCtuQpVector, m_LoopFilter, m_ALF, rateCtrl, threadPool, &m_ctuTasksDoneCounter );
80
4.86k
  m_pcRateCtrl = &rateCtrl;
81
4.86k
}
82
83
84
void EncPicture::compressPicture( Picture& pic, EncGOP& gopEncoder )
85
1.21k
{
86
1.21k
  PROFILER_SCOPE_TOP_LEVEL_EXT( 1, g_timeProfiler, P_TOP_LEVEL, pic.cs );
87
1.21k
  ITT_TASKSTART( itt_domain_picEncoder, itt_handle_start );
88
89
1.21k
  pic.encTime.startTimer();
90
91
1.21k
  pic.createTempBuffers( pic.cs->pcv->maxCUSize );
92
1.21k
  pic.cs->createCoeffs();
93
1.21k
  pic.cs->createTempBuffers( true );
94
1.21k
  pic.cs->initStructData( MAX_INT, false, nullptr );
95
96
1.21k
  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.21k
  else
108
1.21k
  {
109
1.21k
    m_ALF.setAlfWSSD( 0 );
110
1.21k
  }
111
112
  // compress picture
113
1.21k
  xInitPicEncoder( pic );
114
115
  // compress current slice
116
1.21k
  pic.cs->slice = pic.slices[0];
117
1.21k
  std::fill( pic.ctuSlice.begin(), pic.ctuSlice.end(), pic.slices[0] );
118
1.21k
  m_SliceEncoder.compressSlice( &pic );
119
120
1.21k
  ITT_TASKEND( itt_domain_picEncoder, itt_handle_start );
121
1.21k
}
122
123
void EncPicture::finalizePicture( Picture& pic )
124
1.21k
{
125
1.21k
  PROFILER_SCOPE_TOP_LEVEL_EXT( 1, g_timeProfiler, P_TOP_LEVEL, pic.cs );
126
1.21k
  CodingStructure& cs = *(pic.cs);
127
1.21k
  Slice* slice        = pic.slices[0];
128
  // ALF
129
1.21k
  if( slice->sps->alfEnabled )
130
1.21k
  {
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.21k
    pic.picApsMap.setApsIdStart( m_ALF.getApsIdStart() );
137
138
1.21k
    cs.slice->ccAlfFilterParam      = m_ALF.getCcAlfFilterParam();
139
1.21k
    cs.slice->ccAlfFilterControl[0] = m_ALF.getCcAlfControlIdc(COMP_Cb);
140
1.21k
    cs.slice->ccAlfFilterControl[1] = m_ALF.getCcAlfControlIdc(COMP_Cr);
141
142
1.21k
    DTRACE( g_trace_ctx, D_CRC, "ALF" );
143
1.21k
    DTRACE_CRC( g_trace_ctx, D_CRC, cs, cs.getRecoBuf() );
144
1.21k
    DTRACE_PIC_COMP( D_REC_CB_LUMA_ALF,   cs, cs.getRecoBuf(), COMP_Y  );
145
1.21k
    DTRACE_PIC_COMP( D_REC_CB_CHROMA_ALF, cs, cs.getRecoBuf(), COMP_Cb );
146
1.21k
    DTRACE_PIC_COMP( D_REC_CB_CHROMA_ALF, cs, cs.getRecoBuf(), COMP_Cr );
147
1.21k
  }
148
149
  // write picture
150
1.21k
  DTRACE_UPDATE( g_trace_ctx, std::make_pair( "bsfinal", 1 ) );
151
1.21k
  xWriteSliceData( pic );
152
1.21k
  DTRACE_UPDATE( g_trace_ctx, std::make_pair( "bsfinal", 0 ) );
153
154
1.21k
  xCalcDistortion( pic, *slice->sps );
155
156
  // finalize
157
1.21k
  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.21k
  CHECK( m_pcEncCfg->m_alf && m_pcEncCfg->m_alfTempPred && !pic.picApsGlobal, "Missing APS from top level" );
164
1.21k
  if( pic.picApsGlobal )
165
1.21k
  {
166
1.21k
    CHECK( pic.picApsGlobal->poc != pic.poc, "Global APS POC must be consistent with picture poc" );
167
1.21k
    const ParameterSetMap<APS>& src = pic.picApsMap;
168
1.21k
    ParameterSetMap<APS>&       dst = pic.picApsGlobal->apsMap;
169
10.9k
    for( int i = 0; i < ALF_CTB_MAX_NUM_APS; i++ )
170
9.72k
    {
171
9.72k
      const int apsMapIdx = ( i << NUM_APS_TYPE_LEN ) + ALF_APS;
172
9.72k
      const APS* srcAPS = src.getPS( apsMapIdx );
173
9.72k
      if( srcAPS )
174
0
      {
175
0
        APS* dstAPS = dst.allocatePS( apsMapIdx );
176
0
        *dstAPS = *srcAPS;
177
0
      }
178
9.72k
    }
179
1.21k
    dst.setApsIdStart( src.getApsIdStart() );
180
1.21k
  }
181
182
  // cleanup
183
1.21k
  pic.cs->releaseIntermediateData();
184
1.21k
  pic.cs->destroyTempBuffers();
185
1.21k
  pic.cs->destroyCoeffs();
186
1.21k
  pic.destroyTempBuffers();
187
188
1.21k
  pic.encTime.stopTimer();
189
1.21k
}
190
191
uint64_t findDistortionPlane( const CPelBuf& pic0, const CPelBuf& pic1, uint32_t rshift )
192
3.64k
{
193
3.64k
  uint64_t uiTotalDiff;
194
3.64k
  const  Pel*  pSrc0 = pic0.bufAt(0, 0);
195
3.64k
  const  Pel*  pSrc1 = pic1.bufAt(0, 0);
196
197
3.64k
  CHECK(pic0.width  != pic1.width , "Unspecified error");
198
3.64k
  CHECK(pic0.height != pic1.height, "Unspecified error");
199
200
3.64k
  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.64k
  else
215
3.64k
  {
216
3.64k
    uiTotalDiff = 0;
217
374k
    for (int y = 0; y < pic0.height; y++)
218
370k
    {
219
44.3M
      for (int x = 0; x < pic0.width; x++)
220
44.0M
      {
221
44.0M
        Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
222
44.0M
        uiTotalDiff += uint64_t(iTemp * iTemp);
223
44.0M
      }
224
370k
      pSrc0 += pic0.stride;
225
370k
      pSrc1 += pic1.stride;
226
370k
    }
227
3.64k
  }
228
229
3.64k
  return uiTotalDiff;
230
3.64k
}
231
232
void EncPicture::xCalcDistortion( Picture& pic, const SPS& sps )
233
1.21k
{
234
1.21k
  const CPelUnitBuf cPicD = pic.getRecoBuf();
235
1.21k
  const CPelUnitBuf org   = pic.getOrigBuf();
236
1.21k
  const ChromaFormat formatD = cPicD.chromaFormat;
237
1.21k
  const ChromaFormat format  = sps.chromaFormatIdc;
238
239
4.86k
  for (int comp = 0; comp < getNumberValidComponents(formatD); comp++)
240
3.64k
  {
241
3.64k
    const ComponentID compID = ComponentID(comp);
242
3.64k
    const CPelBuf&    p = cPicD.get(compID);
243
3.64k
    const CPelBuf&    o = org.get(compID);
244
245
3.64k
    CHECK(!( p.width  == o.width), "Unspecified error");
246
3.64k
    CHECK(!( p.height == o.height), "Unspecified error");
247
248
3.64k
    const uint32_t   width  = p.width  - (m_pcEncCfg->m_aiPad[ 0 ] >> getComponentScaleX(compID, format));
249
3.64k
    const uint32_t   height = p.height - (m_pcEncCfg->m_aiPad[ 1 ] >> getComponentScaleY(compID, format));
250
251
    // create new buffers with correct dimensions
252
3.64k
    const CPelBuf recPB(p.bufAt(0, 0), p.stride, width, height);
253
3.64k
    const CPelBuf orgPB(o.bufAt(0, 0), o.stride, width, height);
254
3.64k
    const uint32_t    bitDepth = sps.bitDepths[toChannelType(compID)];
255
    //pic.ssd[comp] = findDistortionPlane(recPB, orgPB, 0);
256
3.64k
    const uint64_t uiSSDtemp = findDistortionPlane(recPB, orgPB, 0);
257
3.64k
    const uint32_t maxval = 255 << (bitDepth - 8);
258
3.64k
    const uint32_t size   = width * height;
259
3.64k
    const double fRefValue = (double)maxval * maxval * size;
260
3.64k
    pic.psnr[comp] = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : MAX_DOUBLE;
261
3.64k
    pic.mse [comp] = (double)uiSSDtemp / size;
262
3.64k
  }
263
1.21k
}
264
265
void EncPicture::xInitPicEncoder( Picture& pic )
266
1.21k
{
267
1.21k
  Slice* slice = pic.cs->slice;
268
269
1.21k
  CHECK( slice != pic.slices[0], "Slice pointers don't match!" );
270
271
1.21k
  m_SliceEncoder.initPic( &pic );
272
273
1.21k
  xInitSliceColFromL0Flag( slice );
274
1.21k
  xInitSliceCheckLDC     ( slice );
275
276
1.21k
  if( slice->sps->alfEnabled )
277
1.21k
  {
278
2.43k
    for (int s = 0; s < (int)pic.slices.size(); s++)
279
1.21k
    {
280
1.21k
      pic.slices[s]->alfEnabled[COMP_Y] = false;
281
1.21k
    }
282
1.21k
  }
283
1.21k
}
284
285
286
void EncPicture::xInitSliceColFromL0Flag( Slice* slice ) const
287
1.21k
{
288
1.21k
  if( m_pcEncCfg->m_rprRASLtoolSwitch )
289
0
  {
290
0
    return;
291
0
  }
292
293
1.21k
  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.21k
}
301
302
303
void EncPicture::xInitSliceCheckLDC( Slice* slice ) const
304
1.21k
{
305
1.21k
  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.21k
  else
329
1.21k
  {
330
1.21k
    slice->checkLDC = true;
331
1.21k
  }
332
1.21k
}
333
334
void EncPicture::xWriteSliceData( Picture& pic )
335
1.21k
{
336
1.21k
  const int numSlices = (int)pic.slices.size();
337
338
1.21k
  pic.sliceDataStreams.clear();
339
1.21k
  pic.sliceDataStreams.resize( numSlices );
340
1.21k
  pic.sliceDataNumBins = 0;
341
342
2.43k
  for ( int i = 0; i < numSlices; i++ )
343
1.21k
  {
344
    // set current slice
345
1.21k
    pic.cs->slice = pic.slices[i];
346
1.21k
    m_SliceEncoder.encodeSliceData( &pic );
347
1.21k
  }
348
1.21k
}
349
350
} // namespace vvenc
351
352
//! \}
353