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

Last change on this file since 1136 was 1029, checked in by seregin, 10 years ago

merge with SHM-upgrade branch

  • Property svn:eol-style set to native
File size: 6.1 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-2014, 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 funtionality 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
51using namespace std;
52
53//! \ingroup TLibDecoder
54//! \{
55static Void convertPayloadToRBSP(vector<uint8_t>& nalUnitBuf, TComInputBitstream *bitstream, Bool isVclNalUnit)
56{
57  UInt zeroCount = 0;
58  vector<uint8_t>::iterator it_read, it_write;
59
60  UInt pos = 0;
61  bitstream->clearEmulationPreventionByteLocation();
62  for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++, it_write++, pos++)
63  {
64    assert(zeroCount < 2 || *it_read >= 0x03);
65    if (zeroCount == 2 && *it_read == 0x03)
66    {
67      bitstream->pushEmulationPreventionByteLocation( pos );
68      pos++;
69      it_read++;
70      zeroCount = 0;
71#if RExt__DECODER_DEBUG_BIT_STATISTICS
72      TComCodingStatistics::IncrementStatisticEP(STATS__EMULATION_PREVENTION_3_BYTES, 8, 0);
73#endif
74      if (it_read == nalUnitBuf.end())
75      {
76        break;
77      }
78      assert(*it_read <= 0x03);
79    }
80    zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0;
81    *it_write = *it_read;
82  }
83  assert(zeroCount == 0);
84
85  if (isVclNalUnit)
86  {
87    // Remove cabac_zero_word from payload if present
88    Int n = 0;
89
90    while (it_write[-1] == 0x00)
91    {
92      it_write--;
93      n++;
94    }
95
96    if (n > 0)
97    {
98      printf("\nDetected %d instances of cabac_zero_word\n", n/2);
99    }
100  }
101
102  nalUnitBuf.resize(it_write - nalUnitBuf.begin());
103}
104
105Void readNalUnitHeader(InputNALUnit& nalu)
106{
107  TComInputBitstream& bs = *nalu.m_Bitstream;
108
109  Bool forbidden_zero_bit = bs.read(1);           // forbidden_zero_bit
110  assert(forbidden_zero_bit == 0);
111  nalu.m_nalUnitType = (NalUnitType) bs.read(6);  // nal_unit_type
112  nalu.m_reservedZero6Bits = bs.read(6);       // nuh_reserved_zero_6bits
113#if SVC_EXTENSION
114  nalu.m_layerId = nalu.m_reservedZero6Bits;
115#else
116  assert(nalu.m_reservedZero6Bits == 0);
117#endif
118  nalu.m_temporalId = bs.read(3) - 1;             // nuh_temporal_id_plus1
119#if RExt__DECODER_DEBUG_BIT_STATISTICS
120  TComCodingStatistics::IncrementStatisticEP(STATS__NAL_UNIT_HEADER_BITS, 1+6+6+3, 0);
121#endif
122
123  if ( nalu.m_temporalId )
124  {
125#if !Q0108_TSA_STSA
126    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP
127         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL
128         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
129         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL
130         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
131         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
132         && nalu.m_nalUnitType != NAL_UNIT_VPS
133         && nalu.m_nalUnitType != NAL_UNIT_SPS
134         && nalu.m_nalUnitType != NAL_UNIT_EOS
135         && nalu.m_nalUnitType != NAL_UNIT_EOB );
136#else
137    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP
138         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL
139         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
140         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL
141         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
142         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
143         && nalu.m_nalUnitType != NAL_UNIT_VPS
144         && nalu.m_nalUnitType != NAL_UNIT_SPS
145         && nalu.m_nalUnitType != NAL_UNIT_EOS); 
146#endif
147  }
148  else
149  {
150#if !Q0108_TSA_STSA
151    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R
152         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N
153         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R
154         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N );
155#else
156    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R
157         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N
158         );
159#endif
160  }
161}
162/**
163 * create a NALunit structure with given header values and storage for
164 * a bitstream
165 */
166Void read(InputNALUnit& nalu, vector<uint8_t>& nalUnitBuf)
167{
168  /* perform anti-emulation prevention */
169  TComInputBitstream *pcBitstream = new TComInputBitstream(NULL);
170  convertPayloadToRBSP(nalUnitBuf, pcBitstream, (nalUnitBuf[0] & 64) == 0);
171
172  nalu.m_Bitstream = new TComInputBitstream(&nalUnitBuf);
173  nalu.m_Bitstream->setEmulationPreventionByteLocation(pcBitstream->getEmulationPreventionByteLocation());
174  delete pcBitstream;
175  readNalUnitHeader(nalu);
176}
177//! \}
Note: See TracBrowser for help on using the repository browser.