Skip to content

Commit

Permalink
feat: modify calculation for negative CTS offset
Browse files Browse the repository at this point in the history
  • Loading branch information
lee.fordyce committed Feb 6, 2024
1 parent 85a3c7c commit 8b8c277
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
1 change: 1 addition & 0 deletions packager/live_packager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ class LivePackagerBaseTest : public ::testing::Test {
new_live_config.key_id = key_id_;
break;
}
new_live_config.m2ts_offset_ms = 9000;
live_packager_ = std::make_unique<LivePackager>(new_live_config);
}

Expand Down
8 changes: 2 additions & 6 deletions packager/media/formats/mp4/mp4_media_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,7 @@ bool MP4MediaParser::ParseMoov(BoxReader* reader) {
init_cb_(streams);
if (!FetchKeysIfNecessary(moov_->pssh))
return false;
runs_ = cts_offset_adjustment_
? std::make_unique<TrackRunIteratorExt>(moov_.get())
: std::make_unique<TrackRunIterator>(moov_.get());
runs_.reset(new TrackRunIterator(moov_.get(), cts_offset_adjustment_));
RCHECK(runs_->Init());
ChangeState(kEmittingSamples);
return true;
Expand All @@ -763,9 +761,7 @@ bool MP4MediaParser::ParseMoof(BoxReader* reader) {
MovieFragment moof;
RCHECK(moof.Parse(reader));
if (!runs_)
runs_ = cts_offset_adjustment_
? std::make_unique<TrackRunIteratorExt>(moov_.get())
: std::make_unique<TrackRunIterator>(moov_.get());
runs_.reset(new TrackRunIterator(moov_.get(), cts_offset_adjustment_));
RCHECK(runs_->Init(moof));
if (!FetchKeysIfNecessary(moof.pssh))
return false;
Expand Down
41 changes: 22 additions & 19 deletions packager/media/formats/mp4/track_run_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,16 @@ TrackRunInfo::TrackRunInfo()
aux_info_total_size(0) {}
TrackRunInfo::~TrackRunInfo() {}

TrackRunIterator::TrackRunIterator(const Movie* moov)
: sample_dts_(0), moov_(moov), sample_offset_(0) {
TrackRunIterator::TrackRunIterator(const Movie* moov,
bool cts_offset_adjustment)
: moov_(moov),
sample_dts_(0),
sample_offset_(0),
cts_offset_adjustment_(cts_offset_adjustment) {
CHECK(moov);
}

TrackRunIteratorExt::TrackRunIteratorExt(const shaka::media::mp4::Movie* moov)
: TrackRunIterator(moov) {}

TrackRunIterator::~TrackRunIterator() = default;

TrackRunIteratorExt::~TrackRunIteratorExt() = default;
TrackRunIterator::~TrackRunIterator() {}

static void PopulateSampleInfo(const TrackExtends& trex,
const TrackFragmentHeader& tfhd,
Expand Down Expand Up @@ -456,6 +455,17 @@ void TrackRunIterator::ResetRun() {
sample_dts_ = run_itr_->start_dts;
sample_offset_ = run_itr_->sample_start_offset;
sample_itr_ = run_itr_->samples.begin();

int64_t min_cts_offset(0);
auto min_runs =
std::min_element(run_itr_->samples.begin(), run_itr_->samples.end(),
[](SampleInfo const& s1, SampleInfo const& s2) {
return s1.cts_offset < s2.cts_offset;
});
if (min_runs != run_itr_->samples.end() && min_runs->cts_offset < 0) {
min_cts_offset = min_runs->cts_offset;
}
min_cts_offset_ = min_cts_offset;
}

void TrackRunIterator::AdvanceSample() {
Expand Down Expand Up @@ -583,21 +593,14 @@ int TrackRunIterator::sample_size() const {

int64_t TrackRunIterator::dts() const {
DCHECK(IsSampleValid());
return sample_dts_;
return cts_offset_adjustment_ ? sample_dts_ - abs(min_cts_offset_)
: sample_dts_;
}

int64_t TrackRunIterator::cts() const {
DCHECK(IsSampleValid());
return sample_dts_ + sample_itr_->cts_offset;
}

int64_t TrackRunIteratorExt::cts() const {
DCHECK(IsSampleValid());
auto offset = sample_itr_->cts_offset;
if (offset < 0) {
return sample_dts_ + abs(offset);
}
return sample_dts_ + offset;
return cts_offset_adjustment_ ? dts() + abs(min_cts_offset_)
: sample_dts_ + sample_itr_->cts_offset;
}

int64_t TrackRunIterator::duration() const {
Expand Down
31 changes: 12 additions & 19 deletions packager/media/formats/mp4/track_run_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class TrackRunIterator {
public:
/// Create a new TrackRunIterator from movie box.
/// @param moov should not be NULL.
explicit TrackRunIterator(const Movie* moov);
virtual ~TrackRunIterator();
/// @param cts_offset_adjustment flag to indicate weather or not to perform
/// CTS offset adjustment.
explicit TrackRunIterator(const Movie* moov,
bool cts_offset_adjustment = false);
~TrackRunIterator();

/// For non-fragmented mp4, moov contains all the chunk information; This
/// function sets up the iterator to access all the chunks.
Expand Down Expand Up @@ -93,7 +96,7 @@ class TrackRunIterator {
int64_t sample_offset() const;
int sample_size() const;
int64_t dts() const;
virtual int64_t cts() const;
int64_t cts() const;
int64_t duration() const;
bool is_keyframe() const;
/// @}
Expand All @@ -102,10 +105,6 @@ class TrackRunIterator {
/// false. Result is owned by caller.
std::unique_ptr<DecryptConfig> GetDecryptConfig();

protected:
std::vector<SampleInfo>::const_iterator sample_itr_;
int64_t sample_dts_;

private:
void ResetRun();
const TrackEncryption& track_encryption() const;
Expand All @@ -117,30 +116,24 @@ class TrackRunIterator {

std::vector<TrackRunInfo> runs_;
std::vector<TrackRunInfo>::const_iterator run_itr_;
std::vector<SampleInfo>::const_iterator sample_itr_;

// Track the start dts of the next segment, only useful if decode_time box is
// absent.
std::vector<int64_t> next_fragment_start_dts_;

int64_t sample_dts_;
int64_t sample_offset_;

// TrackId => adjustment map.
std::map<uint32_t, int64_t> timestamp_adjustment_map_;

DISALLOW_COPY_AND_ASSIGN(TrackRunIterator);
};

// TODO: better name?
class TrackRunIteratorExt : public TrackRunIterator {
public:
TrackRunIteratorExt(const Movie* moov);
~TrackRunIteratorExt() override;
// flag used to adjust negative CTS offset values to correct PTS < DTS
bool cts_offset_adjustment_ = false;

using TrackRunIterator::cts;
int64_t cts() const override;
int64_t min_cts_offset_ = 0;

private:
DISALLOW_COPY_AND_ASSIGN(TrackRunIteratorExt);
DISALLOW_COPY_AND_ASSIGN(TrackRunIterator);
};

} // namespace mp4
Expand Down

0 comments on commit 8b8c277

Please sign in to comment.