Skip to content

Commit 1d573e0

Browse files
committed
Changes based on internal review
1 parent 2c3eac4 commit 1d573e0

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

libraries/extractor/src/main/java/androidx/media3/extractor/ts/AdtsReader.java

+28-26
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,7 @@ private void parseAdtsHeader() throws ParserException {
574574
}
575575

576576
@RequiresNonNull("currentOutput")
577-
void readAacProgramConfigElement() {
578-
Format pendingOutputFormat = checkNotNull(this.pendingOutputFormat);
577+
private void readAacProgramConfigElement() throws ParserException {
579578
ParsableBitArray pceBuffer = checkNotNull(this.pceBuffer);
580579

581580
// See ISO 13818-7 Advanced Audio Coding (2006) Table 36 for PCE tag encoding.
@@ -614,37 +613,40 @@ void readAacProgramConfigElement() {
614613
int commentSizeBits = 8; // comment_field_bytes
615614

616615
// Beyond this point, pceBuffer may be empty, so check before consuming.
617-
if (pceBuffer.bitsLeft() >= channelBits + numAlignmentBits + commentSizeBits) {
618-
pceBuffer.skipBits(channelBits);
616+
if (pceBuffer.bitsLeft() < channelBits + numAlignmentBits + commentSizeBits) {
617+
throw ParserException.createForMalformedContainer(/* message= */ null, /* cause= */ null);
618+
}
619619

620-
// Store PCE size excluding initial PCE tag, alignment bits and comment for later.
621-
int numPceBits = pceBuffer.getPosition() - 3 /* PCE tag */;
620+
pceBuffer.skipBits(channelBits);
622621

623-
pceBuffer.skipBits(numAlignmentBits);
624-
int commentSize = pceBuffer.readBits(commentSizeBits);
622+
// Store PCE size excluding initial PCE tag, alignment bits and comment for later.
623+
int numPceBits = pceBuffer.getPosition() - 3 /* PCE tag */;
624+
pceBuffer.skipBits(numAlignmentBits);
625+
int commentSize = pceBuffer.readBits(commentSizeBits);
625626

626-
if (sampleSize >= pceBuffer.getBytePosition() + commentSize) {
627-
// Append PCE to format's audio specific config.
628-
byte[] oldConfig = pendingOutputFormat.initializationData.get(0);
627+
if (sampleSize < pceBuffer.getBytePosition() + commentSize) {
628+
throw ParserException.createForMalformedContainer(/* message= */ null, /* cause= */ null);
629+
}
629630

630-
int configSize = oldConfig.length;
631-
configSize += (numPceBits + 7) / 8 + 1; // Byte align and add a zero length comment.
632-
byte[] newConfig = Arrays.copyOf(oldConfig, configSize);
631+
Format pendingOutputFormat = checkNotNull(this.pendingOutputFormat);
632+
// Append PCE to format's audio specific config.
633+
byte[] oldConfig = pendingOutputFormat.initializationData.get(0);
634+
int configSize = oldConfig.length;
635+
configSize += (numPceBits + 7) / 8 + 1; // Byte align and add a zero length comment.
636+
byte[] newConfig = Arrays.copyOf(oldConfig, configSize);
633637

634-
pceBuffer.setPosition(3 /* PCE tag */);
635-
pceBuffer.readBits(newConfig, oldConfig.length, numPceBits);
638+
pceBuffer.setPosition(3 /* PCE tag */);
639+
pceBuffer.readBits(newConfig, oldConfig.length, numPceBits);
636640

637-
pendingOutputFormat =
638-
pendingOutputFormat
639-
.buildUpon()
640-
.setInitializationData(ImmutableList.of(newConfig))
641-
.build();
641+
pendingOutputFormat =
642+
pendingOutputFormat
643+
.buildUpon()
644+
.setInitializationData(ImmutableList.of(newConfig))
645+
.build();
642646

643-
// Submit PCE-appended output format.
644-
currentOutput.format(pendingOutputFormat);
645-
hasOutputFormat = true;
646-
}
647-
}
647+
// Submit PCE-appended output format.
648+
this.currentOutput.format(pendingOutputFormat);
649+
this.hasOutputFormat = true;
648650
}
649651

650652
// Pass through all accumulated data as sample data.

libraries/extractor/src/test/java/androidx/media3/extractor/ts/AdtsReaderTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ public void aacPceData() throws ParserException {
209209
adtsOutput.assertSample(0, AAC_PCE_ADTS_CONTENT, 0, C.BUFFER_FLAG_KEY_FRAME, null);
210210
}
211211

212+
@Test
213+
public void aacPceDataSplit() throws ParserException {
214+
byte[] first = Arrays.copyOf(AAC_PCE_TEST_DATA, AAC_PCE_ADTS_HEADER.length + 1);
215+
byte[] second =
216+
Arrays.copyOfRange(
217+
AAC_PCE_TEST_DATA, AAC_PCE_ADTS_HEADER.length + 1, AAC_PCE_TEST_DATA.length);
218+
219+
data = new ParsableByteArray(first);
220+
feed();
221+
data = new ParsableByteArray(second);
222+
feed();
223+
224+
assertSampleCounts(0, 1);
225+
adtsOutput.assertSample(0, AAC_PCE_ADTS_CONTENT, 0, C.BUFFER_FLAG_KEY_FRAME, null);
226+
}
227+
212228
@Test
213229
public void aacPceDataFail() throws ParserException {
214230
data = new ParsableByteArray(Arrays.copyOf(AAC_PCE_TEST_DATA, AAC_PCE_TEST_DATA.length));

0 commit comments

Comments
 (0)