DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations 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/audio_stream_info.h"
15 #include "packager/media/base/buffer_reader.h"
16 #include "packager/media/base/decrypt_config.h"
17 #include "packager/media/base/key_source.h"
18 #include "packager/media/base/macros.h"
19 #include "packager/media/base/media_sample.h"
20 #include "packager/media/base/rcheck.h"
21 #include "packager/media/base/video_stream_info.h"
22 #include "packager/media/file/file.h"
23 #include "packager/media/file/file_closer.h"
24 #include "packager/media/filters/avc_decoder_configuration.h"
25 #include "packager/media/filters/hevc_decoder_configuration.h"
26 #include "packager/media/filters/vp_codec_configuration.h"
27 #include "packager/media/formats/mp4/box_definitions.h"
28 #include "packager/media/formats/mp4/box_reader.h"
29 #include "packager/media/formats/mp4/es_descriptor.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_ac_3:
77  return kCodecAC3;
78  case FOURCC_ec_3:
79  return kCodecEAC3;
80  default:
81  return kUnknownAudioCodec;
82  }
83 }
84 
85 // Default DTS audio number of channels for 5.1 channel layout.
86 const uint8_t kDtsAudioNumChannels = 6;
87 const uint64_t kNanosecondsPerSecond = 1000000000ull;
88 
89 } // namespace
90 
91 MP4MediaParser::MP4MediaParser()
92  : state_(kWaitingForInit),
93  decryption_key_source_(NULL),
94  moof_head_(0),
95  mdat_tail_(0) {}
96 
97 MP4MediaParser::~MP4MediaParser() {}
98 
99 void MP4MediaParser::Init(const InitCB& init_cb,
100  const NewSampleCB& new_sample_cb,
101  KeySource* decryption_key_source) {
102  DCHECK_EQ(state_, kWaitingForInit);
103  DCHECK(init_cb_.is_null());
104  DCHECK(!init_cb.is_null());
105  DCHECK(!new_sample_cb.is_null());
106 
107  ChangeState(kParsingBoxes);
108  init_cb_ = init_cb;
109  new_sample_cb_ = new_sample_cb;
110  decryption_key_source_ = decryption_key_source;
111  if (decryption_key_source)
112  decryptor_source_.reset(new DecryptorSource(decryption_key_source));
113 }
114 
115 void MP4MediaParser::Reset() {
116  queue_.Reset();
117  runs_.reset();
118  moof_head_ = 0;
119  mdat_tail_ = 0;
120 }
121 
123  DCHECK_NE(state_, kWaitingForInit);
124  Reset();
125  ChangeState(kParsingBoxes);
126  return true;
127 }
128 
129 bool MP4MediaParser::Parse(const uint8_t* buf, int size) {
130  DCHECK_NE(state_, kWaitingForInit);
131 
132  if (state_ == kError)
133  return false;
134 
135  queue_.Push(buf, size);
136 
137  bool result, err = false;
138 
139  do {
140  if (state_ == kParsingBoxes) {
141  result = ParseBox(&err);
142  } else {
143  DCHECK_EQ(kEmittingSamples, state_);
144  result = EnqueueSample(&err);
145  if (result) {
146  int64_t max_clear = runs_->GetMaxClearOffset() + moof_head_;
147  err = !ReadAndDiscardMDATsUntil(max_clear);
148  }
149  }
150  } while (result && !err);
151 
152  if (err) {
153  DLOG(ERROR) << "Error while parsing MP4";
154  moov_.reset();
155  Reset();
156  ChangeState(kError);
157  return false;
158  }
159 
160  return true;
161 }
162 
163 bool MP4MediaParser::LoadMoov(const std::string& file_path) {
164  scoped_ptr<File, FileCloser> file(
165  File::OpenWithNoBuffering(file_path.c_str(), "r"));
166  if (!file) {
167  LOG(ERROR) << "Unable to open media file '" << file_path << "'";
168  return false;
169  }
170  if (!file->Seek(0)) {
171  LOG(WARNING) << "Filesystem does not support seeking on file '" << file_path
172  << "'";
173  return false;
174  }
175 
176  uint64_t file_position(0);
177  bool mdat_seen(false);
178  while (true) {
179  const uint32_t kBoxHeaderReadSize(16);
180  std::vector<uint8_t> buffer(kBoxHeaderReadSize);
181  int64_t bytes_read = file->Read(&buffer[0], kBoxHeaderReadSize);
182  if (bytes_read == 0) {
183  LOG(ERROR) << "Could not find 'moov' box in file '" << file_path << "'";
184  return false;
185  }
186  if (bytes_read < kBoxHeaderReadSize) {
187  LOG(ERROR) << "Error reading media file '" << file_path << "'";
188  return false;
189  }
190  uint64_t box_size;
191  FourCC box_type;
192  bool err;
193  if (!BoxReader::StartTopLevelBox(&buffer[0], kBoxHeaderReadSize, &box_type,
194  &box_size, &err)) {
195  LOG(ERROR) << "Could not start top level box from file '" << file_path
196  << "'";
197  return false;
198  }
199  if (box_type == FOURCC_mdat) {
200  mdat_seen = true;
201  } else if (box_type == FOURCC_moov) {
202  if (!mdat_seen) {
203  // 'moov' is before 'mdat'. Nothing to do.
204  break;
205  }
206  // 'mdat' before 'moov'. Read and parse 'moov'.
207  if (!Parse(&buffer[0], bytes_read)) {
208  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
209  return false;
210  }
211  uint64_t bytes_to_read = box_size - bytes_read;
212  buffer.resize(bytes_to_read);
213  while (bytes_to_read > 0) {
214  bytes_read = file->Read(&buffer[0], bytes_to_read);
215  if (bytes_read <= 0) {
216  LOG(ERROR) << "Error reading 'moov' contents from file '" << file_path
217  << "'";
218  return false;
219  }
220  if (!Parse(&buffer[0], bytes_read)) {
221  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
222  return false;
223  }
224  bytes_to_read -= bytes_read;
225  }
226  queue_.Reset(); // So that we don't need to adjust data offsets.
227  mdat_tail_ = 0; // So it will skip boxes until mdat.
228  break; // Done.
229  }
230  file_position += box_size;
231  if (!file->Seek(file_position)) {
232  LOG(ERROR) << "Error skipping box in mp4 file '" << file_path << "'";
233  return false;
234  }
235  }
236  return true;
237 }
238 
239 bool MP4MediaParser::ParseBox(bool* err) {
240  const uint8_t* buf;
241  int size;
242  queue_.Peek(&buf, &size);
243  if (!size)
244  return false;
245 
246  scoped_ptr<BoxReader> reader(BoxReader::ReadTopLevelBox(buf, size, err));
247  if (reader.get() == NULL)
248  return false;
249 
250  if (reader->type() == FOURCC_mdat) {
251  // The code ends up here only if a MOOV box is not yet seen.
252  DCHECK(!moov_);
253 
254  NOTIMPLEMENTED() << " Files with MDAT before MOOV is not supported yet.";
255  *err = true;
256  return false;
257  }
258 
259  // Set up mdat offset for ReadMDATsUntil().
260  mdat_tail_ = queue_.head() + reader->size();
261 
262  if (reader->type() == FOURCC_moov) {
263  *err = !ParseMoov(reader.get());
264  } else if (reader->type() == FOURCC_moof) {
265  moof_head_ = queue_.head();
266  *err = !ParseMoof(reader.get());
267 
268  // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
269  // be located anywhere in the file, including inside the 'moof' itself.
270  // (Since 'default-base-is-moof' is mandated, no data references can come
271  // before the head of the 'moof', so keeping this box around is sufficient.)
272  return !(*err);
273  } else {
274  VLOG(2) << "Skipping top-level box: " << FourCCToString(reader->type());
275  }
276 
277  queue_.Pop(reader->size());
278  return !(*err);
279 }
280 
281 bool MP4MediaParser::ParseMoov(BoxReader* reader) {
282  if (moov_)
283  return true; // Already parsed the 'moov' box.
284 
285  moov_.reset(new Movie);
286  RCHECK(moov_->Parse(reader));
287  runs_.reset();
288 
289  std::vector<scoped_refptr<StreamInfo> > streams;
290 
291  for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
292  track != moov_->tracks.end(); ++track) {
293  const uint32_t timescale = track->media.header.timescale;
294 
295  // Calculate duration (based on timescale).
296  uint64_t duration = 0;
297  if (track->media.header.duration > 0) {
298  duration = track->media.header.duration;
299  } else if (moov_->extends.header.fragment_duration > 0) {
300  DCHECK(moov_->header.timescale != 0);
301  duration = Rescale(moov_->extends.header.fragment_duration,
302  moov_->header.timescale,
303  timescale);
304  } else if (moov_->header.duration > 0 &&
305  moov_->header.duration != std::numeric_limits<uint64_t>::max()) {
306  DCHECK(moov_->header.timescale != 0);
307  duration =
308  Rescale(moov_->header.duration, moov_->header.timescale, timescale);
309  }
310 
311  const SampleDescription& samp_descr =
312  track->media.information.sample_table.description;
313 
314  size_t desc_idx = 0;
315 
316  // Read sample description index from mvex if it exists otherwise read
317  // from the first entry in Sample To Chunk box.
318  if (moov_->extends.tracks.size() > 0) {
319  for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
320  const TrackExtends& trex = moov_->extends.tracks[t];
321  if (trex.track_id == track->header.track_id) {
322  desc_idx = trex.default_sample_description_index;
323  break;
324  }
325  }
326  } else {
327  const std::vector<ChunkInfo>& chunk_info =
328  track->media.information.sample_table.sample_to_chunk.chunk_info;
329  RCHECK(chunk_info.size() > 0);
330  desc_idx = chunk_info[0].sample_description_index;
331  }
332  RCHECK(desc_idx > 0);
333  desc_idx -= 1; // BMFF descriptor index is one-based
334 
335  if (samp_descr.type == kAudio) {
336  RCHECK(!samp_descr.audio_entries.empty());
337 
338  // It is not uncommon to find otherwise-valid files with incorrect sample
339  // description indices, so we fail gracefully in that case.
340  if (desc_idx >= samp_descr.audio_entries.size())
341  desc_idx = 0;
342 
343  const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx];
344  const FourCC actual_format = entry.GetActualFormat();
345  AudioCodec codec = FourCCToAudioCodec(actual_format);
346  uint8_t num_channels = 0;
347  uint32_t sampling_frequency = 0;
348  uint64_t codec_delay_ns = 0;
349  uint8_t audio_object_type = 0;
350  uint32_t max_bitrate = 0;
351  uint32_t avg_bitrate = 0;
352  std::vector<uint8_t> extra_data;
353 
354  switch (actual_format) {
355  case FOURCC_mp4a:
356  // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
357  // supported MPEG2 AAC variants.
358  if (entry.esds.es_descriptor.IsAAC()) {
359  codec = kCodecAAC;
360  const AACAudioSpecificConfig& aac_audio_specific_config =
361  entry.esds.aac_audio_specific_config;
362  num_channels = aac_audio_specific_config.num_channels();
363  sampling_frequency = aac_audio_specific_config.frequency();
364  audio_object_type = aac_audio_specific_config.audio_object_type();
365  extra_data = entry.esds.es_descriptor.decoder_specific_info();
366  break;
367  } else if (entry.esds.es_descriptor.IsDTS()) {
368  ObjectType audio_type = entry.esds.es_descriptor.object_type();
369  switch (audio_type) {
370  case kDTSC:
371  codec = kCodecDTSC;
372  break;
373  case kDTSE:
374  codec = kCodecDTSE;
375  break;
376  case kDTSH:
377  codec = kCodecDTSH;
378  break;
379  case kDTSL:
380  codec = kCodecDTSL;
381  break;
382  default:
383  LOG(ERROR) << "Unsupported audio type " << audio_type
384  << " in stsd box.";
385  return false;
386  }
387  num_channels = entry.esds.aac_audio_specific_config.num_channels();
388  // For dts audio in esds, current supported number of channels is 6
389  // as the only supported channel layout is 5.1.
390  if (num_channels != kDtsAudioNumChannels) {
391  LOG(ERROR) << "Unsupported channel count " << num_channels
392  << " for audio type " << audio_type << ".";
393  return false;
394  }
395  sampling_frequency = entry.samplerate;
396  max_bitrate = entry.esds.es_descriptor.max_bitrate();
397  avg_bitrate = entry.esds.es_descriptor.avg_bitrate();
398  } else {
399  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
400  << actual_format << " in stsd box.";
401  return false;
402  }
403  break;
404  case FOURCC_dtsc:
405  FALLTHROUGH_INTENDED;
406  case FOURCC_dtsh:
407  FALLTHROUGH_INTENDED;
408  case FOURCC_dtsl:
409  FALLTHROUGH_INTENDED;
410  case FOURCC_dtse:
411  FALLTHROUGH_INTENDED;
412  case FOURCC_dtsm:
413  extra_data = entry.ddts.extra_data;
414  max_bitrate = entry.ddts.max_bitrate;
415  avg_bitrate = entry.ddts.avg_bitrate;
416  num_channels = entry.channelcount;
417  sampling_frequency = entry.samplerate;
418  break;
419  case FOURCC_ac_3:
420  extra_data = entry.dac3.data;
421  num_channels = entry.channelcount;
422  sampling_frequency = entry.samplerate;
423  break;
424  case FOURCC_ec_3:
425  extra_data = entry.dec3.data;
426  num_channels = entry.channelcount;
427  sampling_frequency = entry.samplerate;
428  break;
429  case FOURCC_Opus:
430  extra_data = entry.dops.opus_identification_header;
431  num_channels = entry.channelcount;
432  sampling_frequency = entry.samplerate;
433  RCHECK(sampling_frequency != 0);
434  codec_delay_ns =
435  entry.dops.preskip * kNanosecondsPerSecond / sampling_frequency;
436  break;
437  default:
438  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
439  << actual_format << " in stsd box.";
440  return false;
441  }
442 
443  // Extract possible seek preroll.
444  uint64_t seek_preroll_ns = 0;
445  for (const auto& sample_group_description :
446  track->media.information.sample_table.sample_group_descriptions) {
447  if (sample_group_description.grouping_type != FOURCC_roll)
448  continue;
449  const auto& audio_roll_recovery_entries =
450  sample_group_description.audio_roll_recovery_entries;
451  if (audio_roll_recovery_entries.size() != 1) {
452  LOG(WARNING) << "Unexpected number of entries in "
453  "SampleGroupDescription table with grouping type "
454  "'roll'.";
455  break;
456  }
457  const int16_t roll_distance_in_samples =
458  audio_roll_recovery_entries[0].roll_distance;
459  if (roll_distance_in_samples < 0) {
460  RCHECK(sampling_frequency != 0);
461  seek_preroll_ns = kNanosecondsPerSecond *
462  (-roll_distance_in_samples) / sampling_frequency;
463  } else {
464  LOG(WARNING)
465  << "Roll distance is supposed to be negative, but seeing "
466  << roll_distance_in_samples;
467  }
468  break;
469  }
470 
471  const bool is_encrypted =
472  entry.sinf.info.track_encryption.default_is_protected == 1;
473  DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
474  streams.push_back(new AudioStreamInfo(
475  track->header.track_id,
476  timescale,
477  duration,
478  codec,
479  AudioStreamInfo::GetCodecString(codec, audio_object_type),
480  track->media.header.language.code,
481  entry.samplesize,
482  num_channels,
483  sampling_frequency,
484  seek_preroll_ns,
485  codec_delay_ns,
486  max_bitrate,
487  avg_bitrate,
488  extra_data.data(),
489  extra_data.size(),
490  is_encrypted));
491  }
492 
493  if (samp_descr.type == kVideo) {
494  RCHECK(!samp_descr.video_entries.empty());
495  if (desc_idx >= samp_descr.video_entries.size())
496  desc_idx = 0;
497  const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
498 
499  uint32_t coded_width = entry.width;
500  uint32_t coded_height = entry.height;
501  uint32_t pixel_width = entry.pixel_aspect.h_spacing;
502  uint32_t pixel_height = entry.pixel_aspect.v_spacing;
503  if (pixel_width == 0 && pixel_height == 0) {
504  pixel_width = 1;
505  pixel_height = 1;
506  }
507  std::string codec_string;
508  uint8_t nalu_length_size = 0;
509 
510  const FourCC actual_format = entry.GetActualFormat();
511  const VideoCodec video_codec = FourCCToVideoCodec(actual_format);
512  switch (actual_format) {
513  case FOURCC_avc1: {
514  AVCDecoderConfiguration avc_config;
515  if (!avc_config.Parse(entry.codec_config_record.data)) {
516  LOG(ERROR) << "Failed to parse avcc.";
517  return false;
518  }
519  codec_string = avc_config.GetCodecString();
520  nalu_length_size = avc_config.nalu_length_size();
521 
522  if (coded_width != avc_config.coded_width() ||
523  coded_height != avc_config.coded_height()) {
524  LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
525  << "," << coded_height
526  << ") does not match with resolution in "
527  "AVCDecoderConfigurationRecord ("
528  << avc_config.coded_width() << ","
529  << avc_config.coded_height()
530  << "). Use AVCDecoderConfigurationRecord.";
531  coded_width = avc_config.coded_width();
532  coded_height = avc_config.coded_height();
533  }
534 
535  if (pixel_width != avc_config.pixel_width() ||
536  pixel_height != avc_config.pixel_height()) {
537  LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
538  << "Pixel aspect ratio in PASP box (" << pixel_width << ","
539  << pixel_height
540  << ") does not match with SAR in AVCDecoderConfigurationRecord "
541  "("
542  << avc_config.pixel_width() << "," << avc_config.pixel_height()
543  << "). Use AVCDecoderConfigurationRecord.";
544  pixel_width = avc_config.pixel_width();
545  pixel_height = avc_config.pixel_height();
546  }
547  break;
548  }
549  case FOURCC_hev1:
550  case FOURCC_hvc1: {
551  HEVCDecoderConfiguration hevc_config;
552  if (!hevc_config.Parse(entry.codec_config_record.data)) {
553  LOG(ERROR) << "Failed to parse hevc.";
554  return false;
555  }
556  codec_string = hevc_config.GetCodecString(video_codec);
557  nalu_length_size = hevc_config.nalu_length_size();
558  break;
559  }
560  case FOURCC_vp08:
561  case FOURCC_vp09:
562  case FOURCC_vp10: {
563  VPCodecConfiguration vp_config;
564  if (!vp_config.Parse(entry.codec_config_record.data)) {
565  LOG(ERROR) << "Failed to parse vpcc.";
566  return false;
567  }
568  codec_string = vp_config.GetCodecString(video_codec);
569  break;
570  }
571  default:
572  LOG(ERROR) << "Unsupported video format "
573  << FourCCToString(actual_format) << " in stsd box.";
574  return false;
575  }
576 
577  const bool is_encrypted =
578  entry.sinf.info.track_encryption.default_is_protected == 1;
579  DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
580  streams.push_back(new VideoStreamInfo(
581  track->header.track_id, timescale, duration, video_codec,
582  codec_string, track->media.header.language.code, coded_width,
583  coded_height, pixel_width, pixel_height,
584  0, // trick_play_rate
585  nalu_length_size, entry.codec_config_record.data.data(),
586  entry.codec_config_record.data.size(), is_encrypted));
587  }
588  }
589 
590  init_cb_.Run(streams);
591  if (!FetchKeysIfNecessary(moov_->pssh))
592  return false;
593  runs_.reset(new TrackRunIterator(moov_.get()));
594  RCHECK(runs_->Init());
595  ChangeState(kEmittingSamples);
596  return true;
597 }
598 
599 bool MP4MediaParser::ParseMoof(BoxReader* reader) {
600  // Must already have initialization segment.
601  RCHECK(moov_.get());
602  MovieFragment moof;
603  RCHECK(moof.Parse(reader));
604  if (!runs_)
605  runs_.reset(new TrackRunIterator(moov_.get()));
606  RCHECK(runs_->Init(moof));
607  if (!FetchKeysIfNecessary(moof.pssh))
608  return false;
609  ChangeState(kEmittingSamples);
610  return true;
611 }
612 
613 bool MP4MediaParser::FetchKeysIfNecessary(
614  const std::vector<ProtectionSystemSpecificHeader>& headers) {
615  if (headers.empty())
616  return true;
617 
618  // An error will be returned later if the samples need to be decrypted.
619  if (!decryption_key_source_)
620  return true;
621 
622  Status status;
623  for (std::vector<ProtectionSystemSpecificHeader>::const_iterator iter =
624  headers.begin(); iter != headers.end(); ++iter) {
625  status = decryption_key_source_->FetchKeys(iter->raw_box);
626  if (!status.ok()) {
627  // If there is an error, try using the next PSSH box and report if none
628  // work.
629  VLOG(1) << "Unable to fetch decryption keys: " << status
630  << ", trying the next PSSH box";
631  continue;
632  }
633  return true;
634  }
635 
636  if (!status.ok()) {
637  LOG(ERROR) << "Error fetching decryption keys: " << status;
638  return false;
639  }
640 
641  LOG(ERROR) << "No viable 'pssh' box found for content decryption.";
642  return false;
643 }
644 
645 bool MP4MediaParser::EnqueueSample(bool* err) {
646  if (!runs_->IsRunValid()) {
647  // Remain in kEnqueueingSamples state, discarding data, until the end of
648  // the current 'mdat' box has been appended to the queue.
649  if (!queue_.Trim(mdat_tail_))
650  return false;
651 
652  ChangeState(kParsingBoxes);
653  return true;
654  }
655 
656  if (!runs_->IsSampleValid()) {
657  runs_->AdvanceRun();
658  return true;
659  }
660 
661  DCHECK(!(*err));
662 
663  const uint8_t* buf;
664  int buf_size;
665  queue_.Peek(&buf, &buf_size);
666  if (!buf_size)
667  return false;
668 
669  // Skip this entire track if it is not audio nor video.
670  if (!runs_->is_audio() && !runs_->is_video())
671  runs_->AdvanceRun();
672 
673  // Attempt to cache the auxiliary information first. Aux info is usually
674  // placed in a contiguous block before the sample data, rather than being
675  // interleaved. If we didn't cache it, this would require that we retain the
676  // start of the segment buffer while reading samples. Aux info is typically
677  // quite small compared to sample data, so this pattern is useful on
678  // memory-constrained devices where the source buffer consumes a substantial
679  // portion of the total system memory.
680  if (runs_->AuxInfoNeedsToBeCached()) {
681  queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
682  if (buf_size < runs_->aux_info_size())
683  return false;
684  *err = !runs_->CacheAuxInfo(buf, buf_size);
685  return !*err;
686  }
687 
688  int64_t sample_offset = runs_->sample_offset() + moof_head_;
689  queue_.PeekAt(sample_offset, &buf, &buf_size);
690  if (buf_size < runs_->sample_size()) {
691  if (sample_offset < queue_.head()) {
692  LOG(ERROR) << "Incorrect sample offset " << sample_offset
693  << " < " << queue_.head();
694  *err = true;
695  }
696  return false;
697  }
698 
699  scoped_refptr<MediaSample> stream_sample(MediaSample::CopyFrom(
700  buf, runs_->sample_size(), runs_->is_keyframe()));
701  if (runs_->is_encrypted()) {
702  if (!decryptor_source_) {
703  *err = true;
704  LOG(ERROR) << "Encrypted media sample encountered, but decryption is not "
705  "enabled";
706  return false;
707  }
708 
709  scoped_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
710  if (!decrypt_config ||
711  !decryptor_source_->DecryptSampleBuffer(decrypt_config.get(),
712  stream_sample->writable_data(),
713  stream_sample->data_size())) {
714  *err = true;
715  LOG(ERROR) << "Cannot decrypt samples.";
716  return false;
717  }
718  }
719 
720  stream_sample->set_dts(runs_->dts());
721  stream_sample->set_pts(runs_->cts());
722  stream_sample->set_duration(runs_->duration());
723 
724  DVLOG(3) << "Pushing frame: "
725  << ", key=" << runs_->is_keyframe()
726  << ", dur=" << runs_->duration()
727  << ", dts=" << runs_->dts()
728  << ", cts=" << runs_->cts()
729  << ", size=" << runs_->sample_size();
730 
731  if (!new_sample_cb_.Run(runs_->track_id(), stream_sample)) {
732  *err = true;
733  LOG(ERROR) << "Failed to process the sample.";
734  return false;
735  }
736 
737  runs_->AdvanceSample();
738  return true;
739 }
740 
741 bool MP4MediaParser::ReadAndDiscardMDATsUntil(const int64_t offset) {
742  bool err = false;
743  while (mdat_tail_ < offset) {
744  const uint8_t* buf;
745  int size;
746  queue_.PeekAt(mdat_tail_, &buf, &size);
747 
748  FourCC type;
749  uint64_t box_sz;
750  if (!BoxReader::StartTopLevelBox(buf, size, &type, &box_sz, &err))
751  break;
752 
753  mdat_tail_ += box_sz;
754  }
755  queue_.Trim(std::min(mdat_tail_, offset));
756  return !err;
757 }
758 
759 void MP4MediaParser::ChangeState(State new_state) {
760  DVLOG(2) << "Changing state: " << new_state;
761  state_ = new_state;
762 }
763 
764 } // namespace mp4
765 } // namespace media
766 } // namespace edash_packager
static BoxReader * ReadTopLevelBox(const uint8_t *buf, const size_t buf_size, bool *err)
Definition: box_reader.cc:37
DecryptorSource wraps KeySource and is responsible for decryptor management.
virtual Status FetchKeys(const std::vector< uint8_t > &pssh_box)=0
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)
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31
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
bool Flush() override WARN_UNUSED_RESULT
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:151
static std::string GetCodecString(AudioCodec codec, uint8_t audio_object_type)