7 #include <packager/media/crypto/subsample_generator.h>
12 #include <absl/log/check.h>
14 #include <packager/macros/compiler.h>
15 #include <packager/media/base/decrypt_config.h>
16 #include <packager/media/base/video_stream_info.h>
17 #include <packager/media/codecs/av1_parser.h>
18 #include <packager/media/codecs/video_slice_header_parser.h>
19 #include <packager/media/codecs/vp8_parser.h>
20 #include <packager/media/codecs/vp9_parser.h>
26 const size_t kAesBlockSize = 16u;
28 uint8_t GetNaluLengthSize(
const StreamInfo& stream_info) {
29 if (stream_info.stream_type() != kStreamVideo)
32 const VideoStreamInfo& video_stream_info =
33 static_cast<const VideoStreamInfo&
>(stream_info);
34 return video_stream_info.nalu_length_size();
37 bool ShouldAlignProtectedData(Codec codec,
38 FourCC protection_scheme,
39 bool vp9_subsample_encryption) {
51 return vp9_subsample_encryption;
61 return protection_scheme == FOURCC_cbc1 ||
62 protection_scheme == FOURCC_cens ||
63 protection_scheme == FOURCC_cenc;
70 class SubsampleOrganizer {
72 SubsampleOrganizer(
bool align_protected_data,
73 std::vector<SubsampleEntry>* subsamples)
74 : align_protected_data_(align_protected_data), subsamples_(subsamples) {}
76 ~SubsampleOrganizer() {
77 if (accumulated_clear_bytes_ > 0) {
78 PushSubsample(accumulated_clear_bytes_, 0);
79 accumulated_clear_bytes_ = 0;
83 void AddSubsample(
size_t clear_bytes,
size_t cipher_bytes) {
84 DCHECK_LT(clear_bytes, std::numeric_limits<uint32_t>::max());
85 DCHECK_LT(cipher_bytes, std::numeric_limits<uint32_t>::max());
87 if (align_protected_data_ && cipher_bytes != 0) {
88 const size_t misalign_bytes = cipher_bytes % kAesBlockSize;
89 clear_bytes += misalign_bytes;
90 cipher_bytes -= misalign_bytes;
93 accumulated_clear_bytes_ += clear_bytes;
95 if (cipher_bytes == 0)
98 PushSubsample(accumulated_clear_bytes_, cipher_bytes);
99 accumulated_clear_bytes_ = 0;
103 SubsampleOrganizer(
const SubsampleOrganizer&) =
delete;
104 SubsampleOrganizer& operator=(
const SubsampleOrganizer&) =
delete;
106 void PushSubsample(
size_t clear_bytes,
size_t cipher_bytes) {
107 const uint16_t kUInt16Max = std::numeric_limits<uint16_t>::max();
108 while (clear_bytes > kUInt16Max) {
109 subsamples_->emplace_back(kUInt16Max, 0);
110 clear_bytes -= kUInt16Max;
112 subsamples_->emplace_back(
static_cast<uint16_t
>(clear_bytes),
113 static_cast<uint32_t
>(cipher_bytes));
116 const bool align_protected_data_ =
false;
117 std::vector<SubsampleEntry>*
const subsamples_ =
nullptr;
118 size_t accumulated_clear_bytes_ = 0;
124 : vp9_subsample_encryption_(vp9_subsample_encryption) {}
126 SubsampleGenerator::~SubsampleGenerator() {}
130 codec_ = stream_info.codec();
131 nalu_length_size_ = GetNaluLengthSize(stream_info);
138 if (vp9_subsample_encryption_)
145 case kCodecH265DolbyVision:
150 if (nalu_length_size_ > 0) {
151 LOG(WARNING) <<
"Unknown video codec '" << codec_ <<
"'";
152 return Status(error::ENCRYPTION_FAILURE,
"Unknown video codec.");
158 const size_t kConfigOBUsOffset = 4;
159 const bool has_config_obus =
160 stream_info.codec_config().size() > kConfigOBUsOffset;
161 std::vector<AV1Parser::Tile> tiles;
162 if (has_config_obus &&
164 &stream_info.codec_config()[kConfigOBUsOffset],
165 stream_info.codec_config().size() - kConfigOBUsOffset, &tiles)) {
167 error::ENCRYPTION_FAILURE,
168 "Failed to parse configOBUs in AV1CodecConfigurationRecord.");
170 DCHECK(tiles.empty());
172 if (header_parser_) {
173 CHECK_NE(nalu_length_size_, 0u) <<
"AnnexB stream is not supported yet";
174 if (!header_parser_->Initialize(stream_info.codec_config())) {
175 return Status(error::ENCRYPTION_FAILURE,
176 "Failed to read SPS and PPS data.");
180 align_protected_data_ = ShouldAlignProtectedData(codec_, protection_scheme,
181 vp9_subsample_encryption_);
183 if (protection_scheme == kAppleSampleAesProtectionScheme) {
184 const size_t kH264LeadingClearBytesSize = 32u;
185 const size_t kAudioLeadingClearBytesSize = 16u;
188 leading_clear_bytes_size_ = kH264LeadingClearBytesSize;
189 min_protected_data_size_ =
190 leading_clear_bytes_size_ + kAesBlockSize + 1u;
193 FALLTHROUGH_INTENDED;
195 leading_clear_bytes_size_ = kAudioLeadingClearBytesSize;
196 min_protected_data_size_ = leading_clear_bytes_size_ + kAesBlockSize;
201 leading_clear_bytes_size_ = 0;
202 min_protected_data_size_ = leading_clear_bytes_size_ + kAesBlockSize;
205 LOG(ERROR) <<
"Unexpected codec for SAMPLE-AES " << codec_;
206 return Status(error::ENCRYPTION_FAILURE,
207 "Unexpected codec for SAMPLE-AES.");
214 const uint8_t* frame,
216 std::vector<SubsampleEntry>* subsamples) {
220 return GenerateSubsamplesFromAV1Frame(frame, frame_size, subsamples);
222 FALLTHROUGH_INTENDED;
224 case kCodecH265DolbyVision:
225 return GenerateSubsamplesFromH26xFrame(frame, frame_size, subsamples);
227 if (vp9_subsample_encryption_)
228 return GenerateSubsamplesFromVPxFrame(frame, frame_size, subsamples);
234 if (leading_clear_bytes_size_ > 0) {
235 SubsampleOrganizer subsample_organizer(align_protected_data_,
237 const size_t clear_bytes =
238 std::min(frame_size, leading_clear_bytes_size_);
239 const size_t cipher_bytes = frame_size - clear_bytes;
240 subsample_organizer.AddSubsample(clear_bytes, cipher_bytes);
249 void SubsampleGenerator::InjectVpxParserForTesting(
250 std::unique_ptr<VPxParser> vpx_parser) {
251 vpx_parser_ = std::move(vpx_parser);
254 void SubsampleGenerator::InjectVideoSliceHeaderParserForTesting(
255 std::unique_ptr<VideoSliceHeaderParser> header_parser) {
256 header_parser_ = std::move(header_parser);
259 void SubsampleGenerator::InjectAV1ParserForTesting(
260 std::unique_ptr<AV1Parser> av1_parser) {
261 av1_parser_ = std::move(av1_parser);
264 Status SubsampleGenerator::GenerateSubsamplesFromVPxFrame(
265 const uint8_t* frame,
267 std::vector<SubsampleEntry>* subsamples) {
269 std::vector<VPxFrameInfo> vpx_frames;
270 if (!vpx_parser_->Parse(frame, frame_size, &vpx_frames))
271 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse vpx frame.");
273 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
275 size_t total_size = 0;
276 for (
const VPxFrameInfo& vpx_frame : vpx_frames) {
277 subsample_organizer.AddSubsample(
278 vpx_frame.uncompressed_header_size,
279 vpx_frame.frame_size - vpx_frame.uncompressed_header_size);
280 total_size += vpx_frame.frame_size;
283 const bool is_superframe = vpx_frames.size() > 1;
285 const size_t index_size = frame_size - total_size;
286 DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
287 DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
288 subsample_organizer.AddSubsample(index_size, 0);
290 DCHECK_EQ(total_size, frame_size);
295 Status SubsampleGenerator::GenerateSubsamplesFromH26xFrame(
296 const uint8_t* frame,
298 std::vector<SubsampleEntry>* subsamples) {
299 DCHECK_NE(nalu_length_size_, 0u);
300 DCHECK(header_parser_);
302 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
304 const Nalu::CodecType nalu_type =
305 (codec_ == kCodecH265 || codec_ == kCodecH265DolbyVision) ? Nalu::kH265
307 NaluReader reader(nalu_type, nalu_length_size_, frame, frame_size);
310 NaluReader::Result result;
311 while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
314 if (leading_clear_bytes_size_ == 0 && !header_parser_->ProcessNalu(nalu)) {
315 LOG(ERROR) <<
"Failed to process NAL unit: NAL type = " << nalu.type();
316 return Status(error::ENCRYPTION_FAILURE,
"Failed to process NAL unit.");
319 const size_t nalu_total_size = nalu.header_size() + nalu.payload_size();
320 size_t clear_bytes = 0;
321 if (nalu.is_video_slice() && nalu_total_size >= min_protected_data_size_) {
322 clear_bytes = leading_clear_bytes_size_;
323 if (clear_bytes == 0) {
326 const int64_t video_slice_header_size =
327 header_parser_->GetHeaderSize(nalu);
328 if (video_slice_header_size < 0) {
329 LOG(ERROR) <<
"Failed to read slice header.";
330 return Status(error::ENCRYPTION_FAILURE,
331 "Failed to read slice header.");
333 clear_bytes = nalu.header_size() + video_slice_header_size;
337 clear_bytes = nalu_total_size;
339 const size_t cipher_bytes = nalu_total_size - clear_bytes;
340 subsample_organizer.AddSubsample(nalu_length_size_ + clear_bytes,
343 if (result != NaluReader::kEOStream) {
344 LOG(ERROR) <<
"Failed to parse NAL units.";
345 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse NAL units.");
350 Status SubsampleGenerator::GenerateSubsamplesFromAV1Frame(
351 const uint8_t* frame,
353 std::vector<SubsampleEntry>* subsamples) {
355 std::vector<AV1Parser::Tile> av1_tiles;
356 if (!av1_parser_->Parse(frame, frame_size, &av1_tiles))
357 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse AV1 frame.");
359 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
361 size_t last_tile_end_offset = 0;
362 for (
const AV1Parser::Tile& tile : av1_tiles) {
363 DCHECK_LE(last_tile_end_offset, tile.start_offset_in_bytes);
366 subsample_organizer.AddSubsample(
367 tile.start_offset_in_bytes - last_tile_end_offset, tile.size_in_bytes);
368 last_tile_end_offset = tile.start_offset_in_bytes + tile.size_in_bytes;
370 DCHECK_LE(last_tile_end_offset, frame_size);
371 if (last_tile_end_offset < frame_size)
372 subsample_organizer.AddSubsample(frame_size - last_tile_end_offset, 0);
All the methods that are virtual are virtual for mocking.