Fix calculations that may lose precision compared to their target type

PiperOrigin-RevId: 444861268
This commit is contained in:
ibaker 2022-04-27 16:05:33 +01:00 committed by Ian Baker
parent 5d1af64605
commit 91b0d55fb3
6 changed files with 10 additions and 7 deletions

View File

@ -1243,7 +1243,7 @@ public final class Util {
long time = dateTime.getTimeInMillis();
if (timezoneShift != 0) {
time -= timezoneShift * 60000;
time -= timezoneShift * 60000L;
}
return time;

View File

@ -412,6 +412,7 @@ public final class AmrExtractor implements Extractor {
* @return The stream bitrate.
*/
private static int getBitrateFromFrameSize(int frameSize, long durationUsPerFrame) {
return (int) ((frameSize * C.BITS_PER_BYTE * C.MICROS_PER_SECOND) / durationUsPerFrame);
return (int)
((frameSize * ((long) C.BITS_PER_BYTE) * C.MICROS_PER_SECOND) / durationUsPerFrame);
}
}

View File

@ -1740,9 +1740,9 @@ public class MatroskaExtractor implements Extractor {
checkArgument(timeUs != C.TIME_UNSET);
byte[] timeCodeData;
int hours = (int) (timeUs / (3600 * C.MICROS_PER_SECOND));
timeUs -= (hours * 3600 * C.MICROS_PER_SECOND);
timeUs -= (hours * 3600L * C.MICROS_PER_SECOND);
int minutes = (int) (timeUs / (60 * C.MICROS_PER_SECOND));
timeUs -= (minutes * 60 * C.MICROS_PER_SECOND);
timeUs -= (minutes * 60L * C.MICROS_PER_SECOND);
int seconds = (int) (timeUs / C.MICROS_PER_SECOND);
timeUs -= (seconds * C.MICROS_PER_SECOND);
int lastValue = (int) (timeUs / lastTimecodeValueScalingFactor);

View File

@ -89,7 +89,7 @@ import com.google.android.exoplayer2.util.Util;
default:
return null;
}
position += segmentSize * scale;
position += segmentSize * ((long) scale);
}
if (inputLength != C.LENGTH_UNSET && inputLength != position) {
Log.w(TAG, "VBRI data size mismatch: " + inputLength + ", " + position);

View File

@ -363,6 +363,7 @@ public final class AdtsExtractor implements Extractor {
* @return The stream bitrate.
*/
private static int getBitrateFromFrameSize(int frameSize, long durationUsPerFrame) {
return (int) ((frameSize * C.BITS_PER_BYTE * C.MICROS_PER_SECOND) / durationUsPerFrame);
return (int)
((frameSize * ((long) C.BITS_PER_BYTE) * C.MICROS_PER_SECOND) / durationUsPerFrame);
}
}

View File

@ -142,7 +142,8 @@ public final class FakeAdaptiveDataSet extends FakeDataSet {
for (int i = 0; i < trackGroup.length; i++) {
String uri = getUri(i);
Format format = trackGroup.getFormat(i);
double avgChunkLength = format.bitrate * chunkDurationUs / (8 * C.MICROS_PER_SECOND);
double avgChunkLength =
format.bitrate * chunkDurationUs / ((double) (8 * C.MICROS_PER_SECOND));
FakeData newData = this.newData(uri);
for (int j = 0; j < fullChunks; j++) {
newData.appendReadData((int) (avgChunkLength * bitrateFactors[j]));