DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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_FILTERS_NALU_READER_H_
8 #define MEDIA_FILTERS_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 edash_packager {
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_TSA_N = 2,
50  H265_TSA_R = 3,
51  H265_STSA_N = 4,
52  H265_STSA_R = 5,
53  H265_RASL_R = 9,
54 
55  H265_RSV_VCL_N10 = 10,
56  H265_RSV_VCL_R15 = 15,
57 
58  H265_BLA_W_LP = 16,
59  H265_IDR_W_RADL = 19,
60  H265_IDR_N_LP = 20,
61  H265_CRA_NUT = 21,
62 
63  H265_RSV_IRAP_VCL22 = 22,
64  H265_RSV_IRAP_VCL23 = 23,
65  H265_RSV_VCL31 = 31,
66 
67  H265_VPS = 32,
68  H265_SPS = 33,
69  H265_PPS = 34,
70  H265_AUD = 35,
71 
72  H265_EOS = 36,
73  H265_EOB = 37,
74 
75  H265_PREFIX_SEI = 39,
76 
77  H265_RSV_NVCL41 = 41,
78  H265_RSV_NVCL44 = 44,
79  H265_UNSPEC48 = 48,
80  H265_UNSPEC55 = 55,
81  };
82  enum CodecType {
83  kH264,
84  kH265,
85  };
86 
87  Nalu();
88 
89  bool Initialize(CodecType type,
90  const uint8_t* data,
91  uint64_t size) WARN_UNUSED_RESULT;
92 
93  const uint8_t* data() const { return data_; }
94  uint64_t header_size() const { return header_size_; }
95  uint64_t payload_size() const { return payload_size_; }
96 
97  // H.264 Specific:
98  int ref_idc() const { return ref_idc_; }
99 
100  // H.265 Specific:
101  int nuh_layer_id() const { return nuh_layer_id_; }
102  int nuh_temporal_id() const { return nuh_temporal_id_; }
103 
104  int type() const { return type_; }
105  bool is_video_slice() const { return is_video_slice_; }
106  bool can_start_access_unit() const { return can_start_access_unit_; }
107 
108  private:
109  bool InitializeFromH264(const uint8_t* data, uint64_t size);
110  bool InitializeFromH265(const uint8_t* data, uint64_t size);
111 
112  // A pointer to the NALU (i.e. points to the header). This pointer is not
113  // owned by this instance.
114  const uint8_t* data_;
115  // NALU header size (e.g. 1 byte for H.264). Note that it does not include
116  // header extension data in some NAL units.
117  uint64_t header_size_;
118  // Size of data after the header.
119  uint64_t payload_size_;
120 
121  int ref_idc_;
122  int nuh_layer_id_;
123  int nuh_temporal_id_;
124  int type_;
125  bool is_video_slice_;
126  bool can_start_access_unit_;
127 
128  // Don't use DISALLOW_COPY_AND_ASSIGN since it is just numbers and a pointer
129  // it does not own. This allows Nalus to be stored in a vector.
130 };
131 
135 class NaluReader {
136  public:
137  enum Result {
138  kOk,
139  kInvalidStream, // error in stream
140  kEOStream, // end of stream
141  };
142 
146  NaluReader(Nalu::CodecType type,
147  uint8_t nal_length_size,
148  const uint8_t* stream,
149  uint64_t stream_size);
150  ~NaluReader();
151 
152  // Find offset from start of data to next NALU start code
153  // and size of found start code (3 or 4 bytes).
154  // If no start code is found, offset is pointing to the first unprocessed byte
155  // (i.e. the first byte that was not considered as a possible start of a start
156  // code) and |*start_code_size| is set to 0.
157  // Postconditions:
158  // - |*offset| is between 0 and |data_size| included.
159  // It is strictly less than |data_size| if |data_size| > 0.
160  // - |*start_code_size| is either 0, 3 or 4.
161  static bool FindStartCode(const uint8_t* data,
162  uint64_t data_size,
163  uint64_t* offset,
164  uint8_t* start_code_size);
165 
171  Result Advance(Nalu* nalu);
172 
174  bool StartsWithStartCode();
175 
176  private:
177  enum Format {
178  kAnnexbByteStreamFormat,
179  kNalUnitStreamFormat
180  };
181 
182  // Move the stream pointer to the beginning of the next NALU,
183  // i.e. pointing at the next start code.
184  // Return true if a NALU has been found.
185  // If a NALU is found:
186  // - its size in bytes is returned in |*nalu_size| and includes
187  // the start code as well as the trailing zero bits.
188  // - the size in bytes of the start code is returned in |*start_code_size|.
189  bool LocateNaluByStartCode(uint64_t* nalu_size, uint8_t* start_code_size);
190 
191  // Pointer to the current NALU in the stream.
192  const uint8_t* stream_;
193  // The remaining size of the stream.
194  uint64_t stream_size_;
195  // The type of NALU being read.
196  Nalu::CodecType nalu_type_;
197  // The number of bytes the prefix length is; only valid if format is
198  // kAnnexbByteStreamFormat.
199  uint8_t nalu_length_size_;
200  // The format of the stream.
201  Format format_;
202 
203  DISALLOW_COPY_AND_ASSIGN(NaluReader);
204 };
205 
206 } // namespace media
207 } // namespace edash_packager
208 
209 #endif // MEDIA_FILTERS_NALU_READER_H_
NaluReader(Nalu::CodecType type, uint8_t nal_length_size, const uint8_t *stream, uint64_t stream_size)
Definition: nalu_reader.cc:167