/work/vvdec/source/Lib/CommonLib/ParameterSetManager.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 "ParameterSetManager.h" |
44 | | |
45 | | namespace vvdec |
46 | | { |
47 | | |
48 | | ParameterSetManager::ActivePSs ParameterSetManager::xActivateParameterSets( const bool isFirstSlice, const Slice* pSlicePilot, const PicHeader* picHeader, const bool rejectSpsChangeInClvs ) |
49 | 0 | { |
50 | 0 | PPS* pps = getPPS( picHeader->getPPSId() ); |
51 | 0 | CHECK( pps == 0, "No PPS present" ); |
52 | |
|
53 | 0 | SPS* sps = getSPS( pps->getSPSId() ); |
54 | 0 | CHECK( sps == 0, "No SPS present" ); |
55 | |
|
56 | 0 | if( isFirstSlice ) |
57 | 0 | { |
58 | 0 | CHECK( !pps->pcv->isCorrect( *sps, *pps ), "PPS has PCV already, but values chaged???" ); |
59 | | |
60 | | // the SPS may only change at the start of a CLVS (IRAP or GDR with NoOutputBeforeRecoveryFlag = 1) |
61 | 0 | const NalUnitType nut = pSlicePilot->getNalUnitType(); |
62 | 0 | const bool isClvss = nut >= NAL_UNIT_CODED_SLICE_IDR_W_RADL && nut <= NAL_UNIT_CODED_SLICE_GDR |
63 | 0 | && !pps->getMixedNaluTypesInPicFlag() && picHeader->getNoOutputBeforeRecoveryFlag(); |
64 | 0 | if( rejectSpsChangeInClvs && !isClvss && m_activeSPSId >= 0 ) |
65 | 0 | { |
66 | 0 | CHECK( pps->getSPSId() != m_activeSPSId, "a different SPS was activated within a CLVS" ); |
67 | 0 | CHECK( sps->getChangedFlag(), "the SPS content changed within a CLVS" ); |
68 | 0 | } |
69 | | |
70 | 0 | sps->clearChangedFlag(); |
71 | 0 | pps->clearChangedFlag(); |
72 | | // APS NAL units can't occur between the slices of a picture, so any APS change after this point is an error |
73 | 0 | m_apsMap.clearChangedFlags(); |
74 | |
|
75 | 0 | if( false == activatePPS( picHeader->getPPSId(), pSlicePilot->isIRAP() ) ) |
76 | 0 | { |
77 | 0 | THROW_RECOVERABLE( "Parameter set activation failed!" ); |
78 | 0 | } |
79 | | |
80 | 0 | m_alfAPSs.fill( nullptr ); |
81 | 0 | m_apsMap.clearActive(); |
82 | 0 | } |
83 | | |
84 | | // luma APSs |
85 | 0 | for( int i = 0; i < pSlicePilot->getAlfApsIdsLuma().size(); i++ ) |
86 | 0 | { |
87 | 0 | int apsId = pSlicePilot->getAlfApsIdsLuma()[i]; |
88 | 0 | APS *alfApsL = getAPS( apsId, ALF_APS ); |
89 | |
|
90 | 0 | if( alfApsL ) |
91 | 0 | { |
92 | 0 | m_alfAPSs[apsId] = alfApsL; |
93 | 0 | if( false == activateAPS( apsId, ALF_APS ) ) |
94 | 0 | { |
95 | 0 | THROW_RECOVERABLE( "APS activation failed!" ); |
96 | 0 | } |
97 | | |
98 | 0 | CHECK( sps->getUseCCALF() == false && ( alfApsL->getCcAlfAPSParam().newCcAlfFilter[0] || alfApsL->getCcAlfAPSParam().newCcAlfFilter[1] ), |
99 | 0 | "When sps_ccalf_enabled_flag is 0, the values of alf_cc_cb_filter_signal_flag and alf_cc_cr_filter_signal_flag shall be equal to 0" ); |
100 | 0 | CHECK( sps->getChromaFormatIdc() == CHROMA_400 && alfApsL->chromaPresentFlag, |
101 | 0 | "When ChromaArrayType is equal to 0, the value of aps_chroma_present_flag of an ALF_APS shall be equal to 0" ); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | if( pSlicePilot->getAlfEnabledFlag( COMPONENT_Cb ) || pSlicePilot->getAlfEnabledFlag( COMPONENT_Cr ) ) |
106 | 0 | { |
107 | | // chroma APS |
108 | 0 | int apsId = pSlicePilot->getAlfApsIdChroma(); |
109 | 0 | APS* alfApsC = getAPS( apsId , ALF_APS ); |
110 | 0 | if( alfApsC ) |
111 | 0 | { |
112 | 0 | m_alfAPSs[apsId] = alfApsC; |
113 | 0 | if( false == activateAPS( apsId, ALF_APS ) ) |
114 | 0 | { |
115 | 0 | THROW_RECOVERABLE( "APS activation failed!" ); |
116 | 0 | } |
117 | 0 | CHECK( sps->getUseCCALF() == false && ( alfApsC->getCcAlfAPSParam().newCcAlfFilter[0] || alfApsC->getCcAlfAPSParam().newCcAlfFilter[1] ), |
118 | 0 | "When sps_ccalf_enabled_flag is 0, the values of alf_cc_cb_filter_signal_flag and alf_cc_cr_filter_signal_flag shall be equal to 0" ); |
119 | 0 | } |
120 | 0 | } |
121 | 0 | if( pSlicePilot->getCcAlfCbEnabledFlag() ) |
122 | 0 | { |
123 | 0 | if( !m_alfAPSs[pSlicePilot->getCcAlfCbApsId()] ) |
124 | 0 | { |
125 | 0 | int apsId = pSlicePilot->getCcAlfCbApsId(); |
126 | 0 | APS *aps = getAPS( apsId, ALF_APS ); |
127 | 0 | if( aps ) |
128 | 0 | { |
129 | 0 | m_alfAPSs[apsId] = aps; |
130 | |
|
131 | 0 | if( false == activateAPS( apsId, ALF_APS ) ) |
132 | 0 | { |
133 | 0 | THROW_RECOVERABLE( "APS activation failed!" ); |
134 | 0 | } |
135 | 0 | } |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | 0 | if( pSlicePilot->getCcAlfCrEnabledFlag() ) |
140 | 0 | { |
141 | 0 | if( !m_alfAPSs[pSlicePilot->getCcAlfCrApsId()] ) |
142 | 0 | { |
143 | 0 | int apsId = pSlicePilot->getCcAlfCrApsId(); |
144 | 0 | APS *aps = getAPS( apsId, ALF_APS ); |
145 | 0 | if( aps ) |
146 | 0 | { |
147 | 0 | m_alfAPSs[apsId] = aps; |
148 | 0 | if( false == activateAPS( apsId, ALF_APS ) ) |
149 | 0 | { |
150 | 0 | THROW_RECOVERABLE( "APS activation failed!" ); |
151 | 0 | } |
152 | 0 | } |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | APS* lmcsAPS = nullptr; |
157 | 0 | if( picHeader->getLmcsAPSId() != -1 ) |
158 | 0 | { |
159 | 0 | lmcsAPS = getAPS( picHeader->getLmcsAPSId(), LMCS_APS ); |
160 | 0 | CHECK( lmcsAPS == 0, "No LMCS APS present" ); |
161 | 0 | } |
162 | | |
163 | 0 | if( lmcsAPS ) |
164 | 0 | { |
165 | 0 | if( false == activateAPS( picHeader->getLmcsAPSId(), LMCS_APS ) ) |
166 | 0 | { |
167 | 0 | THROW_RECOVERABLE( "LMCS APS activation failed!" ); |
168 | 0 | } |
169 | | |
170 | 0 | CHECK( sps->getChromaFormatIdc() == CHROMA_400 && lmcsAPS->chromaPresentFlag, |
171 | 0 | "When ChromaArrayType is equal to 0, the value of aps_chroma_present_flag of an LMCS_APS shall be equal to 0"); |
172 | 0 | CHECK( lmcsAPS->getReshaperAPSInfo().maxNbitsNeededDeltaCW - 1 < 0 || lmcsAPS->getReshaperAPSInfo().maxNbitsNeededDeltaCW - 1 > sps->getBitDepth() - 2, |
173 | 0 | "The value of lmcs_delta_cw_prec_minus1 of an LMCS_APS shall be in the range of 0 to BitDepth 2, inclusive" ); |
174 | 0 | } |
175 | | |
176 | 0 | APS* scalingListAPS = nullptr; |
177 | 0 | if( picHeader->getScalingListAPSId() != -1 ) |
178 | 0 | { |
179 | 0 | scalingListAPS = getAPS( picHeader->getScalingListAPSId(), SCALING_LIST_APS ); |
180 | 0 | CHECK( scalingListAPS == 0, "No ScalingList APS present" ); |
181 | 0 | } |
182 | | |
183 | 0 | if( scalingListAPS ) |
184 | 0 | { |
185 | 0 | if( false == activateAPS( picHeader->getScalingListAPSId(), SCALING_LIST_APS ) ) |
186 | 0 | { |
187 | 0 | THROW_RECOVERABLE( "LMCS APS activation failed!" ); |
188 | 0 | } |
189 | | |
190 | 0 | CHECK( ( sps->getChromaFormatIdc() == CHROMA_400 && scalingListAPS->chromaPresentFlag ) || ( sps->getChromaFormatIdc() != CHROMA_400 && !scalingListAPS->chromaPresentFlag ), |
191 | 0 | "The value of aps_chroma_present_flag of the APS NAL unit having aps_params_type equal to SCALING_APS and adaptation_parameter_set_id equal to ph_scaling_list_aps_id shall be equal to ChromaArrayType = = 0 ? 0 : 1" ); |
192 | 0 | } |
193 | | |
194 | 0 | return { sps, pps, &m_alfAPSs, lmcsAPS, scalingListAPS }; |
195 | 0 | } |
196 | | |
197 | | //! activate a PPS and depending on isIDR parameter also SPS |
198 | | //! \returns true, if activation is successful |
199 | | bool ParameterSetManager::activatePPS( int ppsId, bool isIRAP ) |
200 | 0 | { |
201 | 0 | PPS* pps = m_ppsMap.getPS( ppsId ); |
202 | 0 | if( pps ) |
203 | 0 | { |
204 | 0 | int spsId = pps->getSPSId(); |
205 | 0 | { |
206 | 0 | SPS* sps = m_spsMap.getPS( spsId ); |
207 | 0 | if( sps ) |
208 | 0 | { |
209 | 0 | int vpsId = sps->getVPSId(); |
210 | 0 | if( vpsId != 0 ) |
211 | 0 | { |
212 | 0 | VPS *vps = m_vpsMap.getPS(vpsId); |
213 | 0 | if( vps ) |
214 | 0 | { |
215 | 0 | m_activeVPSId = vpsId; |
216 | 0 | m_vpsMap.setActive(vpsId); |
217 | 0 | } |
218 | 0 | else |
219 | 0 | { |
220 | 0 | msg( WARNING, "Warning: tried to activate PPS that refers to non-existing VPS." ); |
221 | 0 | } |
222 | 0 | } |
223 | 0 | else |
224 | 0 | { |
225 | | //No actual VPS |
226 | 0 | m_activeVPSId = -1; |
227 | 0 | m_vpsMap.clearActive(); |
228 | 0 | } |
229 | |
|
230 | 0 | m_spsMap.clearActive(); |
231 | 0 | m_spsMap.setActive( spsId ); |
232 | 0 | m_activeSPSId = spsId; |
233 | |
|
234 | 0 | m_ppsMap.clearActive(); |
235 | 0 | m_ppsMap.setActive( ppsId ); |
236 | 0 | return true; |
237 | 0 | } |
238 | 0 | else |
239 | 0 | { |
240 | 0 | msg( WARNING, "Warning: tried to activate a PPS that refers to a non-existing SPS." ); |
241 | 0 | } |
242 | 0 | } |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | msg( WARNING, "Warning: tried to activate non-existing PPS." ); |
247 | 0 | } |
248 | | |
249 | | // Failed to activate if reach here. |
250 | 0 | m_activeSPSId = -1; |
251 | 0 | return false; |
252 | 0 | } |
253 | | |
254 | | bool ParameterSetManager::activateAPS( int apsId, int apsType ) |
255 | 0 | { |
256 | 0 | APS* aps = m_apsMap.getPS( ( apsId << NUM_APS_TYPE_LEN ) + apsType ); |
257 | 0 | if( aps ) |
258 | 0 | { |
259 | 0 | m_apsMap.setActive( ( apsId << NUM_APS_TYPE_LEN ) + apsType ); |
260 | 0 | return true; |
261 | 0 | } |
262 | 0 | else |
263 | 0 | { |
264 | 0 | msg( WARNING, "Warning: tried to activate non-existing APS." ); |
265 | 0 | } |
266 | 0 | return false; |
267 | 0 | } |
268 | | |
269 | | } |