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>
26const size_t kAesBlockSize = 16u;
28uint8_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();
37bool 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;
70class 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 size_t clear_at_end = 0;
88 if (align_protected_data_ && cipher_bytes != 0) {
89 clear_at_end = cipher_bytes % kAesBlockSize;
90 cipher_bytes -= clear_at_end;
93 accumulated_clear_bytes_ += clear_bytes;
95 if (cipher_bytes == 0) {
96 accumulated_clear_bytes_ += clear_at_end;
100 PushSubsample(accumulated_clear_bytes_, cipher_bytes);
101 accumulated_clear_bytes_ = clear_at_end;
105 SubsampleOrganizer(
const SubsampleOrganizer&) =
delete;
106 SubsampleOrganizer& operator=(
const SubsampleOrganizer&) =
delete;
108 void PushSubsample(
size_t clear_bytes,
size_t cipher_bytes) {
109 const uint16_t kUInt16Max = std::numeric_limits<uint16_t>::max();
110 while (clear_bytes > kUInt16Max) {
111 subsamples_->emplace_back(kUInt16Max, 0);
112 clear_bytes -= kUInt16Max;
114 subsamples_->emplace_back(
static_cast<uint16_t
>(clear_bytes),
115 static_cast<uint32_t
>(cipher_bytes));
118 const bool align_protected_data_ =
false;
119 std::vector<SubsampleEntry>*
const subsamples_ =
nullptr;
120 size_t accumulated_clear_bytes_ = 0;
127 : vp9_subsample_encryption_(vp9_subsample_encryption), cencv1_(cencv1) {}
129SubsampleGenerator::~SubsampleGenerator() {}
133 codec_ = stream_info.codec();
134 nalu_length_size_ = GetNaluLengthSize(stream_info);
141 if (vp9_subsample_encryption_)
148 case kCodecH265DolbyVision:
153 if (nalu_length_size_ > 0) {
154 LOG(WARNING) <<
"Unknown video codec '" << codec_ <<
"'";
155 return Status(error::ENCRYPTION_FAILURE,
"Unknown video codec.");
161 const size_t kConfigOBUsOffset = 4;
162 const bool has_config_obus =
163 stream_info.codec_config().size() > kConfigOBUsOffset;
164 std::vector<AV1Parser::Tile> tiles;
165 if (has_config_obus &&
167 &stream_info.codec_config()[kConfigOBUsOffset],
168 stream_info.codec_config().size() - kConfigOBUsOffset, &tiles)) {
170 error::ENCRYPTION_FAILURE,
171 "Failed to parse configOBUs in AV1CodecConfigurationRecord.");
173 DCHECK(tiles.empty());
175 if (header_parser_) {
176 CHECK_NE(nalu_length_size_, 0u) <<
"AnnexB stream is not supported yet";
177 if (!header_parser_->Initialize(stream_info.codec_config())) {
178 return Status(error::ENCRYPTION_FAILURE,
179 "Failed to read SPS and PPS data.");
181 if (!header_parser_->InitializeLayered(
182 stream_info.layered_codec_config())) {
183 return Status(error::ENCRYPTION_FAILURE,
184 "Failed to read parameter sets for the layered case.");
188 align_protected_data_ = ShouldAlignProtectedData(codec_, protection_scheme,
189 vp9_subsample_encryption_);
191 if (protection_scheme == kAppleSampleAesProtectionScheme) {
192 const size_t kH264LeadingClearBytesSize = 32u;
193 const size_t kAudioLeadingClearBytesSize = 16u;
196 leading_clear_bytes_size_ = kH264LeadingClearBytesSize;
197 min_protected_data_size_ =
198 leading_clear_bytes_size_ + kAesBlockSize + 1u;
201 FALLTHROUGH_INTENDED;
203 leading_clear_bytes_size_ = kAudioLeadingClearBytesSize;
204 min_protected_data_size_ = leading_clear_bytes_size_ + kAesBlockSize;
209 leading_clear_bytes_size_ = 0;
210 min_protected_data_size_ = leading_clear_bytes_size_ + kAesBlockSize;
213 LOG(ERROR) <<
"Unexpected codec for SAMPLE-AES " << codec_;
214 return Status(error::ENCRYPTION_FAILURE,
215 "Unexpected codec for SAMPLE-AES.");
222 const uint8_t* frame,
224 std::vector<SubsampleEntry>* subsamples) {
228 return GenerateSubsamplesFromAV1Frame(frame, frame_size, subsamples);
230 FALLTHROUGH_INTENDED;
232 case kCodecH265DolbyVision:
233 return GenerateSubsamplesFromH26xFrame(frame, frame_size, subsamples);
235 if (vp9_subsample_encryption_)
236 return GenerateSubsamplesFromVPxFrame(frame, frame_size, subsamples);
242 if (leading_clear_bytes_size_ > 0) {
243 SubsampleOrganizer subsample_organizer(align_protected_data_,
245 const size_t clear_bytes =
246 std::min(frame_size, leading_clear_bytes_size_);
247 const size_t cipher_bytes = frame_size - clear_bytes;
248 subsample_organizer.AddSubsample(clear_bytes, cipher_bytes);
257void SubsampleGenerator::InjectVpxParserForTesting(
258 std::unique_ptr<VPxParser> vpx_parser) {
259 vpx_parser_ = std::move(vpx_parser);
262void SubsampleGenerator::InjectVideoSliceHeaderParserForTesting(
263 std::unique_ptr<VideoSliceHeaderParser> header_parser) {
264 header_parser_ = std::move(header_parser);
267void SubsampleGenerator::InjectAV1ParserForTesting(
268 std::unique_ptr<AV1Parser> av1_parser) {
269 av1_parser_ = std::move(av1_parser);
272Status SubsampleGenerator::GenerateSubsamplesFromVPxFrame(
273 const uint8_t* frame,
275 std::vector<SubsampleEntry>* subsamples) {
277 std::vector<VPxFrameInfo> vpx_frames;
278 if (!vpx_parser_->Parse(frame, frame_size, &vpx_frames))
279 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse vpx frame.");
281 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
283 size_t total_size = 0;
284 for (
const VPxFrameInfo& vpx_frame : vpx_frames) {
285 subsample_organizer.AddSubsample(
286 vpx_frame.uncompressed_header_size,
287 vpx_frame.frame_size - vpx_frame.uncompressed_header_size);
288 total_size += vpx_frame.frame_size;
291 const bool is_superframe = vpx_frames.size() > 1;
293 const size_t index_size = frame_size - total_size;
294 DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
295 DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
296 subsample_organizer.AddSubsample(index_size, 0);
298 DCHECK_EQ(total_size, frame_size);
303Status SubsampleGenerator::GenerateSubsamplesFromH26xFrame(
304 const uint8_t* frame,
306 std::vector<SubsampleEntry>* subsamples) {
307 DCHECK_NE(nalu_length_size_, 0u);
308 DCHECK(header_parser_);
310 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
312 const Nalu::CodecType nalu_type =
313 (codec_ == kCodecH265 || codec_ == kCodecH265DolbyVision) ? Nalu::kH265
315 NaluReader reader(nalu_type, nalu_length_size_, frame, frame_size);
318 NaluReader::Result result;
319 while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
322 if (leading_clear_bytes_size_ == 0 && !header_parser_->ProcessNalu(nalu)) {
323 LOG(ERROR) <<
"Failed to process NAL unit: NAL type = " << nalu.type();
324 return Status(error::ENCRYPTION_FAILURE,
"Failed to process NAL unit.");
327 const size_t nalu_total_size = nalu.header_size() + nalu.payload_size();
328 size_t clear_bytes = 0;
332 clear_bytes = nalu.header_size();
333 }
else if (nalu.is_video_slice() &&
334 nalu_total_size >= min_protected_data_size_) {
335 clear_bytes = leading_clear_bytes_size_;
336 if (clear_bytes == 0) {
339 const int64_t video_slice_header_size =
340 header_parser_->GetHeaderSize(nalu);
341 if (video_slice_header_size < 0) {
342 LOG(ERROR) <<
"Failed to read slice header.";
343 return Status(error::ENCRYPTION_FAILURE,
344 "Failed to read slice header.");
346 clear_bytes = nalu.header_size() + video_slice_header_size;
350 clear_bytes = nalu_total_size;
352 const size_t cipher_bytes = nalu_total_size - clear_bytes;
353 subsample_organizer.AddSubsample(nalu_length_size_ + clear_bytes,
356 if (result != NaluReader::kEOStream) {
357 LOG(ERROR) <<
"Failed to parse NAL units.";
358 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse NAL units.");
363Status SubsampleGenerator::GenerateSubsamplesFromAV1Frame(
364 const uint8_t* frame,
366 std::vector<SubsampleEntry>* subsamples) {
368 std::vector<AV1Parser::Tile> av1_tiles;
369 if (!av1_parser_->Parse(frame, frame_size, &av1_tiles))
370 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse AV1 frame.");
372 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
374 size_t last_tile_end_offset = 0;
375 for (
const AV1Parser::Tile& tile : av1_tiles) {
376 DCHECK_LE(last_tile_end_offset, tile.start_offset_in_bytes);
379 subsample_organizer.AddSubsample(
380 tile.start_offset_in_bytes - last_tile_end_offset, tile.size_in_bytes);
381 last_tile_end_offset = tile.start_offset_in_bytes + tile.size_in_bytes;
383 DCHECK_LE(last_tile_end_offset, frame_size);
384 if (last_tile_end_offset < frame_size)
385 subsample_organizer.AddSubsample(frame_size - last_tile_end_offset, 0);
All the methods that are virtual are virtual for mocking.