2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/mp4_media_parser.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-08-19 22:32:27 +00:00
|
|
|
#include <algorithm>
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/callback.h"
|
|
|
|
#include "packager/base/callback_helpers.h"
|
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
|
|
|
#include "packager/file/file_closer.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/audio_stream_info.h"
|
2015-05-21 00:38:09 +00:00
|
|
|
#include "packager/media/base/buffer_reader.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/decrypt_config.h"
|
|
|
|
#include "packager/media/base/key_source.h"
|
2016-01-06 23:52:18 +00:00
|
|
|
#include "packager/media/base/macros.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/media_sample.h"
|
2016-04-06 23:21:45 +00:00
|
|
|
#include "packager/media/base/rcheck.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2018-01-06 01:18:23 +00:00
|
|
|
#include "packager/media/codecs/ac3_audio_util.h"
|
2018-08-25 02:04:00 +00:00
|
|
|
#include "packager/media/codecs/av1_codec_configuration_record.h"
|
2016-05-25 18:03:17 +00:00
|
|
|
#include "packager/media/codecs/avc_decoder_configuration_record.h"
|
2018-01-06 01:18:23 +00:00
|
|
|
#include "packager/media/codecs/ec3_audio_util.h"
|
2016-05-25 18:03:17 +00:00
|
|
|
#include "packager/media/codecs/es_descriptor.h"
|
|
|
|
#include "packager/media/codecs/hevc_decoder_configuration_record.h"
|
|
|
|
#include "packager/media/codecs/vp_codec_configuration_record.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/box_definitions.h"
|
|
|
|
#include "packager/media/formats/mp4/box_reader.h"
|
|
|
|
#include "packager/media/formats/mp4/track_run_iterator.h"
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-26 20:50:22 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
2013-09-24 04:17:12 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t Rescale(uint64_t time_in_old_scale,
|
|
|
|
uint32_t old_scale,
|
|
|
|
uint32_t new_scale) {
|
2013-10-14 20:55:48 +00:00
|
|
|
return (static_cast<double>(time_in_old_scale) / old_scale) * new_scale;
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 18:34:20 +00:00
|
|
|
H26xStreamFormat GetH26xStreamFormat(FourCC fourcc) {
|
|
|
|
switch (fourcc) {
|
|
|
|
case FOURCC_avc1:
|
|
|
|
return H26xStreamFormat::kNalUnitStreamWithoutParameterSetNalus;
|
|
|
|
case FOURCC_avc3:
|
|
|
|
return H26xStreamFormat::kNalUnitStreamWithParameterSetNalus;
|
|
|
|
case FOURCC_hev1:
|
|
|
|
return H26xStreamFormat::kNalUnitStreamWithParameterSetNalus;
|
|
|
|
case FOURCC_hvc1:
|
|
|
|
return H26xStreamFormat::kNalUnitStreamWithoutParameterSetNalus;
|
|
|
|
default:
|
|
|
|
return H26xStreamFormat::kUnSpecified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:51:08 +00:00
|
|
|
Codec FourCCToCodec(FourCC fourcc) {
|
2015-10-26 20:50:22 +00:00
|
|
|
switch (fourcc) {
|
2018-08-25 02:04:00 +00:00
|
|
|
case FOURCC_av01:
|
|
|
|
return kCodecAV1;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_avc1:
|
2017-03-23 18:34:20 +00:00
|
|
|
case FOURCC_avc3:
|
2015-10-26 20:50:22 +00:00
|
|
|
return kCodecH264;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_hev1:
|
|
|
|
case FOURCC_hvc1:
|
2017-03-23 18:34:20 +00:00
|
|
|
return kCodecH265;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_vp08:
|
2015-10-26 20:50:22 +00:00
|
|
|
return kCodecVP8;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_vp09:
|
2015-10-26 20:50:22 +00:00
|
|
|
return kCodecVP9;
|
2016-06-01 23:28:56 +00:00
|
|
|
case FOURCC_Opus:
|
|
|
|
return kCodecOpus;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsc:
|
2015-12-07 19:56:07 +00:00
|
|
|
return kCodecDTSC;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsh:
|
2015-12-07 19:56:07 +00:00
|
|
|
return kCodecDTSH;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsl:
|
2015-12-07 19:56:07 +00:00
|
|
|
return kCodecDTSL;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtse:
|
2015-12-07 19:56:07 +00:00
|
|
|
return kCodecDTSE;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsp:
|
2015-12-07 19:56:07 +00:00
|
|
|
return kCodecDTSP;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsm:
|
2015-12-07 19:56:07 +00:00
|
|
|
return kCodecDTSM;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_ac_3:
|
2016-01-08 23:56:33 +00:00
|
|
|
return kCodecAC3;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_ec_3:
|
2016-01-15 21:40:44 +00:00
|
|
|
return kCodecEAC3;
|
2018-04-25 18:27:53 +00:00
|
|
|
case FOURCC_fLaC:
|
|
|
|
return kCodecFlac;
|
2015-12-07 19:56:07 +00:00
|
|
|
default:
|
2016-07-27 00:51:08 +00:00
|
|
|
return kUnknownCodec;
|
2015-12-07 19:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 00:53:52 +00:00
|
|
|
Codec ObjectTypeToCodec(ObjectType object_type) {
|
|
|
|
switch (object_type) {
|
|
|
|
case ObjectType::kISO_14496_3:
|
|
|
|
case ObjectType::kISO_13818_7_AAC_LC:
|
|
|
|
return kCodecAAC;
|
|
|
|
case ObjectType::kDTSC:
|
|
|
|
return kCodecDTSC;
|
|
|
|
case ObjectType::kDTSE:
|
|
|
|
return kCodecDTSE;
|
|
|
|
case ObjectType::kDTSH:
|
|
|
|
return kCodecDTSH;
|
|
|
|
case ObjectType::kDTSL:
|
|
|
|
return kCodecDTSL;
|
|
|
|
default:
|
|
|
|
return kUnknownCodec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 18:55:14 +00:00
|
|
|
const uint64_t kNanosecondsPerSecond = 1000000000ull;
|
2014-08-25 22:51:19 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
} // namespace
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
MP4MediaParser::MP4MediaParser()
|
2016-01-13 01:32:48 +00:00
|
|
|
: state_(kWaitingForInit),
|
|
|
|
decryption_key_source_(NULL),
|
|
|
|
moof_head_(0),
|
|
|
|
mdat_tail_(0) {}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-01-13 01:32:48 +00:00
|
|
|
MP4MediaParser::~MP4MediaParser() {}
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
void MP4MediaParser::Init(const InitCB& init_cb,
|
|
|
|
const NewSampleCB& new_sample_cb,
|
2014-08-25 22:51:19 +00:00
|
|
|
KeySource* decryption_key_source) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK_EQ(state_, kWaitingForInit);
|
|
|
|
DCHECK(init_cb_.is_null());
|
|
|
|
DCHECK(!init_cb.is_null());
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!new_sample_cb.is_null());
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
ChangeState(kParsingBoxes);
|
|
|
|
init_cb_ = init_cb;
|
2013-09-24 04:17:12 +00:00
|
|
|
new_sample_cb_ = new_sample_cb;
|
2014-08-25 22:51:19 +00:00
|
|
|
decryption_key_source_ = decryption_key_source;
|
2016-01-13 01:32:48 +00:00
|
|
|
if (decryption_key_source)
|
|
|
|
decryptor_source_.reset(new DecryptorSource(decryption_key_source));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
void MP4MediaParser::Reset() {
|
2013-09-24 01:35:40 +00:00
|
|
|
queue_.Reset();
|
|
|
|
runs_.reset();
|
|
|
|
moof_head_ = 0;
|
|
|
|
mdat_tail_ = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-21 18:30:29 +00:00
|
|
|
bool MP4MediaParser::Flush() {
|
2014-04-24 18:37:33 +00:00
|
|
|
DCHECK_NE(state_, kWaitingForInit);
|
|
|
|
Reset();
|
|
|
|
ChangeState(kParsingBoxes);
|
2016-01-21 18:30:29 +00:00
|
|
|
return true;
|
2014-04-24 18:37:33 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool MP4MediaParser::Parse(const uint8_t* buf, int size) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK_NE(state_, kWaitingForInit);
|
|
|
|
|
|
|
|
if (state_ == kError)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
queue_.Push(buf, size);
|
|
|
|
|
|
|
|
bool result, err = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (state_ == kParsingBoxes) {
|
|
|
|
result = ParseBox(&err);
|
|
|
|
} else {
|
|
|
|
DCHECK_EQ(kEmittingSamples, state_);
|
2013-09-24 04:17:12 +00:00
|
|
|
result = EnqueueSample(&err);
|
2013-09-24 01:35:40 +00:00
|
|
|
if (result) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t max_clear = runs_->GetMaxClearOffset() + moof_head_;
|
2013-09-24 01:35:40 +00:00
|
|
|
err = !ReadAndDiscardMDATsUntil(max_clear);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (result && !err);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
DLOG(ERROR) << "Error while parsing MP4";
|
|
|
|
moov_.reset();
|
|
|
|
Reset();
|
|
|
|
ChangeState(kError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-21 00:38:09 +00:00
|
|
|
bool MP4MediaParser::LoadMoov(const std::string& file_path) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(
|
2015-05-21 00:38:09 +00:00
|
|
|
File::OpenWithNoBuffering(file_path.c_str(), "r"));
|
|
|
|
if (!file) {
|
|
|
|
LOG(ERROR) << "Unable to open media file '" << file_path << "'";
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-26 20:51:40 +00:00
|
|
|
if (!file->Seek(0)) {
|
2015-05-21 00:38:09 +00:00
|
|
|
LOG(WARNING) << "Filesystem does not support seeking on file '" << file_path
|
|
|
|
<< "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t file_position(0);
|
|
|
|
bool mdat_seen(false);
|
|
|
|
while (true) {
|
|
|
|
const uint32_t kBoxHeaderReadSize(16);
|
|
|
|
std::vector<uint8_t> buffer(kBoxHeaderReadSize);
|
|
|
|
int64_t bytes_read = file->Read(&buffer[0], kBoxHeaderReadSize);
|
|
|
|
if (bytes_read == 0) {
|
|
|
|
LOG(ERROR) << "Could not find 'moov' box in file '" << file_path << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (bytes_read < kBoxHeaderReadSize) {
|
|
|
|
LOG(ERROR) << "Error reading media file '" << file_path << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint64_t box_size;
|
|
|
|
FourCC box_type;
|
|
|
|
bool err;
|
2016-05-27 21:38:14 +00:00
|
|
|
if (!BoxReader::StartBox(&buffer[0], kBoxHeaderReadSize, &box_type,
|
|
|
|
&box_size, &err)) {
|
|
|
|
LOG(ERROR) << "Could not start box from file '" << file_path << "'";
|
2015-05-21 00:38:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-06 23:21:45 +00:00
|
|
|
if (box_type == FOURCC_mdat) {
|
2015-05-21 00:38:09 +00:00
|
|
|
mdat_seen = true;
|
2016-04-06 23:21:45 +00:00
|
|
|
} else if (box_type == FOURCC_moov) {
|
2015-05-21 00:38:09 +00:00
|
|
|
if (!mdat_seen) {
|
|
|
|
// 'moov' is before 'mdat'. Nothing to do.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// 'mdat' before 'moov'. Read and parse 'moov'.
|
|
|
|
if (!Parse(&buffer[0], bytes_read)) {
|
|
|
|
LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint64_t bytes_to_read = box_size - bytes_read;
|
|
|
|
buffer.resize(bytes_to_read);
|
|
|
|
while (bytes_to_read > 0) {
|
|
|
|
bytes_read = file->Read(&buffer[0], bytes_to_read);
|
|
|
|
if (bytes_read <= 0) {
|
|
|
|
LOG(ERROR) << "Error reading 'moov' contents from file '" << file_path
|
|
|
|
<< "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Parse(&buffer[0], bytes_read)) {
|
|
|
|
LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bytes_to_read -= bytes_read;
|
|
|
|
}
|
|
|
|
queue_.Reset(); // So that we don't need to adjust data offsets.
|
|
|
|
mdat_tail_ = 0; // So it will skip boxes until mdat.
|
|
|
|
break; // Done.
|
|
|
|
}
|
|
|
|
file_position += box_size;
|
|
|
|
if (!file->Seek(file_position)) {
|
|
|
|
LOG(ERROR) << "Error skipping box in mp4 file '" << file_path << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
bool MP4MediaParser::ParseBox(bool* err) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* buf;
|
2013-09-24 01:35:40 +00:00
|
|
|
int size;
|
|
|
|
queue_.Peek(&buf, &size);
|
2013-11-12 20:32:44 +00:00
|
|
|
if (!size)
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> reader(BoxReader::ReadBox(buf, size, err));
|
2013-11-12 20:32:44 +00:00
|
|
|
if (reader.get() == NULL)
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-04-06 23:21:45 +00:00
|
|
|
if (reader->type() == FOURCC_mdat) {
|
2017-11-30 00:37:35 +00:00
|
|
|
if (!moov_) {
|
|
|
|
// For seekable files, we seek to the 'moov' and load the 'moov' first
|
|
|
|
// then seek back (see LoadMoov function for details); we do not support
|
|
|
|
// having 'mdat' before 'moov' for non-seekable files. The code ends up
|
|
|
|
// here only if it is a non-seekable file.
|
|
|
|
NOTIMPLEMENTED() << " Non-seekable Files with 'mdat' box before 'moov' "
|
|
|
|
"box is not supported.";
|
|
|
|
*err = true;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// This can happen if there are unused 'mdat' boxes, which is unusual
|
|
|
|
// but allowed by the spec. Ignore the 'mdat' and proceed.
|
|
|
|
LOG(INFO)
|
|
|
|
<< "Ignore unused 'mdat' box - this could be as a result of extra "
|
|
|
|
"not usable 'mdat' or 'mdat' associated with unrecognized track.";
|
|
|
|
}
|
2014-03-14 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
// Set up mdat offset for ReadMDATsUntil().
|
|
|
|
mdat_tail_ = queue_.head() + reader->size();
|
|
|
|
|
2016-04-06 23:21:45 +00:00
|
|
|
if (reader->type() == FOURCC_moov) {
|
2013-09-24 01:35:40 +00:00
|
|
|
*err = !ParseMoov(reader.get());
|
2016-04-06 23:21:45 +00:00
|
|
|
} else if (reader->type() == FOURCC_moof) {
|
2013-09-24 01:35:40 +00:00
|
|
|
moof_head_ = queue_.head();
|
|
|
|
*err = !ParseMoof(reader.get());
|
|
|
|
|
|
|
|
// Return early to avoid evicting 'moof' data from queue. Auxiliary info may
|
|
|
|
// be located anywhere in the file, including inside the 'moof' itself.
|
|
|
|
// (Since 'default-base-is-moof' is mandated, no data references can come
|
|
|
|
// before the head of the 'moof', so keeping this box around is sufficient.)
|
|
|
|
return !(*err);
|
|
|
|
} else {
|
2015-05-12 00:52:53 +00:00
|
|
|
VLOG(2) << "Skipping top-level box: " << FourCCToString(reader->type());
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 02:11:13 +00:00
|
|
|
queue_.Pop(static_cast<int>(reader->size()));
|
2013-09-24 01:35:40 +00:00
|
|
|
return !(*err);
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
bool MP4MediaParser::ParseMoov(BoxReader* reader) {
|
2015-05-21 00:38:09 +00:00
|
|
|
if (moov_)
|
|
|
|
return true; // Already parsed the 'moov' box.
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
moov_.reset(new Movie);
|
|
|
|
RCHECK(moov_->Parse(reader));
|
|
|
|
runs_.reset();
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::vector<std::shared_ptr<StreamInfo>> streams;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
|
|
|
|
track != moov_->tracks.end(); ++track) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t timescale = track->media.header.timescale;
|
2013-10-14 20:55:48 +00:00
|
|
|
|
|
|
|
// Calculate duration (based on timescale).
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t duration = 0;
|
2013-10-14 20:55:48 +00:00
|
|
|
if (track->media.header.duration > 0) {
|
|
|
|
duration = track->media.header.duration;
|
|
|
|
} else if (moov_->extends.header.fragment_duration > 0) {
|
|
|
|
DCHECK(moov_->header.timescale != 0);
|
|
|
|
duration = Rescale(moov_->extends.header.fragment_duration,
|
2013-11-12 20:32:44 +00:00
|
|
|
moov_->header.timescale,
|
|
|
|
timescale);
|
2013-10-14 20:55:48 +00:00
|
|
|
} else if (moov_->header.duration > 0 &&
|
2014-09-30 23:52:58 +00:00
|
|
|
moov_->header.duration != std::numeric_limits<uint64_t>::max()) {
|
2013-10-14 20:55:48 +00:00
|
|
|
DCHECK(moov_->header.timescale != 0);
|
2013-11-12 20:32:44 +00:00
|
|
|
duration =
|
|
|
|
Rescale(moov_->header.duration, moov_->header.timescale, timescale);
|
2013-10-14 20:55:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
const SampleDescription& samp_descr =
|
|
|
|
track->media.information.sample_table.description;
|
|
|
|
|
|
|
|
size_t desc_idx = 0;
|
2013-10-08 17:37:58 +00:00
|
|
|
|
|
|
|
// Read sample description index from mvex if it exists otherwise read
|
|
|
|
// from the first entry in Sample To Chunk box.
|
|
|
|
if (moov_->extends.tracks.size() > 0) {
|
|
|
|
for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
|
|
|
|
const TrackExtends& trex = moov_->extends.tracks[t];
|
|
|
|
if (trex.track_id == track->header.track_id) {
|
|
|
|
desc_idx = trex.default_sample_description_index;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-10-08 17:37:58 +00:00
|
|
|
} else {
|
|
|
|
const std::vector<ChunkInfo>& chunk_info =
|
|
|
|
track->media.information.sample_table.sample_to_chunk.chunk_info;
|
|
|
|
RCHECK(chunk_info.size() > 0);
|
|
|
|
desc_idx = chunk_info[0].sample_description_index;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
RCHECK(desc_idx > 0);
|
|
|
|
desc_idx -= 1; // BMFF descriptor index is one-based
|
|
|
|
|
2015-12-21 23:10:17 +00:00
|
|
|
if (samp_descr.type == kAudio) {
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(!samp_descr.audio_entries.empty());
|
|
|
|
|
|
|
|
// It is not uncommon to find otherwise-valid files with incorrect sample
|
|
|
|
// description indices, so we fail gracefully in that case.
|
|
|
|
if (desc_idx >= samp_descr.audio_entries.size())
|
|
|
|
desc_idx = 0;
|
2015-12-07 19:35:11 +00:00
|
|
|
|
2015-12-07 19:56:07 +00:00
|
|
|
const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx];
|
|
|
|
const FourCC actual_format = entry.GetActualFormat();
|
2016-07-27 00:51:08 +00:00
|
|
|
Codec codec = FourCCToCodec(actual_format);
|
2018-05-17 00:53:52 +00:00
|
|
|
uint8_t num_channels = entry.channelcount;
|
|
|
|
uint32_t sampling_frequency = entry.samplerate;
|
2016-05-09 18:55:14 +00:00
|
|
|
uint64_t codec_delay_ns = 0;
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t audio_object_type = 0;
|
2016-01-04 18:57:05 +00:00
|
|
|
uint32_t max_bitrate = 0;
|
|
|
|
uint32_t avg_bitrate = 0;
|
2016-06-27 19:30:32 +00:00
|
|
|
std::vector<uint8_t> codec_config;
|
2015-12-07 19:56:07 +00:00
|
|
|
|
|
|
|
switch (actual_format) {
|
2019-01-04 23:49:57 +00:00
|
|
|
case FOURCC_mp4a: {
|
|
|
|
const DecoderConfigDescriptor& decoder_config =
|
|
|
|
entry.esds.es_descriptor.decoder_config_descriptor();
|
|
|
|
max_bitrate = decoder_config.max_bitrate();
|
|
|
|
avg_bitrate = decoder_config.avg_bitrate();
|
2018-05-17 19:43:21 +00:00
|
|
|
|
2019-01-04 23:49:57 +00:00
|
|
|
codec = ObjectTypeToCodec(decoder_config.object_type());
|
2018-05-17 19:43:21 +00:00
|
|
|
if (codec == kCodecAAC) {
|
|
|
|
const AACAudioSpecificConfig& aac_audio_specific_config =
|
|
|
|
entry.esds.aac_audio_specific_config;
|
|
|
|
num_channels = aac_audio_specific_config.GetNumChannels();
|
|
|
|
sampling_frequency =
|
|
|
|
aac_audio_specific_config.GetSamplesPerSecond();
|
|
|
|
audio_object_type = aac_audio_specific_config.GetAudioObjectType();
|
2019-01-04 23:49:57 +00:00
|
|
|
codec_config =
|
|
|
|
decoder_config.decoder_specific_info_descriptor().data();
|
2018-05-17 19:43:21 +00:00
|
|
|
} else if (codec == kUnknownCodec) {
|
2018-05-17 00:53:52 +00:00
|
|
|
// Intentionally not to fail in the parser as there may be multiple
|
|
|
|
// streams in the source content, which allows the supported stream
|
|
|
|
// to be packaged. An error will be returned if the unsupported
|
|
|
|
// stream is passed to the muxer.
|
|
|
|
LOG(WARNING) << "Unsupported audio object type "
|
2019-01-04 23:49:57 +00:00
|
|
|
<< static_cast<int>(decoder_config.object_type())
|
2018-05-17 00:53:52 +00:00
|
|
|
<< " in stsd.es_desriptor.";
|
2015-12-07 19:56:07 +00:00
|
|
|
}
|
2016-01-06 23:52:18 +00:00
|
|
|
break;
|
2019-01-04 23:49:57 +00:00
|
|
|
}
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsc:
|
2016-01-06 23:52:18 +00:00
|
|
|
FALLTHROUGH_INTENDED;
|
2018-04-25 18:27:53 +00:00
|
|
|
case FOURCC_dtse:
|
|
|
|
FALLTHROUGH_INTENDED;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsh:
|
2016-01-06 23:52:18 +00:00
|
|
|
FALLTHROUGH_INTENDED;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsl:
|
2016-01-06 23:52:18 +00:00
|
|
|
FALLTHROUGH_INTENDED;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_dtsm:
|
2016-06-27 19:30:32 +00:00
|
|
|
codec_config = entry.ddts.extra_data;
|
2016-01-04 18:57:05 +00:00
|
|
|
max_bitrate = entry.ddts.max_bitrate;
|
|
|
|
avg_bitrate = entry.ddts.avg_bitrate;
|
2015-12-07 19:56:07 +00:00
|
|
|
break;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_ac_3:
|
2016-06-27 19:30:32 +00:00
|
|
|
codec_config = entry.dac3.data;
|
2018-01-06 01:18:23 +00:00
|
|
|
num_channels = static_cast<uint8_t>(GetAc3NumChannels(codec_config));
|
2015-12-07 19:56:07 +00:00
|
|
|
break;
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_ec_3:
|
2016-06-27 19:30:32 +00:00
|
|
|
codec_config = entry.dec3.data;
|
2018-01-06 01:18:23 +00:00
|
|
|
num_channels = static_cast<uint8_t>(GetEc3NumChannels(codec_config));
|
2016-01-15 21:40:44 +00:00
|
|
|
break;
|
2018-04-25 18:27:53 +00:00
|
|
|
case FOURCC_fLaC:
|
|
|
|
codec_config = entry.dfla.data;
|
|
|
|
break;
|
2016-05-09 18:55:14 +00:00
|
|
|
case FOURCC_Opus:
|
2016-06-27 19:30:32 +00:00
|
|
|
codec_config = entry.dops.opus_identification_header;
|
2016-05-09 18:55:14 +00:00
|
|
|
codec_delay_ns =
|
|
|
|
entry.dops.preskip * kNanosecondsPerSecond / sampling_frequency;
|
|
|
|
break;
|
2015-12-07 19:56:07 +00:00
|
|
|
default:
|
2018-05-17 00:53:52 +00:00
|
|
|
// Intentionally not to fail in the parser as there may be multiple
|
|
|
|
// streams in the source content, which allows the supported stream to
|
|
|
|
// be packaged.
|
|
|
|
// An error will be returned if the unsupported stream is passed to
|
|
|
|
// the muxer.
|
|
|
|
LOG(WARNING) << "Unsupported audio format '"
|
|
|
|
<< FourCCToString(actual_format) << "' in stsd box.";
|
|
|
|
break;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 23:56:50 +00:00
|
|
|
// Extract possible seek preroll.
|
|
|
|
uint64_t seek_preroll_ns = 0;
|
|
|
|
for (const auto& sample_group_description :
|
|
|
|
track->media.information.sample_table.sample_group_descriptions) {
|
|
|
|
if (sample_group_description.grouping_type != FOURCC_roll)
|
|
|
|
continue;
|
|
|
|
const auto& audio_roll_recovery_entries =
|
|
|
|
sample_group_description.audio_roll_recovery_entries;
|
|
|
|
if (audio_roll_recovery_entries.size() != 1) {
|
|
|
|
LOG(WARNING) << "Unexpected number of entries in "
|
|
|
|
"SampleGroupDescription table with grouping type "
|
|
|
|
"'roll'.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const int16_t roll_distance_in_samples =
|
|
|
|
audio_roll_recovery_entries[0].roll_distance;
|
|
|
|
if (roll_distance_in_samples < 0) {
|
|
|
|
RCHECK(sampling_frequency != 0);
|
|
|
|
seek_preroll_ns = kNanosecondsPerSecond *
|
|
|
|
(-roll_distance_in_samples) / sampling_frequency;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "Roll distance is supposed to be negative, but seeing "
|
|
|
|
<< roll_distance_in_samples;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
// The stream will be decrypted if a |decryptor_source_| is available.
|
2016-04-12 22:39:10 +00:00
|
|
|
const bool is_encrypted =
|
2017-03-11 02:49:55 +00:00
|
|
|
decryptor_source_
|
|
|
|
? false
|
|
|
|
: entry.sinf.info.track_encryption.default_is_protected == 1;
|
2014-03-17 18:36:41 +00:00
|
|
|
DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
|
2017-01-24 00:55:02 +00:00
|
|
|
streams.emplace_back(new AudioStreamInfo(
|
2016-07-27 00:51:08 +00:00
|
|
|
track->header.track_id, timescale, duration, codec,
|
2013-11-12 20:32:44 +00:00
|
|
|
AudioStreamInfo::GetCodecString(codec, audio_object_type),
|
2016-07-27 00:51:08 +00:00
|
|
|
codec_config.data(), codec_config.size(), entry.samplesize,
|
|
|
|
num_channels, sampling_frequency, seek_preroll_ns, codec_delay_ns,
|
|
|
|
max_bitrate, avg_bitrate, track->media.header.language.code,
|
2014-03-17 18:36:41 +00:00
|
|
|
is_encrypted));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2015-12-21 23:10:17 +00:00
|
|
|
if (samp_descr.type == kVideo) {
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(!samp_descr.video_entries.empty());
|
|
|
|
if (desc_idx >= samp_descr.video_entries.size())
|
|
|
|
desc_idx = 0;
|
|
|
|
const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
|
2018-09-11 21:53:55 +00:00
|
|
|
std::vector<uint8_t> codec_configuration_data =
|
|
|
|
entry.codec_configuration.data;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2015-10-27 00:52:57 +00:00
|
|
|
uint32_t coded_width = entry.width;
|
|
|
|
uint32_t coded_height = entry.height;
|
|
|
|
uint32_t pixel_width = entry.pixel_aspect.h_spacing;
|
|
|
|
uint32_t pixel_height = entry.pixel_aspect.v_spacing;
|
|
|
|
if (pixel_width == 0 && pixel_height == 0) {
|
|
|
|
pixel_width = 1;
|
|
|
|
pixel_height = 1;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2015-10-27 00:52:57 +00:00
|
|
|
std::string codec_string;
|
|
|
|
uint8_t nalu_length_size = 0;
|
|
|
|
|
|
|
|
const FourCC actual_format = entry.GetActualFormat();
|
2016-07-27 00:51:08 +00:00
|
|
|
const Codec video_codec = FourCCToCodec(actual_format);
|
2015-10-27 00:52:57 +00:00
|
|
|
switch (actual_format) {
|
2018-08-25 02:04:00 +00:00
|
|
|
case FOURCC_av01: {
|
|
|
|
AV1CodecConfigurationRecord av1_config;
|
2018-09-11 21:53:55 +00:00
|
|
|
if (!av1_config.Parse(codec_configuration_data)) {
|
2018-08-25 02:04:00 +00:00
|
|
|
LOG(ERROR) << "Failed to parse av1c.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
codec_string = av1_config.GetCodecString();
|
|
|
|
break;
|
|
|
|
}
|
2017-03-23 18:34:20 +00:00
|
|
|
case FOURCC_avc1:
|
|
|
|
case FOURCC_avc3: {
|
2016-05-25 17:33:53 +00:00
|
|
|
AVCDecoderConfigurationRecord avc_config;
|
2018-09-11 21:53:55 +00:00
|
|
|
if (!avc_config.Parse(codec_configuration_data)) {
|
2015-10-27 00:52:57 +00:00
|
|
|
LOG(ERROR) << "Failed to parse avcc.";
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-23 18:36:47 +00:00
|
|
|
codec_string = avc_config.GetCodecString(actual_format);
|
2016-02-26 22:34:14 +00:00
|
|
|
nalu_length_size = avc_config.nalu_length_size();
|
2015-10-27 00:52:57 +00:00
|
|
|
|
|
|
|
if (coded_width != avc_config.coded_width() ||
|
|
|
|
coded_height != avc_config.coded_height()) {
|
|
|
|
LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
|
|
|
|
<< "," << coded_height
|
|
|
|
<< ") does not match with resolution in "
|
|
|
|
"AVCDecoderConfigurationRecord ("
|
|
|
|
<< avc_config.coded_width() << ","
|
|
|
|
<< avc_config.coded_height()
|
|
|
|
<< "). Use AVCDecoderConfigurationRecord.";
|
|
|
|
coded_width = avc_config.coded_width();
|
|
|
|
coded_height = avc_config.coded_height();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pixel_width != avc_config.pixel_width() ||
|
|
|
|
pixel_height != avc_config.pixel_height()) {
|
|
|
|
LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
|
|
|
|
<< "Pixel aspect ratio in PASP box (" << pixel_width << ","
|
|
|
|
<< pixel_height
|
|
|
|
<< ") does not match with SAR in AVCDecoderConfigurationRecord "
|
|
|
|
"("
|
|
|
|
<< avc_config.pixel_width() << "," << avc_config.pixel_height()
|
|
|
|
<< "). Use AVCDecoderConfigurationRecord.";
|
|
|
|
pixel_width = avc_config.pixel_width();
|
|
|
|
pixel_height = avc_config.pixel_height();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_hev1:
|
|
|
|
case FOURCC_hvc1: {
|
2016-05-25 17:33:53 +00:00
|
|
|
HEVCDecoderConfigurationRecord hevc_config;
|
2018-09-11 21:53:55 +00:00
|
|
|
if (!hevc_config.Parse(codec_configuration_data)) {
|
2015-10-29 00:26:29 +00:00
|
|
|
LOG(ERROR) << "Failed to parse hevc.";
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-23 18:34:20 +00:00
|
|
|
codec_string = hevc_config.GetCodecString(actual_format);
|
2016-02-26 22:34:14 +00:00
|
|
|
nalu_length_size = hevc_config.nalu_length_size();
|
2015-10-29 00:26:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-06 23:21:45 +00:00
|
|
|
case FOURCC_vp08:
|
2018-08-25 02:04:00 +00:00
|
|
|
case FOURCC_vp09: {
|
2016-05-25 17:33:53 +00:00
|
|
|
VPCodecConfigurationRecord vp_config;
|
2018-09-11 21:53:55 +00:00
|
|
|
if (!vp_config.ParseMP4(codec_configuration_data)) {
|
2015-10-26 20:50:22 +00:00
|
|
|
LOG(ERROR) << "Failed to parse vpcc.";
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-11 21:53:55 +00:00
|
|
|
if (actual_format == FOURCC_vp09 &&
|
|
|
|
(!vp_config.is_level_set() || vp_config.level() == 0)) {
|
|
|
|
const double kUnknownSampleDuration = 0.0;
|
|
|
|
vp_config.SetVP9Level(coded_width, coded_height,
|
|
|
|
kUnknownSampleDuration);
|
|
|
|
vp_config.WriteMP4(&codec_configuration_data);
|
|
|
|
}
|
2015-10-26 20:50:22 +00:00
|
|
|
codec_string = vp_config.GetCodecString(video_codec);
|
|
|
|
break;
|
|
|
|
}
|
2015-10-27 00:52:57 +00:00
|
|
|
default:
|
2018-05-17 00:53:52 +00:00
|
|
|
// Intentionally not to fail in the parser as there may be multiple
|
|
|
|
// streams in the source content, which allows the supported stream to
|
|
|
|
// be packaged.
|
|
|
|
// An error will be returned if the unsupported stream is passed to
|
|
|
|
// the muxer.
|
|
|
|
LOG(WARNING) << "Unsupported video format '"
|
|
|
|
<< FourCCToString(actual_format) << "' in stsd box.";
|
|
|
|
break;
|
2015-09-16 22:22:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
// The stream will be decrypted if a |decryptor_source_| is available.
|
2016-04-12 22:39:10 +00:00
|
|
|
const bool is_encrypted =
|
2017-03-11 02:49:55 +00:00
|
|
|
decryptor_source_
|
|
|
|
? false
|
|
|
|
: entry.sinf.info.track_encryption.default_is_protected == 1;
|
2014-03-17 18:36:41 +00:00
|
|
|
DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> video_stream_info(new VideoStreamInfo(
|
2015-10-26 20:50:22 +00:00
|
|
|
track->header.track_id, timescale, duration, video_codec,
|
2017-03-23 18:34:20 +00:00
|
|
|
GetH26xStreamFormat(actual_format), codec_string,
|
2018-09-11 21:53:55 +00:00
|
|
|
codec_configuration_data.data(), codec_configuration_data.size(),
|
|
|
|
coded_width, coded_height, pixel_width, pixel_height,
|
2017-05-15 16:22:06 +00:00
|
|
|
0, // trick_play_factor
|
2016-07-27 00:51:08 +00:00
|
|
|
nalu_length_size, track->media.header.language.code, is_encrypted));
|
2016-08-17 00:57:34 +00:00
|
|
|
|
|
|
|
// Set pssh raw data if it has.
|
|
|
|
if (moov_->pssh.size() > 0) {
|
|
|
|
std::vector<uint8_t> pssh_raw_data;
|
|
|
|
for (const auto& pssh : moov_->pssh) {
|
|
|
|
pssh_raw_data.insert(pssh_raw_data.end(), pssh.raw_box.begin(),
|
|
|
|
pssh.raw_box.end());
|
|
|
|
}
|
|
|
|
video_stream_info->set_eme_init_data(pssh_raw_data.data(),
|
|
|
|
pssh_raw_data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.push_back(video_stream_info);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
init_cb_.Run(streams);
|
2014-08-25 22:51:19 +00:00
|
|
|
if (!FetchKeysIfNecessary(moov_->pssh))
|
|
|
|
return false;
|
2013-10-08 17:37:58 +00:00
|
|
|
runs_.reset(new TrackRunIterator(moov_.get()));
|
|
|
|
RCHECK(runs_->Init());
|
|
|
|
ChangeState(kEmittingSamples);
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
bool MP4MediaParser::ParseMoof(BoxReader* reader) {
|
2013-10-08 17:37:58 +00:00
|
|
|
// Must already have initialization segment.
|
2014-04-24 18:37:33 +00:00
|
|
|
RCHECK(moov_.get());
|
2013-09-24 01:35:40 +00:00
|
|
|
MovieFragment moof;
|
|
|
|
RCHECK(moof.Parse(reader));
|
2014-04-24 18:37:33 +00:00
|
|
|
if (!runs_)
|
|
|
|
runs_.reset(new TrackRunIterator(moov_.get()));
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(runs_->Init(moof));
|
2014-08-25 22:51:19 +00:00
|
|
|
if (!FetchKeysIfNecessary(moof.pssh))
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
ChangeState(kEmittingSamples);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-25 22:51:19 +00:00
|
|
|
bool MP4MediaParser::FetchKeysIfNecessary(
|
2013-09-24 01:35:40 +00:00
|
|
|
const std::vector<ProtectionSystemSpecificHeader>& headers) {
|
|
|
|
if (headers.empty())
|
2014-08-25 22:51:19 +00:00
|
|
|
return true;
|
|
|
|
|
2014-10-14 02:34:29 +00:00
|
|
|
// An error will be returned later if the samples need to be decrypted.
|
|
|
|
if (!decryption_key_source_)
|
|
|
|
return true;
|
2014-08-25 22:51:19 +00:00
|
|
|
|
2017-04-04 19:43:41 +00:00
|
|
|
std::vector<uint8_t> pssh_raw_data;
|
|
|
|
for (const auto& header : headers) {
|
|
|
|
pssh_raw_data.insert(pssh_raw_data.end(), header.raw_box.begin(),
|
|
|
|
header.raw_box.end());
|
2016-02-19 18:24:33 +00:00
|
|
|
}
|
2017-04-04 19:43:41 +00:00
|
|
|
Status status =
|
|
|
|
decryption_key_source_->FetchKeys(EmeInitDataType::CENC, pssh_raw_data);
|
2016-02-19 18:24:33 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Error fetching decryption keys: " << status;
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2017-04-04 19:43:41 +00:00
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
bool MP4MediaParser::EnqueueSample(bool* err) {
|
2013-09-24 01:35:40 +00:00
|
|
|
if (!runs_->IsRunValid()) {
|
|
|
|
// Remain in kEnqueueingSamples state, discarding data, until the end of
|
|
|
|
// the current 'mdat' box has been appended to the queue.
|
|
|
|
if (!queue_.Trim(mdat_tail_))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ChangeState(kParsingBoxes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!runs_->IsSampleValid()) {
|
|
|
|
runs_->AdvanceRun();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(!(*err));
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* buf;
|
2013-09-24 01:35:40 +00:00
|
|
|
int buf_size;
|
|
|
|
queue_.Peek(&buf, &buf_size);
|
2013-11-12 20:32:44 +00:00
|
|
|
if (!buf_size)
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-03-17 18:36:41 +00:00
|
|
|
// Skip this entire track if it is not audio nor video.
|
|
|
|
if (!runs_->is_audio() && !runs_->is_video())
|
2013-09-24 01:35:40 +00:00
|
|
|
runs_->AdvanceRun();
|
|
|
|
|
|
|
|
// Attempt to cache the auxiliary information first. Aux info is usually
|
|
|
|
// placed in a contiguous block before the sample data, rather than being
|
|
|
|
// interleaved. If we didn't cache it, this would require that we retain the
|
|
|
|
// start of the segment buffer while reading samples. Aux info is typically
|
|
|
|
// quite small compared to sample data, so this pattern is useful on
|
|
|
|
// memory-constrained devices where the source buffer consumes a substantial
|
|
|
|
// portion of the total system memory.
|
|
|
|
if (runs_->AuxInfoNeedsToBeCached()) {
|
|
|
|
queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
|
2013-11-12 20:32:44 +00:00
|
|
|
if (buf_size < runs_->aux_info_size())
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
*err = !runs_->CacheAuxInfo(buf, buf_size);
|
|
|
|
return !*err;
|
|
|
|
}
|
|
|
|
|
2014-12-23 20:10:23 +00:00
|
|
|
int64_t sample_offset = runs_->sample_offset() + moof_head_;
|
|
|
|
queue_.PeekAt(sample_offset, &buf, &buf_size);
|
|
|
|
if (buf_size < runs_->sample_size()) {
|
|
|
|
if (sample_offset < queue_.head()) {
|
|
|
|
LOG(ERROR) << "Incorrect sample offset " << sample_offset
|
|
|
|
<< " < " << queue_.head();
|
|
|
|
*err = true;
|
|
|
|
}
|
2013-11-12 20:32:44 +00:00
|
|
|
return false;
|
2014-12-23 20:10:23 +00:00
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
const uint8_t* media_data = buf;
|
|
|
|
const size_t media_data_size = runs_->sample_size();
|
|
|
|
// Use a dummy data size of 0 to avoid copying overhead.
|
|
|
|
// Actual media data is set later.
|
|
|
|
const size_t kDummyDataSize = 0;
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> stream_sample(
|
2017-09-18 23:31:00 +00:00
|
|
|
MediaSample::CopyFrom(media_data, kDummyDataSize, runs_->is_keyframe()));
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
if (runs_->is_encrypted()) {
|
2017-09-18 23:31:00 +00:00
|
|
|
std::shared_ptr<uint8_t> decrypted_media_data(
|
|
|
|
new uint8_t[media_data_size], std::default_delete<uint8_t[]>());
|
2016-08-17 00:57:34 +00:00
|
|
|
std::unique_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
|
|
|
|
if (!decrypt_config) {
|
2016-01-13 01:32:48 +00:00
|
|
|
*err = true;
|
2016-08-17 00:57:34 +00:00
|
|
|
LOG(ERROR) << "Missing decrypt config.";
|
2016-01-13 01:32:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 00:57:34 +00:00
|
|
|
if (!decryptor_source_) {
|
2017-09-18 23:31:00 +00:00
|
|
|
stream_sample->SetData(media_data, media_data_size);
|
2016-08-17 00:57:34 +00:00
|
|
|
// If the demuxer does not have the decryptor_source_, store
|
|
|
|
// decrypt_config so that the demuxed sample can be decrypted later.
|
|
|
|
stream_sample->set_decrypt_config(std::move(decrypt_config));
|
|
|
|
stream_sample->set_is_encrypted(true);
|
2017-09-18 23:31:00 +00:00
|
|
|
} else {
|
|
|
|
if (!decryptor_source_->DecryptSampleBuffer(decrypt_config.get(),
|
|
|
|
media_data, media_data_size,
|
|
|
|
decrypted_media_data.get())) {
|
|
|
|
*err = true;
|
|
|
|
LOG(ERROR) << "Cannot decrypt samples.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stream_sample->TransferData(std::move(decrypted_media_data),
|
|
|
|
media_data_size);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2017-09-18 23:31:00 +00:00
|
|
|
} else {
|
|
|
|
stream_sample->SetData(media_data, media_data_size);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
stream_sample->set_dts(runs_->dts());
|
|
|
|
stream_sample->set_pts(runs_->cts());
|
|
|
|
stream_sample->set_duration(runs_->duration());
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-03-17 18:36:41 +00:00
|
|
|
DVLOG(3) << "Pushing frame: "
|
2013-09-24 01:35:40 +00:00
|
|
|
<< ", key=" << runs_->is_keyframe()
|
2013-09-24 04:17:12 +00:00
|
|
|
<< ", dur=" << runs_->duration()
|
|
|
|
<< ", dts=" << runs_->dts()
|
|
|
|
<< ", cts=" << runs_->cts()
|
2013-09-24 01:35:40 +00:00
|
|
|
<< ", size=" << runs_->sample_size();
|
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
if (!new_sample_cb_.Run(runs_->track_id(), stream_sample)) {
|
|
|
|
*err = true;
|
|
|
|
LOG(ERROR) << "Failed to process the sample.";
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
runs_->AdvanceSample();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool MP4MediaParser::ReadAndDiscardMDATsUntil(const int64_t offset) {
|
2013-09-24 01:35:40 +00:00
|
|
|
bool err = false;
|
|
|
|
while (mdat_tail_ < offset) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* buf;
|
2013-09-24 01:35:40 +00:00
|
|
|
int size;
|
|
|
|
queue_.PeekAt(mdat_tail_, &buf, &size);
|
|
|
|
|
|
|
|
FourCC type;
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t box_sz;
|
2016-05-27 21:38:14 +00:00
|
|
|
if (!BoxReader::StartBox(buf, size, &type, &box_sz, &err))
|
2013-09-24 01:35:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
mdat_tail_ += box_sz;
|
|
|
|
}
|
|
|
|
queue_.Trim(std::min(mdat_tail_, offset));
|
|
|
|
return !err;
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
void MP4MediaParser::ChangeState(State new_state) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DVLOG(2) << "Changing state: " << new_state;
|
|
|
|
state_ = new_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|