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-2015, 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 | /** \file SyntaxElementParser.cpp |
---|
35 | \brief Parsing functionality high level syntax |
---|
36 | */ |
---|
37 | |
---|
38 | //! \ingroup TLibDecoder |
---|
39 | //! \{ |
---|
40 | |
---|
41 | #include "TLibCommon/CommonDef.h" |
---|
42 | #include "TLibCommon/TComRom.h" |
---|
43 | #include "TLibCommon/TComBitStream.h" |
---|
44 | #include "SyntaxElementParser.h" |
---|
45 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
46 | #include "TLibCommon/TComCodingStatistics.h" |
---|
47 | #endif |
---|
48 | |
---|
49 | #if ENC_DEC_TRACE |
---|
50 | |
---|
51 | Void SyntaxElementParser::xReadCodeTr (UInt length, UInt& rValue, const Char *pSymbolName) |
---|
52 | { |
---|
53 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
54 | xReadCode (length, rValue, pSymbolName); |
---|
55 | #else |
---|
56 | xReadCode (length, rValue); |
---|
57 | #endif |
---|
58 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
59 | if (length < 10) |
---|
60 | { |
---|
61 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", pSymbolName, length, rValue ); |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", pSymbolName, length, rValue ); |
---|
66 | } |
---|
67 | fflush ( g_hTrace ); |
---|
68 | } |
---|
69 | |
---|
70 | Void SyntaxElementParser::xReadUvlcTr (UInt& rValue, const Char *pSymbolName) |
---|
71 | { |
---|
72 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
73 | xReadUvlc (rValue, pSymbolName); |
---|
74 | #else |
---|
75 | xReadUvlc (rValue); |
---|
76 | #endif |
---|
77 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
78 | fprintf( g_hTrace, "%-50s ue(v) : %u\n", pSymbolName, rValue ); |
---|
79 | fflush ( g_hTrace ); |
---|
80 | } |
---|
81 | |
---|
82 | Void SyntaxElementParser::xReadSvlcTr (Int& rValue, const Char *pSymbolName) |
---|
83 | { |
---|
84 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
85 | xReadSvlc (rValue, pSymbolName); |
---|
86 | #else |
---|
87 | xReadSvlc (rValue); |
---|
88 | #endif |
---|
89 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
90 | fprintf( g_hTrace, "%-50s se(v) : %d\n", pSymbolName, rValue ); |
---|
91 | fflush ( g_hTrace ); |
---|
92 | } |
---|
93 | |
---|
94 | Void SyntaxElementParser::xReadFlagTr (UInt& rValue, const Char *pSymbolName) |
---|
95 | { |
---|
96 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
97 | xReadFlag (rValue, pSymbolName); |
---|
98 | #else |
---|
99 | xReadFlag (rValue); |
---|
100 | #endif |
---|
101 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
102 | fprintf( g_hTrace, "%-50s u(1) : %d\n", pSymbolName, rValue ); |
---|
103 | fflush ( g_hTrace ); |
---|
104 | } |
---|
105 | |
---|
106 | Void xTraceAccessUnitDelimiter () |
---|
107 | { |
---|
108 | fprintf( g_hTrace, "=========== Access Unit Delimiter ===========\n"); |
---|
109 | } |
---|
110 | |
---|
111 | Void xTraceFillerData () |
---|
112 | { |
---|
113 | fprintf( g_hTrace, "=========== Filler Data ===========\n"); |
---|
114 | } |
---|
115 | |
---|
116 | #if Q0096_OVERLAY_SEI |
---|
117 | Void SyntaxElementParser::xReadStringTr (UInt buSize, UChar *pValue, UInt& rLength, const Char *pSymbolName) |
---|
118 | { |
---|
119 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
120 | xReadString(buSize, pValue, rLength, pSymbolName); |
---|
121 | #else |
---|
122 | xReadString(buSize, pValue, rLength); |
---|
123 | #endif |
---|
124 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
125 | fprintf( g_hTrace, "%-50s st(v=%d) : %s\n", pSymbolName, rLength, pValue ); |
---|
126 | fflush ( g_hTrace ); |
---|
127 | } |
---|
128 | #endif |
---|
129 | |
---|
130 | #endif |
---|
131 | |
---|
132 | |
---|
133 | // ==================================================================================================================== |
---|
134 | // Protected member functions |
---|
135 | // ==================================================================================================================== |
---|
136 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
137 | Void SyntaxElementParser::xReadCode (UInt uiLength, UInt& ruiCode, const Char *pSymbolName) |
---|
138 | #else |
---|
139 | Void SyntaxElementParser::xReadCode (UInt uiLength, UInt& ruiCode) |
---|
140 | #endif |
---|
141 | { |
---|
142 | assert ( uiLength > 0 ); |
---|
143 | m_pcBitstream->read (uiLength, ruiCode); |
---|
144 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
145 | TComCodingStatistics::IncrementStatisticEP(pSymbolName, uiLength, ruiCode); |
---|
146 | #endif |
---|
147 | } |
---|
148 | |
---|
149 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
150 | Void SyntaxElementParser::xReadUvlc( UInt& ruiVal, const Char *pSymbolName) |
---|
151 | #else |
---|
152 | Void SyntaxElementParser::xReadUvlc( UInt& ruiVal) |
---|
153 | #endif |
---|
154 | { |
---|
155 | UInt uiVal = 0; |
---|
156 | UInt uiCode = 0; |
---|
157 | UInt uiLength; |
---|
158 | m_pcBitstream->read( 1, uiCode ); |
---|
159 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
160 | UInt totalLen=1; |
---|
161 | #endif |
---|
162 | |
---|
163 | if( 0 == uiCode ) |
---|
164 | { |
---|
165 | uiLength = 0; |
---|
166 | |
---|
167 | while( ! ( uiCode & 1 )) |
---|
168 | { |
---|
169 | m_pcBitstream->read( 1, uiCode ); |
---|
170 | uiLength++; |
---|
171 | } |
---|
172 | |
---|
173 | m_pcBitstream->read( uiLength, uiVal ); |
---|
174 | |
---|
175 | uiVal += (1 << uiLength)-1; |
---|
176 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
177 | totalLen+=uiLength+uiLength; |
---|
178 | #endif |
---|
179 | } |
---|
180 | |
---|
181 | ruiVal = uiVal; |
---|
182 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
183 | TComCodingStatistics::IncrementStatisticEP(pSymbolName, Int(totalLen), ruiVal); |
---|
184 | #endif |
---|
185 | } |
---|
186 | |
---|
187 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
188 | Void SyntaxElementParser::xReadSvlc( Int& riVal, const Char *pSymbolName) |
---|
189 | #else |
---|
190 | Void SyntaxElementParser::xReadSvlc( Int& riVal) |
---|
191 | #endif |
---|
192 | { |
---|
193 | UInt uiBits = 0; |
---|
194 | m_pcBitstream->read( 1, uiBits ); |
---|
195 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
196 | UInt totalLen=1; |
---|
197 | #endif |
---|
198 | if( 0 == uiBits ) |
---|
199 | { |
---|
200 | UInt uiLength = 0; |
---|
201 | |
---|
202 | while( ! ( uiBits & 1 )) |
---|
203 | { |
---|
204 | m_pcBitstream->read( 1, uiBits ); |
---|
205 | uiLength++; |
---|
206 | } |
---|
207 | |
---|
208 | m_pcBitstream->read( uiLength, uiBits ); |
---|
209 | |
---|
210 | uiBits += (1 << uiLength); |
---|
211 | riVal = ( uiBits & 1) ? -(Int)(uiBits>>1) : (Int)(uiBits>>1); |
---|
212 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
213 | totalLen+=uiLength+uiLength; |
---|
214 | #endif |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | riVal = 0; |
---|
219 | } |
---|
220 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
221 | TComCodingStatistics::IncrementStatisticEP(pSymbolName, Int(totalLen), riVal); |
---|
222 | #endif |
---|
223 | } |
---|
224 | |
---|
225 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
226 | Void SyntaxElementParser::xReadFlag (UInt& ruiCode, const Char *pSymbolName) |
---|
227 | #else |
---|
228 | Void SyntaxElementParser::xReadFlag (UInt& ruiCode) |
---|
229 | #endif |
---|
230 | { |
---|
231 | m_pcBitstream->read( 1, ruiCode ); |
---|
232 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
233 | TComCodingStatistics::IncrementStatisticEP(pSymbolName, 1, Int(ruiCode)); |
---|
234 | #endif |
---|
235 | } |
---|
236 | |
---|
237 | Void SyntaxElementParser::xReadRbspTrailingBits() |
---|
238 | { |
---|
239 | UInt bit; |
---|
240 | READ_FLAG( bit, "rbsp_stop_one_bit"); |
---|
241 | assert (bit==1); |
---|
242 | Int cnt = 0; |
---|
243 | while (m_pcBitstream->getNumBitsUntilByteAligned()) |
---|
244 | { |
---|
245 | READ_FLAG( bit, "rbsp_alignment_zero_bit"); |
---|
246 | assert (bit==0); |
---|
247 | cnt++; |
---|
248 | } |
---|
249 | assert(cnt<8); |
---|
250 | } |
---|
251 | |
---|
252 | Void AUDReader::parseAccessUnitDelimiter(TComInputBitstream* bs, UInt &picType) |
---|
253 | { |
---|
254 | setBitstream(bs); |
---|
255 | |
---|
256 | #if ENC_DEC_TRACE |
---|
257 | xTraceAccessUnitDelimiter(); |
---|
258 | #endif |
---|
259 | |
---|
260 | READ_CODE (3, picType, "pic_type"); |
---|
261 | xReadRbspTrailingBits(); |
---|
262 | } |
---|
263 | |
---|
264 | Void FDReader::parseFillerData(TComInputBitstream* bs, UInt &fdSize) |
---|
265 | { |
---|
266 | setBitstream(bs); |
---|
267 | #if ENC_DEC_TRACE |
---|
268 | xTraceFillerData(); |
---|
269 | #endif |
---|
270 | UInt ffByte; |
---|
271 | fdSize = 0; |
---|
272 | while( m_pcBitstream->getNumBitsLeft() >8 ) |
---|
273 | { |
---|
274 | READ_CODE (8, ffByte, "ff_byte"); |
---|
275 | assert (ffByte==0xff); |
---|
276 | fdSize++; |
---|
277 | } |
---|
278 | xReadRbspTrailingBits(); |
---|
279 | } |
---|
280 | |
---|
281 | #if Q0096_OVERLAY_SEI |
---|
282 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
283 | Void SyntaxElementParser::xReadString (UInt bufSize, UChar *pVal, UInt& rLength, const Char *pSymbolName) |
---|
284 | #else |
---|
285 | Void SyntaxElementParser::xReadString (UInt bufSize, UChar *pVal, UInt& rLength) |
---|
286 | #endif |
---|
287 | { |
---|
288 | assert( m_pcBitstream->getNumBitsRead() % 8 == 0 ); //always start reading at a byte-aligned position |
---|
289 | assert ( bufSize > 1 ); //last byte always used for null-termination |
---|
290 | UInt val; |
---|
291 | UInt i; |
---|
292 | for (i=0 ; i<bufSize ; ++i ) |
---|
293 | { |
---|
294 | m_pcBitstream->readByte( val ); |
---|
295 | pVal[i] = val; |
---|
296 | if ( val == 0) |
---|
297 | { |
---|
298 | break; |
---|
299 | } |
---|
300 | } |
---|
301 | rLength = i; |
---|
302 | assert( pVal[rLength] == 0 ); |
---|
303 | } |
---|
304 | #endif |
---|
305 | |
---|
306 | //! \} |
---|
307 | |
---|