Skip to content

Commit a32b82f

Browse files
committed
Merge pull request #10786 from TiVo:p-aacutil-test-impl
PiperOrigin-RevId: 490465182
2 parents 0a176d1 + 0feace5 commit a32b82f

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

libraries/extractor/src/main/java/androidx/media3/extractor/AacUtil.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,16 @@ private static int getSamplingFrequency(ParsableBitArray bitArray) throws Parser
332332
int samplingFrequency;
333333
int frequencyIndex = bitArray.readBits(4);
334334
if (frequencyIndex == AUDIO_SPECIFIC_CONFIG_FREQUENCY_INDEX_ARBITRARY) {
335+
if (bitArray.bitsLeft() < 24) {
336+
throw ParserException.createForMalformedContainer(
337+
/* message= */ "AAC header insufficient data", /* cause= */ null);
338+
}
335339
samplingFrequency = bitArray.readBits(24);
336340
} else if (frequencyIndex < 13) {
337341
samplingFrequency = AUDIO_SPECIFIC_CONFIG_SAMPLING_RATE_TABLE[frequencyIndex];
338342
} else {
339-
throw ParserException.createForMalformedContainer(/* message= */ null, /* cause= */ null);
343+
throw ParserException.createForMalformedContainer(
344+
/* message= */ "AAC header wrong Sampling Frequency Index", /* cause= */ null);
340345
}
341346
return samplingFrequency;
342347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.media3.extractor;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static org.junit.Assert.assertThrows;
20+
21+
import androidx.media3.common.ParserException;
22+
import androidx.media3.common.util.Util;
23+
import androidx.test.ext.junit.runners.AndroidJUnit4;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
/** Unit tests for {@link AacUtil}. */
28+
@RunWith(AndroidJUnit4.class)
29+
public final class AacUtilTest {
30+
private static final byte[] AAC_48K_2CH_HEADER = Util.getBytesFromHexString("1190");
31+
32+
private static final byte[] NOT_ENOUGH_ARBITRARY_SAMPLING_FREQ_BITS_HEADER =
33+
Util.getBytesFromHexString("1790");
34+
35+
private static final byte[] ARBITRARY_SAMPLING_FREQ_BITS_HEADER =
36+
Util.getBytesFromHexString("1780000790");
37+
38+
@Test
39+
public void parseAudioSpecificConfig_twoCh48kAac_parsedCorrectly() throws Exception {
40+
AacUtil.Config aac = AacUtil.parseAudioSpecificConfig(AAC_48K_2CH_HEADER);
41+
42+
assertThat(aac.channelCount).isEqualTo(2);
43+
assertThat(aac.sampleRateHz).isEqualTo(48000);
44+
assertThat(aac.codecs).isEqualTo("mp4a.40.2");
45+
}
46+
47+
@Test
48+
public void parseAudioSpecificConfig_arbitrarySamplingFreqHeader_parsedCorrectly()
49+
throws Exception {
50+
AacUtil.Config aac = AacUtil.parseAudioSpecificConfig(ARBITRARY_SAMPLING_FREQ_BITS_HEADER);
51+
assertThat(aac.channelCount).isEqualTo(2);
52+
assertThat(aac.sampleRateHz).isEqualTo(15);
53+
assertThat(aac.codecs).isEqualTo("mp4a.40.2");
54+
}
55+
56+
@Test
57+
public void
58+
parseAudioSpecificConfig_arbitrarySamplingFreqHeaderNotEnoughBits_throwsParserException() {
59+
// ISO 14496-3 1.6.2.1 allows for setting of arbitrary sampling frequency, but if the extra
60+
// frequency bits are missing, make sure the code will throw an exception.
61+
assertThrows(
62+
ParserException.class,
63+
() -> AacUtil.parseAudioSpecificConfig(NOT_ENOUGH_ARBITRARY_SAMPLING_FREQ_BITS_HEADER));
64+
}
65+
}

0 commit comments

Comments
 (0)