1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2012, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "TLibCommon/CommonDef.h" |
---|
35 | #include "TLibCommon/TComBitStream.h" |
---|
36 | #include "TLibCommon/SEI.h" |
---|
37 | #include "TLibCommon/TComSlice.h" |
---|
38 | #include "SyntaxElementParser.h" |
---|
39 | #include "SEIread.h" |
---|
40 | |
---|
41 | //! \ingroup TLibDecoder |
---|
42 | //! \{ |
---|
43 | |
---|
44 | #if ENC_DEC_TRACE |
---|
45 | Void xTraceSEIHeader() |
---|
46 | { |
---|
47 | fprintf( g_hTrace, "=========== SEI message ===========\n"); |
---|
48 | } |
---|
49 | |
---|
50 | Void xTraceSEIMessageType(SEI::PayloadType payloadType) |
---|
51 | { |
---|
52 | switch (payloadType) |
---|
53 | { |
---|
54 | case SEI::DECODED_PICTURE_HASH: |
---|
55 | fprintf( g_hTrace, "=========== Decoded picture hash SEI message ===========\n"); |
---|
56 | break; |
---|
57 | #if ACTIVE_PARAMETER_SETS_SEI_MESSAGE |
---|
58 | case SEI::ACTIVE_PARAMETER_SETS: |
---|
59 | fprintf( g_hTrace, "=========== Active Parameter Sets SEI message ===========\n"); |
---|
60 | break; |
---|
61 | #endif |
---|
62 | case SEI::USER_DATA_UNREGISTERED: |
---|
63 | fprintf( g_hTrace, "=========== User Data Unregistered SEI message ===========\n"); |
---|
64 | break; |
---|
65 | default: |
---|
66 | fprintf( g_hTrace, "=========== Unknown SEI message ===========\n"); |
---|
67 | break; |
---|
68 | } |
---|
69 | } |
---|
70 | #endif |
---|
71 | |
---|
72 | /** |
---|
73 | * unmarshal a single SEI message from bitstream bs |
---|
74 | */ |
---|
75 | void SEIReader::parseSEImessage(TComInputBitstream* bs, SEImessages& seis) |
---|
76 | { |
---|
77 | setBitstream(bs); |
---|
78 | |
---|
79 | assert(!m_pcBitstream->getNumBitsUntilByteAligned()); |
---|
80 | do |
---|
81 | { |
---|
82 | xReadSEImessage(seis); |
---|
83 | /* SEI messages are an integer number of bytes, something has failed |
---|
84 | * in the parsing if bitstream not byte-aligned */ |
---|
85 | assert(!m_pcBitstream->getNumBitsUntilByteAligned()); |
---|
86 | } while (0x80 != m_pcBitstream->peekBits(8)); |
---|
87 | assert(m_pcBitstream->getNumBitsLeft() == 8); /* rsbp_trailing_bits */ |
---|
88 | } |
---|
89 | |
---|
90 | Void SEIReader::xReadSEImessage(SEImessages& seis) |
---|
91 | { |
---|
92 | #if ENC_DEC_TRACE |
---|
93 | xTraceSEIHeader(); |
---|
94 | #endif |
---|
95 | Int payloadType = 0; |
---|
96 | UInt val = 0; |
---|
97 | |
---|
98 | do |
---|
99 | { |
---|
100 | READ_CODE (8, val, "payload_type"); |
---|
101 | payloadType += val; |
---|
102 | } while (val==0xFF); |
---|
103 | |
---|
104 | UInt payloadSize = 0; |
---|
105 | do |
---|
106 | { |
---|
107 | READ_CODE (8, val, "payload_size"); |
---|
108 | payloadSize += val; |
---|
109 | } while (val==0xFF); |
---|
110 | |
---|
111 | #if ENC_DEC_TRACE |
---|
112 | xTraceSEIMessageType((SEI::PayloadType)payloadType); |
---|
113 | #endif |
---|
114 | |
---|
115 | switch (payloadType) |
---|
116 | { |
---|
117 | case SEI::USER_DATA_UNREGISTERED: |
---|
118 | seis.user_data_unregistered = new SEIuserDataUnregistered; |
---|
119 | xParseSEIuserDataUnregistered(*seis.user_data_unregistered, payloadSize); |
---|
120 | break; |
---|
121 | #if ACTIVE_PARAMETER_SETS_SEI_MESSAGE |
---|
122 | case SEI::ACTIVE_PARAMETER_SETS: |
---|
123 | seis.active_parameter_sets = new SEIActiveParameterSets; |
---|
124 | xParseSEIActiveParameterSets(*seis.active_parameter_sets, payloadSize); |
---|
125 | break; |
---|
126 | #endif |
---|
127 | case SEI::DECODED_PICTURE_HASH: |
---|
128 | seis.picture_digest = new SEIDecodedPictureHash; |
---|
129 | xParseSEIDecodedPictureHash(*seis.picture_digest, payloadSize); |
---|
130 | break; |
---|
131 | #if BUFFERING_PERIOD_AND_TIMING_SEI |
---|
132 | case SEI::BUFFERING_PERIOD: |
---|
133 | seis.buffering_period = new SEIBufferingPeriod; |
---|
134 | seis.buffering_period->m_sps = seis.m_pSPS; |
---|
135 | xParseSEIBufferingPeriod(*seis.buffering_period, payloadSize); |
---|
136 | break; |
---|
137 | case SEI::PICTURE_TIMING: |
---|
138 | seis.picture_timing = new SEIPictureTiming; |
---|
139 | seis.picture_timing->m_sps = seis.m_pSPS; |
---|
140 | xParseSEIPictureTiming(*seis.picture_timing, payloadSize); |
---|
141 | break; |
---|
142 | #endif |
---|
143 | #if RECOVERY_POINT_SEI |
---|
144 | case SEI::RECOVERY_POINT: |
---|
145 | seis.recovery_point = new SEIRecoveryPoint; |
---|
146 | xParseSEIRecoveryPoint(*seis.recovery_point, payloadSize); |
---|
147 | break; |
---|
148 | #endif |
---|
149 | default: |
---|
150 | assert(!"Unhandled SEI message"); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * parse bitstream bs and unpack a user_data_unregistered SEI message |
---|
156 | * of payloasSize bytes into sei. |
---|
157 | */ |
---|
158 | Void SEIReader::xParseSEIuserDataUnregistered(SEIuserDataUnregistered &sei, UInt payloadSize) |
---|
159 | { |
---|
160 | assert(payloadSize >= 16); |
---|
161 | UInt val; |
---|
162 | |
---|
163 | for (UInt i = 0; i < 16; i++) |
---|
164 | { |
---|
165 | READ_CODE (8, val, "uuid_iso_iec_11578"); |
---|
166 | sei.uuid_iso_iec_11578[i] = val; |
---|
167 | } |
---|
168 | |
---|
169 | sei.userDataLength = payloadSize - 16; |
---|
170 | if (!sei.userDataLength) |
---|
171 | { |
---|
172 | sei.userData = 0; |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | sei.userData = new UChar[sei.userDataLength]; |
---|
177 | for (UInt i = 0; i < sei.userDataLength; i++) |
---|
178 | { |
---|
179 | READ_CODE (8, val, "user_data" ); |
---|
180 | sei.userData[i] = val; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * parse bitstream bs and unpack a decoded picture hash SEI message |
---|
186 | * of payloadSize bytes into sei. |
---|
187 | */ |
---|
188 | Void SEIReader::xParseSEIDecodedPictureHash(SEIDecodedPictureHash& sei, UInt payloadSize) |
---|
189 | { |
---|
190 | UInt val; |
---|
191 | READ_CODE (8, val, "hash_type"); |
---|
192 | sei.method = static_cast<SEIDecodedPictureHash::Method>(val); |
---|
193 | for(int yuvIdx = 0; yuvIdx < 3; yuvIdx++) |
---|
194 | { |
---|
195 | if(SEIDecodedPictureHash::MD5 == sei.method) |
---|
196 | { |
---|
197 | for (unsigned i = 0; i < 16; i++) |
---|
198 | { |
---|
199 | READ_CODE(8, val, "picture_md5"); |
---|
200 | sei.digest[yuvIdx][i] = val; |
---|
201 | } |
---|
202 | } |
---|
203 | else if(SEIDecodedPictureHash::CRC == sei.method) |
---|
204 | { |
---|
205 | READ_CODE(16, val, "picture_crc"); |
---|
206 | sei.digest[yuvIdx][0] = val >> 8 & 0xFF; |
---|
207 | sei.digest[yuvIdx][1] = val & 0xFF; |
---|
208 | } |
---|
209 | else if(SEIDecodedPictureHash::CHECKSUM == sei.method) |
---|
210 | { |
---|
211 | READ_CODE(32, val, "picture_checksum"); |
---|
212 | sei.digest[yuvIdx][0] = (val>>24) & 0xff; |
---|
213 | sei.digest[yuvIdx][1] = (val>>16) & 0xff; |
---|
214 | sei.digest[yuvIdx][2] = (val>>8) & 0xff; |
---|
215 | sei.digest[yuvIdx][3] = val & 0xff; |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | #if ACTIVE_PARAMETER_SETS_SEI_MESSAGE |
---|
220 | Void SEIReader::xParseSEIActiveParameterSets(SEIActiveParameterSets& sei, unsigned payloadSize) |
---|
221 | { |
---|
222 | UInt val; |
---|
223 | READ_CODE(4, val, "active_vps_id"); |
---|
224 | sei.activeVPSId = val; |
---|
225 | |
---|
226 | READ_CODE(1, val, "active_sps_id_present_flag"); |
---|
227 | sei.activeSPSIdPresentFlag = val; |
---|
228 | |
---|
229 | if(sei.activeSPSIdPresentFlag) |
---|
230 | { |
---|
231 | READ_UVLC(val, "active_seq_param_set_id"); |
---|
232 | sei.activeSeqParamSetId = val; |
---|
233 | } |
---|
234 | |
---|
235 | READ_CODE(1, val, "active_param_set_sei_extension_flag"); |
---|
236 | sei.activeParamSetSEIExtensionFlag = val; |
---|
237 | |
---|
238 | UInt uibits = m_pcBitstream->getNumBitsUntilByteAligned(); |
---|
239 | |
---|
240 | while(uibits--) |
---|
241 | { |
---|
242 | READ_FLAG(val, "alignment_bit"); |
---|
243 | } |
---|
244 | } |
---|
245 | #endif |
---|
246 | |
---|
247 | #if BUFFERING_PERIOD_AND_TIMING_SEI |
---|
248 | Void SEIReader::xParseSEIBufferingPeriod(SEIBufferingPeriod& sei, UInt payloadSize) |
---|
249 | { |
---|
250 | Int i, nalOrVcl; |
---|
251 | UInt code; |
---|
252 | |
---|
253 | TComVUI *pVUI = sei.m_sps->getVuiParameters(); |
---|
254 | |
---|
255 | READ_UVLC( code, "seq_parameter_set_id" ); sei.m_seqParameterSetId = code; |
---|
256 | if( !pVUI->getSubPicCpbParamsPresentFlag() ) |
---|
257 | { |
---|
258 | READ_FLAG( code, "alt_cpb_params_present_flag" ); sei.m_altCpbParamsPresentFlag = code; |
---|
259 | } |
---|
260 | |
---|
261 | for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ ) |
---|
262 | { |
---|
263 | if( ( ( nalOrVcl == 0 ) && ( pVUI->getNalHrdParametersPresentFlag() ) ) || |
---|
264 | ( ( nalOrVcl == 1 ) && ( pVUI->getVclHrdParametersPresentFlag() ) ) ) |
---|
265 | { |
---|
266 | for( i = 0; i < ( pVUI->getCpbCntMinus1( 0 ) + 1 ); i ++ ) |
---|
267 | { |
---|
268 | READ_CODE( ( pVUI->getInitialCpbRemovalDelayLengthMinus1() + 1 ) , code, "initial_cpb_removal_delay" ); |
---|
269 | sei.m_initialCpbRemovalDelay[i][nalOrVcl] = code; |
---|
270 | READ_CODE( ( pVUI->getInitialCpbRemovalDelayLengthMinus1() + 1 ) , code, "initial_cpb_removal_delay_offset" ); |
---|
271 | sei.m_initialCpbRemovalDelayOffset[i][nalOrVcl] = code; |
---|
272 | if( pVUI->getSubPicCpbParamsPresentFlag() || sei.m_altCpbParamsPresentFlag ) |
---|
273 | { |
---|
274 | READ_CODE( ( pVUI->getInitialCpbRemovalDelayLengthMinus1() + 1 ) , code, "initial_alt_cpb_removal_delay" ); |
---|
275 | sei.m_initialAltCpbRemovalDelay[i][nalOrVcl] = code; |
---|
276 | READ_CODE( ( pVUI->getInitialCpbRemovalDelayLengthMinus1() + 1 ) , code, "initial_alt_cpb_removal_delay_offset" ); |
---|
277 | sei.m_initialAltCpbRemovalDelayOffset[i][nalOrVcl] = code; |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | xParseByteAlign(); |
---|
283 | } |
---|
284 | Void SEIReader::xParseSEIPictureTiming(SEIPictureTiming& sei, UInt payloadSize) |
---|
285 | { |
---|
286 | Int i; |
---|
287 | UInt code; |
---|
288 | |
---|
289 | TComVUI *vui = sei.m_sps->getVuiParameters(); |
---|
290 | |
---|
291 | if( !vui->getNalHrdParametersPresentFlag() && !vui->getVclHrdParametersPresentFlag() ) |
---|
292 | { |
---|
293 | return; |
---|
294 | } |
---|
295 | |
---|
296 | READ_CODE( ( vui->getCpbRemovalDelayLengthMinus1() + 1 ), code, "au_cpb_removal_delay" ); |
---|
297 | sei.m_auCpbRemovalDelay = code; |
---|
298 | READ_CODE( ( vui->getDpbOutputDelayLengthMinus1() + 1 ), code, "pic_dpb_output_delay" ); |
---|
299 | sei.m_picDpbOutputDelay = code; |
---|
300 | |
---|
301 | if( sei.m_sps->getVuiParameters()->getSubPicCpbParamsPresentFlag() ) |
---|
302 | { |
---|
303 | READ_UVLC( code, "num_decoding_units_minus1"); |
---|
304 | sei.m_numDecodingUnitsMinus1 = code; |
---|
305 | READ_FLAG( code, "du_common_cpb_removal_delay_flag" ); |
---|
306 | sei.m_duCommonCpbRemovalDelayFlag = code; |
---|
307 | if( sei.m_duCommonCpbRemovalDelayFlag ) |
---|
308 | { |
---|
309 | READ_CODE( ( vui->getDuCpbRemovalDelayLengthMinus1() + 1 ), code, "du_common_cpb_removal_delay_minus1" ); |
---|
310 | sei.m_duCommonCpbRemovalDelayMinus1 = code; |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | if( sei.m_numNalusInDuMinus1 != NULL ) |
---|
315 | { |
---|
316 | delete sei.m_numNalusInDuMinus1; |
---|
317 | } |
---|
318 | sei.m_numNalusInDuMinus1 = new UInt[ ( sei.m_numDecodingUnitsMinus1 + 1 ) ]; |
---|
319 | if( sei.m_duCpbRemovalDelayMinus1 != NULL ) |
---|
320 | { |
---|
321 | delete sei.m_duCpbRemovalDelayMinus1; |
---|
322 | } |
---|
323 | sei.m_duCpbRemovalDelayMinus1 = new UInt[ ( sei.m_numDecodingUnitsMinus1 + 1 ) ]; |
---|
324 | |
---|
325 | for( i = 0; i < ( sei.m_numDecodingUnitsMinus1 + 1 ); i ++ ) |
---|
326 | { |
---|
327 | READ_UVLC( code, "num_nalus_in_du_minus1"); |
---|
328 | sei.m_numNalusInDuMinus1[ i ] = code; |
---|
329 | READ_CODE( ( vui->getDuCpbRemovalDelayLengthMinus1() + 1 ), code, "du_cpb_removal_delay_minus1" ); |
---|
330 | sei.m_duCpbRemovalDelayMinus1[ i ] = code; |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | xParseByteAlign(); |
---|
335 | } |
---|
336 | #endif |
---|
337 | #if RECOVERY_POINT_SEI |
---|
338 | Void SEIReader::xParseSEIRecoveryPoint(SEIRecoveryPoint& sei, UInt payloadSize) |
---|
339 | { |
---|
340 | Int iCode; |
---|
341 | UInt uiCode; |
---|
342 | READ_SVLC( iCode, "recovery_poc_cnt" ); sei.m_recoveryPocCnt = iCode; |
---|
343 | READ_FLAG( uiCode, "exact_matching_flag" ); sei.m_exactMatchingFlag = uiCode; |
---|
344 | READ_FLAG( uiCode, "broken_link_flag" ); sei.m_brokenLinkFlag = uiCode; |
---|
345 | xParseByteAlign(); |
---|
346 | } |
---|
347 | #endif |
---|
348 | |
---|
349 | #if RECOVERY_POINT_SEI || BUFFERING_PERIOD_AND_TIMING_SEI |
---|
350 | Void SEIReader::xParseByteAlign() |
---|
351 | { |
---|
352 | UInt code; |
---|
353 | if( m_pcBitstream->getNumBitsRead() % 8 != 0 ) |
---|
354 | { |
---|
355 | READ_FLAG( code, "bit_equal_to_one" ); assert( code == 1 ); |
---|
356 | } |
---|
357 | while( m_pcBitstream->getNumBitsRead() % 8 != 0 ) |
---|
358 | { |
---|
359 | READ_FLAG( code, "bit_equal_to_zero" ); assert( code == 0 ); |
---|
360 | } |
---|
361 | } |
---|
362 | #endif |
---|
363 | //! \} |
---|