7#include <packager/media/codecs/nalu_reader.h>
14#include <absl/log/check.h>
15#include <absl/log/log.h>
17#include <packager/media/base/buffer_reader.h>
18#include <packager/media/base/decrypt_config.h>
24inline bool IsStartCode(
const uint8_t* data) {
25 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
29void UpdateSubsamples(uint64_t consumed_bytes,
30 std::vector<SubsampleEntry>* subsamples) {
31 if (consumed_bytes == 0 || subsamples->empty()) {
34 size_t num_entries_to_delete = 0;
35 for (SubsampleEntry& subsample : *subsamples) {
36 if (subsample.clear_bytes > consumed_bytes) {
37 subsample.clear_bytes -= consumed_bytes;
41 consumed_bytes -= subsample.clear_bytes;
42 subsample.clear_bytes = 0;
44 if (subsample.cipher_bytes > consumed_bytes) {
45 subsample.cipher_bytes -= consumed_bytes;
49 consumed_bytes -= subsample.cipher_bytes;
50 subsample.cipher_bytes = 0;
51 ++num_entries_to_delete;
54 subsamples->erase(subsamples->begin(),
55 subsamples->begin() + num_entries_to_delete);
58bool IsNaluLengthEncrypted(uint8_t nalu_length_size,
59 const std::vector<SubsampleEntry>& subsamples) {
60 if (subsamples.empty())
63 for (
const SubsampleEntry& subsample : subsamples) {
64 if (subsample.clear_bytes >= nalu_length_size) {
67 nalu_length_size -= subsample.clear_bytes;
68 if (subsample.cipher_bytes > 0) {
77Nalu::Nalu() =
default;
79bool Nalu::Initialize(CodecType type,
const uint8_t* data, uint64_t size) {
80 if (
type == Nalu::kH264) {
81 return InitializeFromH264(
data, size);
83 DCHECK_EQ(Nalu::kH265,
type);
84 return InitializeFromH265(
data, size);
89bool Nalu::InitializeFromH264(
const uint8_t* data, uint64_t size) {
93 const uint8_t header =
data[0];
94 if ((header & 0x80) != 0) {
95 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x"
96 << std::hex << static_cast<int>(header) <<
").";
102 payload_size_ = size - header_size_;
103 ref_idc_ = (header >> 5) & 0x3;
104 type_ = header & 0x1F;
107 if (type_ == Nalu::H264_Unspecified || type_ == Nalu::H264_Reserved17 ||
108 type_ == Nalu::H264_Reserved18 || type_ >= Nalu::H264_Reserved22) {
109 VLOG(1) <<
"Unspecified or reserved nal_unit_type " << type_
110 <<
" (header 0x" << std::hex << static_cast<int>(header) <<
").";
113 }
else if (type_ == Nalu::H264_IDRSlice || type_ == Nalu::H264_SPS ||
114 type_ == Nalu::H264_SPSExtension ||
115 type_ == Nalu::H264_SubsetSPS || type_ == Nalu::H264_PPS) {
117 LOG(WARNING) <<
"nal_ref_idc shall not be equal to 0 for nalu type "
118 << type_ <<
" (header 0x" << std::hex
119 <<
static_cast<int>(header) <<
").";
122 }
else if (type_ == Nalu::H264_SEIMessage ||
123 (type_ >= Nalu::H264_AUD && type_ <= Nalu::H264_FillerData)) {
125 LOG(WARNING) <<
"nal_ref_idc shall be equal to 0 for nalu type " << type_
126 <<
" (header 0x" << std::hex << static_cast<int>(header)
132 is_aud_ = type_ == H264_AUD;
133 is_vcl_ = (type_ >= Nalu::H264_NonIDRSlice && type_ <= Nalu::H264_IDRSlice);
135 (type_ == Nalu::H264_NonIDRSlice || type_ == Nalu::H264_IDRSlice);
136 can_start_access_unit_ =
137 (is_vcl_ || type_ == Nalu::H264_AUD || type_ == Nalu::H264_SPS ||
138 type_ == Nalu::H264_PPS || type_ == Nalu::H264_SEIMessage ||
139 (type_ >= Nalu::H264_PrefixNALUnit && type_ <= Nalu::H264_Reserved18));
144bool Nalu::InitializeFromH265(
const uint8_t* data, uint64_t size) {
148 const uint16_t header = (
data[0] << 8) |
data[1];
149 if ((header & 0x8000) != 0) {
150 LOG(WARNING) <<
"forbidden_zero_bit shall be equal to 0 (header 0x"
151 << std::hex << header <<
").";
157 payload_size_ = size - header_size_;
159 type_ = (header >> 9) & 0x3F;
160 nuh_layer_id_ = (header >> 3) & 0x3F;
161 const int nuh_temporal_id_plus1 = header & 0x7;
162 if (nuh_temporal_id_plus1 == 0) {
163 LOG(WARNING) <<
"nul_temporal_id_plus1 shall not be equal to 0 (header 0x"
164 << std::hex << header <<
").";
167 nuh_temporal_id_ = nuh_temporal_id_plus1 - 1;
169 if (type_ == Nalu::H265_EOB && nuh_layer_id_ != 0) {
170 LOG(WARNING) <<
"nuh_layer_id shall be equal to 0 for nalu type " << type_
171 <<
" (header 0x" << std::hex << header <<
").";
176 if ((type_ >= Nalu::H265_RSV_VCL_N10 && type_ <= Nalu::H265_RSV_VCL_R15) ||
177 (type_ >= Nalu::H265_RSV_IRAP_VCL22 && type_ < Nalu::H265_RSV_VCL31) ||
178 (type_ >= Nalu::H265_RSV_NVCL41)) {
179 VLOG(1) <<
"Unspecified or reserved nal_unit_type " << type_
180 <<
" (header 0x" << std::hex << header <<
").";
184 }
else if ((type_ >= Nalu::H265_BLA_W_LP &&
185 type_ <= Nalu::H265_RSV_IRAP_VCL23) ||
186 type_ == Nalu::H265_VPS || type_ == Nalu::H265_SPS ||
187 type_ == Nalu::H265_EOS || type_ == Nalu::H265_EOB) {
188 if (nuh_temporal_id_ != 0) {
189 LOG(WARNING) <<
"TemporalId shall be equal to 0 for nalu type " << type_
190 <<
" (header 0x" << std::hex << header <<
").";
193 }
else if (type_ == Nalu::H265_TSA_N || type_ == Nalu::H265_TSA_R ||
194 (nuh_layer_id_ == 0 &&
195 (type_ == Nalu::H265_STSA_N || type_ == Nalu::H265_STSA_R))) {
196 if (nuh_temporal_id_ == 0) {
197 LOG(WARNING) <<
"TemporalId shall not be equal to 0 for nalu type "
198 << type_ <<
" (header 0x" << std::hex << header <<
").";
203 is_aud_ = type_ == H265_AUD;
204 is_vcl_ = type_ >= Nalu::H265_TRAIL_N && type_ <= Nalu::H265_RSV_VCL31;
205 is_video_slice_ = is_vcl_;
206 can_start_access_unit_ =
207 nuh_layer_id_ == 0 &&
208 (is_vcl_ || type_ == Nalu::H265_AUD || type_ == Nalu::H265_VPS ||
209 type_ == Nalu::H265_SPS || type_ == Nalu::H265_PPS ||
210 type_ == Nalu::H265_PREFIX_SEI ||
211 (type_ >= Nalu::H265_RSV_NVCL41 && type_ <= Nalu::H265_RSV_NVCL44) ||
212 (type_ >= Nalu::H265_UNSPEC48 && type_ <= Nalu::H265_UNSPEC55));
217 uint8_t nal_length_size,
218 const uint8_t* stream,
219 uint64_t stream_size)
227 uint8_t nal_length_size,
228 const uint8_t* stream,
229 uint64_t stream_size,
230 const std::vector<SubsampleEntry>& subsamples)
232 stream_size_(stream_size),
234 nalu_length_size_(nal_length_size),
235 format_(nal_length_size == 0 ? kAnnexbByteStreamFormat
236 : kNalUnitStreamFormat),
237 subsamples_(subsamples) {
241NaluReader::~NaluReader() {}
244 if (stream_size_ <= 0)
245 return NaluReader::kEOStream;
247 uint8_t nalu_length_size_or_start_code_size;
248 uint64_t nalu_length;
249 if (format_ == kAnnexbByteStreamFormat) {
251 uint64_t nalu_length_with_header;
252 if (!LocateNaluByStartCode(&nalu_length_with_header,
253 &nalu_length_size_or_start_code_size)) {
254 LOG(ERROR) <<
"Could not find next NALU, bytes left in stream: "
259 return NaluReader::kInvalidStream;
261 nalu_length = nalu_length_with_header - nalu_length_size_or_start_code_size;
264 if (IsNaluLengthEncrypted(nalu_length_size_, subsamples_)) {
265 LOG(ERROR) <<
"NALU length is encrypted.";
266 return NaluReader::kInvalidStream;
269 return NaluReader::kInvalidStream;
270 nalu_length_size_or_start_code_size = nalu_length_size_;
272 if (nalu_length + nalu_length_size_ > stream_size_) {
273 LOG(ERROR) <<
"NALU length exceeds stream size: " << stream_size_ <<
" < "
275 return NaluReader::kInvalidStream;
277 if (nalu_length == 0) {
278 LOG(ERROR) <<
"NALU size 0";
279 return NaluReader::kInvalidStream;
283 const uint8_t* nalu_data = stream_ + nalu_length_size_or_start_code_size;
284 if (!nalu->Initialize(nalu_type_, nalu_data, nalu_length))
285 return NaluReader::kInvalidStream;
289 stream_ += nalu_length_size_or_start_code_size + nalu_length;
290 stream_size_ -= nalu_length_size_or_start_code_size + nalu_length;
291 UpdateSubsamples(nalu_length_size_or_start_code_size + nalu_length,
294 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->
type())
295 <<
" at: " <<
reinterpret_cast<const void*
>(nalu->
data())
298 return NaluReader::kOk;
302 if (stream_size_ >= 3) {
303 if (IsStartCode(stream_))
306 if (stream_size_ >= 4) {
307 if (stream_[0] == 0x00 && IsStartCode(stream_ + 1))
314bool NaluReader::FindStartCode(
const uint8_t* data,
317 uint8_t* start_code_size) {
318 uint64_t bytes_left = data_size;
320 while (bytes_left >= 3) {
321 if (IsStartCode(data)) {
323 *offset = data_size - bytes_left;
324 *start_code_size = 3;
328 if (*offset > 0 && *(data - 1) == 0x00) {
330 ++(*start_code_size);
342 *offset = data_size - bytes_left;
343 *start_code_size = 0;
352 uint8_t* start_code_size,
353 const std::vector<SubsampleEntry>& subsamples) {
354 if (subsamples.empty()) {
355 return FindStartCode(data, data_size, offset, start_code_size);
358 uint64_t current_offset = 0;
360 uint16_t clear_bytes = subsample.clear_bytes;
361 if (current_offset + clear_bytes > data_size) {
362 LOG(WARNING) <<
"The sum of subsample sizes is greater than data_size.";
363 clear_bytes = data_size - current_offset;
370 const bool found_start_code = FindStartCode(
371 data + current_offset, clear_bytes, offset, start_code_size);
372 if (found_start_code) {
373 *offset += current_offset;
376 const uint64_t subsample_size =
377 subsample.clear_bytes + subsample.cipher_bytes;
378 current_offset += subsample_size;
379 if (current_offset > data_size) {
382 current_offset = data_size;
383 LOG(WARNING) <<
"The sum of subsamples is greater than data_size.";
390 if (current_offset < data_size) {
391 const bool found_start_code =
392 FindStartCode(data + current_offset, data_size - current_offset, offset,
394 *offset += current_offset;
395 return found_start_code;
400 *offset = current_offset;
401 *start_code_size = 0;
405bool NaluReader::LocateNaluByStartCode(uint64_t* nalu_size,
406 uint8_t* start_code_size) {
408 uint64_t nalu_start_off = 0;
409 uint8_t annexb_start_code_size = 0;
411 &annexb_start_code_size, subsamples_)) {
412 DVLOG(4) <<
"Could not find start code, end of stream?";
417 stream_ += nalu_start_off;
418 stream_size_ -= nalu_start_off;
421 UpdateSubsamples(nalu_start_off, &subsamples_);
423 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
426 std::vector<SubsampleEntry> subsamples_for_finding_next_nalu;
427 if (!subsamples_.empty()) {
428 subsamples_for_finding_next_nalu = subsamples_;
429 UpdateSubsamples(annexb_start_code_size, &subsamples_for_finding_next_nalu);
431 uint64_t max_nalu_data_size = stream_size_ - annexb_start_code_size;
432 if (max_nalu_data_size <= 0) {
433 DVLOG(3) <<
"End of stream";
443 uint64_t nalu_size_without_start_code = 0;
444 uint8_t next_start_code_size = 0;
447 nalu_data, max_nalu_data_size, &nalu_size_without_start_code,
448 &next_start_code_size, 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.