Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvdec/source/Lib/CommonLib/ParameterSetManager.h
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
#pragma once
44
45
#include "CommonDef.h"
46
#include "Slice.h"
47
48
#include <algorithm>
49
#include <map>
50
#include <vector>
51
#include <array>
52
53
namespace vvdec
54
{
55
56
template<class T, int MAX_ID>
57
class ParameterSetMap
58
{
59
public:
60
  struct MapData
61
  {
62
    AlignedByteVec     cNaluData;   // Can be empty
63
    std::shared_ptr<T> parameterSet;
64
    bool               bChanged = false;
65
  };
66
67
  ~ParameterSetMap()
68
4.72k
  {
69
4.72k
    m_paramsetMap.clear();
70
4.72k
    m_lastActiveParameterSet.reset();
71
4.72k
  }
vvdec::ParameterSetMap<vvdec::APS, 64>::~ParameterSetMap()
Line
Count
Source
68
1.18k
  {
69
1.18k
    m_paramsetMap.clear();
70
1.18k
    m_lastActiveParameterSet.reset();
71
1.18k
  }
vvdec::ParameterSetMap<vvdec::PPS, 64>::~ParameterSetMap()
Line
Count
Source
68
1.18k
  {
69
1.18k
    m_paramsetMap.clear();
70
1.18k
    m_lastActiveParameterSet.reset();
71
1.18k
  }
vvdec::ParameterSetMap<vvdec::SPS, 16>::~ParameterSetMap()
Line
Count
Source
68
1.18k
  {
69
1.18k
    m_paramsetMap.clear();
70
1.18k
    m_lastActiveParameterSet.reset();
71
1.18k
  }
vvdec::ParameterSetMap<vvdec::VPS, 16>::~ParameterSetMap()
Line
Count
Source
68
1.18k
  {
69
1.18k
    m_paramsetMap.clear();
70
1.18k
    m_lastActiveParameterSet.reset();
71
1.18k
  }
72
73
  //! \returns true, if no parameter set with this id was stored before or its NAL unit data differs
74
  bool storePS( int psId, T* ps, AlignedByteVec&& cNaluData )
75
29
  {
76
29
    CHECK( psId >= MAX_ID, "Invalid PS id" );
77
29
    bool contentChanged = true;
78
29
    if( m_paramsetMap.find( psId ) != m_paramsetMap.end() )
79
0
    {
80
0
      MapData& mapData   = m_paramsetMap[psId];
81
82
      // work out changed flag, also on the replaced one: a picture being parsed still uses it
83
0
      contentChanged = mapData.cNaluData != cNaluData;
84
0
      mapData.parameterSet->m_changedFlag |= contentChanged;
85
0
      const bool changed = mapData.parameterSet->m_changedFlag;
86
87
      // Don't throw away identical parameter sets as VTM does:
88
      //   The PPS can be identical to a previous one, but the SPS changed, so it needs to be interpreted differently.
89
      //
90
      // if( !mapData.parameterSet->m_changedFlag )
91
      // {
92
      //   // just keep the old one
93
      //   delete ps;
94
      //   return;
95
      // }
96
97
0
      if( find( m_activePsId.begin(), m_activePsId.end(), psId ) != m_activePsId.end() )
98
0
      {
99
0
        std::swap( m_paramsetMap[psId].parameterSet, m_lastActiveParameterSet );
100
0
      }
101
0
      m_paramsetMap[psId].cNaluData.clear();
102
0
      m_paramsetMap[psId].parameterSet.reset(ps);
103
0
      ps->m_changedFlag = changed;
104
0
    }
105
29
    else
106
29
    {
107
29
      m_paramsetMap[psId].parameterSet.reset(ps);
108
29
      ps->m_changedFlag = false;
109
29
    }
110
111
29
    if( !cNaluData.empty() )
112
29
    {
113
29
      m_paramsetMap[psId].cNaluData = std::move( cNaluData );
114
29
    }
115
0
    else
116
0
    {
117
0
      m_paramsetMap[psId].cNaluData.clear();
118
0
    }
119
29
    return contentChanged;
120
29
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::storePS(int, vvdec::VPS*, std::__1::vector<unsigned char, vvdec::AlignedAllocator<unsigned char> >&&)
vvdec::ParameterSetMap<vvdec::SPS, 16>::storePS(int, vvdec::SPS*, std::__1::vector<unsigned char, vvdec::AlignedAllocator<unsigned char> >&&)
Line
Count
Source
75
14
  {
76
14
    CHECK( psId >= MAX_ID, "Invalid PS id" );
77
14
    bool contentChanged = true;
78
14
    if( m_paramsetMap.find( psId ) != m_paramsetMap.end() )
79
0
    {
80
0
      MapData& mapData   = m_paramsetMap[psId];
81
82
      // work out changed flag, also on the replaced one: a picture being parsed still uses it
83
0
      contentChanged = mapData.cNaluData != cNaluData;
84
0
      mapData.parameterSet->m_changedFlag |= contentChanged;
85
0
      const bool changed = mapData.parameterSet->m_changedFlag;
86
87
      // Don't throw away identical parameter sets as VTM does:
88
      //   The PPS can be identical to a previous one, but the SPS changed, so it needs to be interpreted differently.
89
      //
90
      // if( !mapData.parameterSet->m_changedFlag )
91
      // {
92
      //   // just keep the old one
93
      //   delete ps;
94
      //   return;
95
      // }
96
97
0
      if( find( m_activePsId.begin(), m_activePsId.end(), psId ) != m_activePsId.end() )
98
0
      {
99
0
        std::swap( m_paramsetMap[psId].parameterSet, m_lastActiveParameterSet );
100
0
      }
101
0
      m_paramsetMap[psId].cNaluData.clear();
102
0
      m_paramsetMap[psId].parameterSet.reset(ps);
103
0
      ps->m_changedFlag = changed;
104
0
    }
105
14
    else
106
14
    {
107
14
      m_paramsetMap[psId].parameterSet.reset(ps);
108
14
      ps->m_changedFlag = false;
109
14
    }
110
111
14
    if( !cNaluData.empty() )
112
14
    {
113
14
      m_paramsetMap[psId].cNaluData = std::move( cNaluData );
114
14
    }
115
0
    else
116
0
    {
117
0
      m_paramsetMap[psId].cNaluData.clear();
118
0
    }
119
14
    return contentChanged;
120
14
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::PPS, 64>::storePS(int, vvdec::PPS*, std::__1::vector<unsigned char, vvdec::AlignedAllocator<unsigned char> >&&)
vvdec::ParameterSetMap<vvdec::APS, 64>::storePS(int, vvdec::APS*, std::__1::vector<unsigned char, vvdec::AlignedAllocator<unsigned char> >&&)
Line
Count
Source
75
15
  {
76
15
    CHECK( psId >= MAX_ID, "Invalid PS id" );
77
15
    bool contentChanged = true;
78
15
    if( m_paramsetMap.find( psId ) != m_paramsetMap.end() )
79
0
    {
80
0
      MapData& mapData   = m_paramsetMap[psId];
81
82
      // work out changed flag, also on the replaced one: a picture being parsed still uses it
83
0
      contentChanged = mapData.cNaluData != cNaluData;
84
0
      mapData.parameterSet->m_changedFlag |= contentChanged;
85
0
      const bool changed = mapData.parameterSet->m_changedFlag;
86
87
      // Don't throw away identical parameter sets as VTM does:
88
      //   The PPS can be identical to a previous one, but the SPS changed, so it needs to be interpreted differently.
89
      //
90
      // if( !mapData.parameterSet->m_changedFlag )
91
      // {
92
      //   // just keep the old one
93
      //   delete ps;
94
      //   return;
95
      // }
96
97
0
      if( find( m_activePsId.begin(), m_activePsId.end(), psId ) != m_activePsId.end() )
98
0
      {
99
0
        std::swap( m_paramsetMap[psId].parameterSet, m_lastActiveParameterSet );
100
0
      }
101
0
      m_paramsetMap[psId].cNaluData.clear();
102
0
      m_paramsetMap[psId].parameterSet.reset(ps);
103
0
      ps->m_changedFlag = changed;
104
0
    }
105
15
    else
106
15
    {
107
15
      m_paramsetMap[psId].parameterSet.reset(ps);
108
15
      ps->m_changedFlag = false;
109
15
    }
110
111
15
    if( !cNaluData.empty() )
112
15
    {
113
15
      m_paramsetMap[psId].cNaluData = std::move( cNaluData );
114
15
    }
115
0
    else
116
0
    {
117
0
      m_paramsetMap[psId].cNaluData.clear();
118
0
    }
119
15
    return contentChanged;
120
15
  }
121
122
  T* getPS_nothrow( int psId )
123
6
  {
124
6
    auto it = m_paramsetMap.find( psId );
125
6
    return it == m_paramsetMap.end() ? nullptr : it->second.parameterSet.get();
126
6
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::SPS, 16>::getPS_nothrow(int)
vvdec::ParameterSetMap<vvdec::PPS, 64>::getPS_nothrow(int)
Line
Count
Source
123
6
  {
124
6
    auto it = m_paramsetMap.find( psId );
125
6
    return it == m_paramsetMap.end() ? nullptr : it->second.parameterSet.get();
126
6
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::getPS_nothrow(int)
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::getPS_nothrow(int)
127
128
  const T* getPS_nothrow( int psId ) const
129
1
  {
130
1
    auto it = m_paramsetMap.find( psId );
131
1
    return it == m_paramsetMap.end() ? nullptr : it->second.parameterSet.get();
132
1
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::getPS_nothrow(int) const
vvdec::ParameterSetMap<vvdec::SPS, 16>::getPS_nothrow(int) const
Line
Count
Source
129
1
  {
130
1
    auto it = m_paramsetMap.find( psId );
131
1
    return it == m_paramsetMap.end() ? nullptr : it->second.parameterSet.get();
132
1
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::getPS_nothrow(int) const
133
134
  T* getPS( int psId )
135
6
  {
136
6
    T* ps = getPS_nothrow( psId );
137
6
    constexpr static int apsIdShift = std::is_same<T, APS>::value ? NUM_APS_TYPE_LEN : 0;
138
6
    CHECK( !ps, "Missing Parameter Set (id:" << ( psId >> apsIdShift ) << ')' );
139
0
    return ps;
140
6
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::SPS, 16>::getPS(int)
vvdec::ParameterSetMap<vvdec::PPS, 64>::getPS(int)
Line
Count
Source
135
6
  {
136
6
    T* ps = getPS_nothrow( psId );
137
6
    constexpr static int apsIdShift = std::is_same<T, APS>::value ? NUM_APS_TYPE_LEN : 0;
138
6
    CHECK( !ps, "Missing Parameter Set (id:" << ( psId >> apsIdShift ) << ')' );
139
0
    return ps;
140
6
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::getPS(int)
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::getPS(int)
141
142
  const T* getPS( int psId ) const
143
1
  {
144
1
    const T* ps = getPS_nothrow( psId );
145
1
    constexpr static int apsIdShift = std::is_same<T, APS>::value ? NUM_APS_TYPE_LEN : 0;
146
1
    CHECK( !ps, "Missing Parameter Set (id:" << ( psId >> apsIdShift ) << ')' );
147
0
    return ps;
148
1
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::getPS(int) const
vvdec::ParameterSetMap<vvdec::SPS, 16>::getPS(int) const
Line
Count
Source
143
1
  {
144
1
    const T* ps = getPS_nothrow( psId );
145
1
    constexpr static int apsIdShift = std::is_same<T, APS>::value ? NUM_APS_TYPE_LEN : 0;
146
1
    CHECK( !ps, "Missing Parameter Set (id:" << ( psId >> apsIdShift ) << ')' );
147
0
    return ps;
148
1
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::getPS(int) const
149
150
0
  const AlignedByteVec& getNaluData( int psId ) const { return m_paramsetMap.at( psId ).cNaluData; }
151
152
  T* getFirstPS() const
153
0
  {
154
0
    return ( m_paramsetMap.begin() == m_paramsetMap.end() ) ? NULL : m_paramsetMap.begin()->second.parameterSet.get();
155
0
  }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::SPS, 16>::getFirstPS() const
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::getFirstPS() const
156
157
0
  void setActive( int psId ) { m_activePsId.push_back( psId ); }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::setActive(int)
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::SPS, 16>::setActive(int)
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::PPS, 64>::setActive(int)
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::setActive(int)
158
0
  void clearActive()         { m_activePsId.clear(); }
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::APS, 64>::clearActive()
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::VPS, 16>::clearActive()
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::SPS, 16>::clearActive()
Unexecuted instantiation: vvdec::ParameterSetMap<vvdec::PPS, 64>::clearActive()
159
160
  void clearChangedFlags()
161
0
  {
162
0
    for( auto& entry: m_paramsetMap ) entry.second.parameterSet->clearChangedFlag();
163
0
  }
164
165
  bool getAnyChangedFlag() const
166
0
  {
167
0
    return std::any_of( m_paramsetMap.begin(), m_paramsetMap.end(), []( const auto& entry ) { return entry.second.parameterSet->getChangedFlag(); } );
168
0
  }
169
170
  void clearMap()            { m_paramsetMap.clear(); }
171
172
private:
173
  std::map<int, MapData> m_paramsetMap;
174
  std::shared_ptr<T>     m_lastActiveParameterSet;
175
  std::vector<int>       m_activePsId;
176
};
177
178
179
class ParameterSetManager
180
{
181
public:
182
1.18k
  ParameterSetManager()  = default;
183
1.18k
  ~ParameterSetManager() = default;
184
185
  using APSArray = std::array<const APS*, ALF_CTB_MAX_NUM_APS>;
186
187
  struct ActivePSs
188
  {
189
    const SPS*      sps;
190
    const PPS*      pps;
191
          APSArray* alfAPSs;
192
    const APS*      lmcsAps;
193
    const APS*      scalingListAps;
194
  };
195
196
  ActivePSs xActivateParameterSets( const bool isFirstSlice, const Slice* pPilot, const PicHeader* picHeader );
197
198
  //! store parameter set and take ownership of it. returns true, if no parameter set with this id was stored before or its NAL unit data differs
199
0
  bool            storeVPS( VPS* vps, AlignedByteVec& naluData ) { return m_vpsMap.storePS( vps->getVPSId(),                                             vps, std::move( naluData ) ); }
200
14
  bool            storeSPS( SPS* sps, AlignedByteVec& naluData ) { return m_spsMap.storePS( sps->getSPSId(),                                             sps, std::move( naluData ) ); }
201
0
  bool            storePPS( PPS* pps, AlignedByteVec& naluData ) { return m_ppsMap.storePS( pps->getPPSId(),                                             pps, std::move( naluData ) ); }
202
15
  bool            storeAPS( APS* aps, AlignedByteVec& naluData ) { return m_apsMap.storePS( ( aps->getAPSId() << NUM_APS_TYPE_LEN ) + aps->getAPSType(), aps, std::move( naluData ) ); }
203
204
  //! get pointer to existing sequence parameter set
205
0
  const VPS*      getVPS        ( int vpsId )              const { if( !vpsId ) return nullptr; return m_vpsMap.getPS( vpsId );              }
206
1
  const SPS*      getSPS        ( int spsId )              const { return m_spsMap.getPS        ( spsId );                                   }
207
6
  const PPS*      getPPS        ( int ppsId )                    { return xGetPPS( ppsId );                                          }
208
0
  const APS*      getAPS        ( int apsId, int apsType ) const { return m_apsMap.getPS        ( ( apsId << NUM_APS_TYPE_LEN ) + apsType ); }
209
0
  const APS*      getAPS_nothrow( int apsId, int apsType ) const { return m_apsMap.getPS_nothrow( ( apsId << NUM_APS_TYPE_LEN ) + apsType ); }
210
  // getter only used by DecLibParser::prepareLostPicture(). Is it really needed?
211
0
  const APSArray& getAlfAPSs()                             const { return m_alfAPSs; }
212
213
0
  const SPS*      getFirstSPS()                            const { return m_spsMap.getFirstPS();                                     }
214
0
  const APS*      getFirstAPS()                            const { return m_apsMap.getFirstPS();                                     }
215
0
  bool            getAPSChangedFlag()                      const { return m_apsMap.getAnyChangedFlag();                              }
216
217
  //! activate a PPS and, depending on isIDR parameter, also SPS. returns true, if activation is successful
218
  bool            activatePPS( int ppsId, bool isIRAP );
219
  bool            activateAPS( int apsId, int apsType );
220
221
0
  const SPS*      getActiveSPS()                           const { return m_spsMap.getPS( m_activeSPSId );                           }
222
223
private:
224
  //! re-derives the PPS, if the SPS it refers to was replaced since it was parsed
225
  PPS*            xGetPPS( int ppsId );
226
227
  ParameterSetMap<VPS, MAX_NUM_VPS>                            m_vpsMap;
228
  ParameterSetMap<SPS, MAX_NUM_SPS>                            m_spsMap;
229
  ParameterSetMap<PPS, MAX_NUM_PPS>                            m_ppsMap;
230
  ParameterSetMap<APS, ALF_CTB_MAX_NUM_APS * MAX_NUM_APS_TYPE> m_apsMap;
231
232
  APSArray m_alfAPSs;
233
234
  int m_activeSPSId = -1;   // -1 for nothing active
235
  int m_activeVPSId = -1;   // -1 for nothing active
236
};
237
238
}