1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/mp4/aac.h"
9 #include "base/logging.h"
10 #include "media/base/bit_reader.h"
11 #include "media/mp4/rcheck.h"
13 // The following conversion table is extracted from ISO 14496 Part 3 -
14 // Table 1.16 - Sampling Frequency Index.
15 static const int kFrequencyMap
[] = {
16 96000, 88200, 64000, 48000, 44100, 32000, 24000,
17 22050, 16000, 12000, 11025, 8000, 7350
22 static ChannelLayout
ConvertChannelConfigToLayout(uint8 channel_config
) {
23 switch (channel_config
) {
25 return CHANNEL_LAYOUT_MONO
;
27 return CHANNEL_LAYOUT_STEREO
;
29 return CHANNEL_LAYOUT_SURROUND
;
31 return CHANNEL_LAYOUT_4_0
;
33 return CHANNEL_LAYOUT_5_0
;
35 return CHANNEL_LAYOUT_5_1
;
37 return CHANNEL_LAYOUT_7_1
;
42 return CHANNEL_LAYOUT_UNSUPPORTED
;
48 : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0),
49 extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED
) {
55 bool AAC::Parse(const std::vector
<uint8
>& data
) {
56 #if defined(OS_ANDROID)
57 codec_specific_data_
= data
;
62 BitReader
reader(&data
[0], data
.size());
63 uint8 extension_type
= 0;
64 bool ps_present
= false;
65 uint8 extension_frequency_index
= 0xff;
68 extension_frequency_
= 0;
70 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
71 // Syntax of AudioSpecificConfig.
73 // Read base configuration
74 RCHECK(reader
.ReadBits(5, &profile_
));
75 RCHECK(reader
.ReadBits(4, &frequency_index_
));
76 if (frequency_index_
== 0xf)
77 RCHECK(reader
.ReadBits(24, &frequency_
));
78 RCHECK(reader
.ReadBits(4, &channel_config_
));
80 // Read extension configuration.
81 if (profile_
== 5 || profile_
== 29) {
82 ps_present
= (profile_
== 29);
84 RCHECK(reader
.ReadBits(4, &extension_frequency_index
));
85 if (extension_frequency_index
== 0xf)
86 RCHECK(reader
.ReadBits(24, &extension_frequency_
));
87 RCHECK(reader
.ReadBits(5, &profile_
));
90 RCHECK(SkipDecoderGASpecificConfig(&reader
));
91 RCHECK(SkipErrorSpecificConfig());
93 // Read extension configuration again
94 // Note: The check for 16 available bits comes from the AAC spec.
95 if (extension_type
!= 5 && reader
.bits_available() >= 16) {
96 uint16 sync_extension_type
;
97 uint8 sbr_present_flag
;
98 uint8 ps_present_flag
;
100 if (reader
.ReadBits(11, &sync_extension_type
) &&
101 sync_extension_type
== 0x2b7) {
102 if (reader
.ReadBits(5, &extension_type
) && extension_type
== 5) {
103 RCHECK(reader
.ReadBits(1, &sbr_present_flag
));
105 if (sbr_present_flag
) {
106 RCHECK(reader
.ReadBits(4, &extension_frequency_index
));
108 if (extension_frequency_index
== 0xf)
109 RCHECK(reader
.ReadBits(24, &extension_frequency_
));
111 // Note: The check for 12 available bits comes from the AAC spec.
112 if (reader
.bits_available() >= 12) {
113 RCHECK(reader
.ReadBits(11, &sync_extension_type
));
114 if (sync_extension_type
== 0x548) {
115 RCHECK(reader
.ReadBits(1, &ps_present_flag
));
116 ps_present
= ps_present_flag
!= 0;
124 if (frequency_
== 0) {
125 RCHECK(frequency_index_
< arraysize(kFrequencyMap
));
126 frequency_
= kFrequencyMap
[frequency_index_
];
129 if (extension_frequency_
== 0 && extension_frequency_index
!= 0xff) {
130 RCHECK(extension_frequency_index
< arraysize(kFrequencyMap
));
131 extension_frequency_
= kFrequencyMap
[extension_frequency_index
];
134 // When Parametric Stereo is on, mono will be played as stereo.
135 if (ps_present
&& channel_config_
== 1)
136 channel_layout_
= CHANNEL_LAYOUT_STEREO
;
138 channel_layout_
= ConvertChannelConfigToLayout(channel_config_
);
140 return frequency_
!= 0 && channel_layout_
!= CHANNEL_LAYOUT_UNSUPPORTED
&&
141 profile_
>= 1 && profile_
<= 4 && frequency_index_
!= 0xf &&
142 channel_config_
<= 7;
145 int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype
) const {
146 if (extension_frequency_
> 0)
147 return extension_frequency_
;
149 if (!sbr_in_mimetype
)
152 // The following code is written according to ISO 14496 Part 3 Table 1.11 and
153 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
154 // to SBR doubling the AAC sample rate.)
155 // TODO(acolwell) : Extend sample rate cap to 96kHz for Level 5 content.
156 DCHECK_GT(frequency_
, 0);
157 return std::min(2 * frequency_
, 48000);
160 ChannelLayout
AAC::GetChannelLayout(bool sbr_in_mimetype
) const {
161 // Check for implicit signalling of HE-AAC and indicate stereo output
162 // if the mono channel configuration is signalled.
163 // See ISO-14496-3 Section 1.6.6.1.2 for details about this special casing.
164 if (sbr_in_mimetype
&& channel_config_
== 1)
165 return CHANNEL_LAYOUT_STEREO
;
167 return channel_layout_
;
170 bool AAC::ConvertEsdsToADTS(std::vector
<uint8
>* buffer
) const {
171 size_t size
= buffer
->size() + kADTSHeaderSize
;
173 DCHECK(profile_
>= 1 && profile_
<= 4 && frequency_index_
!= 0xf &&
174 channel_config_
<= 7);
176 // ADTS header uses 13 bits for packet size.
177 if (size
>= (1 << 13))
180 std::vector
<uint8
>& adts
= *buffer
;
182 adts
.insert(buffer
->begin(), kADTSHeaderSize
, 0);
185 adts
[2] = ((profile_
- 1) << 6) + (frequency_index_
<< 2) +
186 (channel_config_
>> 2);
187 adts
[3] = ((channel_config_
& 0x3) << 6) + (size
>> 11);
188 adts
[4] = (size
& 0x7ff) >> 3;
189 adts
[5] = ((size
& 7) << 5) + 0x1f;
195 // Currently this function only support GASpecificConfig defined in
196 // ISO 14496 Part 3 Table 4.1 - Syntax of GASpecificConfig()
197 bool AAC::SkipDecoderGASpecificConfig(BitReader
* bit_reader
) const {
211 return SkipGASpecificConfig(bit_reader
);
219 bool AAC::SkipErrorSpecificConfig() const {
239 // The following code is written according to ISO 14496 part 3 Table 4.1 -
241 bool AAC::SkipGASpecificConfig(BitReader
* bit_reader
) const {
242 uint8 extension_flag
= 0;
243 uint8 depends_on_core_coder
;
246 RCHECK(bit_reader
->ReadBits(1, &dummy
)); // frameLengthFlag
247 RCHECK(bit_reader
->ReadBits(1, &depends_on_core_coder
));
248 if (depends_on_core_coder
== 1)
249 RCHECK(bit_reader
->ReadBits(14, &dummy
)); // coreCoderDelay
251 RCHECK(bit_reader
->ReadBits(1, &extension_flag
));
252 RCHECK(channel_config_
!= 0);
254 if (profile_
== 6 || profile_
== 20)
255 RCHECK(bit_reader
->ReadBits(3, &dummy
)); // layerNr
257 if (extension_flag
) {
258 if (profile_
== 22) {
259 RCHECK(bit_reader
->ReadBits(5, &dummy
)); // numOfSubFrame
260 RCHECK(bit_reader
->ReadBits(11, &dummy
)); // layer_length
263 if (profile_
== 17 || profile_
== 19 || profile_
== 20 || profile_
== 23) {
264 RCHECK(bit_reader
->ReadBits(3, &dummy
)); // resilience flags
267 RCHECK(bit_reader
->ReadBits(1, &dummy
)); // extensionFlag3