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 
40  Nalu();
41 
42  bool InitializeFromH264(const uint8_t* data,
43  uint64_t size) WARN_UNUSED_RESULT;
44 
45  const uint8_t* data() const { return data_; }
46  uint64_t header_size() const { return header_size_; }
47  uint64_t payload_size() const { return payload_size_; }
48 
49  int ref_idc() const { return ref_idc_; }
50  int type() const { return type_; }
51  bool is_video_slice() const { return is_video_slice_; }
52 
53  private:
54  // A pointer to the NALU (i.e. points to the header). This pointer is not
55  // owned by this instance.
56  const uint8_t* data_;
57  // NALU header size (e.g. 1 byte for H.264). Note that it does not include
58  // header extension data in some NAL units.
59  uint64_t header_size_;
60  // Size of data after the header.
61  uint64_t payload_size_;
62 
63  int ref_idc_;
64  int type_;
65  bool is_video_slice_;
66 
67  DISALLOW_COPY_AND_ASSIGN(Nalu);
68 };
69 
73 class NaluReader {
74  public:
75  enum Result {
76  kOk,
77  kInvalidStream, // error in stream
78  kEOStream, // end of stream
79  };
80 
84  NaluReader(uint8_t nal_length_size,
85  const uint8_t* stream,
86  uint64_t stream_size);
87  ~NaluReader();
88 
89  // Find offset from start of data to next NALU start code
90  // and size of found start code (3 or 4 bytes).
91  // If no start code is found, offset is pointing to the first unprocessed byte
92  // (i.e. the first byte that was not considered as a possible start of a start
93  // code) and |*start_code_size| is set to 0.
94  // Postconditions:
95  // - |*offset| is between 0 and |data_size| included.
96  // It is strictly less than |data_size| if |data_size| > 0.
97  // - |*start_code_size| is either 0, 3 or 4.
98  static bool FindStartCode(const uint8_t* data,
99  uint64_t data_size,
100  uint64_t* offset,
101  uint8_t* start_code_size);
102 
108  Result Advance(Nalu* nalu);
109 
111  bool StartsWithStartCode();
112 
113  private:
114  enum Format {
115  kAnnexbByteStreamFormat,
116  kNalUnitStreamFormat
117  };
118 
119  // Move the stream pointer to the beginning of the next NALU,
120  // i.e. pointing at the next start code.
121  // Return true if a NALU has been found.
122  // If a NALU is found:
123  // - its size in bytes is returned in |*nalu_size| and includes
124  // the start code as well as the trailing zero bits.
125  // - the size in bytes of the start code is returned in |*start_code_size|.
126  bool LocateNaluByStartCode(uint64_t* nalu_size, uint8_t* start_code_size);
127 
128  // Pointer to the current NALU in the stream.
129  const uint8_t* stream_;
130  // The remaining size of the stream.
131  uint64_t stream_size_;
132  // The number of bytes the prefix length is; only valid if format is
133  // kAnnexbByteStreamFormat.
134  uint8_t nalu_length_size_;
135  // The format of the stream.
136  Format format_;
137 
138  DISALLOW_COPY_AND_ASSIGN(NaluReader);
139 };
140 
141 } // namespace media
142 } // namespace edash_packager
143 
144 #endif // MEDIA_FILTERS_NALU_READER_H_
NaluReader(uint8_t nal_length_size, const uint8_t *stream, uint64_t stream_size)
Definition: nalu_reader.cc:47