Skip to content

Commit

Permalink
Parse opus gain correctly as a signed value
Browse files Browse the repository at this point in the history
Issue: #7046
PiperOrigin-RevId: 303354941
  • Loading branch information
ojw28 committed Mar 30, 2020
1 parent a9cbbf9 commit 69ca534
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
in spherical playbacks
([#6761](https://github.com/google/ExoPlayer/issues/6761)).
* FFmpeg extension: Add support for x86_64.
* Opus extension: Fix parsing of negative gain values
([#7046](https://github.com/google/ExoPlayer/issues/7046)).

### 2.11.3 (2020-02-19) ###

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
@RunWith(AndroidJUnit4.class)
public class OpusPlaybackTest {

private static final String BEAR_OPUS_URI = "asset:///bear-opus.webm";
private static final String BEAR_OPUS_URI = "asset:///bear-opus.mka";
private static final String BEAR_OPUS_NEGATIVE_GAIN_URI =
"asset:///bear-opus-negative-gain.mka";

@Before
public void setUp() {
Expand All @@ -51,6 +53,11 @@ public void testBasicPlayback() throws Exception {
playUri(BEAR_OPUS_URI);
}

@Test
public void basicPlaybackNegativeGain() throws Exception {
playUri(BEAR_OPUS_NEGATIVE_GAIN_URI);
}

private void playUri(String uri) throws Exception {
TestPlaybackRunnable testPlaybackRunnable =
new TestPlaybackRunnable(Uri.parse(uri), ApplicationProvider.getApplicationContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public OpusDecoder(
if (channelCount > 8) {
throw new OpusDecoderException("Invalid channel count: " + channelCount);
}
int preskip = readLittleEndian16(headerBytes, 10);
int gain = readLittleEndian16(headerBytes, 16);
int preskip = readUnsignedLittleEndian16(headerBytes, 10);
int gain = readSignedLittleEndian16(headerBytes, 16);

byte[] streamMap = new byte[8];
int numStreams;
Expand Down Expand Up @@ -228,12 +228,16 @@ private static int nsToSamples(long ns) {
return (int) (ns * SAMPLE_RATE / 1000000000);
}

private static int readLittleEndian16(byte[] input, int offset) {
private static int readUnsignedLittleEndian16(byte[] input, int offset) {
int value = input[offset] & 0xFF;
value |= (input[offset + 1] & 0xFF) << 8;
return value;
}

private static int readSignedLittleEndian16(byte[] input, int offset) {
return (short) readUnsignedLittleEndian16(input, offset);
}

private native long opusInit(int sampleRate, int channelCount, int numStreams, int numCoupled,
int gain, byte[] streamMap);
private native int opusDecode(long decoder, long timeUs, ByteBuffer inputBuffer, int inputSize,
Expand Down

0 comments on commit 69ca534

Please sign in to comment.