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 data_size() const { return data_size_; }
47  uint64_t header_size() const { return header_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  uint64_t data_size_;
58  uint64_t header_size_;
59 
60  int ref_idc_;
61  int type_;
62  bool is_video_slice_;
63 
64  DISALLOW_COPY_AND_ASSIGN(Nalu);
65 };
66 
70 class NaluReader {
71  public:
72  enum Result {
73  kOk,
74  kInvalidStream, // error in stream
75  kEOStream, // end of stream
76  };
77 
81  NaluReader(uint8_t nal_length_size,
82  const uint8_t* stream,
83  uint64_t stream_size);
84  ~NaluReader();
85 
86  // Find offset from start of data to next NALU start code
87  // and size of found start code (3 or 4 bytes).
88  // If no start code is found, offset is pointing to the first unprocessed byte
89  // (i.e. the first byte that was not considered as a possible start of a start
90  // code) and |*start_code_size| is set to 0.
91  // Postconditions:
92  // - |*offset| is between 0 and |data_size| included.
93  // It is strictly less than |data_size| if |data_size| > 0.
94  // - |*start_code_size| is either 0, 3 or 4.
95  static bool FindStartCode(const uint8_t* data,
96  uint64_t data_size,
97  uint64_t* offset,
98  uint8_t* start_code_size);
99 
105  Result Advance(Nalu* nalu);
106 
108  bool StartsWithStartCode();
109 
110  private:
111  enum Format {
112  kAnnexbByteStreamFormat,
113  kNalUnitStreamFormat
114  };
115 
116  // Move the stream pointer to the beginning of the next NALU,
117  // i.e. pointing at the next start code.
118  // Return true if a NALU has been found.
119  // If a NALU is found:
120  // - its size in bytes is returned in |*nalu_size| and includes
121  // the start code as well as the trailing zero bits.
122  // - the size in bytes of the start code is returned in |*start_code_size|.
123  bool LocateNaluByStartCode(uint64_t* nalu_size, uint8_t* start_code_size);
124 
125  // Pointer to the current NALU in the stream.
126  const uint8_t* stream_;
127  // The remaining size of the stream.
128  uint64_t stream_size_;
129  // The number of bytes the prefix length is; only valid if format is
130  // kAnnexbByteStreamFormat.
131  uint8_t nalu_length_size_;
132  // The format of the stream.
133  Format format_;
134 
135  DISALLOW_COPY_AND_ASSIGN(NaluReader);
136 };
137 
138 } // namespace media
139 } // namespace edash_packager
140 
141 #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