7 #include "packager/media/crypto/subsample_generator.h" 12 #include "packager/media/base/decrypt_config.h" 13 #include "packager/media/base/video_stream_info.h" 14 #include "packager/media/codecs/av1_parser.h" 15 #include "packager/media/codecs/video_slice_header_parser.h" 16 #include "packager/media/codecs/vp8_parser.h" 17 #include "packager/media/codecs/vp9_parser.h" 23 const size_t kAesBlockSize = 16u;
25 uint8_t GetNaluLengthSize(
const StreamInfo& stream_info) {
26 if (stream_info.stream_type() != kStreamVideo)
29 const VideoStreamInfo& video_stream_info =
30 static_cast<const VideoStreamInfo&
>(stream_info);
31 return video_stream_info.nalu_length_size();
34 bool ShouldAlignProtectedData(Codec codec,
35 FourCC protection_scheme,
36 bool vp9_subsample_encryption) {
53 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_)
149 if (nalu_length_size_ > 0) {
150 LOG(WARNING) <<
"Unknown video codec '" << codec_ <<
"'";
151 return Status(error::ENCRYPTION_FAILURE,
"Unknown video codec.");
157 const size_t kConfigOBUsOffset = 4;
158 const bool has_config_obus =
159 stream_info.codec_config().size() > kConfigOBUsOffset;
160 std::vector<AV1Parser::Tile> tiles;
161 if (has_config_obus &&
163 &stream_info.codec_config()[kConfigOBUsOffset],
164 stream_info.codec_config().size() - kConfigOBUsOffset, &tiles)) {
166 error::ENCRYPTION_FAILURE,
167 "Failed to parse configOBUs in AV1CodecConfigurationRecord.");
169 DCHECK(tiles.empty());
171 if (header_parser_) {
172 CHECK_NE(nalu_length_size_, 0u) <<
"AnnexB stream is not supported yet";
173 if (!header_parser_->Initialize(stream_info.codec_config())) {
174 return Status(error::ENCRYPTION_FAILURE,
175 "Failed to read SPS and PPS data.");
179 align_protected_data_ = ShouldAlignProtectedData(codec_, protection_scheme,
180 vp9_subsample_encryption_);
182 if (protection_scheme == kAppleSampleAesProtectionScheme) {
183 const size_t kH264LeadingClearBytesSize = 32u;
184 const size_t kAudioLeadingClearBytesSize = 16u;
187 leading_clear_bytes_size_ = kH264LeadingClearBytesSize;
188 min_protected_data_size_ =
189 leading_clear_bytes_size_ + kAesBlockSize + 1u;
192 FALLTHROUGH_INTENDED;
194 leading_clear_bytes_size_ = kAudioLeadingClearBytesSize;
195 min_protected_data_size_ = leading_clear_bytes_size_ + kAesBlockSize;
200 leading_clear_bytes_size_ = 0;
201 min_protected_data_size_ = leading_clear_bytes_size_ + kAesBlockSize;
204 LOG(ERROR) <<
"Unexpected codec for SAMPLE-AES " << codec_;
205 return Status(error::ENCRYPTION_FAILURE,
206 "Unexpected codec for SAMPLE-AES.");
213 const uint8_t* frame,
215 std::vector<SubsampleEntry>* subsamples) {
219 return GenerateSubsamplesFromAV1Frame(frame, frame_size, subsamples);
221 FALLTHROUGH_INTENDED;
223 return GenerateSubsamplesFromH26xFrame(frame, frame_size, subsamples);
225 if (vp9_subsample_encryption_)
226 return GenerateSubsamplesFromVPxFrame(frame, frame_size, subsamples);
232 if (leading_clear_bytes_size_ > 0) {
233 SubsampleOrganizer subsample_organizer(align_protected_data_,
235 const size_t clear_bytes =
236 std::min(frame_size, leading_clear_bytes_size_);
237 const size_t cipher_bytes = frame_size - clear_bytes;
238 subsample_organizer.AddSubsample(clear_bytes, cipher_bytes);
247 void SubsampleGenerator::InjectVpxParserForTesting(
248 std::unique_ptr<VPxParser> vpx_parser) {
249 vpx_parser_ = std::move(vpx_parser);
252 void SubsampleGenerator::InjectVideoSliceHeaderParserForTesting(
253 std::unique_ptr<VideoSliceHeaderParser> header_parser) {
254 header_parser_ = std::move(header_parser);
257 void SubsampleGenerator::InjectAV1ParserForTesting(
258 std::unique_ptr<AV1Parser> av1_parser) {
259 av1_parser_ = std::move(av1_parser);
262 Status SubsampleGenerator::GenerateSubsamplesFromVPxFrame(
263 const uint8_t* frame,
265 std::vector<SubsampleEntry>* subsamples) {
267 std::vector<VPxFrameInfo> vpx_frames;
268 if (!vpx_parser_->Parse(frame, frame_size, &vpx_frames))
269 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse vpx frame.");
271 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
273 size_t total_size = 0;
275 subsample_organizer.AddSubsample(
276 frame.uncompressed_header_size,
277 frame.frame_size - frame.uncompressed_header_size);
278 total_size += frame.frame_size;
281 const bool is_superframe = vpx_frames.size() > 1;
283 const size_t index_size = frame_size - total_size;
284 DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
285 DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
286 subsample_organizer.AddSubsample(index_size, 0);
288 DCHECK_EQ(total_size, frame_size);
293 Status SubsampleGenerator::GenerateSubsamplesFromH26xFrame(
294 const uint8_t* frame,
296 std::vector<SubsampleEntry>* subsamples) {
297 DCHECK_NE(nalu_length_size_, 0u);
298 DCHECK(header_parser_);
300 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
302 const Nalu::CodecType nalu_type =
303 (codec_ == kCodecH265) ? Nalu::kH265 : Nalu::kH264;
304 NaluReader reader(nalu_type, nalu_length_size_, frame, frame_size);
307 NaluReader::Result result;
308 while ((result = reader.
Advance(&nalu)) == NaluReader::kOk) {
310 size_t clear_bytes = 0;
311 if (nalu.
is_video_slice() && nalu_total_size >= min_protected_data_size_) {
312 clear_bytes = leading_clear_bytes_size_;
313 if (clear_bytes == 0) {
316 const int64_t video_slice_header_size =
317 header_parser_->GetHeaderSize(nalu);
318 if (video_slice_header_size < 0) {
319 LOG(ERROR) <<
"Failed to read slice header.";
320 return Status(error::ENCRYPTION_FAILURE,
321 "Failed to read slice header.");
323 clear_bytes = nalu.
header_size() + video_slice_header_size;
327 clear_bytes = nalu_total_size;
329 const size_t cipher_bytes = nalu_total_size - clear_bytes;
330 subsample_organizer.AddSubsample(nalu_length_size_ + clear_bytes,
333 if (result != NaluReader::kEOStream) {
334 LOG(ERROR) <<
"Failed to parse NAL units.";
335 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse NAL units.");
340 Status SubsampleGenerator::GenerateSubsamplesFromAV1Frame(
341 const uint8_t* frame,
343 std::vector<SubsampleEntry>* subsamples) {
345 std::vector<AV1Parser::Tile> av1_tiles;
346 if (!av1_parser_->Parse(frame, frame_size, &av1_tiles))
347 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse AV1 frame.");
349 SubsampleOrganizer subsample_organizer(align_protected_data_, subsamples);
351 size_t last_tile_end_offset = 0;
353 DCHECK_LE(last_tile_end_offset, tile.start_offset_in_bytes);
356 subsample_organizer.AddSubsample(
357 tile.start_offset_in_bytes - last_tile_end_offset, tile.size_in_bytes);
358 last_tile_end_offset = tile.start_offset_in_bytes + tile.size_in_bytes;
360 DCHECK_LE(last_tile_end_offset, frame_size);
361 if (last_tile_end_offset < frame_size)
362 subsample_organizer.AddSubsample(frame_size - last_tile_end_offset, 0);
All the methods that are virtual are virtual for mocking.