Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
* MPEG-TS: Ensure the last frame is rendered for streams where the last
PES packet has a known length
([#3206](https://github.com/androidx/media/pull/3206)).
* MP3: Fix bitrate reporting for files with Xing and VBRI header.
* Inspector:
* Audio:
* Add a 100ms grace period in ExoPlayer's audio renderers when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package androidx.media3.extractor.mp3;

import static androidx.media3.extractor.mp3.Mp3Util.computeAverageBitrate;

import androidx.annotation.VisibleForTesting;
import androidx.media3.common.C;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.IndexSeekMap;
import java.math.RoundingMode;

/** MP3 seeker that builds a time-to-byte mapping as the stream is read. */
/* package */ final class IndexSeeker implements Seeker {
Expand All @@ -40,18 +40,7 @@ public IndexSeeker(long durationUs, long dataStartPosition, long dataEndPosition
durationUs);
this.dataStartPosition = dataStartPosition;
this.dataEndPosition = dataEndPosition;
if (durationUs != C.TIME_UNSET) {
long bitrate =
Util.scaleLargeValue(
dataEndPosition - dataStartPosition,
C.BITS_PER_BYTE * C.MICROS_PER_SECOND,
durationUs,
RoundingMode.HALF_UP);
this.averageBitrate =
bitrate > 0 && bitrate <= Integer.MAX_VALUE ? (int) bitrate : C.RATE_UNSET_INT;
} else {
this.averageBitrate = C.RATE_UNSET_INT;
}
this.averageBitrate = computeAverageBitrate(dataEndPosition - dataStartPosition, durationUs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.extractor.mp3;

import androidx.media3.common.C;
import androidx.media3.common.util.Util;
import java.math.RoundingMode;

/* package */ class Mp3Util {

/* package */ static int computeAverageBitrate(long dataSize, long durationUs) {
if (dataSize <= 0 || durationUs <= 0) {
return C.RATE_UNSET_INT;
}
long averageBitrate =
Util.scaleLargeValue(
dataSize, C.BITS_PER_BYTE * C.MICROS_PER_SECOND, durationUs, RoundingMode.HALF_UP);
return averageBitrate > 0 && averageBitrate <= Integer.MAX_VALUE
? (int) averageBitrate
: C.RATE_UNSET_INT;
}

private Mp3Util() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.mp3;

import static androidx.media3.extractor.mp3.Mp3Util.computeAverageBitrate;
import static java.lang.Math.max;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -106,30 +107,28 @@ public static VbriSeeker create(
endOfMp3Data = max(endOfMp3Data, position);
}

return new VbriSeeker(
timesUs, positions, durationUs, startOfMp3Data, endOfMp3Data, mpegAudioHeader.bitrate);
return new VbriSeeker(timesUs, positions, durationUs, startOfMp3Data, endOfMp3Data);
}

private final long[] timesUs;
private final long[] positions;
private final long durationUs;
private final long dataStartPosition;
private final long dataEndPosition;
private final int bitrate;
private final int averageBitrate;

private VbriSeeker(
long[] timesUs,
long[] positions,
long durationUs,
long dataStartPosition,
long dataEndPosition,
int bitrate) {
long dataEndPosition) {
this.timesUs = timesUs;
this.positions = positions;
this.durationUs = durationUs;
this.dataStartPosition = dataStartPosition;
this.dataEndPosition = dataEndPosition;
this.bitrate = bitrate;
this.averageBitrate = computeAverageBitrate(dataEndPosition - dataStartPosition, durationUs);
}

@Override
Expand Down Expand Up @@ -171,6 +170,6 @@ public long getDataEndPosition() {

@Override
public int getAverageBitrate() {
return bitrate;
return averageBitrate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.mp3;

import static androidx.media3.extractor.mp3.Mp3Util.computeAverageBitrate;
import static com.google.common.base.Preconditions.checkNotNull;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -65,15 +66,14 @@ public static XingSeeker create(XingFrame xingFrame, long position, long streamL
position,
xingFrame.header.frameSize,
durationUs,
xingFrame.header.bitrate,
dataSize,
xingFrame.tableOfContents);
}

private final long dataStartPosition;
private final int xingFrameSize;
private final long durationUs;
private final int bitrate;
private final int averageBitrate;

/** Data size, including the XING frame. */
private final long dataSize;
Expand All @@ -90,13 +90,12 @@ private XingSeeker(
long dataStartPosition,
int xingFrameSize,
long durationUs,
int bitrate,
long dataSize,
@Nullable long[] tableOfContents) {
this.dataStartPosition = dataStartPosition;
this.xingFrameSize = xingFrameSize;
this.durationUs = durationUs;
this.bitrate = bitrate;
this.averageBitrate = computeAverageBitrate(dataSize - xingFrameSize, durationUs);
this.dataSize = dataSize;
this.tableOfContents = tableOfContents;
dataEndPosition = dataSize == C.LENGTH_UNSET ? C.INDEX_UNSET : dataStartPosition + dataSize;
Expand Down Expand Up @@ -173,7 +172,7 @@ public long getDataEndPosition() {

@Override
public int getAverageBitrate() {
return bitrate;
return averageBitrate;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.extractor.mp3;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Truth.assertThat;

import androidx.media3.common.C;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.MpegAudioUtil;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Tests for {@link VbriSeeker}. */
@RunWith(AndroidJUnit4.class)
public final class VbriSeekerTest {

private static final int VBRI_FRAME_HEADER_DATA = 0xFFFB3000;
private static final int VBRI_FRAME_POSITION = 157;

@Test
public void getAverageBitrate_returnsAverageFromDataSizeAndDuration() {
MpegAudioUtil.Header header = new MpegAudioUtil.Header();
header.setForHeaderData(VBRI_FRAME_HEADER_DATA);
int dataSize = 1_000;
int frameCount = 40;
VbriSeeker seeker =
checkNotNull(
VbriSeeker.create(
/* inputLength= */ C.LENGTH_UNSET,
VBRI_FRAME_POSITION,
header,
createVbriFrame(dataSize, frameCount, /* segmentSizes...= */ 400, 600)));
long durationUs =
Util.sampleCountToDurationUs(
((long) frameCount * header.samplesPerFrame) - 1, header.sampleRate);
int expectedAverageBitrate =
(int)
Util.scaleLargeValue(
dataSize, C.BITS_PER_BYTE * C.MICROS_PER_SECOND, durationUs, RoundingMode.HALF_UP);

assertThat(seeker.getAverageBitrate()).isEqualTo(expectedAverageBitrate);
assertThat(seeker.getAverageBitrate()).isNotEqualTo(header.bitrate);
}

private static ParsableByteArray createVbriFrame(
int dataSize, int frameCount, int... segmentSizes) {
ByteBuffer payload = ByteBuffer.allocate(6 + 4 + 4 + 2 + 2 + 2 + 2 + 2 * segmentSizes.length);
payload.position(6);
payload.putInt(dataSize);
payload.putInt(frameCount);
payload.putShort((short) segmentSizes.length);
payload.putShort((short) 1); // scale
payload.putShort((short) 2); // entry size
payload.putShort((short) 0);
for (int segmentSize : segmentSizes) {
payload.putShort((short) segmentSize);
}
return new ParsableByteArray(payload.array());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.media3.extractor.SeekMap.SeekPoints;
import androidx.media3.extractor.SeekPoint;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.math.RoundingMode;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -86,6 +87,20 @@ public void getTimeUsAtEndOfStream() {
assertThat(seeker.getTimeUs(XING_AUDIO_END_POSITION)).isEqualTo(XING_STREAM_DURATION_US);
}

@Test
public void getAverageBitrate_returnsAverageFromDataSizeAndDuration() {
int expectedAverageBitrate =
(int)
Util.scaleLargeValue(
XING_FRAME.dataSize - XING_FRAME.header.frameSize,
C.BITS_PER_BYTE * C.MICROS_PER_SECOND,
XING_STREAM_DURATION_US,
RoundingMode.HALF_UP);

assertThat(seeker.getAverageBitrate()).isEqualTo(expectedAverageBitrate);
assertThat(seeker.getAverageBitrate()).isNotEqualTo(XING_FRAME.header.bitrate);
}

// https://github.com/androidx/media/issues/3117#issuecomment-4046538506
@Test
public void getTimeUsAtEndOfStream_xingLengthLongerThanStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 88
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 88
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 88
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 59
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 30
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 32000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 64000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ track 0:
sample count = 117
track duration = 2807979
format 0:
averageBitrate = 64000
averageBitrate = 108719
containerMimeType = audio/mpeg
sampleMimeType = audio/mpeg
maxInputSize = 4096
Expand Down
Loading