Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

look for sample duration in thfd box and use that if sampleduration i… #252

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions migrationTool/transform/TransMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,32 @@ private void UpdateTrackRunDuration(string fileName)
if (c.Type == MP4BoxType.traf)
{
offsetToTrun += moofBox.ComputeBaseSizeBox();

// get defaultSampleDuration if exists
bool hasDefaultSampleDuration = false;
uint defaultSampleDuration = 0;
foreach (var cc in c.Children)
{
if (cc.Type == MP4BoxType.tfhd)
{
tfhdBox tfhdBox = (tfhdBox)cc;
if (tfhdBox.DefaultSampleDuration != null)
{
hasDefaultSampleDuration = true;
defaultSampleDuration = (uint) tfhdBox.DefaultSampleDuration!;
}
}
}

foreach (var cc in c.Children)
{
if (cc.Type == MP4BoxType.trun)
{
trunBox trunBox = (trunBox)cc; // will throw
trunBox.TrunFlags flag = (trunBox.TrunFlags)trunBox.Flags;
if ((flag & trunBox.TrunFlags.SampleDurationPresent) != trunBox.TrunFlags.SampleDurationPresent)

bool sampleDurationPresent = (flag & trunBox.TrunFlags.SampleDurationPresent) == trunBox.TrunFlags.SampleDurationPresent;
if (!sampleDurationPresent && !hasDefaultSampleDuration)
{
throw new InvalidDataException("Unexpected, sampleDurationPresent must be present");
}
Expand All @@ -261,7 +280,14 @@ private void UpdateTrackRunDuration(string fileName)
ulong totalDuration = 0;
for (int i = 0; i < trunBox.Entries.Count; ++i)
{
totalDuration += (ulong)trunBox.Entries[i].SampleDuration!;
if (sampleDurationPresent)
{
totalDuration += (ulong)trunBox.Entries[i].SampleDuration!;
}
else
{
totalDuration += defaultSampleDuration;
}
}
if (curDecodeTimesIndex + 1 < decodeTimes.Count)
{
Expand Down
Loading