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