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