Coverage Report

Created: 2026-09-01 06:57

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