Coverage Report

Created: 2026-08-13 07:23

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.80k
{
74
4.80k
  m_pcEncCfg = &encCfg;
75
76
4.80k
  if( encCfg.m_alf || encCfg.m_ccalf )
77
4.80k
    m_ALF       .init( encCfg, pps, m_CABACEstimator, m_CtxCache, threadPool );
78
79
4.80k
  m_SliceEncoder.init( encCfg, sps, pps, globalCtuQpVector, m_LoopFilter, m_ALF, rateCtrl, threadPool, &m_ctuTasksDoneCounter );
80
4.80k
  m_pcRateCtrl = &rateCtrl;
81
4.80k
}
82
83
84
void EncPicture::compressPicture( Picture& pic, EncGOP& gopEncoder )
85
1.20k
{
86
1.20k
  PROFILER_SCOPE_TOP_LEVEL_EXT( 1, g_timeProfiler, P_TOP_LEVEL, pic.cs );
87
1.20k
  ITT_TASKSTART( itt_domain_picEncoder, itt_handle_start );
88
89
1.20k
  pic.encTime.startTimer();
90
91
1.20k
  pic.createTempBuffers( pic.cs->pcv->maxCUSize );
92
1.20k
  pic.cs->createCoeffs();
93
1.20k
  pic.cs->createTempBuffers( true );
94
1.20k
  pic.cs->initStructData( MAX_INT, false, nullptr );
95
96
  // compress picture
97
1.20k
  xInitPicEncoder( pic );
98
99
  // compress current slice
100
1.20k
  pic.cs->slice = pic.slices[0];
101
1.20k
  std::fill( pic.ctuSlice.begin(), pic.ctuSlice.end(), pic.slices[0] );
102
1.20k
  m_SliceEncoder.compressSlice( &pic );
103
104
1.20k
  ITT_TASKEND( itt_domain_picEncoder, itt_handle_start );
105
1.20k
}
106
107
void EncPicture::finalizePicture( Picture& pic )
108
1.20k
{
109
1.20k
  PROFILER_SCOPE_TOP_LEVEL_EXT( 1, g_timeProfiler, P_TOP_LEVEL, pic.cs );
110
1.20k
  CodingStructure& cs = *(pic.cs);
111
1.20k
  Slice* slice        = pic.slices[0];
112
  // ALF
113
1.20k
  if( slice->sps->alfEnabled )
114
1.20k
  {
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.20k
    pic.picApsMap.setApsIdStart( m_ALF.getApsIdStart() );
121
122
1.20k
    cs.slice->ccAlfFilterParam      = m_ALF.getCcAlfFilterParam();
123
1.20k
    cs.slice->ccAlfFilterControl[0] = m_ALF.getCcAlfControlIdc(COMP_Cb);
124
1.20k
    cs.slice->ccAlfFilterControl[1] = m_ALF.getCcAlfControlIdc(COMP_Cr);
125
126
1.20k
    DTRACE( g_trace_ctx, D_CRC, "ALF" );
127
1.20k
    DTRACE_CRC( g_trace_ctx, D_CRC, cs, cs.getRecoBuf() );
128
1.20k
    DTRACE_PIC_COMP( D_REC_CB_LUMA_ALF,   cs, cs.getRecoBuf(), COMP_Y  );
129
1.20k
    DTRACE_PIC_COMP( D_REC_CB_CHROMA_ALF, cs, cs.getRecoBuf(), COMP_Cb );
130
1.20k
    DTRACE_PIC_COMP( D_REC_CB_CHROMA_ALF, cs, cs.getRecoBuf(), COMP_Cr );
131
1.20k
  }
132
133
  // write picture
134
1.20k
  DTRACE_UPDATE( g_trace_ctx, std::make_pair( "bsfinal", 1 ) );
135
1.20k
  xWriteSliceData( pic );
136
1.20k
  DTRACE_UPDATE( g_trace_ctx, std::make_pair( "bsfinal", 0 ) );
137
138
1.20k
  xCalcDistortion( pic, *slice->sps );
139
140
  // finalize
141
1.20k
  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.20k
  CHECK( m_pcEncCfg->m_alf && m_pcEncCfg->m_alfTempPred && !pic.picApsGlobal, "Missing APS from top level" );
148
1.20k
  if( pic.picApsGlobal )
149
1.20k
  {
150
1.20k
    CHECK( pic.picApsGlobal->poc != pic.poc, "Global APS POC must be consistent with picture poc" );
151
1.20k
    const ParameterSetMap<APS>& src = pic.picApsMap;
152
1.20k
    ParameterSetMap<APS>&       dst = pic.picApsGlobal->apsMap;
153
10.8k
    for( int i = 0; i < ALF_CTB_MAX_NUM_APS; i++ )
154
9.60k
    {
155
9.60k
      const int apsMapIdx = ( i << NUM_APS_TYPE_LEN ) + ALF_APS;
156
9.60k
      const APS* srcAPS = src.getPS( apsMapIdx );
157
9.60k
      if( srcAPS )
158
0
      {
159
0
        APS* dstAPS = dst.allocatePS( apsMapIdx );
160
0
        *dstAPS = *srcAPS;
161
0
      }
162
9.60k
    }
163
1.20k
    dst.setApsIdStart( src.getApsIdStart() );
164
1.20k
  }
165
166
  // cleanup
167
1.20k
  pic.cs->releaseIntermediateData();
168
1.20k
  pic.cs->destroyTempBuffers();
169
1.20k
  pic.cs->destroyCoeffs();
170
1.20k
  pic.destroyTempBuffers();
171
172
1.20k
  pic.encTime.stopTimer();
173
1.20k
}
174
175
uint64_t findDistortionPlane( const CPelBuf& pic0, const CPelBuf& pic1, uint32_t rshift )
176
3.60k
{
177
3.60k
  uint64_t uiTotalDiff;
178
3.60k
  const  Pel*  pSrc0 = pic0.bufAt(0, 0);
179
3.60k
  const  Pel*  pSrc1 = pic1.bufAt(0, 0);
180
181
3.60k
  CHECK(pic0.width  != pic1.width , "Unspecified error");
182
3.60k
  CHECK(pic0.height != pic1.height, "Unspecified error");
183
184
3.60k
  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.60k
  else
199
3.60k
  {
200
3.60k
    uiTotalDiff = 0;
201
373k
    for (int y = 0; y < pic0.height; y++)
202
369k
    {
203
45.1M
      for (int x = 0; x < pic0.width; x++)
204
44.7M
      {
205
44.7M
        Intermediate_Int iTemp = pSrc0[x] - pSrc1[x];
206
44.7M
        uiTotalDiff += uint64_t(iTemp * iTemp);
207
44.7M
      }
208
369k
      pSrc0 += pic0.stride;
209
369k
      pSrc1 += pic1.stride;
210
369k
    }
211
3.60k
  }
212
213
3.60k
  return uiTotalDiff;
214
3.60k
}
215
216
void EncPicture::xCalcDistortion( Picture& pic, const SPS& sps )
217
1.20k
{
218
1.20k
  const CPelUnitBuf cPicD = pic.getRecoBuf();
219
1.20k
  const CPelUnitBuf org   = pic.getOrigBuf();
220
1.20k
  const ChromaFormat formatD = cPicD.chromaFormat;
221
1.20k
  const ChromaFormat format  = sps.chromaFormatIdc;
222
223
4.80k
  for (int comp = 0; comp < getNumberValidComponents(formatD); comp++)
224
3.60k
  {
225
3.60k
    const ComponentID compID = ComponentID(comp);
226
3.60k
    const CPelBuf&    p = cPicD.get(compID);
227
3.60k
    const CPelBuf&    o = org.get(compID);
228
229
3.60k
    CHECK(!( p.width  == o.width), "Unspecified error");
230
3.60k
    CHECK(!( p.height == o.height), "Unspecified error");
231
232
3.60k
    const uint32_t   width  = p.width  - (m_pcEncCfg->m_aiPad[ 0 ] >> getComponentScaleX(compID, format));
233
3.60k
    const uint32_t   height = p.height - (m_pcEncCfg->m_aiPad[ 1 ] >> getComponentScaleY(compID, format));
234
235
    // create new buffers with correct dimensions
236
3.60k
    const CPelBuf recPB(p.bufAt(0, 0), p.stride, width, height);
237
3.60k
    const CPelBuf orgPB(o.bufAt(0, 0), o.stride, width, height);
238
3.60k
    const uint32_t    bitDepth = sps.bitDepths[toChannelType(compID)];
239
    //pic.ssd[comp] = findDistortionPlane(recPB, orgPB, 0);
240
3.60k
    const uint64_t uiSSDtemp = findDistortionPlane(recPB, orgPB, 0);
241
3.60k
    const uint32_t maxval = 255 << (bitDepth - 8);
242
3.60k
    const uint32_t size   = width * height;
243
3.60k
    const double fRefValue = (double)maxval * maxval * size;
244
3.60k
    pic.psnr[comp] = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : MAX_DOUBLE;
245
3.60k
    pic.mse [comp] = (double)uiSSDtemp / size;
246
3.60k
  }
247
1.20k
}
248
249
void EncPicture::xInitPicEncoder( Picture& pic )
250
1.20k
{
251
1.20k
  Slice* slice = pic.cs->slice;
252
253
1.20k
  CHECK( slice != pic.slices[0], "Slice pointers don't match!" );
254
255
1.20k
  m_SliceEncoder.initPic( &pic );
256
257
1.20k
  xInitSliceColFromL0Flag( slice );
258
1.20k
  xInitSliceCheckLDC     ( slice );
259
260
1.20k
  if( slice->sps->alfEnabled )
261
1.20k
  {
262
2.40k
    for (int s = 0; s < (int)pic.slices.size(); s++)
263
1.20k
    {
264
1.20k
      pic.slices[s]->alfEnabled[COMP_Y] = false;
265
1.20k
    }
266
1.20k
  }
267
1.20k
}
268
269
270
void EncPicture::xInitSliceColFromL0Flag( Slice* slice ) const
271
1.20k
{
272
1.20k
  if( m_pcEncCfg->m_rprRASLtoolSwitch )
273
0
  {
274
0
    return;
275
0
  }
276
277
1.20k
  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.20k
}
285
286
287
void EncPicture::xInitSliceCheckLDC( Slice* slice ) const
288
1.20k
{
289
1.20k
  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.20k
  else
313
1.20k
  {
314
1.20k
    slice->checkLDC = true;
315
1.20k
  }
316
1.20k
}
317
318
void EncPicture::xWriteSliceData( Picture& pic )
319
1.20k
{
320
1.20k
  const int numSlices = (int)pic.slices.size();
321
322
1.20k
  pic.sliceDataStreams.clear();
323
1.20k
  pic.sliceDataStreams.resize( numSlices );
324
1.20k
  pic.sliceDataNumBins = 0;
325
326
2.40k
  for ( int i = 0; i < numSlices; i++ )
327
1.20k
  {
328
    // set current slice
329
1.20k
    pic.cs->slice = pic.slices[i];
330
1.20k
    m_SliceEncoder.encodeSliceData( &pic );
331
1.20k
  }
332
1.20k
}
333
334
} // namespace vvenc
335
336
//! \}
337