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 | /** |
---|
35 | \file NALread.cpp |
---|
36 | \brief reading functionality for NAL units |
---|
37 | */ |
---|
38 | |
---|
39 | |
---|
40 | #include <vector> |
---|
41 | #include <algorithm> |
---|
42 | #include <ostream> |
---|
43 | |
---|
44 | #include "NALread.h" |
---|
45 | #include "TLibCommon/NAL.h" |
---|
46 | #include "TLibCommon/TComBitStream.h" |
---|
47 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
48 | #include "TLibCommon/TComCodingStatistics.h" |
---|
49 | #endif |
---|
50 | #if ENC_DEC_TRACE && DEC_NUH_TRACE |
---|
51 | #include "TLibCommon/TComRom.h" |
---|
52 | #endif |
---|
53 | |
---|
54 | using namespace std; |
---|
55 | |
---|
56 | //! \ingroup TLibDecoder |
---|
57 | //! \{ |
---|
58 | static Void convertPayloadToRBSP(vector<uint8_t>& nalUnitBuf, TComInputBitstream *bitstream, Bool isVclNalUnit) |
---|
59 | { |
---|
60 | UInt zeroCount = 0; |
---|
61 | vector<uint8_t>::iterator it_read, it_write; |
---|
62 | |
---|
63 | UInt pos = 0; |
---|
64 | bitstream->clearEmulationPreventionByteLocation(); |
---|
65 | for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++, it_write++, pos++) |
---|
66 | { |
---|
67 | assert(zeroCount < 2 || *it_read >= 0x03); |
---|
68 | if (zeroCount == 2 && *it_read == 0x03) |
---|
69 | { |
---|
70 | bitstream->pushEmulationPreventionByteLocation( pos ); |
---|
71 | pos++; |
---|
72 | it_read++; |
---|
73 | zeroCount = 0; |
---|
74 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
75 | TComCodingStatistics::IncrementStatisticEP(STATS__EMULATION_PREVENTION_3_BYTES, 8, 0); |
---|
76 | #endif |
---|
77 | if (it_read == nalUnitBuf.end()) |
---|
78 | { |
---|
79 | break; |
---|
80 | } |
---|
81 | assert(*it_read <= 0x03); |
---|
82 | } |
---|
83 | zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0; |
---|
84 | *it_write = *it_read; |
---|
85 | } |
---|
86 | assert(zeroCount == 0); |
---|
87 | |
---|
88 | if (isVclNalUnit) |
---|
89 | { |
---|
90 | // Remove cabac_zero_word from payload if present |
---|
91 | Int n = 0; |
---|
92 | |
---|
93 | while (it_write[-1] == 0x00) |
---|
94 | { |
---|
95 | it_write--; |
---|
96 | n++; |
---|
97 | } |
---|
98 | |
---|
99 | if (n > 0) |
---|
100 | { |
---|
101 | printf("\nDetected %d instances of cabac_zero_word\n", n/2); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | nalUnitBuf.resize(it_write - nalUnitBuf.begin()); |
---|
106 | } |
---|
107 | |
---|
108 | #if ENC_DEC_TRACE && DEC_NUH_TRACE |
---|
109 | void xTraceNalUnitHeader(InputNALUnit& nalu) |
---|
110 | { |
---|
111 | fprintf( g_hTrace, "*********** NAL UNIT (%s) ***********\n", nalUnitTypeToString(nalu.m_nalUnitType) ); |
---|
112 | |
---|
113 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
114 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "forbidden_zero_bit", 1, 0 ); |
---|
115 | |
---|
116 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
117 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "nal_unit_type", 6, nalu.m_nalUnitType ); |
---|
118 | |
---|
119 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
120 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "nuh_layer_id", 6, nalu.m_nuhLayerId ); |
---|
121 | |
---|
122 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
123 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "nuh_temporal_id_plus1", 3, nalu.m_temporalId + 1 ); |
---|
124 | |
---|
125 | fflush ( g_hTrace ); |
---|
126 | } |
---|
127 | #endif |
---|
128 | |
---|
129 | Void readNalUnitHeader(InputNALUnit& nalu) |
---|
130 | { |
---|
131 | TComInputBitstream& bs = nalu.getBitstream(); |
---|
132 | |
---|
133 | Bool forbidden_zero_bit = bs.read(1); // forbidden_zero_bit |
---|
134 | assert(forbidden_zero_bit == 0); |
---|
135 | nalu.m_nalUnitType = (NalUnitType) bs.read(6); // nal_unit_type |
---|
136 | #if NH_MV |
---|
137 | nalu.m_nuhLayerId = bs.read(6); // layerId |
---|
138 | #else |
---|
139 | nalu.m_nuhLayerId = bs.read(6); // nuh_layer_id |
---|
140 | #endif |
---|
141 | nalu.m_temporalId = bs.read(3) - 1; // nuh_temporal_id_plus1 |
---|
142 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
143 | TComCodingStatistics::IncrementStatisticEP(STATS__NAL_UNIT_HEADER_BITS, 1+6+6+3, 0); |
---|
144 | #endif |
---|
145 | |
---|
146 | #if ENC_DEC_TRACE && DEC_NUH_TRACE |
---|
147 | xTraceNalUnitHeader(nalu); |
---|
148 | #endif |
---|
149 | |
---|
150 | // only check these rules for base layer |
---|
151 | if (nalu.m_nuhLayerId == 0) |
---|
152 | { |
---|
153 | if ( nalu.m_temporalId ) |
---|
154 | { |
---|
155 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP |
---|
156 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL |
---|
157 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP |
---|
158 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL |
---|
159 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP |
---|
160 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA |
---|
161 | && nalu.m_nalUnitType != NAL_UNIT_VPS |
---|
162 | && nalu.m_nalUnitType != NAL_UNIT_SPS |
---|
163 | && nalu.m_nalUnitType != NAL_UNIT_EOS |
---|
164 | && nalu.m_nalUnitType != NAL_UNIT_EOB ); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | #if NH_MV |
---|
169 | // If nal_unit_type is in the range of BLA_W_LP to RSV_IRAP_VCL23, inclusive, i.e. the coded |
---|
170 | // slice segment belongs to an IRAP picture, TemporalId shall be equal to 0. |
---|
171 | // Otherwise, when nal_unit_type is equal to TSA_R, TSA_N, STSA_R, or STSA_N, TemporalId shall not be equal to 0. |
---|
172 | // Otherwise, if nal_unit_type is equal to TSA or TSA_N, TemporalId shall not be equal to 0. |
---|
173 | // Otherwise, when nuh_layer_id is equal to 0 and nal_unit_type is equal to STSA_R or STSA_N, |
---|
174 | // TemporalId shall not be equal to 0. |
---|
175 | |
---|
176 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R |
---|
177 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N ); |
---|
178 | |
---|
179 | assert( nalu.m_nuhLayerId > 0 |
---|
180 | || ( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R |
---|
181 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N ) ); |
---|
182 | #else |
---|
183 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R |
---|
184 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N |
---|
185 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R |
---|
186 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N ); |
---|
187 | #endif |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | /** |
---|
192 | * create a NALunit structure with given header values and storage for |
---|
193 | * a bitstream |
---|
194 | */ |
---|
195 | Void read(InputNALUnit& nalu) |
---|
196 | { |
---|
197 | TComInputBitstream &bitstream = nalu.getBitstream(); |
---|
198 | vector<uint8_t>& nalUnitBuf=bitstream.getFifo(); |
---|
199 | // perform anti-emulation prevention |
---|
200 | convertPayloadToRBSP(nalUnitBuf, &bitstream, (nalUnitBuf[0] & 64) == 0); |
---|
201 | bitstream.resetToStart(); |
---|
202 | readNalUnitHeader(nalu); |
---|
203 | } |
---|
204 | //! \} |
---|