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_CodedSliceExtension = 20,
38  };
39  enum H265NaluType {
40  H265_TRAIL_N = 0,
41  H265_RASL_R = 9,
42 
43  H265_BLA_W_LP = 16,
44  H265_IDR_W_RADL = 19,
45  H265_IDR_N_LP = 20,
46  H265_CRA_NUT = 21,
47  H265_RSV_IRAP_VCL23 = 23,
48 
49  H265_VPS = 32,
50  H265_SPS = 33,
51  H265_PPS = 34,
52  H265_AUD = 35,
53  };
54 
55  Nalu();
56 
57  bool InitializeFromH264(const uint8_t* data,
58  uint64_t size) WARN_UNUSED_RESULT;
59 
60  bool InitializeFromH265(const uint8_t* data,
61  uint64_t size) WARN_UNUSED_RESULT;
62 
63  const uint8_t* data() const { return data_; }
64  uint64_t header_size() const { return header_size_; }
65  uint64_t payload_size() const { return payload_size_; }
66 
67  // H.264 Specific:
68  int ref_idc() const { return ref_idc_; }
69 
70  // H.265 Specific:
71  int nuh_layer_id() const { return nuh_layer_id_; }
72  int nuh_temporal_id() const { return nuh_temporal_id_; }
73 
74  int type() const { return type_; }
75  bool is_video_slice() const { return is_video_slice_; }
76 
77  private:
78  // A pointer to the NALU (i.e. points to the header). This pointer is not
79  // owned by this instance.
80  const uint8_t* data_;
81  // NALU header size (e.g. 1 byte for H.264). Note that it does not include
82  // header extension data in some NAL units.
83  uint64_t header_size_;
84  // Size of data after the header.
85  uint64_t payload_size_;
86 
87  int ref_idc_;
88  int nuh_layer_id_;
89  int nuh_temporal_id_;
90  int type_;
91  bool is_video_slice_;
92 
93  // Don't use DISALLOW_COPY_AND_ASSIGN since it is just numbers and a pointer
94  // it does not own. This allows Nalus to be stored in a vector.
95 };
96 
100 class NaluReader {
101  public:
102  enum Result {
103  kOk,
104  kInvalidStream, // error in stream
105  kEOStream, // end of stream
106  };
107  enum NaluType {
108  kH264,
109  kH265,
110  };
111 
115  NaluReader(NaluType type,
116  uint8_t nal_length_size,
117  const uint8_t* stream,
118  uint64_t stream_size);
119  ~NaluReader();
120 
121  // Find offset from start of data to next NALU start code
122  // and size of found start code (3 or 4 bytes).
123  // If no start code is found, offset is pointing to the first unprocessed byte
124  // (i.e. the first byte that was not considered as a possible start of a start
125  // code) and |*start_code_size| is set to 0.
126  // Postconditions:
127  // - |*offset| is between 0 and |data_size| included.
128  // It is strictly less than |data_size| if |data_size| > 0.
129  // - |*start_code_size| is either 0, 3 or 4.
130  static bool FindStartCode(const uint8_t* data,
131  uint64_t data_size,
132  uint64_t* offset,
133  uint8_t* start_code_size);
134 
140  Result Advance(Nalu* nalu);
141 
143  bool StartsWithStartCode();
144 
145  private:
146  enum Format {
147  kAnnexbByteStreamFormat,
148  kNalUnitStreamFormat
149  };
150 
151  // Move the stream pointer to the beginning of the next NALU,
152  // i.e. pointing at the next start code.
153  // Return true if a NALU has been found.
154  // If a NALU is found:
155  // - its size in bytes is returned in |*nalu_size| and includes
156  // the start code as well as the trailing zero bits.
157  // - the size in bytes of the start code is returned in |*start_code_size|.
158  bool LocateNaluByStartCode(uint64_t* nalu_size, uint8_t* start_code_size);
159 
160  // Pointer to the current NALU in the stream.
161  const uint8_t* stream_;
162  // The remaining size of the stream.
163  uint64_t stream_size_;
164  // The type of NALU being read.
165  NaluType nalu_type_;
166  // The number of bytes the prefix length is; only valid if format is
167  // kAnnexbByteStreamFormat.
168  uint8_t nalu_length_size_;
169  // The format of the stream.
170  Format format_;
171 
172  DISALLOW_COPY_AND_ASSIGN(NaluReader);
173 };
174 
175 } // namespace media
176 } // namespace edash_packager
177 
178 #endif // MEDIA_FILTERS_NALU_READER_H_
NaluReader(NaluType type, uint8_t nal_length_size, const uint8_t *stream, uint64_t stream_size)
Definition: nalu_reader.cc:73