source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibDecoder/NALread.cpp @ 1530

Last change on this file since 1530 was 1353, checked in by seregin, 9 years ago

port rev 4431

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
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
54using namespace std;
55
56//! \ingroup TLibDecoder
57//! \{
58static 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
109void 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
129Void 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  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
139  TComCodingStatistics::IncrementStatisticEP(STATS__NAL_UNIT_HEADER_BITS, 1+6+6+3, 0);
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    {
151      assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP
152           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL
153           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
154           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL
155           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
156           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
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    {
164      assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R
165           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N
166           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R
167           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N );
168    }
169  }
170#if SVC_EXTENSION
171  else if ( nalu.m_temporalId )
172  {
173    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP
174         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL
175         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
176         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL
177         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
178         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
179         && nalu.m_nalUnitType != NAL_UNIT_VPS
180         && nalu.m_nalUnitType != NAL_UNIT_SPS
181         && nalu.m_nalUnitType != NAL_UNIT_EOS);
182  }
183  else
184  {
185    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R
186         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N
187         );
188  }
189#endif
190}
191/**
192 * create a NALunit structure with given header values and storage for
193 * a bitstream
194 */
195Void 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//! \}
Note: See TracBrowser for help on using the repository browser.