Skip to content

Commit

Permalink
look for sample duration in thfd box and use that if sampleduration i…
Browse files Browse the repository at this point in the history
…s not present in trun box
  • Loading branch information
pohhsu committed May 30, 2024
1 parent 8caf933 commit 5a76ce3
Showing 1 changed file with 28 additions and 2 deletions.
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

0 comments on commit 5a76ce3

Please sign in to comment.