2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/fragmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
2016-05-06 23:56:50 +00:00
|
|
|
#include "packager/media/base/audio_stream_info.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/formats/mp4/box_definitions.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-18 18:49:49 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace {
|
2014-09-30 23:52:58 +00:00
|
|
|
const int64_t kInvalidTime = std::numeric_limits<int64_t>::max();
|
2016-05-06 23:56:50 +00:00
|
|
|
|
|
|
|
uint64_t GetSeekPreroll(const StreamInfo& stream_info) {
|
2016-07-12 20:48:28 +00:00
|
|
|
if (stream_info.stream_type() != kStreamAudio)
|
|
|
|
return 0;
|
2016-05-06 23:56:50 +00:00
|
|
|
const AudioStreamInfo& audio_stream_info =
|
|
|
|
static_cast<const AudioStreamInfo&>(stream_info);
|
|
|
|
return audio_stream_info.seek_preroll_ns();
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-05-06 23:56:50 +00:00
|
|
|
Fragmenter::Fragmenter(scoped_refptr<StreamInfo> info, TrackFragment* traf)
|
2016-07-12 20:48:28 +00:00
|
|
|
: use_decoding_timestamp_in_timeline_(false),
|
|
|
|
traf_(traf),
|
2016-05-06 23:56:50 +00:00
|
|
|
seek_preroll_(GetSeekPreroll(*info)),
|
2014-06-30 16:32:06 +00:00
|
|
|
fragment_initialized_(false),
|
2014-04-18 18:49:49 +00:00
|
|
|
fragment_finalized_(false),
|
|
|
|
fragment_duration_(0),
|
|
|
|
earliest_presentation_time_(kInvalidTime),
|
|
|
|
first_sap_time_(kInvalidTime) {
|
|
|
|
DCHECK(traf);
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Fragmenter::~Fragmenter() {}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Status Fragmenter::AddSample(scoped_refptr<MediaSample> sample) {
|
2014-05-08 20:53:08 +00:00
|
|
|
DCHECK(sample);
|
2016-07-18 18:37:19 +00:00
|
|
|
if (sample->duration() == 0) {
|
|
|
|
LOG(WARNING) << "Unexpected sample with zero duration @ dts "
|
|
|
|
<< sample->dts();
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-06-30 16:32:06 +00:00
|
|
|
if (!fragment_initialized_) {
|
|
|
|
Status status = InitializeFragment(sample->dts());
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:50:40 +00:00
|
|
|
if (sample->side_data_size() > 0)
|
|
|
|
LOG(WARNING) << "MP4 samples do not support side data. Side data ignored.";
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
// Fill in sample parameters. It will be optimized later.
|
2016-11-09 02:11:13 +00:00
|
|
|
traf_->runs[0].sample_sizes.push_back(
|
|
|
|
static_cast<uint32_t>(sample->data_size()));
|
2013-11-12 20:37:58 +00:00
|
|
|
traf_->runs[0].sample_durations.push_back(sample->duration());
|
|
|
|
traf_->runs[0].sample_flags.push_back(
|
|
|
|
sample->is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
|
|
|
|
|
|
|
|
data_->AppendArray(sample->data(), sample->data_size());
|
|
|
|
fragment_duration_ += sample->duration();
|
|
|
|
|
2016-07-12 20:48:28 +00:00
|
|
|
const int64_t pts = sample->pts();
|
|
|
|
const int64_t dts = sample->dts();
|
2014-01-22 23:51:26 +00:00
|
|
|
|
2016-07-12 20:48:28 +00:00
|
|
|
const int64_t timestamp = use_decoding_timestamp_in_timeline_ ? dts : pts;
|
|
|
|
// Set |earliest_presentation_time_| to |timestamp| if |timestamp| is smaller
|
|
|
|
// or if it is not yet initialized (kInvalidTime > timestamp is always true).
|
|
|
|
if (earliest_presentation_time_ > timestamp)
|
|
|
|
earliest_presentation_time_ = timestamp;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-07-12 20:48:28 +00:00
|
|
|
traf_->runs[0].sample_composition_time_offsets.push_back(pts - dts);
|
|
|
|
if (pts != dts)
|
2014-06-30 16:32:06 +00:00
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
if (sample->is_key_frame()) {
|
2014-01-22 23:51:26 +00:00
|
|
|
if (first_sap_time_ == kInvalidTime)
|
|
|
|
first_sap_time_ = pts;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status Fragmenter::InitializeFragment(int64_t first_sample_dts) {
|
2014-06-30 16:32:06 +00:00
|
|
|
fragment_initialized_ = true;
|
2013-11-12 20:37:58 +00:00
|
|
|
fragment_finalized_ = false;
|
2014-06-30 16:32:06 +00:00
|
|
|
traf_->decode_time.decode_time = first_sample_dts;
|
2013-11-12 20:37:58 +00:00
|
|
|
traf_->runs.clear();
|
|
|
|
traf_->runs.resize(1);
|
|
|
|
traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
|
2016-05-06 23:56:50 +00:00
|
|
|
traf_->sample_group_descriptions.clear();
|
|
|
|
traf_->sample_to_groups.clear();
|
2015-06-02 19:11:23 +00:00
|
|
|
traf_->header.sample_description_index = 1; // 1-based.
|
|
|
|
traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask |
|
|
|
|
TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
|
2013-11-12 20:37:58 +00:00
|
|
|
fragment_duration_ = 0;
|
|
|
|
earliest_presentation_time_ = kInvalidTime;
|
|
|
|
first_sap_time_ = kInvalidTime;
|
|
|
|
data_.reset(new BufferWriter());
|
2014-04-18 18:49:49 +00:00
|
|
|
return Status::OK;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
void Fragmenter::FinalizeFragment() {
|
2013-11-12 20:37:58 +00:00
|
|
|
// Optimize trun box.
|
2016-11-09 02:11:13 +00:00
|
|
|
traf_->runs[0].sample_count =
|
|
|
|
static_cast<uint32_t>(traf_->runs[0].sample_sizes.size());
|
2013-11-12 20:37:58 +00:00
|
|
|
if (OptimizeSampleEntries(&traf_->runs[0].sample_durations,
|
|
|
|
&traf_->header.default_sample_duration)) {
|
|
|
|
traf_->header.flags |=
|
|
|
|
TrackFragmentHeader::kDefaultSampleDurationPresentMask;
|
|
|
|
} else {
|
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
|
|
|
|
}
|
|
|
|
if (OptimizeSampleEntries(&traf_->runs[0].sample_sizes,
|
|
|
|
&traf_->header.default_sample_size)) {
|
|
|
|
traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
|
|
|
|
} else {
|
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
|
|
|
|
}
|
|
|
|
if (OptimizeSampleEntries(&traf_->runs[0].sample_flags,
|
|
|
|
&traf_->header.default_sample_flags)) {
|
|
|
|
traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
|
|
|
|
} else {
|
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:56:50 +00:00
|
|
|
// Add SampleToGroup boxes. A SampleToGroup box with grouping type of 'roll'
|
|
|
|
// needs to be added if there is seek preroll, referencing sample group
|
|
|
|
// description in track level; Also need to add SampleToGroup boxes
|
|
|
|
// correponding to every SampleGroupDescription boxes, referencing sample
|
|
|
|
// group description in fragment level.
|
|
|
|
DCHECK_EQ(traf_->sample_to_groups.size(), 0u);
|
|
|
|
if (seek_preroll_ > 0) {
|
|
|
|
traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
|
|
|
|
SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
|
|
|
|
sample_to_group.grouping_type = FOURCC_roll;
|
|
|
|
|
|
|
|
sample_to_group.entries.resize(1);
|
|
|
|
SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
|
|
|
|
sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
|
|
|
|
sample_to_group_entry.group_description_index =
|
|
|
|
SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
|
|
|
|
}
|
2016-07-12 20:48:28 +00:00
|
|
|
for (const auto& sample_group_description :
|
|
|
|
traf_->sample_group_descriptions) {
|
2016-05-06 23:56:50 +00:00
|
|
|
traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
|
|
|
|
SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
|
|
|
|
sample_to_group.grouping_type = sample_group_description.grouping_type;
|
|
|
|
|
|
|
|
sample_to_group.entries.resize(1);
|
|
|
|
SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
|
|
|
|
sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
|
|
|
|
sample_to_group_entry.group_description_index =
|
|
|
|
SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
fragment_finalized_ = true;
|
2014-06-30 16:32:06 +00:00
|
|
|
fragment_initialized_ = false;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
void Fragmenter::GenerateSegmentReference(SegmentReference* reference) {
|
2014-03-26 22:09:43 +00:00
|
|
|
// NOTE: Daisy chain is not supported currently.
|
2013-11-12 20:37:58 +00:00
|
|
|
reference->reference_type = false;
|
|
|
|
reference->subsegment_duration = fragment_duration_;
|
|
|
|
reference->starts_with_sap = StartsWithSAP();
|
|
|
|
if (kInvalidTime == first_sap_time_) {
|
|
|
|
reference->sap_type = SegmentReference::TypeUnknown;
|
|
|
|
reference->sap_delta_time = 0;
|
|
|
|
} else {
|
|
|
|
reference->sap_type = SegmentReference::Type1;
|
|
|
|
reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
|
|
|
|
}
|
|
|
|
reference->earliest_presentation_time = earliest_presentation_time_;
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
bool Fragmenter::StartsWithSAP() {
|
2013-11-12 20:37:58 +00:00
|
|
|
DCHECK(!traf_->runs.empty());
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t start_sample_flag;
|
2013-11-12 20:37:58 +00:00
|
|
|
if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
|
|
|
|
DCHECK(!traf_->runs[0].sample_flags.empty());
|
|
|
|
start_sample_flag = traf_->runs[0].sample_flags[0];
|
|
|
|
} else {
|
|
|
|
DCHECK(traf_->header.flags &
|
|
|
|
TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
|
|
|
|
start_sample_flag = traf_->header.default_sample_flags;
|
|
|
|
}
|
|
|
|
return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|