Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/Picture.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     Picture.cpp
45
 *  \brief    Description of a coded picture
46
 */
47
48
#include "Picture.h"
49
#include "SEI.h"
50
51
#include <algorithm>
52
#include <math.h>
53
54
//! \ingroup CommonLib
55
//! \{
56
57
namespace vvenc {
58
59
// ---------------------------------------------------------------------------
60
// picture methods
61
// ---------------------------------------------------------------------------
62
63
64
65
66
// ====================================================================================================================
67
// AMaxBT block statistics
68
// ====================================================================================================================
69
70
71
void BlkStat::storeBlkSize( const Picture& pic )
72
0
{
73
0
  const Slice& slice = *(pic.slices[ 0 ]);
74
75
0
  ::memset( m_uiBlkSize, 0, sizeof( m_uiBlkSize ) );
76
0
  ::memset( m_uiNumBlk,  0, sizeof( m_uiNumBlk ) );
77
78
0
  if ( ! slice.isIRAP() )
79
0
  {
80
0
    const int refLayer = std::min<int>( slice.TLayer, NUM_AMAXBT_LAYER - 1 );
81
0
    for ( const CodingUnit *cu : pic.cs->cus )
82
0
    {
83
0
      m_uiBlkSize[ refLayer ] += cu->Y().area();
84
0
      m_uiNumBlk [ refLayer ] += 1;
85
0
    }
86
0
  }
87
0
}
88
89
void BlkStat::updateMaxBT( const Slice& slice, const BlkStat& blkStat )
90
0
{
91
0
  if ( ! slice.isIRAP() )
92
0
  {
93
0
    const int refLayer = std::min<int>( slice.TLayer, NUM_AMAXBT_LAYER - 1 );
94
0
    m_uiBlkSize[ refLayer ] += blkStat.m_uiBlkSize[ refLayer ];
95
0
    m_uiNumBlk [ refLayer ] += blkStat.m_uiNumBlk [ refLayer ];
96
0
  }
97
0
}
98
99
void BlkStat::setSliceMaxBT( Slice& slice )
100
0
{
101
0
  if( ! slice.isIRAP() )
102
0
  {
103
0
    const int refLayer = std::min<int>( slice.TLayer, NUM_AMAXBT_LAYER - 1 );
104
0
    if( m_bResetAMaxBT && slice.poc > m_uiPrevISlicePOC )
105
0
    {
106
0
      ::memset( m_uiBlkSize, 0, sizeof( m_uiBlkSize ) );
107
0
      ::memset( m_uiNumBlk,  0, sizeof( m_uiNumBlk ) );
108
0
      m_bResetAMaxBT = false;
109
0
    }
110
111
0
    if( refLayer >= 0 && m_uiNumBlk[ refLayer ] != 0 )
112
0
    {
113
0
      slice.picHeader->splitConsOverride = true;
114
0
      double dBlkSize = sqrt( ( double ) m_uiBlkSize[refLayer] / m_uiNumBlk[refLayer] );
115
0
      if( dBlkSize < AMAXBT_TH32 || slice.sps->CTUSize == 32 )
116
0
      {
117
0
        slice.picHeader->maxBTSize[1] = ( 32 > MAX_BT_SIZE_INTER ? MAX_BT_SIZE_INTER : 32 );
118
0
      }
119
0
      else if( dBlkSize < AMAXBT_TH64 || slice.sps->CTUSize == 64 )
120
0
      {
121
0
        slice.picHeader->maxBTSize[1] = ( 64 > MAX_BT_SIZE_INTER ? MAX_BT_SIZE_INTER : 64 );
122
0
      }
123
0
      else
124
0
      {
125
0
        slice.picHeader->maxBTSize[1] = ( 128 > MAX_BT_SIZE_INTER ? MAX_BT_SIZE_INTER : 128 );
126
0
      }
127
128
0
      m_uiBlkSize[ refLayer ] = 0;
129
0
      m_uiNumBlk [ refLayer ] = 0;
130
0
    }
131
0
  }
132
0
  else
133
0
  {
134
0
    if( m_bResetAMaxBT )
135
0
    {
136
0
      ::memset( m_uiBlkSize, 0, sizeof( m_uiBlkSize ) );
137
0
      ::memset( m_uiNumBlk,  0, sizeof( m_uiNumBlk ) );
138
0
    }
139
140
0
    m_uiPrevISlicePOC = slice.poc;
141
0
    m_bResetAMaxBT    = true;
142
0
  }
143
0
}
144
145
146
// ====================================================================================================================
147
// Picture
148
// ====================================================================================================================
149
150
151
Picture::Picture()
152
4.29k
    : cs                  ( nullptr )
153
4.29k
    , vps                 ( nullptr )
154
4.29k
    , dci                 ( nullptr )
155
4.29k
    , picApsMap           ( MAX_NUM_APS * MAX_NUM_APS_TYPE )
156
4.29k
    , isInitDone          ( false )
157
4.29k
    , isReconstructed     ( false )
158
4.29k
    , isEncoded           ( false )
159
4.29k
    , isBorderExtended    ( false )
160
4.29k
    , isReferenced        ( false )
161
4.29k
    , isNeededForOutput   ( false )
162
4.29k
    , isFinished          ( false )
163
4.29k
    , isLongTerm          ( false )
164
4.29k
    , isFlush             ( false )
165
4.29k
    , isInProcessList     ( false )
166
4.29k
    , precedingDRAP       ( false )
167
4.29k
    , gopEntry            ( nullptr )
168
4.29k
    , refCounter          ( 0 )
169
4.29k
    , poc                 ( 0 )
170
4.29k
    , TLayer              ( std::numeric_limits<uint32_t>::max() )
171
4.29k
    , layerId             ( 0 )
172
4.29k
    , isSubPicBorderSaved (false)
173
4.29k
    , sliceDataNumBins    ( 0 )
174
4.29k
    , cts                 ( 0 )
175
4.29k
    , ctsValid            ( false )
176
4.29k
    , picsInMissing       ( 0 )
177
4.29k
    , picOutOffset        ( 0 )
178
4.29k
    , isPreAnalysis       ( false )
179
4.29k
    , encStartOrder       ( 0 )
180
4.29k
    , m_picShared         ( nullptr )
181
4.29k
    , gopAdaptedQP        ( 0 )
182
4.29k
    , force2ndOrder       ( false )
183
4.29k
    , isSceneCutGOP       ( false )
184
4.29k
    , isSceneCutCheckAdjQP( false )
185
4.29k
    , isMeanQPLimited     ( false )
186
4.29k
    , picInitialQP        ( -1 )
187
4.29k
    , picInitialLambda    ( -1.0 )
188
4.29k
    , picQPFraction       ( 0.0 )
189
4.29k
    , picMemorySTA        ( -1 )
190
4.29k
    , picVA               ()
191
4.29k
    , isSccWeak           ( false )
192
4.29k
    , isSccStrong         ( false )
193
4.29k
    , useME               ( false )
194
4.29k
    , useMCTF             ( false )
195
4.29k
    , useTS               ( false )
196
4.29k
    , useBDPCM            ( false )
197
4.29k
    , useIBC              ( false )
198
4.29k
    , useSAO              ( false )
199
4.29k
    , useNumRefs          ( false )
200
4.29k
    , useFastMrg          ( 0 )
201
4.29k
    , useQtbttSpeedUpMode ( 0 )
202
4.29k
    , actualHeadBits      ( 0 )
203
4.29k
    , actualTotalBits     ( 0 )
204
4.29k
    , encRCPic            ()
205
4.29k
    , picApsGlobal        ( nullptr )
206
4.29k
    , refApsGlobal        ( nullptr )
207
4.29k
{
208
4.29k
  std::fill_n( m_sharedBufs, (int)NUM_PIC_TYPES, nullptr );
209
4.29k
  std::fill_n( m_bufsOrigPrev, NUM_QPA_PREV_FRAMES, nullptr );
210
4.29k
}
211
212
void Picture::create( ChromaFormat _chromaFormat, const Size& size, unsigned _maxCUSize, unsigned _margin, bool _decoder )
213
4.29k
{
214
4.29k
  UnitArea::operator=( UnitArea( _chromaFormat, Area( Position{ 0, 0 }, size ) ) );
215
4.29k
  margin            =  _margin;
216
217
4.29k
  if( _decoder )
218
0
  {
219
0
    m_picBufs[ PIC_RESIDUAL   ].create( _chromaFormat, Area( 0, 0, _maxCUSize, _maxCUSize ) );
220
0
    m_picBufs[ PIC_PREDICTION ].create( _chromaFormat, Area( 0, 0, _maxCUSize, _maxCUSize ) );
221
0
  }
222
4.29k
}
223
224
void Picture::reset()
225
4.29k
{
226
  // reset picture
227
4.29k
  isInitDone           = false;
228
4.29k
  isReconstructed      = false;
229
4.29k
  isEncoded            = false;
230
4.29k
  isBorderExtended     = false;
231
4.29k
  isReferenced         = true;
232
4.29k
  isNeededForOutput    = true;
233
4.29k
  isFinished           = false;
234
4.29k
  isLongTerm           = false;
235
4.29k
  isFlush              = false;
236
4.29k
  isInProcessList      = false;
237
4.29k
  isMeanQPLimited      = false;
238
4.29k
  precedingDRAP        = false;
239
240
4.29k
  gopEntry             = nullptr;
241
4.29k
  refCounter           = 0;
242
4.29k
  poc                  = -1;
243
4.29k
  TLayer               = std::numeric_limits<uint32_t>::max();
244
4.29k
  gopAdaptedQP         = 0;
245
4.29k
  force2ndOrder        = false;
246
4.29k
  isSceneCutGOP        = false;
247
4.29k
  isSceneCutCheckAdjQP = false;
248
4.29k
  actualHeadBits       = 0;
249
4.29k
  actualTotalBits      = 0;
250
4.29k
  encRCPic             . reset();
251
4.29k
  picApsGlobal         = nullptr;
252
4.29k
  refApsGlobal         = nullptr;
253
254
4.29k
  cts                  = 0;
255
4.29k
  ctsValid             = false;
256
4.29k
  picsInMissing        = 0;
257
4.29k
  picOutOffset         = 0;
258
259
4.29k
  picVA.reset();
260
4.29k
  for( auto& spectra : lostGrainSpectra )
261
12.8k
  {
262
12.8k
    spectra.clear();
263
12.8k
  }
264
265
4.29k
  std::fill_n( m_sharedBufs, (int)NUM_PIC_TYPES, nullptr );
266
4.29k
  std::fill_n( m_bufsOrigPrev, NUM_QPA_PREV_FRAMES, nullptr );
267
268
4.29k
  if( m_tileColsDone )
269
0
    std::fill( m_tileColsDone->begin(), m_tileColsDone->end(), 0 );
270
271
4.29k
  encTime.resetTimer();
272
4.29k
}
273
274
void Picture::destroy( bool bPicHeader )
275
4.29k
{
276
25.7k
  for (uint32_t t = 0; t < NUM_PIC_TYPES; t++)
277
21.4k
  {
278
21.4k
    m_picBufs[  t ].destroy();
279
21.4k
  }
280
4.29k
  if( cs )
281
1.43k
  {
282
1.43k
    if( bPicHeader && cs->picHeader )
283
1.43k
    {
284
1.43k
      delete cs->picHeader;
285
1.43k
    }
286
1.43k
    cs->picHeader = nullptr;
287
1.43k
    cs->destroy();
288
1.43k
    delete cs;
289
1.43k
    cs = nullptr;
290
1.43k
  }
291
292
4.29k
  for( auto &ps : slices )
293
1.43k
  {
294
1.43k
    delete ps;
295
1.43k
  }
296
4.29k
  slices.clear();
297
298
4.29k
  for( auto &psei : SEIs )
299
0
  {
300
0
    delete psei;
301
0
  }
302
303
4.29k
  delete m_tileColsDone;
304
305
4.29k
  SEIs.clear();
306
4.29k
}
307
308
void Picture::linkSharedBuffers( PelStorage* origBuf, PelStorage* filteredBuf, PelStorage* prevOrigBufs[ NUM_QPA_PREV_FRAMES ], PicShared* picShared )
309
4.29k
{
310
4.29k
  m_picShared                      = picShared;
311
4.29k
  m_sharedBufs[ PIC_ORIGINAL ]      = origBuf;
312
4.29k
  m_sharedBufs[ PIC_FILT_ORIGINAL ] = filteredBuf;
313
12.8k
  for( int i = 0; i < NUM_QPA_PREV_FRAMES; i++ )
314
8.59k
    m_bufsOrigPrev[ i ] = prevOrigBufs[ i ];
315
4.29k
}
316
317
void Picture::releasePrevBuffers()
318
10.0k
{
319
30.0k
  for( int i = 0; i < NUM_QPA_PREV_FRAMES; i++ )
320
20.0k
    m_bufsOrigPrev[ i ] = nullptr;
321
10.0k
}
322
323
void Picture::releaseSharedBuffers()
324
4.29k
{
325
4.29k
  m_picShared                      = nullptr;
326
4.29k
  m_sharedBufs[ PIC_ORIGINAL ]     = nullptr;
327
4.29k
  m_sharedBufs[ PIC_FILT_ORIGINAL ] = nullptr;
328
4.29k
}
329
330
void Picture::createEncodingBuffers()
331
1.43k
{
332
1.43k
  CHECK( !cs, "Coding structure is required at this point!" );
333
334
1.43k
  if( !m_picBufs[PIC_RECONSTRUCTION].valid() )
335
1.43k
  {
336
1.43k
    m_picBufs[PIC_RECONSTRUCTION].create( chromaFormat, Area( lumaPos(), lumaSize() ), cs->sps->CTUSize, margin, MEMORY_ALIGN_DEF_SIZE );
337
1.43k
  }
338
1.43k
  cs->createMotionBuf();
339
1.43k
  if( cs->sps->temporalMVPEnabled )
340
1.43k
  {
341
1.43k
    cs->createColocatedMotionBuf();
342
1.43k
  }
343
1.43k
  cs->rebindPicBufs();
344
1.43k
}
345
346
void Picture::releaseEncodingBuffers()
347
1.43k
{
348
1.43k
  m_picBufs[PIC_RECONSTRUCTION].destroy();
349
1.43k
  if( cs )
350
1.43k
  {
351
1.43k
    cs->destroyMotionBuf();
352
1.43k
    cs->rebindPicBufs();
353
1.43k
  }
354
1.43k
}
355
356
17.6k
const CPelBuf     Picture::getOrigBufPrev (const CompArea &blk, const PrevFrameType type) const { return (m_bufsOrigPrev[ type ] && blk.valid() ? m_bufsOrigPrev[ type ]->getBuf (blk) : PelBuf()); }
357
0
const CPelUnitBuf Picture::getOrigBufPrev (const PrevFrameType type) const { return (m_bufsOrigPrev[ type ] ? *m_bufsOrigPrev[ type ] : PelUnitBuf()); }
358
7.16k
const CPelBuf     Picture::getOrigBufPrev (const ComponentID compID, const PrevFrameType type) const { return (m_bufsOrigPrev[ type ] ? m_bufsOrigPrev[ type ]->getBuf (compID) : PelBuf()); }
359
360
void Picture::finalInit( const VPS& _vps, const SPS& sps, const PPS& pps, PicHeader* picHeader, XUCache& unitCache, std::mutex* mutex, APS** alfAps )
361
1.43k
{
362
1.43k
  for( auto &sei : SEIs )
363
0
  {
364
0
    delete sei;
365
0
  }
366
1.43k
  SEIs.clear();
367
368
1.43k
  for( size_t i = 0; i < slices.size(); i++ )
369
0
  {
370
0
    delete slices[i];
371
0
  }
372
1.43k
  slices.clear();
373
1.43k
  ctuSlice.clear();
374
1.43k
  ctuSlice.resize( pps.pcv->sizeInCtus, nullptr );
375
376
1.43k
  const ChromaFormat chromaFormatIDC = sps.chromaFormatIdc;
377
1.43k
  const int          iWidth  = pps.picWidthInLumaSamples;
378
1.43k
  const int          iHeight = pps.picHeightInLumaSamples;
379
380
1.43k
  if( cs )
381
0
  {
382
0
    CHECK( cs->sps != &sps,  "picture initialization error: sps changed" );
383
0
    CHECK( cs->vps != &_vps, "picture initialization error: vps changed" );
384
0
  }
385
1.43k
  else
386
1.43k
  {
387
1.43k
    cs = new CodingStructure( unitCache, mutex );
388
1.43k
    cs->pps = &pps;
389
1.43k
    cs->sps = &sps;
390
1.43k
    cs->vps = &_vps;
391
1.43k
    cs->createPicLevel( UnitArea( chromaFormatIDC, Area( 0, 0, iWidth, iHeight )), pps.pcv );
392
1.43k
  }
393
394
1.43k
  cs->picture   = this;
395
1.43k
  cs->lumaCS    = cs;
396
1.43k
  cs->slice     = nullptr;  // the slices for this picture have not been set at this point. update cs->slice after swapSliceObject()
397
1.43k
  cs->picHeader = picHeader;
398
1.43k
  if( alfAps )
399
0
  {
400
0
    memcpy( cs->alfAps, alfAps, sizeof( cs->alfAps ) );
401
0
  }
402
1.43k
  cs->pcv     = pps.pcv;
403
1.43k
  vps         = &_vps;
404
1.43k
  dci         = nullptr;
405
406
1.43k
  if( !m_tileColsDone )
407
1.43k
  {
408
1.43k
    m_tileColsDone = new std::vector<std::atomic<int>> ( pps.pcv->heightInCtus );
409
1.43k
  }
410
1.43k
  std::fill( m_tileColsDone->begin(), m_tileColsDone->end(), 0 );
411
412
1.43k
  sliceDataStreams.clear();
413
1.43k
  sliceDataNumBins = 0;
414
1.43k
}
415
416
void Picture::setSccFlags( const VVEncCfg* encCfg )
417
2.86k
{
418
2.86k
  useME      = encCfg->m_motionEstimationSearchMethodSCC > 0                          && isSccStrong;
419
2.86k
  useTS      = encCfg->m_TS == 1                || ( encCfg->m_TS == 2                && isSccWeak );
420
2.86k
  useBDPCM   = encCfg->m_useBDPCM == 1          || ( encCfg->m_useBDPCM == 2          && isSccWeak );
421
2.86k
  useMCTF    = encCfg->m_vvencMCTF.MCTF == 1    || ( encCfg->m_vvencMCTF.MCTF == 2    && ! isSccStrong );
422
2.86k
  useIBC     = encCfg->m_IBCMode == 1           || ( encCfg->m_IBCMode == 2           && isSccStrong );
423
2.86k
  useSAO     = encCfg->m_bUseSAO                && ( !encCfg->m_saoScc                || isSccWeak );
424
2.86k
  useSelectiveRdoq = encCfg->m_useSelectiveRDOQ == 2 ? !isSccWeak : !!encCfg->m_useSelectiveRDOQ;
425
2.86k
  useNumRefs = isSccStrong;
426
2.86k
  useFastMrg = isSccStrong ? 0 : std::max(0, encCfg->m_useFastMrg - 2);
427
2.86k
  useQtbttSpeedUpMode = encCfg->m_qtbttSpeedUpMode;
428
429
2.86k
  if( ( encCfg->m_qtbttSpeedUpMode & 2 ) && isSccStrong )
430
0
  {
431
0
    useQtbttSpeedUpMode &= ~1;
432
0
  }
433
2.86k
}
434
435
Slice* Picture::allocateNewSlice()
436
1.43k
{
437
1.43k
  slices.push_back( new Slice );
438
1.43k
  Slice& slice = *slices.back();
439
440
1.43k
  slice.pic     = this;
441
1.43k
  slice.pps     = cs->pps;
442
1.43k
  slice.sps     = cs->sps;
443
1.43k
  slice.vps     = cs->vps;
444
445
1.43k
  memcpy( slice.alfAps, cs->alfAps, sizeof(cs->alfAps) );
446
447
1.43k
  if( slices.size() >= 2 )
448
0
  {
449
0
    slice.copySliceInfo( slices[ slices.size() - 2 ] );
450
0
  }
451
452
1.43k
  return slices.back();
453
1.43k
}
454
455
Slice* Picture::swapSliceObject( Slice*  p, uint32_t i )
456
0
{
457
0
  p->pic     = this;
458
0
  p->pps     = cs->pps;
459
0
  p->sps     = cs->sps;
460
0
  p->vps     = cs->vps;
461
0
  memcpy( p->alfAps, cs->alfAps, sizeof(cs->alfAps) );
462
463
0
  Slice*  pTmp = slices[ i ];
464
0
  slices[ i ] = p;
465
466
0
  pTmp->pic = ( nullptr );
467
0
  pTmp->sps = ( nullptr );
468
0
  pTmp->pps = ( nullptr );
469
0
  memset( pTmp->alfAps, 0, sizeof( *pTmp->alfAps ) * ALF_CTB_MAX_NUM_APS );
470
471
0
  return pTmp;
472
0
}
473
474
void Picture::extendPicBorder()
475
0
{
476
0
  if ( isBorderExtended )
477
0
  {
478
0
    return;
479
0
  }
480
481
0
  for(int comp=0; comp<getNumberValidComponents( cs->area.chromaFormat ); comp++)
482
0
  {
483
0
    ComponentID compID = ComponentID( comp );
484
0
    PelBuf p = m_picBufs[ PIC_RECONSTRUCTION ].get( compID );
485
0
    Pel* piTxt = p.bufAt(0,0);
486
0
    int xmargin = margin >> getComponentScaleX( compID, cs->area.chromaFormat );
487
0
    int ymargin = margin >> getComponentScaleY( compID, cs->area.chromaFormat );
488
489
0
    Pel*  pi = piTxt;
490
    // do left and right margins
491
0
      for (int y = 0; y < p.height; y++)
492
0
      {
493
0
        for (int x = 0; x < xmargin; x++ )
494
0
        {
495
0
          pi[ -xmargin + x ] = pi[0];
496
0
          pi[  p.width + x ] = pi[p.width-1];
497
0
        }
498
0
        pi += p.stride;
499
0
      }
500
501
    // pi is now the (0,height) (bottom left of image within bigger picture
502
0
    pi -= (p.stride + xmargin);
503
    // pi is now the (-marginX, height-1)
504
0
    for (int y = 0; y < ymargin; y++ )
505
0
    {
506
0
      ::memcpy( pi + (y+1)*p.stride, pi, sizeof(Pel)*(p.width + (xmargin << 1)));
507
0
    }
508
509
    // pi is still (-marginX, height-1)
510
0
    pi -= ((p.height-1) * p.stride);
511
    // pi is now (-marginX, 0)
512
0
    for (int y = 0; y < ymargin; y++ )
513
0
    {
514
0
      ::memcpy( pi - (y+1)*p.stride, pi, sizeof(Pel)*(p.width + (xmargin<<1)) );
515
0
    }
516
0
  }
517
518
0
  isBorderExtended = true;
519
0
}
520
521
// m_ctuLineBottoms keeps the bottom rows of CTU line n at its rows n * rows .. ( n + 1 ) * rows - 1: the row of picture row y
522
static int ctuLineBottomsRow( const int y, const int ctuHeight, const int rows )
523
81.3k
{
524
81.3k
  const int line = y / ctuHeight;
525
81.3k
  CHECKD( y < ( line + 1 ) * ctuHeight - rows, "row not kept: above the bottom rows of its CTU line" );
526
81.3k
  return y - ( line + 1 ) * ( ctuHeight - rows );
527
81.3k
}
528
529
const CPelBuf Picture::getRecoAboveCtuTop( const CompArea& blk ) const
530
74.8k
{
531
74.8k
  if( !m_ctuLineBottoms )
532
0
  {
533
0
    return getRecoBuf( blk );
534
0
  }
535
74.8k
  const int scaleY    = getComponentScaleY( blk.compID, chromaFormat );
536
74.8k
  const int ctuHeight = cs->pcv->maxCUSize >> scaleY;
537
74.8k
  const int rows      = CTU_LINE_BOTTOM_ROWS >> scaleY;
538
74.8k
  CHECKD( blk.y / ctuHeight != ( blk.y + (int)blk.height - 1 ) / ctuHeight, "rows of two CTU lines" );
539
74.8k
  return m_ctuLineBottoms->get( blk.compID ).subBuf( blk.x, ctuLineBottomsRow( blk.y, ctuHeight, rows ), blk.width, blk.height );
540
74.8k
}
541
542
void Picture::keepCtuBottomRows( const UnitArea& ctuArea )
543
2.16k
{
544
8.64k
  for( uint32_t c = 0; c < getNumberValidComponents( chromaFormat ); c++ )
545
6.48k
  {
546
6.48k
    const CompArea& ctu = ctuArea.blocks[c];
547
6.48k
    const int scaleY    = getComponentScaleY( ctu.compID, chromaFormat );
548
6.48k
    const int ctuHeight = cs->pcv->maxCUSize >> scaleY;
549
6.48k
    const int rows      = CTU_LINE_BOTTOM_ROWS >> scaleY;
550
6.48k
    const int y         = ctu.y + ctu.height - rows;
551
6.48k
    const CompArea bottom( ctu.compID, chromaFormat, Area( ctu.x, y, ctu.width, rows ) );
552
6.48k
    m_ctuLineBottoms->get( ctu.compID ).subBuf( ctu.x, ctuLineBottomsRow( y, ctuHeight, rows ), ctu.width, rows ).copyFrom( getRecoBuf( bottom ) );
553
6.48k
  }
554
2.16k
}
555
556
PelUnitBuf Picture::getPicBuf( const UnitArea& unit, const PictureType type )
557
148k
{
558
148k
  if( chromaFormat == CHROMA_400 )
559
0
  {
560
0
    return PelUnitBuf( chromaFormat, getPicBuf( unit.Y(), type ) );
561
0
  }
562
148k
  else
563
148k
  {
564
148k
    return PelUnitBuf( chromaFormat, getPicBuf( unit.Y(), type ), getPicBuf( unit.Cb(), type ), getPicBuf( unit.Cr(), type ) );
565
148k
  }
566
148k
}
567
568
const CPelUnitBuf Picture::getPicBuf( const UnitArea& unit, const PictureType type ) const
569
0
{
570
0
  if( chromaFormat == CHROMA_400 )
571
0
  {
572
0
    return CPelUnitBuf( chromaFormat, getPicBuf( unit.Y(), type ) );
573
0
  }
574
0
  else
575
0
  {
576
0
    return CPelUnitBuf( chromaFormat, getPicBuf( unit.Y(), type ), getPicBuf( unit.Cb(), type ), getPicBuf( unit.Cr(), type ) );
577
0
  }
578
0
}
579
580
PelUnitBuf Picture::getSharedBuf( const UnitArea& unit, const PictureType type )
581
0
{
582
0
  if( chromaFormat == CHROMA_400 )
583
0
  {
584
0
    return PelUnitBuf( chromaFormat, getSharedBuf( unit.Y(), type ) );
585
0
  }
586
0
  else
587
0
  {
588
0
    return PelUnitBuf( chromaFormat, getSharedBuf( unit.Y(), type ), getSharedBuf( unit.Cb(), type ), getSharedBuf( unit.Cr(), type ) );
589
0
  }
590
0
}
591
592
const CPelUnitBuf Picture::getSharedBuf( const UnitArea& unit, const PictureType type ) const
593
4.65k
{
594
4.65k
  if( chromaFormat == CHROMA_400 )
595
0
  {
596
0
    return CPelUnitBuf( chromaFormat, getSharedBuf( unit.Y(), type ) );
597
0
  }
598
4.65k
  else
599
4.65k
  {
600
4.65k
    return CPelUnitBuf( chromaFormat, getSharedBuf( unit.Y(), type ), getSharedBuf( unit.Cb(), type ), getSharedBuf( unit.Cr(), type ) );
601
4.65k
  }
602
4.65k
}
603
604
void Picture::resizeAlfCtuBuffers( int numEntries )
605
1.43k
{
606
5.72k
  for( int compIdx = 0; compIdx < MAX_NUM_COMP; compIdx++ )
607
4.29k
  {
608
4.29k
    m_alfCtuEnabled[compIdx].resize( numEntries );
609
4.29k
    std::fill( m_alfCtuEnabled[compIdx].begin(), m_alfCtuEnabled[compIdx].end(), 0 );
610
4.29k
  }
611
612
1.43k
  m_alfCtbFilterIndex.resize(numEntries);
613
6.08k
  for (int i = 0; i < numEntries; i++)
614
4.65k
  {
615
4.65k
    m_alfCtbFilterIndex[i] = 0;
616
4.65k
  }
617
618
4.29k
  for( int compIdx = 1; compIdx < MAX_NUM_COMP; compIdx++ )
619
2.86k
  {
620
2.86k
    m_alfCtuAlternative[compIdx].resize( numEntries );
621
2.86k
    std::fill( m_alfCtuAlternative[compIdx].begin(), m_alfCtuAlternative[compIdx].end(), 0 );
622
2.86k
  }
623
1.43k
}
624
625
626
} // namespace vvenc
627
628
//! \}
629