Shaka Packager SDK
Loading...
Searching...
No Matches
nalu_reader.h
1// Copyright 2016 Google LLC. 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 PACKAGER_MEDIA_CODECS_NALU_READER_H_
8#define PACKAGER_MEDIA_CODECS_NALU_READER_H_
9
10#include <cstdint>
11#include <cstdlib>
12#include <vector>
13
14#include <packager/macros/classes.h>
15#include <packager/media/base/decrypt_config.h>
16
17namespace shaka {
18namespace media {
19
20// Used as the |nalu_length_size| argument to NaluReader to indicate to use
21// AnnexB byte streams. An AnnexB byte stream starts with 3 or 4 byte start
22// codes instead of a fixed size NAL unit length.
23const uint8_t kIsAnnexbByteStream = 0;
24
27class Nalu {
28 public:
29 enum H264NaluType {
30 H264_Unspecified = 0,
31 H264_NonIDRSlice = 1,
32 H264_IDRSlice = 5,
33 H264_SEIMessage = 6,
34 H264_SPS = 7,
35 H264_PPS = 8,
36 H264_AUD = 9,
37 H264_EOSeq = 10,
38 H264_FillerData = 12,
39 H264_SPSExtension = 13,
40 H264_PrefixNALUnit = 14,
41 H264_SubsetSPS = 15,
42 H264_DepthParameterSet = 16,
43 H264_Reserved17 = 17,
44 H264_Reserved18 = 18,
45 H264_CodedSliceExtension = 20,
46 H264_Reserved22 = 22,
47 };
48 enum H265NaluType {
49 H265_TRAIL_N = 0,
50 H265_TRAIL_R = 1,
51 H265_TSA_N = 2,
52 H265_TSA_R = 3,
53 H265_STSA_N = 4,
54 H265_STSA_R = 5,
55 H265_RASL_R = 9,
56
57 H265_RSV_VCL_N10 = 10,
58 H265_RSV_VCL_R15 = 15,
59
60 H265_BLA_W_LP = 16,
61 H265_IDR_W_RADL = 19,
62 H265_IDR_N_LP = 20,
63 H265_CRA_NUT = 21,
64
65 H265_RSV_IRAP_VCL22 = 22,
66 H265_RSV_IRAP_VCL23 = 23,
67 H265_RSV_VCL31 = 31,
68
69 H265_VPS = 32,
70 H265_SPS = 33,
71 H265_PPS = 34,
72 H265_AUD = 35,
73
74 H265_EOS = 36,
75 H265_EOB = 37,
76 H265_FD = 38,
77
78 H265_PREFIX_SEI = 39,
79
80 H265_RSV_NVCL41 = 41,
81 H265_RSV_NVCL44 = 44,
82 H265_UNSPEC48 = 48,
83 H265_UNSPEC55 = 55,
84 };
85 enum CodecType {
86 kH264,
87 kH265,
88 };
89
90 Nalu();
91
92 [[nodiscard]] bool Initialize(CodecType type,
93 const uint8_t* data,
94 uint64_t size);
95
97 const uint8_t* data() const { return data_; }
98
100 uint64_t header_size() const { return header_size_; }
102 uint64_t payload_size() const { return payload_size_; }
103
104 // H.264 Specific:
105 int ref_idc() const { return ref_idc_; }
106
107 // H.265 Specific:
108 int nuh_layer_id() const { return nuh_layer_id_; }
109 int nuh_temporal_id() const { return nuh_temporal_id_; }
110
113 int type() const { return type_; }
114 bool is_aud() const { return is_aud_; }
115 bool is_vcl() const { return is_vcl_; }
117 bool is_video_slice() const { return is_video_slice_; }
118 bool can_start_access_unit() const { return can_start_access_unit_; }
119
120 private:
121 bool InitializeFromH264(const uint8_t* data, uint64_t size);
122 bool InitializeFromH265(const uint8_t* data, uint64_t size);
123
124 // A pointer to the NALU (i.e. points to the header). This pointer is not
125 // owned by this instance.
126 const uint8_t* data_ = nullptr;
127 // NALU header size (e.g. 1 byte for H.264). Note that it does not include
128 // header extension data in some NAL units.
129 uint64_t header_size_ = 0;
130 // Size of data after the header.
131 uint64_t payload_size_ = 0;
132
133 int ref_idc_ = 0;
134 int nuh_layer_id_ = 0;
135 int nuh_temporal_id_ = 0;
136 int type_ = 0;
137 bool is_aud_ = false;
138 bool is_vcl_ = false;
139 bool is_video_slice_ = false;
140 bool can_start_access_unit_ = false;
141
142 // Don't use DISALLOW_COPY_AND_ASSIGN since it is just numbers and a pointer
143 // it does not own. This allows Nalus to be stored in a vector.
144};
145
150 public:
151 enum Result {
152 kOk,
153 kInvalidStream, // error in stream
154 kEOStream, // end of stream
155 };
156
160 NaluReader(Nalu::CodecType type,
161 uint8_t nal_length_size,
162 const uint8_t* stream,
163 uint64_t stream_size);
164
175 NaluReader(Nalu::CodecType type,
176 uint8_t nal_length_size,
177 const uint8_t* stream,
178 uint64_t stream_size,
179 const std::vector<SubsampleEntry>& subsamples);
180 ~NaluReader();
181
182 // Find offset from start of data to next NALU start code
183 // and size of found start code (3 or 4 bytes).
184 // If no start code is found, offset is pointing to the first unprocessed byte
185 // (i.e. the first byte that was not considered as a possible start of a start
186 // code) and |*start_code_size| is set to 0.
187 // Postconditions:
188 // - |*offset| is between 0 and |data_size| included.
189 // It is strictly less than |data_size| if |data_size| > 0.
190 // - |*start_code_size| is either 0, 3 or 4.
191 static bool FindStartCode(const uint8_t* data,
192 uint64_t data_size,
193 uint64_t* offset,
194 uint8_t* start_code_size);
195
204 static bool FindStartCodeInClearRange(
205 const uint8_t* data,
206 uint64_t data_size,
207 uint64_t* offset,
208 uint8_t* start_code_size,
209 const std::vector<SubsampleEntry>& subsamples);
210
216 Result Advance(Nalu* nalu);
217
219 bool StartsWithStartCode();
220
221 private:
222 enum Format { kAnnexbByteStreamFormat, kNalUnitStreamFormat };
223
224 // Move the stream pointer to the beginning of the next NALU,
225 // i.e. pointing at the next start code.
226 // Return true if a NALU has been found.
227 // If a NALU is found:
228 // - its size in bytes is returned in |*nalu_size| and includes
229 // the start code as well as the trailing zero bits.
230 // - the size in bytes of the start code is returned in |*start_code_size|.
231 bool LocateNaluByStartCode(uint64_t* nalu_size, uint8_t* start_code_size);
232
233 // Pointer to the current NALU in the stream.
234 const uint8_t* stream_;
235 // The remaining size of the stream.
236 uint64_t stream_size_;
237 // The type of NALU being read.
238 Nalu::CodecType nalu_type_;
239 // The number of bytes the prefix length is; only valid if format is
240 // kAnnexbByteStreamFormat.
241 uint8_t nalu_length_size_;
242 // The format of the stream.
243 Format format_;
244
245 // subsamples left in stream_.
246 std::vector<SubsampleEntry> subsamples_;
247
248 DISALLOW_COPY_AND_ASSIGN(NaluReader);
249};
250
251} // namespace media
252} // namespace shaka
253
254#endif // PACKAGER_MEDIA_CODECS_NALU_READER_H_
static bool FindStartCodeInClearRange(const uint8_t *data, uint64_t data_size, uint64_t *offset, uint8_t *start_code_size, const std::vector< SubsampleEntry > &subsamples)
Result Advance(Nalu *nalu)
bool is_video_slice() const
Slice data partition NALs are not considered as slice NALs.
uint64_t header_size() const
The size of the header, e.g. 1 for H.264.
const uint8_t * data() const
This is the pointer to the Nalu data, pointing to the header.
Definition nalu_reader.h:97
uint64_t payload_size() const
Size of this Nalu minus header_size().
All the methods that are virtual are virtual for mocking.