Skip to content

Commit

Permalink
Use scaleLargeTimestamp in TimestampAdjuster
Browse files Browse the repository at this point in the history
This helps avoid overflows in intermediate calculations.

Verified the value in the test using `BigInteger`:

```
jshell> BigInteger.valueOf(1L << 52).multiply(BigInteger.valueOf(90000)).divide(BigInteger.valueOf(1000000))
$3 ==> 405323966463344
```

Issue: #1763

#cherrypick

PiperOrigin-RevId: 684028178
  • Loading branch information
icbaker authored and copybara-github committed Oct 9, 2024
1 parent 2c46cea commit b6d0540
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
* Add workaround for codecs that get stuck after the last sample without
returning an end-of-stream signal.
* Text:
* Ensure WebVTT in HLS with very large subtitle timestamps (which overflow
a 64-bit `long` when represented as microseconds and multiplied by the
`90,000` MPEG timebase) are displayed
([#1763](https://github.com/androidx/media/issues/1763)).
* Metadata:
* Assign the `C.TRACK_TYPE_METADATA` type to tracks containing icy or
vnd.dvb.ait content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public synchronized boolean isInitialized() {
* @return The corresponding value in microseconds.
*/
public static long ptsToUs(long pts) {
return (pts * C.MICROS_PER_SECOND) / 90000;
return Util.scaleLargeTimestamp(pts, C.MICROS_PER_SECOND, 90000);
}

/**
Expand All @@ -295,6 +295,6 @@ public static long usToWrappedPts(long us) {
* @return The corresponding value as a 90 kHz clock timestamp.
*/
public static long usToNonWrappedPts(long us) {
return (us * 90000) / C.MICROS_PER_SECOND;
return Util.scaleLargeTimestamp(us, 90000, C.MICROS_PER_SECOND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,10 @@ public void adjustTsTimestamp_justBeyondWraparoundFollowedByMuchLargerValue_assu

assertThat(secondAdjustedTimestampUs - firstAdjustedTimestampUs).isGreaterThan(0x100000000L);
}

// https://github.com/androidx/media/issues/1763
@Test
public void usToWrappedPts_usTimestampCloseToOverflow_doesntOverflow() {
assertThat(TimestampAdjuster.usToNonWrappedPts(1L << 52)).isEqualTo(405323966463344L);
}
}

0 comments on commit b6d0540

Please sign in to comment.