Clip float point PCM to its allowed range before resampling

PiperOrigin-RevId: 321340777
This commit is contained in:
olly 2020-07-15 13:14:57 +01:00 committed by Oliver Woodman
parent c4171d50cc
commit 890c4adbed

View File

@ -17,6 +17,7 @@ package com.google.android.exoplayer2.audio;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Util;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** /**
@ -115,9 +116,13 @@ import java.nio.ByteBuffer;
// 32 bit floating point -> 16 bit resampling. Floating point values are in the range // 32 bit floating point -> 16 bit resampling. Floating point values are in the range
// [-1.0, 1.0], so need to be scaled by Short.MAX_VALUE. // [-1.0, 1.0], so need to be scaled by Short.MAX_VALUE.
for (int i = position; i < limit; i += 4) { for (int i = position; i < limit; i += 4) {
short value = (short) (inputBuffer.getFloat(i) * Short.MAX_VALUE); // Clamp to avoid integer overflow if the floating point values exceed their allowed range
buffer.put((byte) (value & 0xFF)); // [Internal ref: b/161204847].
buffer.put((byte) ((value >> 8) & 0xFF)); float floatValue =
Util.constrainValue(inputBuffer.getFloat(i), /* min= */ -1, /* max= */ 1);
short shortValue = (short) (floatValue * Short.MAX_VALUE);
buffer.put((byte) (shortValue & 0xFF));
buffer.put((byte) ((shortValue >> 8) & 0xFF));
} }
break; break;
case C.ENCODING_PCM_16BIT: case C.ENCODING_PCM_16BIT: