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