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

Last change on this file since 42 was 2, checked in by seregin, 12 years ago

Initial import by Vadim Seregin <vseregin@…>

File size: 6.7 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-2012, 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#include <vector>
35#include <algorithm>
36#include <ostream>
37
38#include "NALread.h"
39#include "TLibCommon/NAL.h"
40#include "TLibCommon/TComBitStream.h"
41
42using namespace std;
43
44//! \ingroup TLibDecoder
45//! \{
46static void convertPayloadToRBSP(vector<uint8_t>& nalUnitBuf, TComInputBitstream *pcBitstream)
47{
48  unsigned zeroCount = 0;
49  vector<uint8_t>::iterator it_read, it_write;
50
51  for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++, it_write++)
52  {
53    if (zeroCount == 2 && *it_read == 0x03)
54    {
55      it_read++;
56      zeroCount = 0;
57    }
58    zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0;
59    *it_write = *it_read;
60  }
61
62  nalUnitBuf.resize(it_write - nalUnitBuf.begin());
63}
64
65#if NAL_UNIT_HEADER
66Void readNalUnitHeader(InputNALUnit& nalu)
67{
68  TComInputBitstream& bs = *nalu.m_Bitstream;
69
70  bool forbidden_zero_bit = bs.read(1);           // forbidden_zero_bit
71  assert(forbidden_zero_bit == 0);
72  nalu.m_nalUnitType = (NalUnitType) bs.read(6);  // nal_unit_type
73#if SVC_EXTENSION
74  nalu.m_reservedZero6Bits = bs.read(6);       // nuh_reserved_zero_6bits
75  nalu.m_layerId = nalu.m_reservedZero6Bits;
76#else
77#if TARGET_DECLAYERID_SET
78  nalu.m_reservedZero6Bits = bs.read(6);       // nuh_reserved_zero_6bits
79  assert(nalu.m_reservedZero6Bits == 0);
80#else
81  unsigned reserved_one_6bits = bs.read(6);       // nuh_reserved_zero_6bits
82  assert(reserved_one_6bits == 0);
83#endif
84#endif
85  nalu.m_temporalId = bs.read(3) - 1;             // nuh_temporal_id_plus1
86
87  if ( nalu.m_temporalId )
88  {
89#if NAL_UNIT_TYPES_J1003_D7
90    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA
91         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLANT
92         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
93         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR
94         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
95         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
96         && nalu.m_nalUnitType != NAL_UNIT_VPS
97         && nalu.m_nalUnitType != NAL_UNIT_SPS
98         && nalu.m_nalUnitType != NAL_UNIT_EOS
99         && nalu.m_nalUnitType != NAL_UNIT_EOB );
100#else
101    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
102         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRANT
103         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA
104         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLANT
105         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR
106         && nalu.m_nalUnitType != NAL_UNIT_VPS
107         && nalu.m_nalUnitType != NAL_UNIT_SPS );
108#endif
109  }
110  else
111  {
112#if NAL_UNIT_TYPES_J1003_D7
113    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TLA
114         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N
115         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R
116         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N );
117#else
118    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TLA );
119#endif
120  }
121}
122#endif
123/**
124 * create a NALunit structure with given header values and storage for
125 * a bitstream
126 */
127void read(InputNALUnit& nalu, vector<uint8_t>& nalUnitBuf)
128{
129  /* perform anti-emulation prevention */
130  TComInputBitstream *pcBitstream = new TComInputBitstream(NULL);
131  convertPayloadToRBSP(nalUnitBuf, pcBitstream);
132
133  nalu.m_Bitstream = new TComInputBitstream(&nalUnitBuf);
134  delete pcBitstream;
135#if NAL_UNIT_HEADER
136  readNalUnitHeader(nalu);
137#else
138  TComInputBitstream& bs = *nalu.m_Bitstream;
139
140  bool forbidden_zero_bit = bs.read(1);
141  assert(forbidden_zero_bit == 0);
142#if !REMOVE_NAL_REF_FLAG
143  nalu.m_nalRefFlag  = (bs.read(1) != 0 );
144#endif
145  nalu.m_nalUnitType = (NalUnitType) bs.read(6);
146#if REMOVE_NAL_REF_FLAG
147  unsigned reserved_one_6bits = bs.read(6);
148  assert(reserved_one_6bits == 0);
149#endif
150#if TEMPORAL_ID_PLUS1
151  nalu.m_temporalId = bs.read(3) - 1;
152#if !REMOVE_NAL_REF_FLAG
153  unsigned reserved_one_5bits = bs.read(5);
154  assert(reserved_one_5bits == 0);
155#endif
156#else
157  nalu.m_temporalId = bs.read(3);
158  unsigned reserved_one_5bits = bs.read(5);
159#if SVC_EXTENSION
160  assert(reserved_one_5bits >= 1);
161  nalu.m_layerId = reserved_one_5bits - 1;
162#else
163  assert(reserved_one_5bits == 1);
164#endif
165#endif
166
167  if ( nalu.m_temporalId )
168  {
169#if NAL_UNIT_TYPES_J1003_D7
170    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA
171         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLANT
172         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
173         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR
174         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
175         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA );
176#else
177    assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
178         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRANT
179         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA
180         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLANT
181         && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR );
182#endif
183  }
184#endif
185}
186//! \}
Note: See TracBrowser for help on using the repository browser.