7 #include <packager/media/codecs/nalu_reader.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/macros/logging.h>
15 #include <packager/media/base/buffer_reader.h>
16 #include <packager/media/codecs/h264_parser.h>
22 inline bool IsStartCode(
const uint8_t* data) {
23 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
27 void UpdateSubsamples(uint64_t consumed_bytes,
28 std::vector<SubsampleEntry>* subsamples) {
29 if (consumed_bytes == 0 || subsamples->empty()) {
32 size_t num_entries_to_delete = 0;
33 for (SubsampleEntry& subsample : *subsamples) {
34 if (subsample.clear_bytes > consumed_bytes) {
35 subsample.clear_bytes -= consumed_bytes;
39 consumed_bytes -= subsample.clear_bytes;
40 subsample.clear_bytes = 0;
42 if (subsample.cipher_bytes > consumed_bytes) {
43 subsample.cipher_bytes -= consumed_bytes;
47 consumed_bytes -= subsample.cipher_bytes;
48 subsample.cipher_bytes = 0;
49 ++num_entries_to_delete;
52 subsamples->erase(subsamples->begin(),
53 subsamples->begin() + num_entries_to_delete);
56 bool IsNaluLengthEncrypted(
57 uint8_t nalu_length_size,
58 const std::vector<SubsampleEntry>& subsamples) {
59 if (subsamples.empty())
62 for (
const SubsampleEntry& subsample : subsamples) {
63 if (subsample.clear_bytes >= nalu_length_size) {
66 nalu_length_size -= subsample.clear_bytes;
67 if (subsample.cipher_bytes > 0) {
76 Nalu::Nalu() =
default;
78 bool Nalu::Initialize(CodecType type,
81 if (
type == Nalu::kH264) {
82 return InitializeFromH264(
data, size);
84 DCHECK_EQ(Nalu::kH265,
type);
85 return InitializeFromH265(
data, size);
90 bool Nalu::InitializeFromH264(
const uint8_t* data, uint64_t size) {
94 const uint8_t header =
data[0];
95 if ((header & 0x80) != 0) {
96 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x"
97 << std::hex << static_cast<int>(header) <<
").";
103 payload_size_ = size - header_size_;
104 ref_idc_ = (header >> 5) & 0x3;
105 type_ = header & 0x1F;
108 if (type_ == Nalu::H264_Unspecified || type_ == Nalu::H264_Reserved17 ||
109 type_ == Nalu::H264_Reserved18 || type_ >= Nalu::H264_Reserved22) {
110 VLOG(1) <<
"Unspecified or reserved nal_unit_type " << type_
111 <<
" (header 0x" << std::hex << static_cast<int>(header) <<
").";
114 }
else if (type_ == Nalu::H264_IDRSlice || type_ == Nalu::H264_SPS ||
115 type_ == Nalu::H264_SPSExtension ||
116 type_ == Nalu::H264_SubsetSPS || type_ == Nalu::H264_PPS) {
118 LOG(WARNING) <<
"nal_ref_idc shall not be equal to 0 for nalu type "
119 << type_ <<
" (header 0x" << std::hex
120 <<
static_cast<int>(header) <<
").";
123 }
else if (type_ == Nalu::H264_SEIMessage ||
124 (type_ >= Nalu::H264_AUD && type_ <= Nalu::H264_FillerData)) {
126 LOG(WARNING) <<
"nal_ref_idc shall be equal to 0 for nalu type " << type_
127 <<
" (header 0x" << std::hex << static_cast<int>(header)
133 is_aud_ = type_ == H264_AUD;
134 is_vcl_ = (type_ >= Nalu::H264_NonIDRSlice && type_ <= Nalu::H264_IDRSlice);
136 (type_ == Nalu::H264_NonIDRSlice || type_ == Nalu::H264_IDRSlice);
137 can_start_access_unit_ =
138 (is_vcl_ || type_ == Nalu::H264_AUD || type_ == Nalu::H264_SPS ||
139 type_ == Nalu::H264_PPS || type_ == Nalu::H264_SEIMessage ||
140 (type_ >= Nalu::H264_PrefixNALUnit && type_ <= Nalu::H264_Reserved18));
145 bool Nalu::InitializeFromH265(
const uint8_t* data, uint64_t size) {
149 const uint16_t header = (
data[0] << 8) |
data[1];
150 if ((header & 0x8000) != 0) {
151 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x"
152 << std::hex << header <<
").";
158 payload_size_ = size - header_size_;
160 type_ = (header >> 9) & 0x3F;
161 nuh_layer_id_ = (header >> 3) & 0x3F;
162 const int nuh_temporal_id_plus1 = header & 0x7;
163 if (nuh_temporal_id_plus1 == 0) {
164 LOG(WARNING) <<
"nul_temporal_id_plus1 shall not be equal to 0 (header 0x"
165 << std::hex << header <<
").";
168 nuh_temporal_id_ = nuh_temporal_id_plus1 - 1;
170 if (type_ == Nalu::H265_EOB && nuh_layer_id_ != 0) {
171 LOG(WARNING) <<
"nuh_layer_id shall be equal to 0 for nalu type " << type_
172 <<
" (header 0x" << std::hex << header <<
").";
177 if ((type_ >= Nalu::H265_RSV_VCL_N10 && type_ <= Nalu::H265_RSV_VCL_R15) ||
178 (type_ >= Nalu::H265_RSV_IRAP_VCL22 && type_ < Nalu::H265_RSV_VCL31) ||
179 (type_ >= Nalu::H265_RSV_NVCL41)) {
180 VLOG(1) <<
"Unspecified or reserved nal_unit_type " << type_
181 <<
" (header 0x" << std::hex << header <<
").";
185 }
else if ((type_ >= Nalu::H265_BLA_W_LP &&
186 type_ <= Nalu::H265_RSV_IRAP_VCL23) ||
187 type_ == Nalu::H265_VPS || type_ == Nalu::H265_SPS ||
188 type_ == Nalu::H265_EOS || type_ == Nalu::H265_EOB) {
189 if (nuh_temporal_id_ != 0) {
190 LOG(WARNING) <<
"TemporalId shall be equal to 0 for nalu type " << type_
191 <<
" (header 0x" << std::hex << header <<
").";
194 }
else if (type_ == Nalu::H265_TSA_N || type_ == Nalu::H265_TSA_R ||
195 (nuh_layer_id_ == 0 &&
196 (type_ == Nalu::H265_STSA_N || type_ == Nalu::H265_STSA_R))) {
197 if (nuh_temporal_id_ == 0) {
198 LOG(WARNING) <<
"TemporalId shall not be equal to 0 for nalu type "
199 << type_ <<
" (header 0x" << std::hex << header <<
").";
204 is_aud_ = type_ == H265_AUD;
205 is_vcl_ = type_ >= Nalu::H265_TRAIL_N && type_ <= Nalu::H265_RSV_VCL31;
206 is_video_slice_ = is_vcl_;
207 can_start_access_unit_ =
208 nuh_layer_id_ == 0 &&
209 (is_vcl_ || type_ == Nalu::H265_AUD || type_ == Nalu::H265_VPS ||
210 type_ == Nalu::H265_SPS || type_ == Nalu::H265_PPS ||
211 type_ == Nalu::H265_PREFIX_SEI ||
212 (type_ >= Nalu::H265_RSV_NVCL41 && type_ <= Nalu::H265_RSV_NVCL44) ||
213 (type_ >= Nalu::H265_UNSPEC48 && type_ <= Nalu::H265_UNSPEC55));
218 uint8_t nal_length_size,
219 const uint8_t* stream,
220 uint64_t stream_size)
228 uint8_t nal_length_size,
229 const uint8_t* stream,
230 uint64_t stream_size,
231 const std::vector<SubsampleEntry>& subsamples)
233 stream_size_(stream_size),
235 nalu_length_size_(nal_length_size),
236 format_(nal_length_size == 0 ? kAnnexbByteStreamFormat
237 : kNalUnitStreamFormat),
238 subsamples_(subsamples) {
242 NaluReader::~NaluReader() {}
245 if (stream_size_ <= 0)
246 return NaluReader::kEOStream;
248 uint8_t nalu_length_size_or_start_code_size;
249 uint64_t nalu_length;
250 if (format_ == kAnnexbByteStreamFormat) {
252 uint64_t nalu_length_with_header;
253 if (!LocateNaluByStartCode(&nalu_length_with_header,
254 &nalu_length_size_or_start_code_size)) {
255 LOG(ERROR) <<
"Could not find next NALU, bytes left in stream: "
260 return NaluReader::kInvalidStream;
262 nalu_length = nalu_length_with_header - nalu_length_size_or_start_code_size;
265 if (IsNaluLengthEncrypted(nalu_length_size_, subsamples_)) {
266 LOG(ERROR) <<
"NALU length is encrypted.";
267 return NaluReader::kInvalidStream;
270 return NaluReader::kInvalidStream;
271 nalu_length_size_or_start_code_size = nalu_length_size_;
273 if (nalu_length + nalu_length_size_ > stream_size_) {
274 LOG(ERROR) <<
"NALU length exceeds stream size: "
275 << stream_size_ <<
" < " << nalu_length;
276 return NaluReader::kInvalidStream;
278 if (nalu_length == 0) {
279 LOG(ERROR) <<
"NALU size 0";
280 return NaluReader::kInvalidStream;
284 const uint8_t* nalu_data = stream_ + nalu_length_size_or_start_code_size;
285 if (!nalu->Initialize(nalu_type_, nalu_data, nalu_length))
286 return NaluReader::kInvalidStream;
290 stream_ += nalu_length_size_or_start_code_size + nalu_length;
291 stream_size_ -= nalu_length_size_or_start_code_size + nalu_length;
292 UpdateSubsamples(nalu_length_size_or_start_code_size + nalu_length,
295 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->
type())
296 <<
" at: " <<
reinterpret_cast<const void*
>(nalu->
data())
299 return NaluReader::kOk;
303 if (stream_size_ >= 3) {
304 if (IsStartCode(stream_))
307 if (stream_size_ >= 4) {
308 if (stream_[0] == 0x00 && IsStartCode(stream_ + 1))
315 bool NaluReader::FindStartCode(
const uint8_t* data,
318 uint8_t* start_code_size) {
319 uint64_t bytes_left = data_size;
321 while (bytes_left >= 3) {
322 if (IsStartCode(data)) {
324 *offset = data_size - bytes_left;
325 *start_code_size = 3;
329 if (*offset > 0 && *(data - 1) == 0x00) {
331 ++(*start_code_size);
343 *offset = data_size - bytes_left;
344 *start_code_size = 0;
353 uint8_t* start_code_size,
354 const std::vector<SubsampleEntry>& subsamples) {
355 if (subsamples.empty()) {
356 return FindStartCode(data, data_size, offset, start_code_size);
359 uint64_t current_offset = 0;
361 uint16_t clear_bytes = subsample.clear_bytes;
362 if (current_offset + clear_bytes > data_size) {
363 LOG(WARNING) <<
"The sum of subsample sizes is greater than data_size.";
364 clear_bytes = data_size - current_offset;
371 const bool found_start_code = FindStartCode(
372 data + current_offset, clear_bytes, offset, start_code_size);
373 if (found_start_code) {
374 *offset += current_offset;
377 const uint64_t subsample_size =
378 subsample.clear_bytes + subsample.cipher_bytes;
379 current_offset += subsample_size;
380 if (current_offset > data_size) {
383 current_offset = data_size;
384 LOG(WARNING) <<
"The sum of subsamples is greater than data_size.";
391 if (current_offset < data_size) {
392 const bool found_start_code =
393 FindStartCode(data + current_offset, data_size - current_offset, offset,
395 *offset += current_offset;
396 return found_start_code;
401 *offset = current_offset;
402 *start_code_size = 0;
406 bool NaluReader::LocateNaluByStartCode(uint64_t* nalu_size,
407 uint8_t* start_code_size) {
409 uint64_t nalu_start_off = 0;
410 uint8_t annexb_start_code_size = 0;
412 stream_, stream_size_,
413 &nalu_start_off, &annexb_start_code_size, subsamples_)) {
414 DVLOG(4) <<
"Could not find start code, end of stream?";
419 stream_ += nalu_start_off;
420 stream_size_ -= nalu_start_off;
423 UpdateSubsamples(nalu_start_off, &subsamples_);
425 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
428 std::vector<SubsampleEntry> subsamples_for_finding_next_nalu;
429 if (!subsamples_.empty()) {
430 subsamples_for_finding_next_nalu = subsamples_;
431 UpdateSubsamples(annexb_start_code_size, &subsamples_for_finding_next_nalu);
433 uint64_t max_nalu_data_size = stream_size_ - annexb_start_code_size;
434 if (max_nalu_data_size <= 0) {
435 DVLOG(3) <<
"End of stream";
445 uint64_t nalu_size_without_start_code = 0;
446 uint8_t next_start_code_size = 0;
449 nalu_data, max_nalu_data_size,
450 &nalu_size_without_start_code, &next_start_code_size,
451 subsamples_for_finding_next_nalu)) {
452 nalu_data += max_nalu_data_size;
456 nalu_data += nalu_size_without_start_code + next_start_code_size;
457 max_nalu_data_size -= nalu_size_without_start_code + next_start_code_size;
458 UpdateSubsamples(nalu_size_without_start_code + next_start_code_size,
459 &subsamples_for_finding_next_nalu);
463 if (nalu.Initialize(nalu_type_, nalu_data, max_nalu_data_size)) {
464 nalu_data -= next_start_code_size;
467 LOG(WARNING) <<
"Seeing invalid NAL unit. Emulation prevention may not "
468 "have been applied properly. Assuming it is part of the "
469 "previous NAL unit.";
471 *nalu_size = nalu_data - stream_;
472 *start_code_size = annexb_start_code_size;
All the methods that are virtual are virtual for mocking.