DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
encrypting_fragmenter.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/mp4/encrypting_fragmenter.h"
8 
9 #include <limits>
10 
11 #include "packager/media/base/aes_encryptor.h"
12 #include "packager/media/base/aes_pattern_cryptor.h"
13 #include "packager/media/base/buffer_reader.h"
14 #include "packager/media/base/key_source.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/codecs/nalu_reader.h"
17 #include "packager/media/codecs/vp8_parser.h"
18 #include "packager/media/codecs/vp9_parser.h"
19 #include "packager/media/formats/mp4/box_definitions.h"
20 
21 namespace shaka {
22 namespace media {
23 namespace mp4 {
24 
25 namespace {
26 const size_t kCencBlockSize = 16u;
27 
28 // Adds one or more subsamples to |*subsamples|. This may add more than one
29 // if one of the values overflows the integer in the subsample.
30 void AddSubsamples(uint64_t clear_bytes,
31  uint64_t cipher_bytes,
32  std::vector<SubsampleEntry>* subsamples) {
33  CHECK_LT(cipher_bytes, std::numeric_limits<uint32_t>::max());
34  const uint64_t kUInt16Max = std::numeric_limits<uint16_t>::max();
35  while (clear_bytes > kUInt16Max) {
36  subsamples->push_back(SubsampleEntry(kUInt16Max, 0));
37  clear_bytes -= kUInt16Max;
38  }
39 
40  if (clear_bytes > 0 || cipher_bytes > 0)
41  subsamples->push_back(SubsampleEntry(clear_bytes, cipher_bytes));
42 }
43 
44 VideoCodec GetVideoCodec(const StreamInfo& stream_info) {
45  if (stream_info.stream_type() != kStreamVideo)
46  return kUnknownVideoCodec;
47  const VideoStreamInfo& video_stream_info =
48  static_cast<const VideoStreamInfo&>(stream_info);
49  return video_stream_info.codec();
50 }
51 
52 uint8_t GetNaluLengthSize(const StreamInfo& stream_info) {
53  if (stream_info.stream_type() != kStreamVideo)
54  return 0;
55 
56  const VideoStreamInfo& video_stream_info =
57  static_cast<const VideoStreamInfo&>(stream_info);
58  return video_stream_info.nalu_length_size();
59 }
60 } // namespace
61 
63  scoped_refptr<StreamInfo> info,
64  TrackFragment* traf,
65  scoped_ptr<EncryptionKey> encryption_key,
66  int64_t clear_time,
67  FourCC protection_scheme,
68  uint8_t crypt_byte_block,
69  uint8_t skip_byte_block)
70  : Fragmenter(info, traf),
71  info_(info),
72  encryption_key_(encryption_key.Pass()),
73  nalu_length_size_(GetNaluLengthSize(*info)),
74  video_codec_(GetVideoCodec(*info)),
75  clear_time_(clear_time),
76  protection_scheme_(protection_scheme),
77  crypt_byte_block_(crypt_byte_block),
78  skip_byte_block_(skip_byte_block) {
79  DCHECK(encryption_key_);
80  switch (video_codec_) {
81  case kCodecVP8:
82  vpx_parser_.reset(new VP8Parser);
83  break;
84  case kCodecVP9:
85  vpx_parser_.reset(new VP9Parser);
86  break;
87  case kCodecH264:
88  header_parser_.reset(new H264VideoSliceHeaderParser);
89  break;
90  case kCodecHVC1:
91  FALLTHROUGH_INTENDED;
92  case kCodecHEV1:
93  header_parser_.reset(new H265VideoSliceHeaderParser);
94  break;
95  default:
96  if (nalu_length_size_ > 0) {
97  LOG(WARNING) << "Unknown video codec '" << video_codec_
98  << "', whole subsamples will be encrypted.";
99  }
100  }
101 }
102 
103 EncryptingFragmenter::~EncryptingFragmenter() {}
104 
105 Status EncryptingFragmenter::AddSample(scoped_refptr<MediaSample> sample) {
106  DCHECK(sample);
107  if (!fragment_initialized()) {
108  Status status = InitializeFragment(sample->dts());
109  if (!status.ok())
110  return status;
111  }
112  if (encryptor_) {
113  Status status = EncryptSample(sample);
114  if (!status.ok())
115  return status;
116  }
117  return Fragmenter::AddSample(sample);
118 }
119 
121  Status status = Fragmenter::InitializeFragment(first_sample_dts);
122  if (!status.ok())
123  return status;
124 
125  if (header_parser_ && !header_parser_->Initialize(info_->codec_config()))
126  return Status(error::MUXER_FAILURE, "Fail to read SPS and PPS data.");
127 
128  traf()->auxiliary_size.sample_info_sizes.clear();
129  traf()->auxiliary_offset.offsets.clear();
130  if (IsSubsampleEncryptionRequired()) {
131  traf()->sample_encryption.flags |=
132  SampleEncryption::kUseSubsampleEncryption;
133  }
134  traf()->sample_encryption.sample_encryption_entries.clear();
135 
136  const bool enable_encryption = clear_time_ <= 0;
137  if (!enable_encryption) {
138  // This fragment should be in clear text.
139  // At most two sample description entries, an encrypted entry and a clear
140  // entry, are generated. The 1-based clear entry index is always 2.
141  const uint32_t kClearSampleDescriptionIndex = 2;
142 
143  traf()->header.flags |=
144  TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
145  traf()->header.sample_description_index = kClearSampleDescriptionIndex;
146  }
147  return PrepareFragmentForEncryption(enable_encryption);
148 }
149 
151  if (encryptor_) {
152  DCHECK_LE(clear_time_, 0);
154  } else {
155  DCHECK_GT(clear_time_, 0);
156  clear_time_ -= fragment_duration();
157  }
159 }
160 
162  bool enable_encryption) {
163  return (!enable_encryption || encryptor_) ? Status::OK : CreateEncryptor();
164 }
165 
167  // The offset will be adjusted in Segmenter after knowing moof size.
168  traf()->auxiliary_offset.offsets.push_back(0);
169 
170  // For 'cbcs' scheme, Constant IVs SHALL be used.
171  const size_t per_sample_iv_size =
172  (protection_scheme_ == FOURCC_cbcs) ? 0 : encryptor_->iv().size();
173  traf()->sample_encryption.iv_size = per_sample_iv_size;
174 
175  // Optimize saiz box.
176  SampleAuxiliaryInformationSize& saiz = traf()->auxiliary_size;
177  saiz.sample_count = traf()->runs[0].sample_sizes.size();
178  if (!saiz.sample_info_sizes.empty()) {
179  if (!OptimizeSampleEntries(&saiz.sample_info_sizes,
180  &saiz.default_sample_info_size)) {
181  saiz.default_sample_info_size = 0;
182  }
183  } else {
184  // |sample_info_sizes| table is filled in only for subsample encryption,
185  // otherwise |sample_info_size| is just the IV size.
186  DCHECK(!IsSubsampleEncryptionRequired());
187  saiz.default_sample_info_size = per_sample_iv_size;
188  }
189 
190  // It should only happen with full sample encryption + constant iv, i.e.
191  // 'cbcs' applying to audio.
192  if (saiz.default_sample_info_size == 0 && saiz.sample_info_sizes.empty()) {
193  DCHECK_EQ(protection_scheme_, FOURCC_cbcs);
194  DCHECK(!IsSubsampleEncryptionRequired());
195  // ISO/IEC 23001-7:2016(E) The sample auxiliary information would then be
196  // empty and should be emitted. Clear saiz and saio boxes so they are not
197  // written.
198  saiz.sample_count = 0;
199  traf()->auxiliary_offset.offsets.clear();
200  }
201 }
202 
204  DCHECK(encryption_key_);
205  scoped_ptr<AesCryptor> encryptor;
206  switch (protection_scheme_) {
207  case FOURCC_cenc:
208  encryptor.reset(new AesCtrEncryptor);
209  break;
210  case FOURCC_cbc1:
211  encryptor.reset(new AesCbcEncryptor(kNoPadding));
212  break;
213  case FOURCC_cens:
214  encryptor.reset(new AesPatternCryptor(
215  crypt_byte_block(), skip_byte_block(),
217  AesCryptor::kDontUseConstantIv,
218  scoped_ptr<AesCryptor>(new AesCtrEncryptor())));
219  break;
220  case FOURCC_cbcs:
221  encryptor.reset(new AesPatternCryptor(
222  crypt_byte_block(), skip_byte_block(),
224  AesCryptor::kUseConstantIv,
225  scoped_ptr<AesCryptor>(new AesCbcEncryptor(kNoPadding))));
226  break;
227  default:
228  return Status(error::MUXER_FAILURE, "Unsupported protection scheme.");
229  }
230 
231  DCHECK(!encryption_key_->iv.empty());
232  const bool initialized =
233  encryptor->InitializeWithIv(encryption_key_->key, encryption_key_->iv);
234  if (!initialized)
235  return Status(error::MUXER_FAILURE, "Failed to create the encryptor.");
236  encryptor_ = encryptor.Pass();
237  return Status::OK;
238 }
239 
240 void EncryptingFragmenter::EncryptBytes(uint8_t* data, uint32_t size) {
241  DCHECK(encryptor_);
242  CHECK(encryptor_->Crypt(data, size, data));
243 }
244 
245 Status EncryptingFragmenter::EncryptSample(scoped_refptr<MediaSample> sample) {
246  DCHECK(encryptor_);
247 
248  SampleEncryptionEntry sample_encryption_entry;
249  // For 'cbcs' scheme, Constant IVs SHALL be used.
250  if (protection_scheme_ != FOURCC_cbcs)
251  sample_encryption_entry.initialization_vector = encryptor_->iv();
252  uint8_t* data = sample->writable_data();
253  if (IsSubsampleEncryptionRequired()) {
254  if (vpx_parser_) {
255  std::vector<VPxFrameInfo> vpx_frames;
256  if (!vpx_parser_->Parse(sample->data(), sample->data_size(),
257  &vpx_frames)) {
258  return Status(error::MUXER_FAILURE, "Failed to parse vpx frame.");
259  }
260 
261  const bool is_superframe = vpx_frames.size() > 1;
262  for (const VPxFrameInfo& frame : vpx_frames) {
263  SubsampleEntry subsample;
264  subsample.clear_bytes = frame.uncompressed_header_size;
265  subsample.cipher_bytes =
266  frame.frame_size - frame.uncompressed_header_size;
267 
268  // "VP Codec ISO Media File Format Binding" document requires that the
269  // encrypted bytes of each frame within the superframe must be block
270  // aligned so that the counter state can be computed for each frame
271  // within the superframe.
272  // ISO/IEC 23001-7:2016 10.2 'cbc1' 10.3 'cens'
273  // The BytesOfProtectedData size SHALL be a multiple of 16 bytes to
274  // avoid partial blocks in Subsamples.
275  if (is_superframe || protection_scheme_ == FOURCC_cbc1 ||
276  protection_scheme_ == FOURCC_cens) {
277  const uint16_t misalign_bytes =
278  subsample.cipher_bytes % kCencBlockSize;
279  subsample.clear_bytes += misalign_bytes;
280  subsample.cipher_bytes -= misalign_bytes;
281  }
282 
283  sample_encryption_entry.subsamples.push_back(subsample);
284  if (subsample.cipher_bytes > 0)
285  EncryptBytes(data + subsample.clear_bytes, subsample.cipher_bytes);
286  data += frame.frame_size;
287  }
288  // Add subsample for the superframe index if exists.
289  if (is_superframe) {
290  size_t index_size = sample->data() + sample->data_size() - data;
291  DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
292  DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
293  SubsampleEntry subsample;
294  subsample.clear_bytes = index_size;
295  subsample.cipher_bytes = 0;
296  sample_encryption_entry.subsamples.push_back(subsample);
297  }
298  } else {
299  const Nalu::CodecType nalu_type =
300  (video_codec_ == kCodecHVC1 || video_codec_ == kCodecHEV1)
301  ? Nalu::kH265
302  : Nalu::kH264;
303  NaluReader reader(nalu_type, nalu_length_size_, data,
304  sample->data_size());
305 
306  // Store the current length of clear data. This is used to squash
307  // multiple unencrypted NAL units into fewer subsample entries.
308  uint64_t accumulated_clear_bytes = 0;
309 
310  Nalu nalu;
311  NaluReader::Result result;
312  while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
313  if (nalu.is_video_slice()) {
314  // For video-slice NAL units, encrypt the video slice. This skips
315  // the frame header. If this is an unrecognized codec (e.g. H.265),
316  // the whole NAL unit will be encrypted.
317  const int64_t video_slice_header_size =
318  header_parser_ ? header_parser_->GetHeaderSize(nalu) : 0;
319  if (video_slice_header_size < 0)
320  return Status(error::MUXER_FAILURE, "Failed to read slice header.");
321 
322  uint64_t current_clear_bytes =
323  nalu.header_size() + video_slice_header_size;
324  uint64_t cipher_bytes = nalu.payload_size() - video_slice_header_size;
325 
326  // ISO/IEC 23001-7:2016 10.2 'cbc1' 10.3 'cens'
327  // The BytesOfProtectedData size SHALL be a multiple of 16 bytes to
328  // avoid partial blocks in Subsamples.
329  if (protection_scheme_ == FOURCC_cbc1 ||
330  protection_scheme_ == FOURCC_cens) {
331  const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
332  current_clear_bytes += misalign_bytes;
333  cipher_bytes -= misalign_bytes;
334  }
335 
336  const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
337  EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
338 
339  AddSubsamples(
340  accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
341  cipher_bytes, &sample_encryption_entry.subsamples);
342  accumulated_clear_bytes = 0;
343  } else {
344  // For non-video-slice NAL units, don't encrypt.
345  accumulated_clear_bytes +=
346  nalu_length_size_ + nalu.header_size() + nalu.payload_size();
347  }
348  }
349  if (result != NaluReader::kEOStream)
350  return Status(error::MUXER_FAILURE, "Failed to parse NAL units.");
351  AddSubsamples(accumulated_clear_bytes, 0,
352  &sample_encryption_entry.subsamples);
353  }
354  DCHECK_EQ(sample_encryption_entry.GetTotalSizeOfSubsamples(),
355  sample->data_size());
356 
357  // The length of per-sample auxiliary datum, defined in CENC ch. 7.
358  traf()->auxiliary_size.sample_info_sizes.push_back(
359  sample_encryption_entry.ComputeSize());
360  } else {
361  DCHECK_LE(crypt_byte_block(), 1u);
362  DCHECK_EQ(skip_byte_block(), 0u);
363  EncryptBytes(data, sample->data_size());
364  }
365 
366  traf()->sample_encryption.sample_encryption_entries.push_back(
367  sample_encryption_entry);
368  encryptor_->UpdateIv();
369  return Status::OK;
370 }
371 
372 bool EncryptingFragmenter::IsSubsampleEncryptionRequired() {
373  return vpx_parser_ || nalu_length_size_ != 0;
374 }
375 
376 } // namespace mp4
377 } // namespace media
378 } // namespace shaka
Status AddSample(scoped_refptr< MediaSample > sample) override
Status InitializeFragment(int64_t first_sample_dts) override
virtual Status AddSample(scoped_refptr< MediaSample > sample)
Definition: fragmenter.cc:45
void FinalizeFragment() override
Finalize and optimize the fragment.
virtual Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:85
EncryptingFragmenter(scoped_refptr< StreamInfo > info, TrackFragment *traf, scoped_ptr< EncryptionKey > encryption_key, int64_t clear_time, FourCC protection_scheme, uint8_t crypt_byte_block, uint8_t skip_byte_block)
virtual void FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:104
virtual void FinalizeFragmentForEncryption()
Finalize current fragment for encryption.
Class to parse a vp9 bit stream.
Definition: vp9_parser.h:20
Implements pattern-based encryption/decryption.
virtual Status PrepareFragmentForEncryption(bool enable_encryption)
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:92