7 #include "packager/media/filters/nalu_reader.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/buffer_reader.h"
13 #include "packager/media/filters/h264_parser.h"
15 namespace edash_packager {
19 inline bool IsStartCode(
const uint8_t* data) {
20 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
32 is_video_slice_(false) {}
34 bool Nalu::Initialize(CodecType type,
37 if (type == Nalu::kH264) {
38 return InitializeFromH264(data, size);
40 DCHECK_EQ(Nalu::kH265, type);
41 return InitializeFromH265(data, size);
46 bool Nalu::InitializeFromH264(
const uint8_t* data, uint64_t size) {
50 const uint8_t header = data[0];
51 if ((header & 0x80) != 0) {
52 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x"
53 << std::hex << static_cast<int>(header) <<
").";
59 payload_size_ = size - header_size_;
60 ref_idc_ = (header >> 5) & 0x3;
61 type_ = header & 0x1F;
64 if (type_ == Nalu::H264_Unspecified || type_ == Nalu::H264_Reserved17 ||
65 type_ == Nalu::H264_Reserved18 || type_ >= Nalu::H264_Reserved22) {
66 LOG(WARNING) <<
"Unspecified or reserved nal_unit_type " << type_
67 <<
" (header 0x" << std::hex << static_cast<int>(header)
70 }
else if (type_ == Nalu::H264_IDRSlice || type_ == Nalu::H264_SPS ||
71 type_ == Nalu::H264_SPSExtension || type_ == Nalu::H264_SubsetSPS ||
72 type_ == Nalu::H264_PPS) {
74 LOG(WARNING) <<
"nal_ref_idc shall not be equal to 0 for nalu type "
75 << type_ <<
" (header 0x" << std::hex
76 <<
static_cast<int>(header) <<
").";
79 }
else if (type_ == Nalu::H264_SEIMessage ||
80 (type_ >= Nalu::H264_AUD && type_ <= Nalu::H264_FillerData)) {
82 LOG(WARNING) <<
"nal_ref_idc shall be equal to 0 for nalu type " << type_
83 <<
" (header 0x" << std::hex << static_cast<int>(header)
89 is_video_slice_ = (type_ >= Nalu::H264_NonIDRSlice &&
90 type_ <= Nalu::H264_IDRSlice);
95 bool Nalu::InitializeFromH265(
const uint8_t* data, uint64_t size) {
99 const uint16_t header = (data[0] << 8) | data[1];
100 if ((header & 0x8000) != 0) {
101 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x"
102 << std::hex << header <<
").";
108 payload_size_ = size - header_size_;
110 type_ = (header >> 9) & 0x3F;
111 nuh_layer_id_ = (header >> 3) & 0x3F;
112 const int nuh_temporal_id_plus1 = header & 0x7;
113 if (nuh_temporal_id_plus1 == 0) {
114 LOG(WARNING) <<
"nul_temporal_id_plus1 shall not be equal to 0 (header 0x"
115 << std::hex << header <<
").";
118 nuh_temporal_id_ = nuh_temporal_id_plus1 - 1;
120 if (type_ == Nalu::H265_EOB && nuh_layer_id_ != 0) {
121 LOG(WARNING) <<
"nuh_layer_id shall be equal to 0 for nalu type " << type_
122 <<
" (header 0x" << std::hex << header <<
").";
127 if ((type_ >= Nalu::H265_RSV_VCL_N10 && type_ <= Nalu::H265_RSV_VCL_R15) ||
128 (type_ >= Nalu::H265_RSV_IRAP_VCL22 && type_ < Nalu::H265_RSV_VCL31) ||
129 (type_ >= Nalu::H265_RSV_NVCL41)) {
130 LOG(WARNING) <<
"Unspecified or reserved nal_unit_type " << type_
131 <<
" (header 0x" << std::hex << header <<
").";
133 }
else if ((type_ >= Nalu::H265_BLA_W_LP &&
134 type_ <= Nalu::H265_RSV_IRAP_VCL23) ||
135 type_ == Nalu::H265_VPS || type_ == Nalu::H265_SPS ||
136 type_ == Nalu::H265_EOS || type_ == Nalu::H265_EOB) {
137 if (nuh_temporal_id_ != 0) {
138 LOG(WARNING) <<
"TemporalId shall be equal to 0 for nalu type " << type_
139 <<
" (header 0x" << std::hex << header <<
").";
142 }
else if (type_ == Nalu::H265_TSA_N || type_ == Nalu::H265_TSA_R ||
143 (nuh_layer_id_ == 0 &&
144 (type_ == Nalu::H265_STSA_N || type_ == Nalu::H265_STSA_R))) {
145 if (nuh_temporal_id_ == 0) {
146 LOG(WARNING) <<
"TemporalId shall not be equal to 0 for nalu type "
147 << type_ <<
" (header 0x" << std::hex << header <<
").";
152 is_video_slice_ = type_ >= Nalu::H265_TRAIL_N && type_ <= Nalu::H265_CRA_NUT;
157 uint8_t nal_length_size,
158 const uint8_t* stream,
159 uint64_t stream_size)
161 stream_size_(stream_size),
163 nalu_length_size_(nal_length_size),
164 format_(nal_length_size == 0 ? kAnnexbByteStreamFormat
165 : kNalUnitStreamFormat) {
168 NaluReader::~NaluReader() {}
171 if (stream_size_ <= 0)
172 return NaluReader::kEOStream;
174 uint8_t nalu_length_size_or_start_code_size;
175 uint64_t nalu_length;
176 if (format_ == kAnnexbByteStreamFormat) {
178 uint64_t nalu_length_with_header;
179 if (!LocateNaluByStartCode(&nalu_length_with_header,
180 &nalu_length_size_or_start_code_size)) {
181 LOG(ERROR) <<
"Could not find next NALU, bytes left in stream: "
186 return NaluReader::kInvalidStream;
188 nalu_length = nalu_length_with_header - nalu_length_size_or_start_code_size;
192 return NaluReader::kInvalidStream;
193 nalu_length_size_or_start_code_size = nalu_length_size_;
195 if (nalu_length + nalu_length_size_ > stream_size_) {
196 LOG(ERROR) <<
"NALU length exceeds stream size: "
197 << stream_size_ <<
" < " << nalu_length;
198 return NaluReader::kInvalidStream;
200 if (nalu_length == 0) {
201 LOG(ERROR) <<
"NALU size 0";
202 return NaluReader::kInvalidStream;
206 const uint8_t* nalu_data = stream_ + nalu_length_size_or_start_code_size;
207 if (!nalu->Initialize(nalu_type_, nalu_data, nalu_length))
208 return NaluReader::kInvalidStream;
212 stream_ += nalu_length_size_or_start_code_size + nalu_length;
213 stream_size_ -= nalu_length_size_or_start_code_size + nalu_length;
215 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->type())
216 <<
" at: " << reinterpret_cast<const void*>(nalu->data())
217 <<
" data size: " << nalu->payload_size();
219 return NaluReader::kOk;
223 if (stream_size_ >= 3) {
224 if (IsStartCode(stream_))
227 if (stream_size_ >= 4) {
228 if (stream_[0] == 0x00 && IsStartCode(stream_ + 1))
235 bool NaluReader::FindStartCode(
const uint8_t* data,
238 uint8_t* start_code_size) {
239 uint64_t bytes_left = data_size;
241 while (bytes_left >= 3) {
242 if (IsStartCode(data)) {
244 *offset = data_size - bytes_left;
245 *start_code_size = 3;
249 if (*offset > 0 && *(data - 1) == 0x00) {
251 ++(*start_code_size);
263 *offset = data_size - bytes_left;
264 *start_code_size = 0;
268 bool NaluReader::LocateNaluByStartCode(uint64_t* nalu_size,
269 uint8_t* start_code_size) {
271 uint64_t nalu_start_off = 0;
272 uint8_t annexb_start_code_size = 0;
273 if (!FindStartCode(stream_, stream_size_,
274 &nalu_start_off, &annexb_start_code_size)) {
275 DVLOG(4) <<
"Could not find start code, end of stream?";
280 stream_ += nalu_start_off;
281 stream_size_ -= nalu_start_off;
283 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
284 uint64_t max_nalu_data_size = stream_size_ - annexb_start_code_size;
285 if (max_nalu_data_size <= 0) {
286 DVLOG(3) <<
"End of stream";
296 uint64_t nalu_size_without_start_code = 0;
297 uint8_t next_start_code_size = 0;
299 if (!FindStartCode(nalu_data, max_nalu_data_size,
300 &nalu_size_without_start_code, &next_start_code_size)) {
301 nalu_data += max_nalu_data_size;
305 nalu_data += nalu_size_without_start_code + next_start_code_size;
306 max_nalu_data_size -= nalu_size_without_start_code + next_start_code_size;
310 if (nalu.Initialize(nalu_type_, nalu_data, max_nalu_data_size)) {
311 nalu_data -= next_start_code_size;
314 LOG(WARNING) <<
"Seeing invalid NAL unit. Emulation prevention may not "
315 "have been applied properly. Assuming it is part of the "
316 "previous NAL unit.";
318 *nalu_size = nalu_data - stream_;
319 *start_code_size = annexb_start_code_size;