Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvdec/source/Lib/CommonLib/PicListManager.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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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
#include "PicListManager.h"
44
45
#include "Picture.h"
46
47
#ifndef DEBUG_PIC_ORDER
48
#  define DEBUG_PIC_ORDER 0
49
#endif
50
#if DEBUG_PIC_ORDER
51
#  define IF_DEBUG_PIC_ORDER( ... ) __VA_ARGS__
52
#else
53
#  define IF_DEBUG_PIC_ORDER( ... )
54
#endif
55
56
namespace vvdec
57
{
58
static std::ostream& operator<<( std::ostream& strm, PicList& picList )
59
0
{
60
0
  for( auto& p: picList )
61
0
  {
62
0
    char stateC = ' ';
63
0
    switch (p->progress) {
64
0
    case Picture::init:           stateC = ' '; break;
65
0
    case Picture::parsing:        stateC = 'p'; break;
66
0
    case Picture::parsed:         stateC = '.'; break;
67
0
    case Picture::reconstructing: stateC = 'x'; break;
68
0
    case Picture::reconstructed:
69
0
    case Picture::finished:       stateC = 'X';
70
0
      if( !p->neededForOutput )
71
0
      {
72
0
        stateC = 'o';
73
0
        if( p->lockedByApplication )   stateC = 'L';
74
0
        else if( p->dpbReferenceMark ) stateC = 'R';
75
0
      }
76
0
    }
77
0
    strm << p->poc << stateC << ' ';
78
0
  }
79
0
80
0
  return strm;
81
0
}
82
83
static std::ostream& operator<<( std::ostream& strm, PicListRange& picRange )
84
0
{
85
0
  for( auto& p: picRange )
86
0
  {
87
0
    strm << p->poc << ( p->progress >= Picture::finished ? "x" : " " ) << " ";
88
0
  }
89
0
  return strm;
90
0
}
91
92
static bool isIDR( NalUnitType type )
93
6
{
94
6
  return type == NAL_UNIT_CODED_SLICE_IDR_W_RADL || type == NAL_UNIT_CODED_SLICE_IDR_N_LP;
95
6
}
96
static bool isIDR( const Picture* pic )
97
6
{
98
6
  return isIDR( pic->eNalUnitType );
99
6
}
100
101
void PicListManager::deleteBuffers()
102
2.80k
{
103
2.80k
  for( auto& pcPic: m_cPicList )
104
714
  {
105
714
    pcPic->destroy();
106
107
714
    delete pcPic;
108
714
    pcPic = NULL;
109
714
  }
110
2.80k
  m_cPicList.clear();
111
2.80k
}
112
113
void PicListManager::create(int frameDelay, int decInstances, const UserAllocator& userAllocator )
114
935
{
115
935
  m_parseFrameDelay      = frameDelay;
116
935
  m_parallelDecInst      = decInstances;
117
935
  m_userAllocator        = userAllocator;
118
935
}
119
120
Picture* PicListManager::getNewPicBuffer( const SPS& sps, const PPS& pps, const uint32_t temporalLayer, const int layerId, const VPS* vps )
121
714
{
122
714
  CHECK_FATAL( m_parseFrameDelay < 0, "Parser frame delay is invalid" );
123
124
714
  Picture*  pcPic         = nullptr;
125
714
  const int iMaxRefPicNum = ( vps == nullptr || vps->m_numLayersInOls[vps->m_iTargetLayer] == 1 )
126
714
                              ? sps.getMaxDecPicBuffering( temporalLayer ) + 1
127
714
                              : vps->getMaxDecPicBuffering( temporalLayer );   // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
128
714
  const unsigned int picMargin = 16 + sps.getMaxCUWidth();
129
130
714
  bool externAllocator = m_userAllocator.enabled;
131
714
  if ( externAllocator )
132
0
  {
133
0
    if (sps.getBitDepth() == 8 )
134
0
    {
135
0
      externAllocator = false;
136
0
    }
137
0
  }
138
714
  UserAllocator* userAllocator = externAllocator ? &m_userAllocator : nullptr;
139
140
714
  if( m_cPicList.size() < (uint32_t)iMaxRefPicNum + m_parseFrameDelay )
141
714
  {
142
714
    pcPic = new Picture();
143
714
    pcPic->create( sps.getChromaFormatIdc(),
144
714
                   Size( pps.getPicWidthInLumaSamples(), pps.getPicHeightInLumaSamples() ),
145
714
                   sps.getMaxCUWidth(),
146
714
                   picMargin,
147
714
                   layerId,
148
714
                   userAllocator );
149
714
    pcPic->createWrapAroundBuf( sps.getUseWrapAround(), sps.getMaxCUWidth() );
150
714
    m_cPicList.push_back( pcPic );
151
152
714
    return pcPic;
153
714
  }
154
155
0
  for( PicList::iterator itPic = m_cPicList.begin(); itPic != m_cPicList.end(); ++itPic )
156
0
  {
157
0
    Picture* pic = *itPic;
158
0
    if( pic->progress < Picture::reconstructed || pic->stillReferenced || pic->dpbReferenceMark || pic->neededForOutput || pic->lockedByApplication )
159
0
    {
160
0
      continue;
161
0
    }
162
163
0
    if ( externAllocator )
164
0
    {
165
0
      pic->destroy();
166
0
      pic->create( sps.getChromaFormatIdc(),
167
0
                   Size( pps.getPicWidthInLumaSamples(), pps.getPicHeightInLumaSamples() ),
168
0
                   sps.getMaxCUWidth(),
169
0
                   picMargin,
170
0
                   layerId,
171
0
                   userAllocator );
172
0
      pic->createWrapAroundBuf( sps.getUseWrapAround(), sps.getMaxCUWidth() );
173
0
      pic->resetForUse( layerId );
174
0
    }
175
176
    // take the picture to be reused and move it to the end of the list
177
0
    move_to_end( itPic, m_cPicList );
178
179
0
    pcPic = pic;
180
181
0
    if ( externAllocator )
182
0
    {
183
0
      return pcPic;
184
0
    }
185
186
0
    break;
187
0
  }
188
189
0
  if( !pcPic )
190
0
  {
191
    // There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
192
0
    pcPic = new Picture();
193
0
    pcPic->create( sps.getChromaFormatIdc(),
194
0
                   Size( pps.getPicWidthInLumaSamples(), pps.getPicHeightInLumaSamples() ),
195
0
                   sps.getMaxCUWidth(),
196
0
                   picMargin,
197
0
                   layerId,
198
0
                   userAllocator );
199
0
    pcPic->createWrapAroundBuf( sps.getUseWrapAround(), sps.getMaxCUWidth() );
200
0
    m_cPicList.push_back( pcPic );
201
202
0
    return pcPic;
203
0
  }
204
205
0
  if( pcPic->lumaSize() != Size( pps.getPicWidthInLumaSamples(), pps.getPicHeightInLumaSamples() )
206
0
      || pcPic->cs->pcv->maxCUWidth != sps.getMaxCUWidth()
207
0
      || pcPic->cs->pcv->maxCUHeight != sps.getMaxCUHeight()
208
0
      || pcPic->layerId != layerId
209
0
      || pcPic->margin != picMargin
210
0
  )
211
0
  {
212
0
    pcPic->destroy();
213
0
    pcPic->create( sps.getChromaFormatIdc(),
214
0
                   Size( pps.getPicWidthInLumaSamples(), pps.getPicHeightInLumaSamples() ),
215
0
                   sps.getMaxCUWidth(),
216
0
                   picMargin,
217
0
                   layerId,
218
0
                   userAllocator );
219
0
    pcPic->createWrapAroundBuf( sps.getUseWrapAround(), sps.getMaxCUWidth() );
220
0
  }
221
222
0
  pcPic->resetForUse( layerId );
223
224
0
  return pcPic;
225
0
}
226
227
void PicListManager::markUnusedPicturesReusable()
228
3
{
229
  // collect all pictures, that are still referenced by an in-progress picture
230
3
  m_allRefPics.clear();
231
3
  m_allRefPics.reserve( m_cPicList.size() );
232
3
  for( auto& pic: m_cPicList )
233
3
  {
234
3
    if( pic->progress >= Picture::reconstructed )
235
3
    {
236
3
      continue;
237
3
    }
238
239
0
    for( auto& s: pic->slices )
240
0
    {
241
0
      for( auto l: { REF_PIC_LIST_0, REF_PIC_LIST_1 } )
242
0
      {
243
0
        for( int iRefIdx = 0; iRefIdx < s->getNumRefIdx( l ); iRefIdx++ )
244
0
        {
245
0
          const Picture* refPic = s->getRefPic( l, iRefIdx );
246
0
          m_allRefPics.insert( refPic );
247
0
        }
248
0
      }
249
0
    }
250
0
  }
251
252
  // remove stillReferenced flag from all others
253
3
  for( auto& pic: m_cPicList )
254
3
  {
255
3
    if( pic->progress < Picture::finished                                   // only unmark pictures up to the first unfinished pic
256
3
        && !( pic->progress == Picture::reconstructed && pic->wasLost ) )   // unless that is not a filled-in lost picture
257
3
    {
258
3
      break;
259
3
    }
260
261
0
    if( pic->stillReferenced && !pic->dpbReferenceMark && m_allRefPics.count( pic ) == 0 )
262
0
    {
263
0
      pic->stillReferenced = false;
264
0
      pic->m_subPicRefBufs.clear();
265
0
    }
266
0
  }
267
3
}
268
269
Picture* PicListManager::findClosestPic( int iLostPoc )
270
0
{
271
0
  int      closestPoc = INT32_MAX;
272
0
  Picture* closestPic = nullptr;
273
0
  for( auto& rpcPic: m_cPicList )
274
0
  {
275
0
    if( rpcPic->progress >= Picture::reconstructed && abs( rpcPic->getPOC() - iLostPoc ) < closestPoc
276
0
        && abs( rpcPic->getPOC() - iLostPoc ) != 0 )
277
0
    {
278
0
      closestPoc = abs( rpcPic->getPOC() - iLostPoc );
279
0
      closestPic = rpcPic;
280
0
    }
281
0
  }
282
0
  return closestPic;
283
0
}
284
285
Picture* PicListManager::getNextOutputPic( uint32_t numReorderPicsHighestTid,
286
                                           uint32_t maxDecPicBufferingHighestTid,
287
                                           bool     bFlush )
288
1.44k
{
289
1.44k
  (void)maxDecPicBufferingHighestTid; // unused
290
291
1.44k
  if( m_cPicList.empty() )
292
0
  {
293
0
    return nullptr;
294
0
  }
295
296
  // find picture range up to the first random access point
297
1.44k
  auto seqStart = m_cPicList.cbegin();
298
1.44k
  auto seqEnd   = m_cPicList.cend();
299
300
1.44k
  bool foundOutputPic = false;
301
1.45k
  for( auto itPic = seqStart; itPic != m_cPicList.cend(); ++itPic )
302
1.44k
  {
303
1.44k
    if( !( *itPic )->neededForOutput && ( *itPic )->progress >= Picture::reconstructed )
304
9
    {
305
9
      continue;
306
9
    }
307
308
1.43k
    if( ( *itPic )->progress < Picture::finished )
309
1.42k
    {
310
1.42k
      seqEnd = itPic;
311
1.42k
      break;
312
1.42k
    }
313
314
6
    if( isIDR( *itPic ) && !(*itPic)->getMixedNaluTypesInPicFlag())
315
6
    {
316
6
      if( !foundOutputPic ) // if there was no picture needed for output before the first RAP,
317
6
      {                     // we begin the range at the RAP...
318
6
        seqStart = itPic;
319
6
      }
320
0
      else                  // ...otherwise it ends at the RAP
321
0
      {
322
0
        seqEnd = itPic;
323
0
        break;
324
0
      }
325
6
    }
326
327
6
    foundOutputPic |= (*itPic)->neededForOutput;
328
329
    // ignore pictures, that are not needed for output or referenced any more
330
6
    if( !foundOutputPic && !(*itPic)->dpbReferenceMark )
331
0
    {
332
0
      seqStart = itPic;
333
0
    }
334
6
  }
335
1.44k
  if( !foundOutputPic )
336
1.43k
  {
337
1.43k
    return nullptr;
338
1.43k
  }
339
340
6
  PicListRange picRange{ seqStart, seqEnd };
341
342
6
  IF_DEBUG_PIC_ORDER( std::cout << "list:  " << m_cPicList << std::flush );
343
//  IF_DEBUG_PIC_ORDER( std::cout << std::endl << "range: " << picRange << std::flush );
344
345
6
  if( m_tuneInDelay <= numReorderPicsHighestTid + m_parallelDecInst + 1 && !bFlush )
346
3
  {
347
3
    IF_DEBUG_PIC_ORDER( std::cout << std::endl );
348
3
    ++m_tuneInDelay;
349
3
    return nullptr;
350
3
  }
351
352
  // when there is an IDR picture coming up, we can flush all pictures before that
353
3
  if( seqEnd != m_cPicList.cend() && isIDR( *seqEnd ) && !(*seqEnd)->getMixedNaluTypesInPicFlag() )
354
0
  {
355
0
    bFlush = true;
356
0
    IF_DEBUG_PIC_ORDER( std::cout << " flush" );
357
0
  }
358
359
3
  unsigned numPicsNotYetDisplayed = 0;
360
3
  unsigned dpbFullness            = 0;
361
3
  if( !bFlush )
362
0
  {
363
0
    for( auto& pcPic: picRange )
364
0
    {
365
0
      if( pcPic->neededForOutput && pcPic->progress >= Picture::finished )
366
0
      {
367
0
        numPicsNotYetDisplayed++;
368
0
        dpbFullness++;
369
0
      }
370
0
      else if( pcPic->dpbReferenceMark && pcPic->progress >= Picture::finished )
371
0
      {
372
0
        dpbFullness++;
373
0
      }
374
0
    }
375
0
  }
376
377
3
  IF_DEBUG_PIC_ORDER( std::cout << "   " << numPicsNotYetDisplayed << '/' << numReorderPicsHighestTid << " "<< dpbFullness << '/' << maxDecPicBufferingHighestTid << "   " );
378
3
  (void)dpbFullness;
379
380
3
  Picture * lowestPOCPic = nullptr;
381
3
  if( numPicsNotYetDisplayed > numReorderPicsHighestTid + ( m_firstOutputPic ? MAX_OUT_OF_ORDER_PICS : 0 )
382
//      || dpbFullness > maxDecPicBufferingHighestTid
383
3
      || bFlush )
384
3
  {
385
386
3
    for( auto& pcPic: picRange )
387
3
    {
388
      //CHECK( pcPic->fieldPic, "Interlaced not suported" );
389
390
3
      if( pcPic->neededForOutput && pcPic->progress >= Picture::finished &&
391
3
          ( lowestPOCPic==nullptr || pcPic->poc < lowestPOCPic->poc ) )
392
3
      {
393
3
        lowestPOCPic = pcPic;
394
3
      }
395
3
    }
396
3
  }
397
398
3
  if( lowestPOCPic )
399
3
  {
400
3
    m_firstOutputPic                  = false;
401
3
    lowestPOCPic->lockedByApplication = true;
402
3
    lowestPOCPic->neededForOutput     = false;
403
404
3
    IF_DEBUG_PIC_ORDER( std::cout << " ==> " << lowestPOCPic->poc );
405
3
  }
406
3
  IF_DEBUG_PIC_ORDER( std::cout << std::endl );
407
408
3
  return lowestPOCPic;
409
6
}
410
411
void PicListManager::releasePicture( Picture* pic )
412
713
{
413
713
  if( pic )
414
3
  {
415
3
    pic->lockedByApplication = false;
416
3
  }
417
713
}
418
419
}   // namespace vvdec