NALread.cpp 6.19 KB
Newer Older
1
2
3
4
5
/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.
 *
6
 * Copyright (c) 2010-2020, ITU/ISO/IEC
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

34
35
/**
 \file     NALread.cpp
36
 \brief    reading functionality for NAL units
37
38
39
 */


40
41
42
43
44
#include <vector>
#include <algorithm>
#include <ostream>

#include "NALread.h"
Frank Bossen's avatar
Frank Bossen committed
45
46
#include "TLibCommon/NAL.h"
#include "TLibCommon/TComBitStream.h"
47
48
49
#if RExt__DECODER_DEBUG_BIT_STATISTICS
#include "TLibCommon/TComCodingStatistics.h"
#endif
50
51
52
#if ENC_DEC_TRACE && DEC_NUH_TRACE
#include "TLibCommon/TComRom.h"
#endif
53
54
55

using namespace std;

Frank Bossen's avatar
Frank Bossen committed
56
57
//! \ingroup TLibDecoder
//! \{
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
64
65
  UInt pos = 0;
  bitstream->clearEmulationPreventionByteLocation();
  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
71
      bitstream->pushEmulationPreventionByteLocation( pos );
      pos++;
72
      it_read++;
73
      zeroCount = 0;
74
75
76
#if RExt__DECODER_DEBUG_BIT_STATISTICS
      TComCodingStatistics::IncrementStatisticEP(STATS__EMULATION_PREVENTION_3_BYTES, 8, 0);
#endif
77
78
79
80
      if (it_read == nalUnitBuf.end())
      {
        break;
      }
81
      assert(*it_read <= 0x03);
82
    }
83
84
85
    zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0;
    *it_write = *it_read;
  }
86
  assert(zeroCount == 0);
87

88
89
90
91
  if (isVclNalUnit)
  {
    // Remove cabac_zero_word from payload if present
    Int n = 0;
92

93
94
95
96
97
    while (it_write[-1] == 0x00)
    {
      it_write--;
      n++;
    }
98

99
100
    if (n > 0)
    {
101
      printf("\nDetected %d instances of cabac_zero_word\n", n/2);
102
103
    }
  }
104
105
106
107

  nalUnitBuf.resize(it_write - nalUnitBuf.begin());
}

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#if ENC_DEC_TRACE && DEC_NUH_TRACE
void xTraceNalUnitHeader(InputNALUnit& nalu)
{
  fprintf( g_hTrace, "*********** NAL UNIT (%s) ***********\n", nalUnitTypeToString(nalu.m_nalUnitType) );

  fprintf( g_hTrace, "%8lld  ", g_nSymbolCounter++ );
  fprintf( g_hTrace, "%-50s u(%d)  : %u\n", "forbidden_zero_bit", 1, 0 ); 

  fprintf( g_hTrace, "%8lld  ", g_nSymbolCounter++ );
  fprintf( g_hTrace, "%-50s u(%d)  : %u\n", "nal_unit_type", 6, nalu.m_nalUnitType ); 

  fprintf( g_hTrace, "%8lld  ", g_nSymbolCounter++ );
  fprintf( g_hTrace, "%-50s u(%d)  : %u\n", "nuh_layer_id", 6, nalu.m_nuhLayerId );

  fprintf( g_hTrace, "%8lld  ", g_nSymbolCounter++ );
  fprintf( g_hTrace, "%-50s u(%d)  : %u\n", "nuh_temporal_id_plus1", 3, nalu.m_temporalId + 1 );

  fflush ( g_hTrace );
}
#endif

129
130
Void readNalUnitHeader(InputNALUnit& nalu)
{
131
  TComInputBitstream& bs = nalu.getBitstream();
132

David Flynn's avatar
David Flynn committed
133
  Bool forbidden_zero_bit = bs.read(1);           // forbidden_zero_bit
134
135
  assert(forbidden_zero_bit == 0);
  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
139
140
#if RExt__DECODER_DEBUG_BIT_STATISTICS
  TComCodingStatistics::IncrementStatisticEP(STATS__NAL_UNIT_HEADER_BITS, 1+6+6+3, 0);
#endif
141

142
143
144
145
#if ENC_DEC_TRACE && DEC_NUH_TRACE
  xTraceNalUnitHeader(nalu);
#endif

146
147
  // only check these rules for base layer
  if (nalu.m_nuhLayerId == 0)
148
  {
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    if ( nalu.m_temporalId )
    {
      assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA
           && nalu.m_nalUnitType != NAL_UNIT_VPS
           && nalu.m_nalUnitType != NAL_UNIT_SPS
           && nalu.m_nalUnitType != NAL_UNIT_EOS
           && nalu.m_nalUnitType != NAL_UNIT_EOB );
    }
    else
    {
      assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R
           && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N );
    }
169
170
  }
}
171
172
173
174
/**
 * create a NALunit structure with given header values and storage for
 * a bitstream
 */
175
Void read(InputNALUnit& nalu)
176
{
177
178
179
180
181
  TComInputBitstream &bitstream = nalu.getBitstream();
  vector<uint8_t>& nalUnitBuf=bitstream.getFifo();
  // perform anti-emulation prevention
  convertPayloadToRBSP(nalUnitBuf, &bitstream, (nalUnitBuf[0] & 64) == 0);
  bitstream.resetToStart();
182
  readNalUnitHeader(nalu);
183
}
Frank Bossen's avatar
Frank Bossen committed
184
//! \}