Use scaleLargeTimestamp in TimestampAdjuster

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: androidx/media#1763

#cherrypick

PiperOrigin-RevId: 684028178
This commit is contained in:
ibaker 2024-10-09 07:24:19 -07:00 committed by Copybara-Service
parent 2c46cea088
commit b6d0540059
3 changed files with 12 additions and 2 deletions

View File

@ -74,6 +74,10 @@
* Add workaround for codecs that get stuck after the last sample without * Add workaround for codecs that get stuck after the last sample without
returning an end-of-stream signal. returning an end-of-stream signal.
* Text: * 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: * Metadata:
* Assign the `C.TRACK_TYPE_METADATA` type to tracks containing icy or * Assign the `C.TRACK_TYPE_METADATA` type to tracks containing icy or
vnd.dvb.ait content. vnd.dvb.ait content.

View File

@ -271,7 +271,7 @@ public final class TimestampAdjuster {
* @return The corresponding value in microseconds. * @return The corresponding value in microseconds.
*/ */
public static long ptsToUs(long pts) { public static long ptsToUs(long pts) {
return (pts * C.MICROS_PER_SECOND) / 90000; return Util.scaleLargeTimestamp(pts, C.MICROS_PER_SECOND, 90000);
} }
/** /**
@ -295,6 +295,6 @@ public final class TimestampAdjuster {
* @return The corresponding value as a 90 kHz clock timestamp. * @return The corresponding value as a 90 kHz clock timestamp.
*/ */
public static long usToNonWrappedPts(long us) { public static long usToNonWrappedPts(long us) {
return (us * 90000) / C.MICROS_PER_SECOND; return Util.scaleLargeTimestamp(us, 90000, C.MICROS_PER_SECOND);
} }
} }

View File

@ -240,4 +240,10 @@ public class TimestampAdjusterTest {
assertThat(secondAdjustedTimestampUs - firstAdjustedTimestampUs).isGreaterThan(0x100000000L); 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);
}
} }