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/CodingStructure.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     CodingStructure.h
45
 *  \brief    A class managing the coding information for a specific image part
46
 */
47
48
#include "CodingStructure.h"
49
#include "Unit.h"
50
#include "Slice.h"
51
#include "Picture.h"
52
#include "UnitTools.h"
53
#include "UnitPartitioner.h"
54
55
//! \ingroup CommonLib
56
//! \{
57
58
namespace vvenc {
59
60
// ---------------------------------------------------------------------------
61
// coding structure method definitions
62
// ---------------------------------------------------------------------------
63
64
CodingStructure::CodingStructure( XUCache& unitCache, std::mutex* mutex )
65
173k
  : area            ()
66
173k
  , picture         ( nullptr )
67
173k
  , parent          ( nullptr )
68
173k
  , lumaCS          ( nullptr )
69
173k
  , picHeader       ( nullptr )
70
173k
  , m_isTuEnc       ( false )
71
173k
  , m_cuCache       ( unitCache.cuCache )
72
173k
  , m_tuCache       ( unitCache.tuCache )
73
173k
  , m_unitCacheMutex( mutex )
74
173k
  , bestParent      ( nullptr )
75
173k
  , resetIBCBuffer  ( false )
76
173k
{
77
693k
  for( uint32_t i = 0; i < MAX_NUM_COMP; i++ )
78
519k
  {
79
519k
    m_coeffs[ i ] = nullptr;
80
519k
    m_offsets[ i ] = 0;
81
519k
  }
82
83
519k
  for( uint32_t i = 0; i < MAX_NUM_CH; i++ )
84
346k
  {
85
346k
    m_cuPtr   [ i ] = nullptr;
86
346k
  }
87
88
519k
  for( int i = 0; i < NUM_EDGE_DIR; i++ )
89
346k
  {
90
346k
    m_lfParam [ i ] = nullptr;
91
346k
  }
92
93
173k
  m_motionBuf             = nullptr;
94
173k
  m_colocatedMotion       = nullptr;
95
173k
  m_colocatedMotionStride = 0;
96
97
173k
  m_numTUs = m_numCUs = 0;
98
173k
}
99
100
void CodingStructure::destroy()
101
167k
{
102
167k
  picture   = nullptr;
103
167k
  parent    = nullptr;
104
167k
  lumaCS     = nullptr;
105
106
167k
  m_pred.destroy();
107
167k
  m_resi.destroy();
108
167k
  m_reco.destroy();
109
167k
  m_org = nullptr;
110
111
167k
  destroyCoeffs();
112
167k
  destroyMotionBuf();
113
114
167k
  destroyTempBuffers();
115
116
167k
  if ( m_unitCacheMutex ) m_unitCacheMutex->lock();
117
118
167k
  m_tuCache.cache( tus );
119
167k
  m_cuCache.cache( cus );
120
121
167k
  if ( m_unitCacheMutex ) m_unitCacheMutex->unlock();
122
167k
}
123
124
void CodingStructure::releaseIntermediateData()
125
544k
{
126
544k
  clearTUs();
127
544k
  clearCUs();
128
544k
}
129
130
CodingUnit* CodingStructure::getLumaCU( const Position& pos )
131
0
{
132
0
  const ChannelType effChType = CH_L;
133
0
  const CompArea& _blk = area.blocks[effChType];
134
0
  CHECK( !_blk.contains( pos ), "must contain the pos" );
135
136
0
  return m_cuPtr[effChType][rsAddr( pos, _blk.pos(), _blk.width, unitScale[effChType] )];
137
0
}
138
139
CodingUnit* CodingStructure::getCU( const Position& pos, const ChannelType effChType, const TreeType _treeType )
140
6.09M
{
141
6.09M
  CHECKD(_treeType == TREE_C && effChType == CH_L && parent == nullptr, "parent shall be valid; consider using function getLumaCU()");
142
143
6.09M
  CodingStructure* cs = _treeType == TREE_C && effChType == CH_L ? parent : this;
144
7.40M
  while (cs && !cs->area.blocks[effChType].contains(pos)) cs = cs->parent;
145
146
6.09M
  if (!cs)
147
193
  {
148
193
    return nullptr;
149
193
  }
150
6.09M
  else
151
6.09M
  {
152
6.09M
    const Area& _blk = cs->area.blocks[effChType];
153
6.09M
    return cs->m_cuPtr[effChType][rsAddr(pos, _blk.pos(), _blk.width, unitScale[effChType])];
154
6.09M
  }
155
6.09M
}
156
157
const CodingUnit* CodingStructure::getCU( const Position& pos, const ChannelType effChType, const TreeType _treeType ) const
158
3.16M
{
159
3.16M
  CHECKD(_treeType == TREE_C && effChType == CH_L && parent == nullptr, "parent shall be valid; consider using function getLumaCU()");
160
161
3.16M
  const CodingStructure* cs = _treeType == TREE_C && effChType == CH_L ? parent : this;
162
7.24M
  while (cs && !cs->area.blocks[effChType].contains(pos)) cs = cs->parent;
163
164
3.16M
  if (!cs)
165
18.0k
  {
166
18.0k
    return nullptr;
167
18.0k
  }
168
3.15M
  else
169
3.15M
  {
170
3.15M
    const Area& _blk = cs->area.blocks[effChType];
171
3.15M
    return cs->m_cuPtr[effChType][rsAddr( pos, _blk.pos(), _blk.width, unitScale[effChType] )];
172
3.15M
  }
173
3.16M
}
174
175
TransformUnit* CodingStructure::getTU( const Position& pos, const ChannelType effChType, const int subTuIdx )
176
437k
{
177
437k
  const CompArea& _blk = area.blocks[effChType];
178
179
437k
  if( !_blk.contains( pos ) )
180
0
  {
181
0
    if( parent ) return parent->getTU( pos, effChType );
182
0
    else         return nullptr;
183
0
  }
184
437k
  else
185
437k
  {
186
437k
    CodingUnit* cu = m_cuPtr[effChType][rsAddr( pos, _blk.pos(), _blk.width, unitScale[effChType] )];
187
437k
    if( !cu ) return nullptr;
188
189
437k
    TransformUnit* ptu = cu->firstTU;
190
191
437k
    while( ptu && !ptu->blocks[effChType].contains( pos ) )
192
0
    {
193
0
      ptu = ptu->next;
194
0
    }
195
196
437k
    return ptu;
197
437k
  }
198
437k
}
199
200
const TransformUnit * CodingStructure::getTU( const Position& pos, const ChannelType effChType, const int subTuIdx ) const
201
193k
{
202
193k
  const CompArea& _blk = area.blocks[effChType];
203
204
193k
  if( !_blk.contains( pos ) )
205
0
  {
206
0
    if( parent ) return parent->getTU( pos, effChType );
207
0
    else         return nullptr;
208
0
  }
209
193k
  else
210
193k
  {
211
193k
    const CodingUnit* cu = m_cuPtr[effChType][rsAddr( pos, _blk.pos(), _blk.width, unitScale[effChType] )];
212
193k
    if( !cu ) return nullptr;
213
214
193k
    const TransformUnit* ptu = cu->firstTU;
215
216
194k
    while( ptu && !ptu->blocks[effChType].contains( pos ) )
217
294
    {
218
294
      ptu = ptu->next;
219
294
    }
220
221
193k
    return ptu;
222
193k
  }
223
193k
}
224
225
CodingUnit& CodingStructure::addCU( const UnitArea& unit, const ChannelType chType, CodingUnit* cuInit )
226
523k
{
227
523k
  CodingUnit* cu;
228
229
523k
  if( cuInit )
230
180k
  {
231
180k
    cu = cuInit;
232
180k
  }
233
343k
  else
234
343k
  {
235
343k
    if( m_unitCacheMutex ) m_unitCacheMutex->lock();
236
237
343k
    cu = m_cuCache.get();
238
239
343k
    if( m_unitCacheMutex ) m_unitCacheMutex->unlock();
240
241
343k
    cu->UnitArea::operator=( unit );
242
343k
    cu->initData();
243
343k
    cu->slice   = nullptr;
244
343k
  }
245
  
246
523k
  cu->next      = nullptr;
247
523k
  cu->firstTU   = nullptr;
248
523k
  cu->lastTU    = nullptr;
249
523k
  cu->chType    = chType;
250
523k
  cu->cs        = this;
251
252
523k
  CodingUnit *prevCU = m_numCUs > 0 ? cus.back() : nullptr;
253
254
523k
  if( prevCU )
255
145k
  {
256
145k
    const int prevCuCtuRsAddr = getCtuAddr( recalcPosition( area.chromaFormat, prevCU->chType, CH_L, prevCU->blocks[prevCU->chType] ), *pcv );
257
145k
    const int currCuCtuRsAddr = getCtuAddr( recalcPosition( area.chromaFormat,         chType, CH_L,     cu->blocks[        chType] ), *pcv );
258
259
145k
    if( prevCuCtuRsAddr == currCuCtuRsAddr )
260
141k
    {
261
141k
      prevCU->next = cu;
262
141k
    }
263
145k
  }
264
265
523k
  cus.push_back( cu );
266
267
523k
  Mv* prevCuMvd = cuInit ? cuInit->mvdL0SubPu : nullptr;
268
  
269
523k
  cu->idx        = ++m_numCUs;
270
523k
  cu->mvdL0SubPu = nullptr;
271
272
523k
  if( isLuma( chType ) && unit.lheight() >= 8 && unit.lwidth() >= 8 && unit.Y().area() >= 128 )
273
326k
  {
274
326k
    CHECKD( m_dmvrMvCacheOffset >= m_dmvrMvCache.size(), "dmvr cache offset out of bounds" );
275
276
326k
    int mvdArrSize       = std::max<int>( 1, unit.lwidth() >> DMVR_SUBCU_SIZE_LOG2 ) * std::max<int>( 1, unit.lheight() >> DMVR_SUBCU_SIZE_LOG2 );
277
326k
    cu->mvdL0SubPu       = &m_dmvrMvCache[m_dmvrMvCacheOffset];
278
326k
    m_dmvrMvCacheOffset += mvdArrSize;
279
280
326k
    if( prevCuMvd )
281
77.0k
      memcpy( cu->mvdL0SubPu, prevCuMvd, sizeof( Mv ) * mvdArrSize );
282
326k
  }
283
284
523k
  uint32_t numCh = getNumberValidChannels( area.chromaFormat );
285
286
1.57M
  for( uint32_t i = 0; i < numCh; i++ )
287
1.04M
  {
288
1.04M
    if( !cu->blocks[i].valid() )
289
523k
    {
290
523k
      continue;
291
523k
    }
292
293
523k
    const CompArea& _selfBlk = area.blocks[i];
294
523k
    const CompArea     &_blk = cu-> blocks[i];
295
296
523k
    const UnitScale& scale = unitScale[_blk.compID];
297
523k
    const Area scaledSelf  = scale.scale( _selfBlk );
298
523k
    const Area scaledBlk   = scale.scale(     _blk );
299
523k
    CodingUnit **cuPtr     = m_cuPtr[i] + rsAddr( scaledBlk.pos(), scaledSelf.pos(), scaledSelf.width );
300
301
523k
    CHECKD( *cuPtr, "Overwriting a pre-existing value, should be '0'!" );
302
303
523k
    g_pelBufOP.fillPtrMap( ( void** ) cuPtr, scaledSelf.width, scaledBlk.width, scaledBlk.height, ( void* ) cu );
304
523k
  }
305
306
523k
  return *cu;
307
523k
}
308
309
TransformUnit& CodingStructure::addTU( const UnitArea& unit, const ChannelType chType, CodingUnit* cu, TransformUnit* tuInit )
310
616k
{
311
616k
  TransformUnit* tu;
312
313
616k
  if( tuInit )
314
210k
  {
315
210k
    tu = tuInit;
316
210k
  }
317
406k
  else
318
406k
  {
319
406k
    if( m_unitCacheMutex ) m_unitCacheMutex->lock();
320
321
406k
    tu = m_tuCache.get();
322
323
406k
    if( m_unitCacheMutex ) m_unitCacheMutex->unlock();
324
325
406k
    tu->UnitArea::operator=( unit );
326
406k
    tu->initData();
327
406k
  }
328
329
616k
  tu->next   = nullptr;
330
616k
  tu->prev   = nullptr;
331
616k
  tu->cs     = this;
332
616k
  tu->cu     = cu;
333
616k
  tu->chType = chType;
334
335
616k
  TransformUnit *prevTU = m_numTUs > 0 ? tus.back() : nullptr;
336
337
616k
  if( prevTU && prevTU->cu == tu->cu )
338
21.8k
  {
339
21.8k
    prevTU->next = tu;
340
21.8k
    tu->prev     = prevTU;
341
21.8k
  }
342
343
616k
  tus.push_back( tu );
344
345
616k
  if( tu->cu )
346
545k
  {
347
545k
    if( tu->cu->firstTU == nullptr )
348
517k
    {
349
517k
      tu->cu->firstTU = tu;
350
517k
    }
351
545k
    tu->cu->lastTU = tu;
352
545k
  }
353
354
616k
  uint32_t idx = ++m_numTUs;
355
616k
  tu->idx = idx;
356
357
616k
  uint32_t numCh = getNumberValidComponents( area.chromaFormat );
358
359
  // the pointers go straight into the TU: TransformUnit::init reading a local array just written here would be a wide load after narrow stores, on a path this hot
360
2.46M
  for( uint32_t i = 0; i < numCh; i++ )
361
1.85M
  {
362
1.85M
    if( !tu->blocks[i].valid() )
363
969k
    {
364
969k
      tu->m_coeffs[i] = nullptr;
365
969k
      continue;
366
969k
    }
367
368
880k
    TCoeffSig* const coeffs = m_coeffs[i] + m_offsets[i];
369
370
880k
    unsigned areaSize = tu->blocks[i].area();
371
880k
    m_offsets[i] += areaSize;
372
373
880k
    const bool cpyRsi = tuInit &&
374
311k
                      ( tuInit->cbf[i] ||
375
254k
                 ( i && tuInit->jointCbCr && numCh > 1 && ( TU::getCbf( *tuInit, COMP_Cb ) || TU::getCbf( *tuInit, COMP_Cr ) ) )
376
311k
                      );
377
378
880k
    if( cpyRsi )
379
57.4k
      memcpy( coeffs, tu->m_coeffs[i], areaSize * sizeof( TCoeffSig ) );
380
381
880k
    tu->m_coeffs[i] = coeffs;
382
880k
  }
383
384
616k
  return *tu;
385
616k
}
386
387
void CodingStructure::addEmptyTUs( Partitioner &partitioner, CodingUnit* cu )
388
26.7k
{
389
26.7k
  const UnitArea& area    = partitioner.currArea();
390
26.7k
  bool            split   = partitioner.canSplit(TU_MAX_TR_SPLIT, *this);
391
26.7k
  const unsigned  trDepth = partitioner.currTrDepth;
392
393
26.7k
  if( split )
394
0
  {
395
0
    partitioner.splitCurrArea( TU_MAX_TR_SPLIT, *this );
396
0
    do
397
0
    {
398
0
      addEmptyTUs( partitioner, cu );
399
0
    } while( partitioner.nextPart( *this ) );
400
401
0
    partitioner.exitCurrSplit();
402
0
  }
403
26.7k
  else
404
26.7k
  {
405
26.7k
    TransformUnit& tu = addTU(CS::getArea(*this, area, partitioner.chType, TreeType(partitioner.treeType)), partitioner.chType, cu);
406
26.7k
    tu.depth = trDepth;
407
26.7k
  }
408
26.7k
}
409
410
CUTraverser CodingStructure::traverseCUs( const UnitArea& unit, const ChannelType effChType )
411
9.30k
{
412
  //  CHECK( _treeType != treeType, "not good");
413
9.30k
  CodingUnit* firstCU = getCU( isLuma( effChType ) ? unit.lumaPos() : unit.chromaPos(), effChType, TREE_D );
414
9.30k
  CodingUnit* lastCU = firstCU;
415
9.30k
  if( !CS::isDualITree( *this ) ) //for a more generalized separate tree
416
0
  {
417
0
    bool bContinue = true;
418
0
    CodingUnit* currCU = firstCU;
419
0
    while( bContinue )
420
0
    {
421
0
      if( currCU == nullptr )
422
0
      {
423
0
        bContinue = false;
424
0
        lastCU = currCU;
425
0
      }
426
0
      else if( currCU->chType != effChType )
427
0
      {
428
0
        lastCU = currCU;
429
0
        currCU = currCU->next;
430
0
      }
431
0
      else
432
0
      {
433
0
        if( unit.contains( *currCU ) )
434
0
        {
435
0
          lastCU = currCU;
436
0
          currCU = currCU->next;
437
0
        }
438
0
        else
439
0
        {
440
0
          bContinue = false;
441
0
          lastCU = currCU;
442
0
        }
443
0
      }
444
0
    }
445
0
  }
446
9.30k
  else
447
9.30k
  {
448
47.5k
  do { } while( lastCU && (0 != ( lastCU = lastCU->next )) && unit.contains( *lastCU ) );
449
9.30k
  }
450
451
9.30k
  return CUTraverser( firstCU, lastCU );
452
9.30k
}
453
454
TUTraverser CodingStructure::traverseTUs( const UnitArea& unit, const ChannelType effChType )
455
0
{
456
0
  TransformUnit* firstTU = getTU( isLuma( effChType ) ? unit.lumaPos() : unit.chromaPos(), effChType );
457
0
  TransformUnit* lastTU  = firstTU;
458
459
0
  do { } while( lastTU && (0 != ( lastTU = lastTU->next )) && unit.contains( *lastTU ) );
460
461
0
  return TUTraverser( firstTU, lastTU );
462
0
}
463
464
cCUTraverser CodingStructure::traverseCUs( const UnitArea& unit, const ChannelType effChType ) const
465
0
{
466
0
  const CodingUnit* firstCU = getCU( isLuma( effChType ) ? unit.lumaPos() : unit.chromaPos(), effChType, TREE_D );
467
0
  const CodingUnit* lastCU  = firstCU;
468
469
0
  do { } while( lastCU && (0 != ( lastCU = lastCU->next )) && unit.contains( *lastCU ) );
470
471
0
  return cCUTraverser( firstCU, lastCU );
472
0
}
473
474
cTUTraverser CodingStructure::traverseTUs( const UnitArea& unit, const ChannelType effChType ) const
475
0
{
476
0
  const TransformUnit* firstTU = getTU( isLuma( effChType ) ? unit.lumaPos() : unit.chromaPos(), effChType );
477
0
  const TransformUnit* lastTU  = firstTU;
478
479
0
  do { } while( lastTU && (0 != ( lastTU = lastTU->next )) && unit.contains( *lastTU ) );
480
481
0
  return cTUTraverser( firstTU, lastTU );
482
0
}
483
484
485
486
LFPBuf CodingStructure::getLoopFilterParamBuf(const DeblockEdgeDir& edgeDir)
487
9.30k
{
488
9.30k
  return LFPBuf(m_lfParam[edgeDir], m_mapSize[0]);
489
9.30k
}
490
491
const CLFPBuf CodingStructure::getLoopFilterParamBuf(const DeblockEdgeDir& edgeDir) const
492
0
{
493
0
  return CLFPBuf(m_lfParam[edgeDir], m_mapSize[0]);
494
0
}
495
496
497
// coding utilities
498
499
void CodingStructure::allocateVectorsAtPicLevel()
500
1.43k
{
501
1.43k
  const int  twice = ( !pcv->ISingleTree && slice->isIRAP() && pcv->chrFormat != CHROMA_400 ) ? 2 : 1;
502
1.43k
  size_t allocSize = twice * unitScale[0].scale( area.blocks[0].size() ).area();
503
504
1.43k
  cus.reserve( allocSize );
505
1.43k
  tus.reserve( allocSize );
506
1.43k
}
507
508
void CodingStructure::releaseVectorsAtPicLevel()
509
1.43k
{
510
1.43k
  CHECKD( !cus.empty() || !tus.empty(), "Clearing non-empty CU/TU vectors, risking a memory leak!" );
511
512
  // clear() keeps the capacity, which is one pointer per 4x4 block of the picture, so swap the
513
  // lists with empty ones to return it.
514
1.43k
  std::vector<CodingUnit*>().swap( cus );
515
1.43k
  std::vector<TransformUnit*>().swap( tus );
516
1.43k
}
517
518
519
520
void CodingStructure::createForSearch( const ChromaFormat _chromaFormat, const Area& _area )
521
166k
{
522
166k
  createInternals( UnitArea( _chromaFormat, _area ), false );
523
524
166k
  m_reco.create( area );
525
166k
  m_pred.create( area );
526
166k
  m_resi.create( area );
527
166k
}
528
529
void CodingStructure::createPicLevel( const UnitArea& _unit, const PreCalcValues* _pcv )
530
1.43k
{
531
1.43k
  pcv = _pcv;
532
533
1.43k
  createInternals( _unit, true );
534
1.43k
}
535
536
static constexpr int UnitScaleArray[NUM_CHROMA_FORMAT][MAX_NUM_COMP][2] =
537
{
538
  { {2,2}, {0,0}, {0,0} },  // 4:0:0
539
  { {2,2}, {1,1}, {1,1} },  // 4:2:0
540
  { {2,2}, {1,2}, {1,2} },  // 4:2:2
541
  { {2,2}, {2,2}, {2,2} }   // 4:4:4
542
};
543
544
void CodingStructure::createInternals( const UnitArea& _unit, const bool isTopLayer )
545
167k
{
546
167k
  area     = _unit;
547
167k
  _maxArea = _unit;
548
549
167k
  unitScale[COMP_Y ] = UnitScale( UnitScaleArray[area.chromaFormat][COMP_Y ][0], UnitScaleArray[area.chromaFormat][COMP_Y ][1] );
550
167k
  unitScale[COMP_Cb] = UnitScale( UnitScaleArray[area.chromaFormat][COMP_Cb][0], UnitScaleArray[area.chromaFormat][COMP_Cb][1] );
551
167k
  unitScale[COMP_Cr] = UnitScale( UnitScaleArray[area.chromaFormat][COMP_Cr][0], UnitScaleArray[area.chromaFormat][COMP_Cr][1] );
552
553
167k
  picture = nullptr;
554
167k
  parent  = nullptr;
555
167k
  lumaCS  = nullptr;
556
557
  // The picture-level structure gets its motion field when the picture's encoding starts (see
558
  // Picture::createEncodingBuffers), so pictures that wait in the GOP queue do not hold one.
559
167k
  if( isTopLayer )
560
1.43k
  {
561
1.43k
    motionLutBuf.resize( pps->getNumTileLineIds() );
562
1.43k
  }
563
166k
  else
564
166k
  {
565
166k
    createMotionBuf();
566
166k
    createCoeffs();
567
166k
    createTempBuffers( false );
568
166k
    initStructData( MAX_INT, false, nullptr );
569
166k
  }
570
167k
}
571
572
void CodingStructure::createTempBuffers( const bool isTopLayer )
573
167k
{
574
167k
  unsigned numCh = getNumberValidChannels( area.chromaFormat );
575
576
502k
  for( unsigned i = 0; i < numCh; i++ )
577
335k
  {
578
335k
    Size allocArea  = area.blocks[i].size();
579
335k
    m_mapSize[i]    = unitScale[i].scale(allocArea);
580
581
335k
    unsigned _area  = unitScale[i].scale( area.blocks[i].size() ).area();
582
583
335k
    m_cuPtr[i]      = _area > 0 ? new CodingUnit*    [_area] : nullptr;
584
335k
  }
585
586
167k
  clearCUs( true );
587
588
502k
  for( unsigned i = 0; i < NUM_EDGE_DIR; i++ )
589
335k
  {
590
335k
    m_lfParam[i] = ( isTopLayer && m_mapSize[0].area() > 0 ) ? ( LoopFilterParam* ) xMalloc( LoopFilterParam, m_mapSize[0].area() ) : nullptr;
591
335k
  }
592
593
167k
  unsigned _maxNumDmvrMvs = ( area.lwidth() >> 3 ) * ( area.lheight() >> 3 );
594
167k
  m_dmvrMvCache.resize( _maxNumDmvrMvs );
595
167k
}
596
597
void CodingStructure::destroyTempBuffers()
598
168k
{
599
506k
  for( uint32_t i = 0; i < MAX_NUM_CH; i++ )
600
337k
  {
601
337k
    delete[] m_cuPtr[i];
602
337k
    m_cuPtr[i] = nullptr;
603
337k
  }
604
605
506k
  for( int i = 0; i < NUM_EDGE_DIR; i++ )
606
337k
  {
607
337k
    xFree( m_lfParam[i] );
608
337k
    m_lfParam[i] = nullptr;
609
337k
  }
610
611
  // swap the contents of the vector so that memory released
612
168k
  std::vector<Mv>().swap( m_dmvrMvCache );
613
168k
  std::vector<CodingUnit*>().swap( cus );
614
168k
  std::vector<TransformUnit*>().swap( tus );
615
168k
}
616
617
void CodingStructure::addMiToLut( static_vector<HPMVInfo, MAX_NUM_HMVP_CANDS>& lut, const HPMVInfo& mi )
618
0
{
619
0
  size_t currCnt = lut.size();
620
621
0
  bool pruned      = false;
622
0
  int  sameCandIdx = 0;
623
624
0
  for( int idx = 0; idx < currCnt; idx++ )
625
0
  {
626
0
    if( lut[idx] == mi )
627
0
    {
628
0
      sameCandIdx = idx;
629
0
      pruned = true;
630
0
      break;
631
0
    }
632
0
  }
633
634
0
  if( pruned || currCnt == lut.capacity() )
635
0
  {
636
0
    lut.erase( lut.begin() + sameCandIdx );
637
0
  }
638
639
0
  lut.push_back(mi);
640
0
}
641
642
void CodingStructure::rebindPicBufs()
643
2.86k
{
644
2.86k
  CHECK( parent, "rebindPicBufs can only be used for the top level CodingStructure" );
645
646
2.86k
  if( !picture->m_picBufs[ PIC_RECONSTRUCTION ].bufs.empty() ) m_reco.createFromBuf( picture->m_picBufs[ PIC_RECONSTRUCTION ] );
647
1.43k
  else                                                         m_reco.destroy();
648
2.86k
  if( !picture->m_picBufs[ PIC_PREDICTION     ].bufs.empty() ) m_pred.createFromBuf( picture->m_picBufs[ PIC_PREDICTION ] );
649
2.86k
  else                                                         m_pred.destroy();
650
2.86k
  if( !picture->m_picBufs[ PIC_RESIDUAL       ].bufs.empty() ) m_resi.createFromBuf( picture->m_picBufs[ PIC_RESIDUAL ] );
651
2.86k
  else                                                         m_resi.destroy();
652
2.86k
}
653
654
void CodingStructure::createCoeffs()
655
167k
{
656
167k
  const unsigned numComp = getNumberValidComponents( area.chromaFormat );
657
670k
  for( unsigned i = 0; i < numComp; i++ )
658
502k
  {
659
502k
    unsigned _area = area.blocks[i].area();
660
502k
    m_coeffs[i] = _area > 0 ? ( TCoeffSig* ) xMalloc( TCoeffSig, _area ) : nullptr;
661
502k
  }
662
663
670k
  for( unsigned i = 0; i < numComp; i++ )
664
502k
  {
665
502k
    m_offsets[i] = 0;
666
502k
  }
667
167k
}
668
669
void CodingStructure::createMotionBuf()
670
167k
{
671
167k
  if( !m_motionBuf )
672
167k
  {
673
167k
    m_motionBuf = new MotionInfo[g_miScaling.scale( area.lumaSize() ).area()];
674
167k
  }
675
167k
}
676
677
void CodingStructure::destroyMotionBuf()
678
168k
{
679
168k
  destroyFullMotionBuf();
680
168k
  delete[] m_colocatedMotion;
681
168k
  m_colocatedMotion = nullptr;
682
168k
}
683
684
// Temporal motion vector prediction reads the motion of other pictures on this grid (see
685
// CU::getColocatedMVP), so the colocated field holds one motion per grid cell.
686
static constexpr int COL_MOTION_GRID_LOG2 = 3;
687
static constexpr int COL_MOTION_GRID      = 1 << COL_MOTION_GRID_LOG2;
688
static_assert( COL_MOTION_GRID == 4 * std::max<int>( 1, 4 * AMVP_DECIMATION_FACTOR / 4 ), "The grid must match CU::getColocatedMVP" );
689
690
void CodingStructure::createColocatedMotionBuf()
691
1.43k
{
692
1.43k
  CHECK( parent, "Only the picture-level coding structure has a colocated motion field" );
693
694
1.43k
  m_colocatedMotionStride = ( area.lwidth() + COL_MOTION_GRID - 1 ) >> COL_MOTION_GRID_LOG2;
695
1.43k
  if( !m_colocatedMotion )
696
1.43k
  {
697
1.43k
    const int heightOnGrid = ( area.lheight() + COL_MOTION_GRID - 1 ) >> COL_MOTION_GRID_LOG2;
698
1.43k
    m_colocatedMotion = new MotionInfo[m_colocatedMotionStride * heightOnGrid];
699
1.43k
  }
700
1.43k
}
701
702
MotionInfo& CodingStructure::colocatedMotionInfo( const Position& pos )
703
562k
{
704
562k
  CHECKD( ( pos.x | pos.y ) & ( COL_MOTION_GRID - 1 ), "Colocated motion lies on the grid only" );
705
562k
  return m_colocatedMotion[( pos.y >> COL_MOTION_GRID_LOG2 ) * m_colocatedMotionStride + ( pos.x >> COL_MOTION_GRID_LOG2 )];
706
562k
}
707
708
const MotionInfo& CodingStructure::getColocatedMotionInfo( const Position& pos ) const
709
0
{
710
0
  CHECKD( parent, "Colocated motion is read through the picture-level coding structure only" );
711
0
  CHECKD( ( pos.x | pos.y ) & ( COL_MOTION_GRID - 1 ), "Colocated motion is read on the grid only" );
712
0
  CHECKD( !m_colocatedMotion, "Colocated motion is read only when temporal motion vector prediction is enabled" );
713
0
  return m_colocatedMotion[( pos.y >> COL_MOTION_GRID_LOG2 ) * m_colocatedMotionStride + ( pos.x >> COL_MOTION_GRID_LOG2 )];
714
0
}
715
716
void CodingStructure::destroyCoeffs()
717
168k
{
718
675k
  for( uint32_t i = 0; i < MAX_NUM_COMP; i++ )
719
506k
  {
720
506k
    if( m_coeffs[i] ) { xFree( m_coeffs[i] ); m_coeffs[i] = nullptr; }
721
506k
  }
722
168k
}
723
724
void CodingStructure::initSubStructure( CodingStructure& subStruct, const ChannelType _chType, const UnitArea& subArea, const bool isTuEnc, PelStorage* pOrgBuffer )
725
450k
{
726
450k
  CHECK( this == &subStruct, "Trying to init self as sub-structure" );
727
728
450k
  subStruct.parent = this;
729
730
450k
  if( pOrgBuffer ) pOrgBuffer->compactResize( subArea );
731
732
450k
  subStruct.m_org    = (pOrgBuffer) ? pOrgBuffer : m_org;
733
450k
  subStruct.compactResize( subArea );
734
735
450k
  subStruct.costDbOffset = 0;
736
737
450k
  if( parent )
738
431k
  {
739
    // allow this to be false at the top level (need for edge CTU's)
740
431k
    CHECKD( !area.contains( subArea ), "Trying to init sub-structure not contained in the parent" );
741
431k
  }
742
743
450k
  subStruct.parent    = this;
744
450k
  subStruct.picture   = picture;
745
450k
  subStruct.lumaCS    = picture->cs;
746
747
450k
  subStruct.sps       = sps;
748
450k
  subStruct.vps       = vps;
749
450k
  subStruct.pps       = pps;
750
450k
  subStruct.picHeader = picHeader;
751
752
450k
  memcpy(subStruct.alfAps, alfAps, sizeof(alfAps));
753
754
450k
  subStruct.slice     = slice;
755
450k
  subStruct.baseQP    = baseQP;
756
450k
  subStruct.prevQP[_chType]
757
450k
                      = prevQP[_chType];
758
450k
  subStruct.pcv       = pcv;
759
760
450k
  subStruct.m_isTuEnc = isTuEnc;
761
762
450k
  if( nullptr == parent )
763
18.6k
  {
764
18.6k
    const int ctuPosX = subArea.lx() >> pcv->maxCUSizeLog2;
765
18.6k
    const int ctuPosY = subArea.ly() >> pcv->maxCUSizeLog2;
766
18.6k
    subStruct.motionLut = motionLutBuf[pps->getTileLineId( ctuPosX, ctuPosY )];
767
18.6k
  }
768
431k
  else
769
431k
  {
770
431k
    subStruct.motionLut = motionLut;
771
431k
  }
772
773
450k
  subStruct.initStructData( currQP[_chType] );
774
775
450k
  if( isTuEnc )
776
137k
  {
777
137k
    CHECKD( area != subStruct.area, "Trying to init sub-structure for TU-encoding of incompatible size" );
778
779
137k
    for( const auto &pcu : cus )
780
137k
    {
781
137k
      CodingUnit &cu = subStruct.addCU( *pcu, _chType );
782
783
137k
      cu = *pcu;
784
137k
    }
785
137k
  }
786
450k
}
787
788
void CodingStructure::useSubStructure( CodingStructure& subStruct, const ChannelType chType, const TreeType _treeType, const UnitArea& subArea, const bool cpyRecoToPic )
789
186k
{
790
186k
  UnitArea clippedArea = clipArea( subArea, *picture );
791
792
186k
  CPelUnitBuf subRecoBuf = static_cast<const CodingStructure&>( subStruct ).getRecoBuf( clippedArea ); // const overload: no conversion from PelUnitBuf
793
794
186k
  if( parent )
795
177k
  {
796
    // copy data to picture
797
177k
    getRecoBuf( clippedArea ).copyFrom( subRecoBuf );
798
177k
  }
799
800
186k
  if( cpyRecoToPic )
801
148k
  {
802
148k
    picture->getRecoBuf( clippedArea ).copyFrom( subRecoBuf );
803
148k
  }
804
805
186k
  if( !subStruct.m_isTuEnc && ( ( !slice->isIntra() || slice->sps->IBC ) && chType != CH_C ) )
806
62.4k
  {
807
    // copy motion buffer
808
62.4k
    MotionBuf ownMB  = getMotionBuf          ( clippedArea );
809
62.4k
    CMotionBuf subMB = subStruct.getMotionBuf( clippedArea );
810
811
62.4k
    ownMB.copyFrom( subMB );
812
813
62.4k
    if( nullptr == parent )
814
4.65k
    {
815
4.65k
      const int ctuPosX = subStruct.area.lx() >> pcv->maxCUSizeLog2;
816
4.65k
      const int ctuPosY = subStruct.area.ly() >> pcv->maxCUSizeLog2;
817
4.65k
      motionLutBuf[pps->getTileLineId( ctuPosX, ctuPosY )] = subStruct.motionLut;
818
4.65k
    }
819
57.8k
    else
820
57.8k
    {
821
57.8k
      motionLut = subStruct.motionLut;
822
57.8k
    }
823
62.4k
  }
824
825
186k
  fracBits += subStruct.fracBits;
826
186k
  dist     += subStruct.dist;
827
186k
  cost     += subStruct.cost;
828
186k
  costDbOffset += subStruct.costDbOffset;
829
830
186k
  if( parent )
831
177k
  {
832
    // allow this to be false at the top level
833
177k
    CHECKD( !area.contains( subArea ), "Trying to use a sub-structure not contained in self" );
834
177k
  }
835
836
  // copy the CUs over
837
186k
  if( subStruct.m_isTuEnc )
838
30.0k
  {
839
    // don't copy if the substruct was created for encoding of the TUs
840
30.0k
  }
841
156k
  else
842
156k
  {
843
156k
    if( &m_cuCache == &subStruct.m_cuCache )
844
147k
    {
845
      // copy the CUs over with taking ownership
846
147k
      for( const auto& pcu : subStruct.cus )
847
180k
      {
848
        // add an analogue CU into own CU store
849
180k
        const UnitArea& cuPatch = *pcu;
850
180k
        addCU( cuPatch, pcu->chType, pcu );
851
180k
      }
852
853
147k
      subStruct.cus.resize( 0 );
854
147k
    }
855
9.30k
    else
856
9.30k
    {
857
      // copy the CUs over
858
9.30k
      for( const auto& pcu : subStruct.cus )
859
47.6k
      {
860
        // add an analogue CU into own CU store
861
47.6k
        const UnitArea& cuPatch = *pcu;
862
863
47.6k
        CodingUnit& cu = addCU( cuPatch, pcu->chType );
864
865
        // copy the CU info from subPatch
866
47.6k
        cu = *pcu;
867
47.6k
      }
868
9.30k
    }
869
156k
  }
870
871
186k
  if( &m_tuCache == &subStruct.m_tuCache )
872
177k
  {
873
    // copy the TUs over with taking ownership
874
177k
    for( const auto& ptu : subStruct.tus )
875
210k
    {
876
      // add an analogue TU into own TU store
877
210k
      const UnitArea& tuPatch = *ptu;
878
210k
      addTU( tuPatch, ptu->chType, getCU( tuPatch.blocks[ptu->chType].pos(), ptu->chType, _treeType ), ptu );
879
210k
    }
880
881
177k
    subStruct.tus.resize( 0 );
882
177k
  }
883
9.30k
  else
884
9.30k
  {
885
    // copy the TUs over
886
9.30k
    for( const auto& ptu : subStruct.tus )
887
47.6k
    {
888
      // add an analogue TU into own TU store
889
47.6k
      const UnitArea& tuPatch = *ptu;
890
891
47.6k
      TransformUnit& tu = addTU( tuPatch, ptu->chType, getCU( tuPatch.blocks[ptu->chType], ptu->chType, _treeType ) );
892
893
      // copy the TU info from subPatch
894
47.6k
      tu = *ptu;
895
47.6k
    }
896
9.30k
  }
897
186k
}
898
899
void CodingStructure::copyStructure( const CodingStructure& other, const ChannelType chType, const TreeType _treeType, const bool copyTUs, const bool copyRecoBuf )
900
0
{
901
0
  fracBits      = other.fracBits;
902
0
  dist          = other.dist;
903
0
  cost          = other.cost;
904
0
  costDbOffset  = other.costDbOffset;
905
0
  CHECKD( area != other.area, "Incompatible sizes" );
906
907
0
  const UnitArea dualITreeArea = CS::getArea( *this, area, chType, _treeType );
908
909
  // copy the CUs over
910
0
  for (const auto &pcu : other.cus)
911
0
  {
912
0
    if( !dualITreeArea.contains( *pcu ) )
913
0
    {
914
0
      continue;
915
0
    }
916
    // add an analogue CU into own CU store
917
0
    const UnitArea& cuPatch = *pcu;
918
919
0
    CodingUnit &cu = addCU(cuPatch, pcu->chType);
920
921
    // copy the CU info from subPatch
922
0
    cu = *pcu;
923
0
  }
924
925
0
  if (!other.slice->isIntra() || other.slice->sps->IBC)
926
0
  {
927
    // copy motion buffer
928
0
    MotionBuf  ownMB = getMotionBuf();
929
0
    CMotionBuf subMB = other.getMotionBuf();
930
931
0
    ownMB.copyFrom( subMB );
932
933
0
    motionLut = other.motionLut;
934
0
  }
935
936
0
  if( copyTUs )
937
0
  {
938
    // copy the TUs over
939
0
    for( const auto &ptu : other.tus )
940
0
    {
941
0
      if( !dualITreeArea.contains( *ptu ) )
942
0
      {
943
0
        continue;
944
0
      }
945
      // add an analogue TU into own TU store
946
0
      const UnitArea& tuPatch = *ptu;
947
948
0
      TransformUnit& tu = addTU( tuPatch, ptu->chType, getCU( tuPatch.blocks[ptu->chType], ptu->chType, _treeType) );
949
950
      // copy the TU info from subPatch
951
0
      tu = *ptu;
952
0
    }
953
0
  }
954
955
0
  if( copyRecoBuf )
956
0
  {
957
0
    CPelUnitBuf recoBuf = other.getRecoBuf( area );
958
959
0
    if( parent )
960
0
    {
961
      // copy data to self for neighbors
962
0
      getRecoBuf( area ).copyFrom( recoBuf );
963
0
    }
964
965
    // copy data to picture
966
0
    picture->getRecoBuf( area ).copyFrom( recoBuf );
967
0
  }
968
0
}
969
970
void CodingStructure::compactResize( const UnitArea& _area )
971
510k
{
972
510k
  m_pred   .compactResize( _area );
973
510k
  m_reco   .compactResize( _area );
974
510k
  m_resi   .compactResize( _area );
975
976
2.04M
  for( uint32_t i = 0; i < _area.blocks.size(); i++ )
977
1.53M
  {
978
1.53M
    CHECK( _maxArea.blocks[i].area() < _area.blocks[i].area(), "Trying to init sub-structure of incompatible size" );
979
1.53M
  }
980
981
510k
  area = _area;
982
510k
}
983
984
void CodingStructure::initStructData( const int QP, const bool skipMotBuf, const UnitArea* _area )
985
1.09M
{
986
1.09M
  clearTUs( false );
987
1.09M
  clearCUs( false );
988
989
1.09M
  if( _area ) compactResize( *_area );
990
991
1.09M
  if( QP < MAX_INT )
992
853k
  {
993
853k
    currQP[0] = currQP[1] = QP;
994
853k
  }
995
996
1.09M
  if( !skipMotBuf && ( !parent || ( ( !slice->isIntra() || slice->sps->IBC ) && !m_isTuEnc ) ) )
997
265k
  {
998
265k
    getMotionBuf().memset( -1 );
999
265k
  }
1000
1001
1.09M
  m_dmvrMvCacheOffset = 0;
1002
1003
1.09M
  fracBits      = 0;
1004
1.09M
  dist          = 0;
1005
1.09M
  cost          = MAX_DOUBLE;
1006
1.09M
  lumaCost      = MAX_DOUBLE;
1007
1.09M
  costDbOffset  = 0;
1008
1.09M
  interHad      = MAX_DISTORTION;
1009
1.09M
}
1010
1011
1012
void CodingStructure::clearTUs( bool force )
1013
1.74M
{
1014
  // no TU was added since the last clear (or construction): nothing to reset
1015
1.74M
  if( !m_numTUs && !force ) return;
1016
1017
442k
  memset( m_offsets, 0, sizeof( m_offsets ) );
1018
1019
442k
  for( auto &pcu : cus )
1020
330k
  {
1021
330k
    pcu->firstTU = pcu->lastTU = nullptr;
1022
330k
  }
1023
1024
442k
  if ( m_unitCacheMutex ) m_unitCacheMutex->lock();
1025
442k
  m_tuCache.cache( tus );
1026
442k
  if ( m_unitCacheMutex ) m_unitCacheMutex->unlock();
1027
1028
442k
  m_numTUs = 0;
1029
442k
}
1030
1031
void CodingStructure::clearCUs( bool force )
1032
1.81M
{
1033
1.81M
  if( !m_numCUs && !force ) return;
1034
1035
544k
  int numCh = getNumberValidChannels( area.chromaFormat );
1036
1.63M
  for( int i = 0; i < numCh; i++ )
1037
1.08M
  {
1038
1.08M
    memset( m_cuPtr[i], 0, sizeof( *m_cuPtr[0] ) * unitScale[i].scaleArea( area.blocks[i].area() ) );
1039
1.08M
  }
1040
1041
544k
  if ( m_unitCacheMutex ) m_unitCacheMutex->lock();
1042
544k
  m_cuCache.cache( cus );
1043
544k
  if ( m_unitCacheMutex ) m_unitCacheMutex->unlock();
1044
1045
544k
  m_numCUs = 0;
1046
544k
}
1047
1048
MotionBuf CodingStructure::getMotionBuf( const Area& _area )
1049
417k
{
1050
417k
  const CompArea& _luma = area.Y();
1051
1052
417k
  CHECKD( !_luma.contains( _area ), "Trying to access motion information outside of this coding structure" );
1053
1054
417k
  const Area miArea   = g_miScaling.scale( _area );
1055
417k
  const Area selfArea = g_miScaling.scale( _luma );
1056
1057
417k
  return MotionBuf( m_motionBuf + rsAddr( miArea.pos(), selfArea.pos(), selfArea.width ), selfArea.width, miArea.size() );
1058
417k
}
1059
1060
const CMotionBuf CodingStructure::getMotionBuf( const Area& _area ) const
1061
0
{
1062
0
  const CompArea& _luma = area.Y();
1063
1064
0
  CHECKD( !_luma.contains( _area ), "Trying to access motion information outside of this coding structure" );
1065
1066
0
  const Area miArea   = g_miScaling.scale( _area );
1067
0
  const Area selfArea = g_miScaling.scale( _luma );
1068
1069
0
  return MotionBuf( m_motionBuf + rsAddr( miArea.pos(), selfArea.pos(), selfArea.width ), selfArea.width, miArea.size() );
1070
0
}
1071
1072
MotionInfo& CodingStructure::getMotionInfo( const Position& pos )
1073
562k
{
1074
562k
  CHECKD( !area.Y().contains( pos ), "Trying to access motion information outside of this coding structure" );
1075
1076
  // bypass the motion buf calling and get the value directly
1077
562k
  const unsigned stride = g_miScaling.scaleHor( area.lumaSize().width );
1078
562k
  const Position miPos  = g_miScaling.scale( pos - area.lumaPos() );
1079
1080
562k
  return *( m_motionBuf + miPos.y * stride + miPos.x );
1081
562k
}
1082
1083
const MotionInfo& CodingStructure::getMotionInfo( const Position& pos ) const
1084
0
{
1085
0
  CHECKD( !area.Y().contains( pos ), "Trying to access motion information outside of this coding structure" );
1086
1087
  // bypass the motion buf calling and get the value directly
1088
0
  const unsigned stride = g_miScaling.scaleHor( area.lumaSize().width );
1089
0
  const Position miPos  = g_miScaling.scale( pos - area.lumaPos() );
1090
1091
0
  return *( m_motionBuf + miPos.y * stride + miPos.x );
1092
0
}
1093
1094
PelUnitBuf CodingStructure::getBuf( const UnitArea& unit, const PictureType type )
1095
385k
{
1096
  // no parent fetching for buffers
1097
385k
  if( area.chromaFormat == CHROMA_400 )
1098
0
  {
1099
0
    return PelUnitBuf( area.chromaFormat, getBuf( unit.Y(), type ) );
1100
0
  }
1101
385k
  else
1102
385k
  {
1103
385k
    return PelUnitBuf( area.chromaFormat, getBuf( unit.Y(), type ), getBuf( unit.Cb(), type ), getBuf( unit.Cr(), type ) );
1104
385k
  }
1105
385k
}
1106
1107
const CPelUnitBuf CodingStructure::getBuf( const UnitArea& unit, const PictureType type ) const
1108
186k
{
1109
  // no parent fetching for buffers
1110
186k
  if( area.chromaFormat == CHROMA_400 )
1111
0
  {
1112
0
    return CPelUnitBuf( area.chromaFormat, getBuf( unit.Y(), type ) );
1113
0
  }
1114
186k
  else
1115
186k
  {
1116
186k
    return CPelUnitBuf( area.chromaFormat, getBuf( unit.Y(), type ), getBuf( unit.Cb(), type ), getBuf( unit.Cr(), type ) );
1117
186k
  }
1118
186k
}
1119
1120
const CodingUnit* CodingStructure::getCURestricted( const Position& pos, const CodingUnit& curCu, const ChannelType _chType ) const
1121
2.38M
{
1122
2.38M
  const int csx    = getChannelTypeScaleX( _chType, area.chromaFormat );
1123
2.38M
  const int csy    = getChannelTypeScaleY( _chType, area.chromaFormat );
1124
2.38M
  const int xshift = pcv->maxCUSizeLog2 - csx;
1125
2.38M
  const int yshift = pcv->maxCUSizeLog2 - csy;
1126
2.38M
  const int ydiff  = ( pos.y >> yshift ) - ( curCu.blocks[_chType].y >> yshift );
1127
2.38M
  const int xdiff  = ( pos.x >> xshift ) - ( curCu.blocks[_chType].x >> xshift );
1128
1129
2.38M
  if( !xdiff && !ydiff )
1130
1.01M
  {
1131
1.01M
    const CodingUnit* cu = getCU( pos, _chType, curCu.treeType );
1132
1133
1.01M
    return ( cu && ( cu->cs != curCu.cs || cu->idx <= curCu.idx ) ) ? cu : nullptr;
1134
1.01M
  }
1135
1136
1.36M
  if( ydiff > 0 || ( ydiff == 0 && xdiff > 0 ) || ( ydiff == -1 && xdiff > ( sps->entropyCodingSyncEnabled ? 0 : 1 ) ) )
1137
37.1k
    return nullptr;
1138
1139
1.32M
  if( pos.x < 0 || pos.y < 0 || ( pos.x * ( 1 << csx ) ) >= pcv->lumaWidth || pps->getTileIdx( pos.x >> xshift, pos.y >> yshift ) != curCu.tileIdx ) return nullptr;
1140
1141
335k
  const CodingUnit* cu = getCU( pos, _chType, curCu.treeType );
1142
1143
335k
  return ( cu && CU::isSameSlice( *cu, curCu ) ) ? cu : nullptr;
1144
1.32M
}
1145
1146
const CodingUnit *CodingStructure::getCURestricted( const Position &pos, const Position curPos, const unsigned curSliceIdx, const unsigned curTileIdx, const ChannelType _chType, const TreeType _treeType ) const
1147
2.41M
{
1148
2.41M
  const int csx    = getChannelTypeScaleX( _chType, area.chromaFormat );
1149
2.41M
  const int csy    = getChannelTypeScaleY( _chType, area.chromaFormat );
1150
2.41M
  const int xshift = pcv->maxCUSizeLog2 - csx;
1151
2.41M
  const int yshift = pcv->maxCUSizeLog2 - csy;
1152
2.41M
  const int ydiff  = ( pos.y >> yshift ) - ( curPos.y >> yshift );
1153
2.41M
  const int xdiff  = ( pos.x >> xshift ) - ( curPos.x >> xshift );
1154
1155
2.41M
  if( !xdiff && !ydiff )
1156
669k
  {
1157
669k
    return getCU( pos, _chType, _treeType );
1158
669k
  }
1159
1160
1.74M
  if( ydiff > 0 || ( ydiff == 0 && xdiff > 0 ) || ( ydiff == -1 && xdiff > ( sps->entropyCodingSyncEnabled ? 0 : 1 ) ) )
1161
39.9k
    return nullptr;
1162
1163
1.70M
  if( pos.x < 0 || pos.y < 0 || ( pos.x << csx ) >= pcv->lumaWidth || pps->getTileIdx( pos.x >> xshift, pos.y >> yshift ) != curTileIdx ) return nullptr;
1164
1165
736k
  const CodingUnit* cu = getCU( pos, _chType, _treeType );
1166
1167
736k
  return ( cu && cu->slice->independentSliceIdx == curSliceIdx && cu->tileIdx == curTileIdx ) ? cu : nullptr;
1168
1.70M
}
1169
1170
const TransformUnit* CodingStructure::getTURestricted( const Position& pos, const TransformUnit& curTu, const ChannelType _chType ) const
1171
0
{
1172
0
  if( sps->entropyCodingSyncEnabled )
1173
0
  {
1174
0
    const int xshift = pcv->maxCUSizeLog2 - getChannelTypeScaleX( _chType, curTu.chromaFormat );
1175
0
    const int yshift = pcv->maxCUSizeLog2 - getChannelTypeScaleY( _chType, curTu.chromaFormat );
1176
0
    if( (pos.x >> xshift) > (curTu.blocks[_chType].x >> xshift) || (pos.y >> yshift) > (curTu.blocks[_chType].y >> yshift) )
1177
0
      return nullptr;
1178
0
  }
1179
0
  const TransformUnit* tu = getTU( pos, _chType );
1180
0
  return ( tu && CU::isSameSliceAndTile( *tu->cu, *curTu.cu ) && ( tu->cs != curTu.cs || tu->idx <= curTu.idx ) ) ? tu : nullptr;
1181
0
}
1182
1183
} // namespace vvenc
1184
1185
//! \}
1186