7 #include "packager/media/codecs/nalu_reader.h" 11 #include "packager/base/logging.h" 12 #include "packager/media/base/buffer_reader.h" 13 #include "packager/media/codecs/h264_parser.h" 19 inline bool IsStartCode(
const uint8_t* data) {
20 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
24 void UpdateSubsamples(uint64_t consumed_bytes,
25 std::vector<SubsampleEntry>* subsamples) {
26 if (consumed_bytes == 0 || subsamples->empty()) {
29 size_t num_entries_to_delete = 0;
30 for (SubsampleEntry& subsample : *subsamples) {
31 if (subsample.clear_bytes > consumed_bytes) {
32 subsample.clear_bytes -= consumed_bytes;
36 consumed_bytes -= subsample.clear_bytes;
37 subsample.clear_bytes = 0;
39 if (subsample.cipher_bytes > consumed_bytes) {
40 subsample.cipher_bytes -= consumed_bytes;
44 consumed_bytes -= subsample.cipher_bytes;
45 subsample.cipher_bytes = 0;
46 ++num_entries_to_delete;
49 subsamples->erase(subsamples->begin(),
50 subsamples->begin() + num_entries_to_delete);
53 bool IsNaluLengthEncrypted(
54 uint8_t nalu_length_size,
55 const std::vector<SubsampleEntry>& subsamples) {
56 if (subsamples.empty())
59 for (
const SubsampleEntry& subsample : subsamples) {
60 if (subsample.clear_bytes >= nalu_length_size) {
63 nalu_length_size -= subsample.clear_bytes;
64 if (subsample.cipher_bytes > 0) {
73 Nalu::Nalu() =
default;
75 bool Nalu::Initialize(CodecType type,
78 if (type == Nalu::kH264) {
79 return InitializeFromH264(data, size);
81 DCHECK_EQ(Nalu::kH265, type);
82 return InitializeFromH265(data, size);
87 bool Nalu::InitializeFromH264(
const uint8_t* data, uint64_t size) {
91 const uint8_t header = data[0];
92 if ((header & 0x80) != 0) {
93 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x" 94 << std::hex << static_cast<int>(header) <<
").";
100 payload_size_ = size - header_size_;
101 ref_idc_ = (header >> 5) & 0x3;
102 type_ = header & 0x1F;
105 if (type_ == Nalu::H264_Unspecified || type_ == Nalu::H264_Reserved17 ||
106 type_ == Nalu::H264_Reserved18 || type_ >= Nalu::H264_Reserved22) {
107 VLOG(1) <<
"Unspecified or reserved nal_unit_type " << type_
108 <<
" (header 0x" << std::hex << static_cast<int>(header) <<
").";
111 }
else if (type_ == Nalu::H264_IDRSlice || type_ == Nalu::H264_SPS ||
112 type_ == Nalu::H264_SPSExtension ||
113 type_ == Nalu::H264_SubsetSPS || type_ == Nalu::H264_PPS) {
115 LOG(WARNING) <<
"nal_ref_idc shall not be equal to 0 for nalu type " 116 << type_ <<
" (header 0x" << std::hex
117 <<
static_cast<int>(header) <<
").";
120 }
else if (type_ == Nalu::H264_SEIMessage ||
121 (type_ >= Nalu::H264_AUD && type_ <= Nalu::H264_FillerData)) {
123 LOG(WARNING) <<
"nal_ref_idc shall be equal to 0 for nalu type " << type_
124 <<
" (header 0x" << std::hex << static_cast<int>(header)
130 is_aud_ = type_ == H264_AUD;
131 is_vcl_ = (type_ >= Nalu::H264_NonIDRSlice && type_ <= Nalu::H264_IDRSlice);
133 (type_ == Nalu::H264_NonIDRSlice || type_ == Nalu::H264_IDRSlice);
134 can_start_access_unit_ =
135 (is_vcl_ || type_ == Nalu::H264_AUD || type_ == Nalu::H264_SPS ||
136 type_ == Nalu::H264_PPS || type_ == Nalu::H264_SEIMessage ||
137 (type_ >= Nalu::H264_PrefixNALUnit && type_ <= Nalu::H264_Reserved18));
142 bool Nalu::InitializeFromH265(
const uint8_t* data, uint64_t size) {
146 const uint16_t header = (data[0] << 8) | data[1];
147 if ((header & 0x8000) != 0) {
148 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x" 149 << std::hex << header <<
").";
155 payload_size_ = size - header_size_;
157 type_ = (header >> 9) & 0x3F;
158 nuh_layer_id_ = (header >> 3) & 0x3F;
159 const int nuh_temporal_id_plus1 = header & 0x7;
160 if (nuh_temporal_id_plus1 == 0) {
161 LOG(WARNING) <<
"nul_temporal_id_plus1 shall not be equal to 0 (header 0x" 162 << std::hex << header <<
").";
165 nuh_temporal_id_ = nuh_temporal_id_plus1 - 1;
167 if (type_ == Nalu::H265_EOB && nuh_layer_id_ != 0) {
168 LOG(WARNING) <<
"nuh_layer_id shall be equal to 0 for nalu type " << type_
169 <<
" (header 0x" << std::hex << header <<
").";
174 if ((type_ >= Nalu::H265_RSV_VCL_N10 && type_ <= Nalu::H265_RSV_VCL_R15) ||
175 (type_ >= Nalu::H265_RSV_IRAP_VCL22 && type_ < Nalu::H265_RSV_VCL31) ||
176 (type_ >= Nalu::H265_RSV_NVCL41)) {
177 VLOG(1) <<
"Unspecified or reserved nal_unit_type " << type_
178 <<
" (header 0x" << std::hex << header <<
").";
182 }
else if ((type_ >= Nalu::H265_BLA_W_LP &&
183 type_ <= Nalu::H265_RSV_IRAP_VCL23) ||
184 type_ == Nalu::H265_VPS || type_ == Nalu::H265_SPS ||
185 type_ == Nalu::H265_EOS || type_ == Nalu::H265_EOB) {
186 if (nuh_temporal_id_ != 0) {
187 LOG(WARNING) <<
"TemporalId shall be equal to 0 for nalu type " << type_
188 <<
" (header 0x" << std::hex << header <<
").";
191 }
else if (type_ == Nalu::H265_TSA_N || type_ == Nalu::H265_TSA_R ||
192 (nuh_layer_id_ == 0 &&
193 (type_ == Nalu::H265_STSA_N || type_ == Nalu::H265_STSA_R))) {
194 if (nuh_temporal_id_ == 0) {
195 LOG(WARNING) <<
"TemporalId shall not be equal to 0 for nalu type " 196 << type_ <<
" (header 0x" << std::hex << header <<
").";
201 is_aud_ = type_ == H265_AUD;
202 is_vcl_ = type_ >= Nalu::H265_TRAIL_N && type_ <= Nalu::H265_RSV_VCL31;
203 is_video_slice_ = is_vcl_;
204 can_start_access_unit_ =
205 nuh_layer_id_ == 0 &&
206 (is_vcl_ || type_ == Nalu::H265_AUD || type_ == Nalu::H265_VPS ||
207 type_ == Nalu::H265_SPS || type_ == Nalu::H265_PPS ||
208 type_ == Nalu::H265_PREFIX_SEI ||
209 (type_ >= Nalu::H265_RSV_NVCL41 && type_ <= Nalu::H265_RSV_NVCL44) ||
210 (type_ >= Nalu::H265_UNSPEC48 && type_ <= Nalu::H265_UNSPEC55));
215 uint8_t nal_length_size,
216 const uint8_t* stream,
217 uint64_t stream_size)
225 uint8_t nal_length_size,
226 const uint8_t* stream,
227 uint64_t stream_size,
228 const std::vector<SubsampleEntry>& subsamples)
230 stream_size_(stream_size),
232 nalu_length_size_(nal_length_size),
233 format_(nal_length_size == 0 ? kAnnexbByteStreamFormat
234 : kNalUnitStreamFormat),
235 subsamples_(subsamples) {
239 NaluReader::~NaluReader() {}
242 if (stream_size_ <= 0)
243 return NaluReader::kEOStream;
245 uint8_t nalu_length_size_or_start_code_size;
246 uint64_t nalu_length;
247 if (format_ == kAnnexbByteStreamFormat) {
249 uint64_t nalu_length_with_header;
250 if (!LocateNaluByStartCode(&nalu_length_with_header,
251 &nalu_length_size_or_start_code_size)) {
252 LOG(ERROR) <<
"Could not find next NALU, bytes left in stream: " 257 return NaluReader::kInvalidStream;
259 nalu_length = nalu_length_with_header - nalu_length_size_or_start_code_size;
262 if (IsNaluLengthEncrypted(nalu_length_size_, subsamples_)) {
263 LOG(ERROR) <<
"NALU length is encrypted.";
264 return NaluReader::kInvalidStream;
267 return NaluReader::kInvalidStream;
268 nalu_length_size_or_start_code_size = nalu_length_size_;
270 if (nalu_length + nalu_length_size_ > stream_size_) {
271 LOG(ERROR) <<
"NALU length exceeds stream size: " 272 << stream_size_ <<
" < " << nalu_length;
273 return NaluReader::kInvalidStream;
275 if (nalu_length == 0) {
276 LOG(ERROR) <<
"NALU size 0";
277 return NaluReader::kInvalidStream;
281 const uint8_t* nalu_data = stream_ + nalu_length_size_or_start_code_size;
282 if (!nalu->Initialize(nalu_type_, nalu_data, nalu_length))
283 return NaluReader::kInvalidStream;
287 stream_ += nalu_length_size_or_start_code_size + nalu_length;
288 stream_size_ -= nalu_length_size_or_start_code_size + nalu_length;
289 UpdateSubsamples(nalu_length_size_or_start_code_size + nalu_length,
292 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->
type())
293 <<
" at: " << reinterpret_cast<const void*>(nalu->
data())
296 return NaluReader::kOk;
300 if (stream_size_ >= 3) {
301 if (IsStartCode(stream_))
304 if (stream_size_ >= 4) {
305 if (stream_[0] == 0x00 && IsStartCode(stream_ + 1))
312 bool NaluReader::FindStartCode(
const uint8_t* data,
315 uint8_t* start_code_size) {
316 uint64_t bytes_left = data_size;
318 while (bytes_left >= 3) {
319 if (IsStartCode(data)) {
321 *offset = data_size - bytes_left;
322 *start_code_size = 3;
326 if (*offset > 0 && *(data - 1) == 0x00) {
328 ++(*start_code_size);
340 *offset = data_size - bytes_left;
341 *start_code_size = 0;
350 uint8_t* start_code_size,
351 const std::vector<SubsampleEntry>& subsamples) {
352 if (subsamples.empty()) {
353 return FindStartCode(data, data_size, offset, start_code_size);
356 uint64_t current_offset = 0;
358 uint16_t clear_bytes = subsample.clear_bytes;
359 if (current_offset + clear_bytes > data_size) {
360 LOG(WARNING) <<
"The sum of subsample sizes is greater than data_size.";
361 clear_bytes = data_size - current_offset;
368 const bool found_start_code = FindStartCode(
369 data + current_offset, clear_bytes, offset, start_code_size);
370 if (found_start_code) {
371 *offset += current_offset;
374 const uint64_t subsample_size =
375 subsample.clear_bytes + subsample.cipher_bytes;
376 current_offset += subsample_size;
377 if (current_offset > data_size) {
380 current_offset = data_size;
381 LOG(WARNING) <<
"The sum of subsamples is greater than data_size.";
388 if (current_offset < data_size) {
389 const bool found_start_code =
390 FindStartCode(data + current_offset, data_size - current_offset, offset,
392 *offset += current_offset;
393 return found_start_code;
398 *offset = current_offset;
399 *start_code_size = 0;
403 bool NaluReader::LocateNaluByStartCode(uint64_t* nalu_size,
404 uint8_t* start_code_size) {
406 uint64_t nalu_start_off = 0;
407 uint8_t annexb_start_code_size = 0;
409 stream_, stream_size_,
410 &nalu_start_off, &annexb_start_code_size, subsamples_)) {
411 DVLOG(4) <<
"Could not find start code, end of stream?";
416 stream_ += nalu_start_off;
417 stream_size_ -= nalu_start_off;
420 UpdateSubsamples(nalu_start_off, &subsamples_);
422 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
425 std::vector<SubsampleEntry> subsamples_for_finding_next_nalu;
426 if (!subsamples_.empty()) {
427 subsamples_for_finding_next_nalu = subsamples_;
428 UpdateSubsamples(annexb_start_code_size, &subsamples_for_finding_next_nalu);
430 uint64_t max_nalu_data_size = stream_size_ - annexb_start_code_size;
431 if (max_nalu_data_size <= 0) {
432 DVLOG(3) <<
"End of stream";
442 uint64_t nalu_size_without_start_code = 0;
443 uint8_t next_start_code_size = 0;
446 nalu_data, max_nalu_data_size,
447 &nalu_size_without_start_code, &next_start_code_size,
448 subsamples_for_finding_next_nalu)) {
449 nalu_data += max_nalu_data_size;
453 nalu_data += nalu_size_without_start_code + next_start_code_size;
454 max_nalu_data_size -= nalu_size_without_start_code + next_start_code_size;
455 UpdateSubsamples(nalu_size_without_start_code + next_start_code_size,
456 &subsamples_for_finding_next_nalu);
460 if (nalu.Initialize(nalu_type_, nalu_data, max_nalu_data_size)) {
461 nalu_data -= next_start_code_size;
464 LOG(WARNING) <<
"Seeing invalid NAL unit. Emulation prevention may not " 465 "have been applied properly. Assuming it is part of the " 466 "previous NAL unit.";
468 *nalu_size = nalu_data - stream_;
469 *start_code_size = annexb_start_code_size;
All the methods that are virtual are virtual for mocking.