DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
mp4_media_parser.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "packager/media/formats/mp4/mp4_media_parser.h"
6 
7 #include <limits>
8 
9 #include "packager/base/callback.h"
10 #include "packager/base/callback_helpers.h"
11 #include "packager/base/logging.h"
12 #include "packager/base/memory/ref_counted.h"
13 #include "packager/base/strings/string_number_conversions.h"
14 #include "packager/media/base/aes_encryptor.h"
15 #include "packager/media/base/audio_stream_info.h"
16 #include "packager/media/base/buffer_reader.h"
17 #include "packager/media/base/decrypt_config.h"
18 #include "packager/media/base/key_source.h"
19 #include "packager/media/base/media_sample.h"
20 #include "packager/media/base/video_stream_info.h"
21 #include "packager/media/file/file.h"
22 #include "packager/media/file/file_closer.h"
23 #include "packager/media/filters/avc_decoder_configuration.h"
24 #include "packager/media/filters/hevc_decoder_configuration.h"
25 #include "packager/media/filters/vp_codec_configuration.h"
26 #include "packager/media/formats/mp4/box_definitions.h"
27 #include "packager/media/formats/mp4/box_reader.h"
28 #include "packager/media/formats/mp4/es_descriptor.h"
29 #include "packager/media/formats/mp4/rcheck.h"
30 #include "packager/media/formats/mp4/track_run_iterator.h"
31 
32 namespace edash_packager {
33 namespace media {
34 namespace mp4 {
35 namespace {
36 
37 uint64_t Rescale(uint64_t time_in_old_scale,
38  uint32_t old_scale,
39  uint32_t new_scale) {
40  return (static_cast<double>(time_in_old_scale) / old_scale) * new_scale;
41 }
42 
43 VideoCodec FourCCToVideoCodec(FourCC fourcc) {
44  switch (fourcc) {
45  case FOURCC_AVC1:
46  return kCodecH264;
47  case FOURCC_HEV1:
48  return kCodecHEV1;
49  case FOURCC_HVC1:
50  return kCodecHVC1;
51  case FOURCC_VP08:
52  return kCodecVP8;
53  case FOURCC_VP09:
54  return kCodecVP9;
55  case FOURCC_VP10:
56  return kCodecVP10;
57  default:
58  return kUnknownVideoCodec;
59  }
60 }
61 
62 AudioCodec FourCCToAudioCodec(FourCC fourcc) {
63  switch(fourcc) {
64  case FOURCC_DTSC:
65  return kCodecDTSC;
66  case FOURCC_DTSH:
67  return kCodecDTSH;
68  case FOURCC_DTSL:
69  return kCodecDTSL;
70  case FOURCC_DTSE:
71  return kCodecDTSE;
72  case FOURCC_DTSP:
73  return kCodecDTSP;
74  case FOURCC_DTSM:
75  return kCodecDTSM;
76  case FOURCC_EAC3:
77  return kCodecEAC3;
78  default:
79  return kUnknownAudioCodec;
80  }
81 }
82 
83 const char kWidevineKeySystemId[] = "edef8ba979d64acea3c827dcd51d21ed";
84 
85 } // namespace
86 
87 MP4MediaParser::MP4MediaParser()
88  : state_(kWaitingForInit), moof_head_(0), mdat_tail_(0) {}
89 
90 MP4MediaParser::~MP4MediaParser() {
91  STLDeleteValues(&decryptor_map_);
92 }
93 
94 void MP4MediaParser::Init(const InitCB& init_cb,
95  const NewSampleCB& new_sample_cb,
96  KeySource* decryption_key_source) {
97  DCHECK_EQ(state_, kWaitingForInit);
98  DCHECK(init_cb_.is_null());
99  DCHECK(!init_cb.is_null());
100  DCHECK(!new_sample_cb.is_null());
101 
102  ChangeState(kParsingBoxes);
103  init_cb_ = init_cb;
104  new_sample_cb_ = new_sample_cb;
105  decryption_key_source_ = decryption_key_source;
106 }
107 
108 void MP4MediaParser::Reset() {
109  queue_.Reset();
110  runs_.reset();
111  moof_head_ = 0;
112  mdat_tail_ = 0;
113 }
114 
116  DCHECK_NE(state_, kWaitingForInit);
117  Reset();
118  ChangeState(kParsingBoxes);
119 }
120 
121 bool MP4MediaParser::Parse(const uint8_t* buf, int size) {
122  DCHECK_NE(state_, kWaitingForInit);
123 
124  if (state_ == kError)
125  return false;
126 
127  queue_.Push(buf, size);
128 
129  bool result, err = false;
130 
131  do {
132  if (state_ == kParsingBoxes) {
133  result = ParseBox(&err);
134  } else {
135  DCHECK_EQ(kEmittingSamples, state_);
136  result = EnqueueSample(&err);
137  if (result) {
138  int64_t max_clear = runs_->GetMaxClearOffset() + moof_head_;
139  err = !ReadAndDiscardMDATsUntil(max_clear);
140  }
141  }
142  } while (result && !err);
143 
144  if (err) {
145  DLOG(ERROR) << "Error while parsing MP4";
146  moov_.reset();
147  Reset();
148  ChangeState(kError);
149  return false;
150  }
151 
152  return true;
153 }
154 
155 bool MP4MediaParser::LoadMoov(const std::string& file_path) {
156  scoped_ptr<File, FileCloser> file(
157  File::OpenWithNoBuffering(file_path.c_str(), "r"));
158  if (!file) {
159  LOG(ERROR) << "Unable to open media file '" << file_path << "'";
160  return false;
161  }
162  if (!file->Seek(0)) {
163  LOG(WARNING) << "Filesystem does not support seeking on file '" << file_path
164  << "'";
165  return false;
166  }
167 
168  uint64_t file_position(0);
169  bool mdat_seen(false);
170  while (true) {
171  const uint32_t kBoxHeaderReadSize(16);
172  std::vector<uint8_t> buffer(kBoxHeaderReadSize);
173  int64_t bytes_read = file->Read(&buffer[0], kBoxHeaderReadSize);
174  if (bytes_read == 0) {
175  LOG(ERROR) << "Could not find 'moov' box in file '" << file_path << "'";
176  return false;
177  }
178  if (bytes_read < kBoxHeaderReadSize) {
179  LOG(ERROR) << "Error reading media file '" << file_path << "'";
180  return false;
181  }
182  uint64_t box_size;
183  FourCC box_type;
184  bool err;
185  if (!BoxReader::StartTopLevelBox(&buffer[0], kBoxHeaderReadSize, &box_type,
186  &box_size, &err)) {
187  LOG(ERROR) << "Could not start top level box from file '" << file_path
188  << "'";
189  return false;
190  }
191  if (box_type == FOURCC_MDAT) {
192  mdat_seen = true;
193  } else if (box_type == FOURCC_MOOV) {
194  if (!mdat_seen) {
195  // 'moov' is before 'mdat'. Nothing to do.
196  break;
197  }
198  // 'mdat' before 'moov'. Read and parse 'moov'.
199  if (!Parse(&buffer[0], bytes_read)) {
200  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
201  return false;
202  }
203  uint64_t bytes_to_read = box_size - bytes_read;
204  buffer.resize(bytes_to_read);
205  while (bytes_to_read > 0) {
206  bytes_read = file->Read(&buffer[0], bytes_to_read);
207  if (bytes_read <= 0) {
208  LOG(ERROR) << "Error reading 'moov' contents from file '" << file_path
209  << "'";
210  return false;
211  }
212  if (!Parse(&buffer[0], bytes_read)) {
213  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
214  return false;
215  }
216  bytes_to_read -= bytes_read;
217  }
218  queue_.Reset(); // So that we don't need to adjust data offsets.
219  mdat_tail_ = 0; // So it will skip boxes until mdat.
220  break; // Done.
221  }
222  file_position += box_size;
223  if (!file->Seek(file_position)) {
224  LOG(ERROR) << "Error skipping box in mp4 file '" << file_path << "'";
225  return false;
226  }
227  }
228  return true;
229 }
230 
231 bool MP4MediaParser::ParseBox(bool* err) {
232  const uint8_t* buf;
233  int size;
234  queue_.Peek(&buf, &size);
235  if (!size)
236  return false;
237 
238  scoped_ptr<BoxReader> reader(BoxReader::ReadTopLevelBox(buf, size, err));
239  if (reader.get() == NULL)
240  return false;
241 
242  if (reader->type() == FOURCC_MDAT) {
243  // The code ends up here only if a MOOV box is not yet seen.
244  DCHECK(!moov_);
245 
246  NOTIMPLEMENTED() << " Files with MDAT before MOOV is not supported yet.";
247  *err = true;
248  return false;
249  }
250 
251  // Set up mdat offset for ReadMDATsUntil().
252  mdat_tail_ = queue_.head() + reader->size();
253 
254  if (reader->type() == FOURCC_MOOV) {
255  *err = !ParseMoov(reader.get());
256  } else if (reader->type() == FOURCC_MOOF) {
257  moof_head_ = queue_.head();
258  *err = !ParseMoof(reader.get());
259 
260  // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
261  // be located anywhere in the file, including inside the 'moof' itself.
262  // (Since 'default-base-is-moof' is mandated, no data references can come
263  // before the head of the 'moof', so keeping this box around is sufficient.)
264  return !(*err);
265  } else {
266  VLOG(2) << "Skipping top-level box: " << FourCCToString(reader->type());
267  }
268 
269  queue_.Pop(reader->size());
270  return !(*err);
271 }
272 
273 bool MP4MediaParser::ParseMoov(BoxReader* reader) {
274  if (moov_)
275  return true; // Already parsed the 'moov' box.
276 
277  moov_.reset(new Movie);
278  RCHECK(moov_->Parse(reader));
279  runs_.reset();
280 
281  std::vector<scoped_refptr<StreamInfo> > streams;
282 
283  for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
284  track != moov_->tracks.end(); ++track) {
285  const uint32_t timescale = track->media.header.timescale;
286 
287  // Calculate duration (based on timescale).
288  uint64_t duration = 0;
289  if (track->media.header.duration > 0) {
290  duration = track->media.header.duration;
291  } else if (moov_->extends.header.fragment_duration > 0) {
292  DCHECK(moov_->header.timescale != 0);
293  duration = Rescale(moov_->extends.header.fragment_duration,
294  moov_->header.timescale,
295  timescale);
296  } else if (moov_->header.duration > 0 &&
297  moov_->header.duration != std::numeric_limits<uint64_t>::max()) {
298  DCHECK(moov_->header.timescale != 0);
299  duration =
300  Rescale(moov_->header.duration, moov_->header.timescale, timescale);
301  }
302 
303  const SampleDescription& samp_descr =
304  track->media.information.sample_table.description;
305 
306  size_t desc_idx = 0;
307 
308  // Read sample description index from mvex if it exists otherwise read
309  // from the first entry in Sample To Chunk box.
310  if (moov_->extends.tracks.size() > 0) {
311  for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
312  const TrackExtends& trex = moov_->extends.tracks[t];
313  if (trex.track_id == track->header.track_id) {
314  desc_idx = trex.default_sample_description_index;
315  break;
316  }
317  }
318  } else {
319  const std::vector<ChunkInfo>& chunk_info =
320  track->media.information.sample_table.sample_to_chunk.chunk_info;
321  RCHECK(chunk_info.size() > 0);
322  desc_idx = chunk_info[0].sample_description_index;
323  }
324  RCHECK(desc_idx > 0);
325  desc_idx -= 1; // BMFF descriptor index is one-based
326 
327  if (track->media.handler.type == kAudio) {
328  RCHECK(!samp_descr.audio_entries.empty());
329 
330  // It is not uncommon to find otherwise-valid files with incorrect sample
331  // description indices, so we fail gracefully in that case.
332  if (desc_idx >= samp_descr.audio_entries.size())
333  desc_idx = 0;
334 
335  const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx];
336  const FourCC actual_format = entry.GetActualFormat();
337  AudioCodec codec = FourCCToAudioCodec(actual_format);
338  uint8_t num_channels = 0;
339  uint32_t sampling_frequency = 0;
340  uint8_t audio_object_type = 0;
341  std::vector<uint8_t> extra_data;
342 
343  switch (actual_format) {
344  case FOURCC_MP4A:
345  // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
346  // supported MPEG2 AAC variants.
347  if (entry.esds.es_descriptor.IsAAC()) {
348  codec = kCodecAAC;
349  const AACAudioSpecificConfig& aac_audio_specific_config =
350  entry.esds.aac_audio_specific_config;
351  num_channels = aac_audio_specific_config.num_channels();
352  sampling_frequency = aac_audio_specific_config.frequency();
353  audio_object_type = aac_audio_specific_config.audio_object_type();
354  extra_data = entry.esds.es_descriptor.decoder_specific_info();
355  break;
356  } else {
357  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
358  << actual_format << " in stsd box.";
359  return false;
360  }
361  case FOURCC_DTSC:
362  case FOURCC_DTSH:
363  case FOURCC_DTSL:
364  case FOURCC_DTSE:
365  case FOURCC_DTSM:
366  extra_data = entry.ddts.data;
367  num_channels = entry.channelcount;
368  sampling_frequency = entry.samplerate;
369  break;
370  case FOURCC_EAC3:
371  num_channels = entry.channelcount;
372  sampling_frequency = entry.samplerate;
373  break;
374  default:
375  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
376  << actual_format << " in stsd box.";
377  return false;
378  }
379 
380  bool is_encrypted = entry.sinf.info.track_encryption.is_encrypted;
381  DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
382  streams.push_back(new AudioStreamInfo(
383  track->header.track_id,
384  timescale,
385  duration,
386  codec,
387  AudioStreamInfo::GetCodecString(codec, audio_object_type),
388  track->media.header.language,
389  entry.samplesize,
390  num_channels,
391  sampling_frequency,
392  extra_data.size() ? &extra_data[0] : NULL,
393  extra_data.size(),
394  is_encrypted));
395  }
396 
397  if (track->media.handler.type == kVideo) {
398  RCHECK(!samp_descr.video_entries.empty());
399  if (desc_idx >= samp_descr.video_entries.size())
400  desc_idx = 0;
401  const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
402 
403  uint32_t coded_width = entry.width;
404  uint32_t coded_height = entry.height;
405  uint32_t pixel_width = entry.pixel_aspect.h_spacing;
406  uint32_t pixel_height = entry.pixel_aspect.v_spacing;
407  if (pixel_width == 0 && pixel_height == 0) {
408  pixel_width = 1;
409  pixel_height = 1;
410  }
411  std::string codec_string;
412  uint8_t nalu_length_size = 0;
413 
414  const FourCC actual_format = entry.GetActualFormat();
415  const VideoCodec video_codec = FourCCToVideoCodec(actual_format);
416  switch (actual_format) {
417  case FOURCC_AVC1: {
418  AVCDecoderConfiguration avc_config;
419  if (!avc_config.Parse(entry.codec_config_record.data)) {
420  LOG(ERROR) << "Failed to parse avcc.";
421  return false;
422  }
423  codec_string = avc_config.GetCodecString();
424  nalu_length_size = avc_config.length_size();
425 
426  if (coded_width != avc_config.coded_width() ||
427  coded_height != avc_config.coded_height()) {
428  LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
429  << "," << coded_height
430  << ") does not match with resolution in "
431  "AVCDecoderConfigurationRecord ("
432  << avc_config.coded_width() << ","
433  << avc_config.coded_height()
434  << "). Use AVCDecoderConfigurationRecord.";
435  coded_width = avc_config.coded_width();
436  coded_height = avc_config.coded_height();
437  }
438 
439  if (pixel_width != avc_config.pixel_width() ||
440  pixel_height != avc_config.pixel_height()) {
441  LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
442  << "Pixel aspect ratio in PASP box (" << pixel_width << ","
443  << pixel_height
444  << ") does not match with SAR in AVCDecoderConfigurationRecord "
445  "("
446  << avc_config.pixel_width() << "," << avc_config.pixel_height()
447  << "). Use AVCDecoderConfigurationRecord.";
448  pixel_width = avc_config.pixel_width();
449  pixel_height = avc_config.pixel_height();
450  }
451  break;
452  }
453  case FOURCC_HEV1:
454  case FOURCC_HVC1: {
455  HEVCDecoderConfiguration hevc_config;
456  if (!hevc_config.Parse(entry.codec_config_record.data)) {
457  LOG(ERROR) << "Failed to parse hevc.";
458  return false;
459  }
460  codec_string = hevc_config.GetCodecString(video_codec);
461  nalu_length_size = hevc_config.length_size();
462  break;
463  }
464  case FOURCC_VP08:
465  case FOURCC_VP09:
466  case FOURCC_VP10: {
467  VPCodecConfiguration vp_config;
468  if (!vp_config.Parse(entry.codec_config_record.data)) {
469  LOG(ERROR) << "Failed to parse vpcc.";
470  return false;
471  }
472  codec_string = vp_config.GetCodecString(video_codec);
473  break;
474  }
475  default:
476  LOG(ERROR) << "Unsupported video format "
477  << FourCCToString(actual_format) << " in stsd box.";
478  return false;
479  }
480 
481  bool is_encrypted = entry.sinf.info.track_encryption.is_encrypted;
482  DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
483  streams.push_back(new VideoStreamInfo(
484  track->header.track_id, timescale, duration, video_codec,
485  codec_string, track->media.header.language, coded_width, coded_height,
486  pixel_width, pixel_height,
487  0, // trick_play_rate
488  nalu_length_size, vector_as_array(&entry.codec_config_record.data),
489  entry.codec_config_record.data.size(), is_encrypted));
490  }
491  }
492 
493  init_cb_.Run(streams);
494  if (!FetchKeysIfNecessary(moov_->pssh))
495  return false;
496  runs_.reset(new TrackRunIterator(moov_.get()));
497  RCHECK(runs_->Init());
498  ChangeState(kEmittingSamples);
499  return true;
500 }
501 
502 bool MP4MediaParser::ParseMoof(BoxReader* reader) {
503  // Must already have initialization segment.
504  RCHECK(moov_.get());
505  MovieFragment moof;
506  RCHECK(moof.Parse(reader));
507  if (!runs_)
508  runs_.reset(new TrackRunIterator(moov_.get()));
509  RCHECK(runs_->Init(moof));
510  if (!FetchKeysIfNecessary(moof.pssh))
511  return false;
512  ChangeState(kEmittingSamples);
513  return true;
514 }
515 
516 bool MP4MediaParser::FetchKeysIfNecessary(
517  const std::vector<ProtectionSystemSpecificHeader>& headers) {
518  if (headers.empty())
519  return true;
520 
521  // An error will be returned later if the samples need to be decrypted.
522  if (!decryption_key_source_)
523  return true;
524 
525  // TODO(tinskip): Pass in raw 'pssh' boxes to FetchKeys. This will allow
526  // supporting multiple keysystems. Move this to KeySource.
527  std::vector<uint8_t> widevine_system_id;
528  base::HexStringToBytes(kWidevineKeySystemId, &widevine_system_id);
529  for (std::vector<ProtectionSystemSpecificHeader>::const_iterator iter =
530  headers.begin(); iter != headers.end(); ++iter) {
531  if (iter->system_id == widevine_system_id) {
532  Status status = decryption_key_source_->FetchKeys(iter->data);
533  if (!status.ok()) {
534  LOG(ERROR) << "Error fetching decryption keys: " << status;
535  return false;
536  }
537  return true;
538  }
539  }
540 
541  LOG(ERROR) << "No viable 'pssh' box found for content decryption.";
542  return false;
543 }
544 
545 bool MP4MediaParser::EnqueueSample(bool* err) {
546  if (!runs_->IsRunValid()) {
547  // Remain in kEnqueueingSamples state, discarding data, until the end of
548  // the current 'mdat' box has been appended to the queue.
549  if (!queue_.Trim(mdat_tail_))
550  return false;
551 
552  ChangeState(kParsingBoxes);
553  return true;
554  }
555 
556  if (!runs_->IsSampleValid()) {
557  runs_->AdvanceRun();
558  return true;
559  }
560 
561  DCHECK(!(*err));
562 
563  const uint8_t* buf;
564  int buf_size;
565  queue_.Peek(&buf, &buf_size);
566  if (!buf_size)
567  return false;
568 
569  // Skip this entire track if it is not audio nor video.
570  if (!runs_->is_audio() && !runs_->is_video())
571  runs_->AdvanceRun();
572 
573  // Attempt to cache the auxiliary information first. Aux info is usually
574  // placed in a contiguous block before the sample data, rather than being
575  // interleaved. If we didn't cache it, this would require that we retain the
576  // start of the segment buffer while reading samples. Aux info is typically
577  // quite small compared to sample data, so this pattern is useful on
578  // memory-constrained devices where the source buffer consumes a substantial
579  // portion of the total system memory.
580  if (runs_->AuxInfoNeedsToBeCached()) {
581  queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
582  if (buf_size < runs_->aux_info_size())
583  return false;
584  *err = !runs_->CacheAuxInfo(buf, buf_size);
585  return !*err;
586  }
587 
588  int64_t sample_offset = runs_->sample_offset() + moof_head_;
589  queue_.PeekAt(sample_offset, &buf, &buf_size);
590  if (buf_size < runs_->sample_size()) {
591  if (sample_offset < queue_.head()) {
592  LOG(ERROR) << "Incorrect sample offset " << sample_offset
593  << " < " << queue_.head();
594  *err = true;
595  }
596  return false;
597  }
598 
599  scoped_refptr<MediaSample> stream_sample(MediaSample::CopyFrom(
600  buf, runs_->sample_size(), runs_->is_keyframe()));
601  if (runs_->is_encrypted()) {
602  scoped_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
603  if (!decrypt_config ||
604  !DecryptSampleBuffer(decrypt_config.get(),
605  stream_sample->writable_data(),
606  stream_sample->data_size())) {
607  *err = true;
608  LOG(ERROR) << "Cannot decrypt samples.";
609  return false;
610  }
611  }
612 
613  stream_sample->set_dts(runs_->dts());
614  stream_sample->set_pts(runs_->cts());
615  stream_sample->set_duration(runs_->duration());
616 
617  DVLOG(3) << "Pushing frame: "
618  << ", key=" << runs_->is_keyframe()
619  << ", dur=" << runs_->duration()
620  << ", dts=" << runs_->dts()
621  << ", cts=" << runs_->cts()
622  << ", size=" << runs_->sample_size();
623 
624  if (!new_sample_cb_.Run(runs_->track_id(), stream_sample)) {
625  *err = true;
626  LOG(ERROR) << "Failed to process the sample.";
627  return false;
628  }
629 
630  runs_->AdvanceSample();
631  return true;
632 }
633 
634 bool MP4MediaParser::DecryptSampleBuffer(const DecryptConfig* decrypt_config,
635  uint8_t* buffer,
636  size_t buffer_size) {
637  DCHECK(decrypt_config);
638  DCHECK(buffer);
639 
640  if (!decryption_key_source_) {
641  LOG(ERROR) << "Encrypted media sample encountered, but decryption is not "
642  "enabled";
643  return false;
644  }
645 
646  // Get the encryptor object.
647  AesCtrEncryptor* encryptor;
648  DecryptorMap::iterator found = decryptor_map_.find(decrypt_config->key_id());
649  if (found == decryptor_map_.end()) {
650  // Create new AesCtrEncryptor
651  EncryptionKey key;
652  Status status(decryption_key_source_->GetKey(decrypt_config->key_id(),
653  &key));
654  if (!status.ok()) {
655  LOG(ERROR) << "Error retrieving decryption key: " << status;
656  return false;
657  }
658  scoped_ptr<AesCtrEncryptor> new_encryptor(new AesCtrEncryptor);
659  if (!new_encryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
660  LOG(ERROR) << "Failed to initialize AesCtrEncryptor for decryption.";
661  return false;
662  }
663  encryptor = new_encryptor.release();
664  decryptor_map_[decrypt_config->key_id()] = encryptor;
665  } else {
666  encryptor = found->second;
667  }
668  if (!encryptor->SetIv(decrypt_config->iv())) {
669  LOG(ERROR) << "Invalid initialization vector.";
670  return false;
671  }
672 
673  if (decrypt_config->subsamples().empty()) {
674  // Sample not encrypted using subsample encryption. Decrypt whole.
675  if (!encryptor->Decrypt(buffer, buffer_size, buffer)) {
676  LOG(ERROR) << "Error during bulk sample decryption.";
677  return false;
678  }
679  return true;
680  }
681 
682  // Subsample decryption.
683  const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
684  uint8_t* current_ptr = buffer;
685  const uint8_t* buffer_end = buffer + buffer_size;
686  current_ptr += decrypt_config->data_offset();
687  if (current_ptr > buffer_end) {
688  LOG(ERROR) << "Subsample data_offset too large.";
689  return false;
690  }
691  for (std::vector<SubsampleEntry>::const_iterator iter = subsamples.begin();
692  iter != subsamples.end();
693  ++iter) {
694  if ((current_ptr + iter->clear_bytes + iter->cipher_bytes) > buffer_end) {
695  LOG(ERROR) << "Subsamples overflow sample buffer.";
696  return false;
697  }
698  current_ptr += iter->clear_bytes;
699  if (!encryptor->Decrypt(current_ptr, iter->cipher_bytes, current_ptr)) {
700  LOG(ERROR) << "Error decrypting subsample buffer.";
701  return false;
702  }
703  current_ptr += iter->cipher_bytes;
704  }
705  return true;
706 }
707 
708 bool MP4MediaParser::ReadAndDiscardMDATsUntil(const int64_t offset) {
709  bool err = false;
710  while (mdat_tail_ < offset) {
711  const uint8_t* buf;
712  int size;
713  queue_.PeekAt(mdat_tail_, &buf, &size);
714 
715  FourCC type;
716  uint64_t box_sz;
717  if (!BoxReader::StartTopLevelBox(buf, size, &type, &box_sz, &err))
718  break;
719 
720  mdat_tail_ += box_sz;
721  }
722  queue_.Trim(std::min(mdat_tail_, offset));
723  return !err;
724 }
725 
726 void MP4MediaParser::ChangeState(State new_state) {
727  DVLOG(2) << "Changing state: " << new_state;
728  state_ = new_state;
729 }
730 
731 } // namespace mp4
732 } // namespace media
733 } // namespace edash_packager
static BoxReader * ReadTopLevelBox(const uint8_t *buf, const size_t buf_size, bool *err)
Definition: box_reader.cc:37
virtual Status GetKey(TrackType track_type, EncryptionKey *key)
Definition: key_source.cc:46
bool Parse(const uint8_t *buf, int size) override
static scoped_refptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45
void PeekAt(int64_t offset, const uint8_t **buf, int *size)
bool LoadMoov(const std::string &file_path)
virtual Status FetchKeys(const std::vector< uint8_t > &content_id, const std::string &policy)
Definition: key_source.cc:30
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
static bool StartTopLevelBox(const uint8_t *buf, const size_t buf_size, FourCC *type, uint64_t *box_size, bool *err) WARN_UNUSED_RESULT
Definition: box_reader.cc:60
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:129
static std::string GetCodecString(AudioCodec codec, uint8_t audio_object_type)