HEVC Test Model (HM)  HM-16.18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NALread.cpp
Go to the documentation of this file.
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-2017, 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 
40 #include <vector>
41 #include <algorithm>
42 #include <ostream>
43 
44 #include "NALread.h"
45 #include "TLibCommon/NAL.h"
47 #if RExt__DECODER_DEBUG_BIT_STATISTICS
49 #endif
50 #if ENC_DEC_TRACE && DEC_NUH_TRACE
51 #include "TLibCommon/TComRom.h"
52 #endif
53 
54 using namespace std;
55 
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;
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
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 
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  nalu.m_nuhLayerId = bs.read(6); // nuh_layer_id
137  nalu.m_temporalId = bs.read(3) - 1; // nuh_temporal_id_plus1
138 #if RExt__DECODER_DEBUG_BIT_STATISTICS
140 #endif
141 
142 #if ENC_DEC_TRACE && DEC_NUH_TRACE
143  xTraceNalUnitHeader(nalu);
144 #endif
145 
146  // only check these rules for base layer
147  if (nalu.m_nuhLayerId == 0)
148  {
149  if ( nalu.m_temporalId )
150  {
157  && nalu.m_nalUnitType != NAL_UNIT_VPS
158  && nalu.m_nalUnitType != NAL_UNIT_SPS
159  && nalu.m_nalUnitType != NAL_UNIT_EOS
160  && nalu.m_nalUnitType != NAL_UNIT_EOB );
161  }
162  else
163  {
168  }
169  }
170 }
176 {
177  TComInputBitstream &bitstream = nalu.getBitstream();
178  vector<uint8_t>& nalUnitBuf=bitstream.getFifo();
179  // perform anti-emulation prevention
180  convertPayloadToRBSP(nalUnitBuf, &bitstream, (nalUnitBuf[0] & 64) == 0);
181  bitstream.resetToStart();
182  readNalUnitHeader(nalu);
183 }
void Void
Definition: TypeDef.h:203
const TChar * nalUnitTypeToString(NalUnitType type)
Definition: TComRom.cpp:53
reading functionality for NAL units
global variables &amp; functions (header)
const std::vector< uint8_t > & getFifo() const
Void readNalUnitHeader(InputNALUnit &nalu)
Definition: NALread.cpp:129
unsigned int UInt
Definition: TypeDef.h:212
NalUnitType m_nalUnitType
nal_unit_type
Definition: NAL.h:49
static Void convertPayloadToRBSP(vector< uint8_t > &nalUnitBuf, TComInputBitstream *bitstream, Bool isVclNalUnit)
Definition: NALread.cpp:58
Void pushEmulationPreventionByteLocation(UInt pos)
bool Bool
Definition: TypeDef.h:204
class for handling bitstream (header)
UInt m_nuhLayerId
nuh_layer_id
Definition: NAL.h:51
const TComInputBitstream & getBitstream() const
Definition: NALread.h:64
Void read(UInt uiNumberOfBits, UInt &ruiBits)
int Int
Definition: TypeDef.h:211
NalUnitType
Definition: TypeDef.h:664
Void read(InputNALUnit &nalu)
Definition: NALread.cpp:175
UInt m_temporalId
temporal_id
Definition: NAL.h:50
static Void IncrementStatisticEP(const TComCodingStatisticsClassType &stat, const Int numBits, const Int value)
Void clearEmulationPreventionByteLocation()