DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
nalu_reader.h
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MEDIA_CODECS_NALU_READER_H_
8 #define MEDIA_CODECS_NALU_READER_H_
9 
10 #include <stdint.h>
11 #include <stdlib.h>
12 
13 #include "packager/base/compiler_specific.h"
14 #include "packager/base/macros.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 // Used as the |nalu_length_size| argument to NaluReader to indicate to use
20 // AnnexB byte streams. An AnnexB byte stream starts with 3 or 4 byte start
21 // codes instead of a fixed size NAL unit length.
22 const uint8_t kIsAnnexbByteStream = 0;
23 
26 class Nalu {
27  public:
28  enum H264NaluType {
29  H264_Unspecified = 0,
30  H264_NonIDRSlice = 1,
31  H264_IDRSlice = 5,
32  H264_SEIMessage = 6,
33  H264_SPS = 7,
34  H264_PPS = 8,
35  H264_AUD = 9,
36  H264_EOSeq = 10,
37  H264_FillerData = 12,
38  H264_SPSExtension = 13,
39  H264_PrefixNALUnit = 14,
40  H264_SubsetSPS = 15,
41  H264_DepthParameterSet = 16,
42  H264_Reserved17 = 17,
43  H264_Reserved18 = 18,
44  H264_CodedSliceExtension = 20,
45  H264_Reserved22 = 22,
46  };
47  enum H265NaluType {
48  H265_TRAIL_N = 0,
49  H265_TRAIL_R = 1,
50  H265_TSA_N = 2,
51  H265_TSA_R = 3,
52  H265_STSA_N = 4,
53  H265_STSA_R = 5,
54  H265_RASL_R = 9,
55 
56  H265_RSV_VCL_N10 = 10,
57  H265_RSV_VCL_R15 = 15,
58 
59  H265_BLA_W_LP = 16,
60  H265_IDR_W_RADL = 19,
61  H265_IDR_N_LP = 20,
62  H265_CRA_NUT = 21,
63 
64  H265_RSV_IRAP_VCL22 = 22,
65  H265_RSV_IRAP_VCL23 = 23,
66  H265_RSV_VCL31 = 31,
67 
68  H265_VPS = 32,
69  H265_SPS = 33,
70  H265_PPS = 34,
71  H265_AUD = 35,
72 
73  H265_EOS = 36,
74  H265_EOB = 37,
75  H265_FD = 38,
76 
77  H265_PREFIX_SEI = 39,
78 
79  H265_RSV_NVCL41 = 41,
80  H265_RSV_NVCL44 = 44,
81  H265_UNSPEC48 = 48,
82  H265_UNSPEC55 = 55,
83  };
84  enum CodecType {
85  kH264,
86  kH265,
87  };
88 
89  Nalu();
90 
91  bool Initialize(CodecType type,
92  const uint8_t* data,
93  uint64_t size) WARN_UNUSED_RESULT;
94 
96  const uint8_t* data() const { return data_; }
97 
99  uint64_t header_size() const { return header_size_; }
101  uint64_t payload_size() const { return payload_size_; }
102 
103  // H.264 Specific:
104  int ref_idc() const { return ref_idc_; }
105 
106  // H.265 Specific:
107  int nuh_layer_id() const { return nuh_layer_id_; }
108  int nuh_temporal_id() const { return nuh_temporal_id_; }
109 
112  int type() const { return type_; }
113  bool is_aud() const { return is_aud_; }
114  bool is_video_slice() const { return is_video_slice_; }
115  bool can_start_access_unit() const { return can_start_access_unit_; }
116 
117  private:
118  bool InitializeFromH264(const uint8_t* data, uint64_t size);
119  bool InitializeFromH265(const uint8_t* data, uint64_t size);
120 
121  // A pointer to the NALU (i.e. points to the header). This pointer is not
122  // owned by this instance.
123  const uint8_t* data_ = nullptr;
124  // NALU header size (e.g. 1 byte for H.264). Note that it does not include
125  // header extension data in some NAL units.
126  uint64_t header_size_ = 0;
127  // Size of data after the header.
128  uint64_t payload_size_ = 0;
129 
130  int ref_idc_ = 0;
131  int nuh_layer_id_ = 0;
132  int nuh_temporal_id_ = 0;
133  int type_ = 0;
134  bool is_aud_ = false;
135  bool is_video_slice_ = false;
136  bool can_start_access_unit_ = false;
137 
138  // Don't use DISALLOW_COPY_AND_ASSIGN since it is just numbers and a pointer
139  // it does not own. This allows Nalus to be stored in a vector.
140 };
141 
145 class NaluReader {
146  public:
147  enum Result {
148  kOk,
149  kInvalidStream, // error in stream
150  kEOStream, // end of stream
151  };
152 
156  NaluReader(Nalu::CodecType type,
157  uint8_t nal_length_size,
158  const uint8_t* stream,
159  uint64_t stream_size);
160  ~NaluReader();
161 
162  // Find offset from start of data to next NALU start code
163  // and size of found start code (3 or 4 bytes).
164  // If no start code is found, offset is pointing to the first unprocessed byte
165  // (i.e. the first byte that was not considered as a possible start of a start
166  // code) and |*start_code_size| is set to 0.
167  // Postconditions:
168  // - |*offset| is between 0 and |data_size| included.
169  // It is strictly less than |data_size| if |data_size| > 0.
170  // - |*start_code_size| is either 0, 3 or 4.
171  static bool FindStartCode(const uint8_t* data,
172  uint64_t data_size,
173  uint64_t* offset,
174  uint8_t* start_code_size);
175 
181  Result Advance(Nalu* nalu);
182 
184  bool StartsWithStartCode();
185 
186  private:
187  enum Format {
188  kAnnexbByteStreamFormat,
189  kNalUnitStreamFormat
190  };
191 
192  // Move the stream pointer to the beginning of the next NALU,
193  // i.e. pointing at the next start code.
194  // Return true if a NALU has been found.
195  // If a NALU is found:
196  // - its size in bytes is returned in |*nalu_size| and includes
197  // the start code as well as the trailing zero bits.
198  // - the size in bytes of the start code is returned in |*start_code_size|.
199  bool LocateNaluByStartCode(uint64_t* nalu_size, uint8_t* start_code_size);
200 
201  // Pointer to the current NALU in the stream.
202  const uint8_t* stream_;
203  // The remaining size of the stream.
204  uint64_t stream_size_;
205  // The type of NALU being read.
206  Nalu::CodecType nalu_type_;
207  // The number of bytes the prefix length is; only valid if format is
208  // kAnnexbByteStreamFormat.
209  uint8_t nalu_length_size_;
210  // The format of the stream.
211  Format format_;
212 
213  DISALLOW_COPY_AND_ASSIGN(NaluReader);
214 };
215 
216 } // namespace media
217 } // namespace shaka
218 
219 #endif // MEDIA_CODECS_NALU_READER_H_
uint64_t payload_size() const
Size of this Nalu minus header_size().
Definition: nalu_reader.h:101
int type() const
Definition: nalu_reader.h:112
uint64_t header_size() const
The size of the header, e.g. 1 for H.264.
Definition: nalu_reader.h:99
Result Advance(Nalu *nalu)
Definition: nalu_reader.cc:175
NaluReader(Nalu::CodecType type, uint8_t nal_length_size, const uint8_t *stream, uint64_t stream_size)
Definition: nalu_reader.cc:161
const uint8_t * data() const
This is the pointer to the Nalu data, pointing to the header.
Definition: nalu_reader.h:96