Coverage Report

Created: 2026-04-01 07:49

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