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
GetChannelLayout(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
) {
59 BitReader
reader(&data
[0], data
.size());
60 uint8 extension_type
= 0;
61 bool ps_present
= false;
62 uint8 extension_frequency_index
= 0xff;
65 extension_frequency_
= 0;
67 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
68 // Syntax of AudioSpecificConfig.
70 // Read base configuration
71 RCHECK(reader
.ReadBits(5, &profile_
));
72 RCHECK(reader
.ReadBits(4, &frequency_index_
));
73 if (frequency_index_
== 0xf)
74 RCHECK(reader
.ReadBits(24, &frequency_
));
75 RCHECK(reader
.ReadBits(4, &channel_config_
));
77 // Read extension configuration.
78 if (profile_
== 5 || profile_
== 29) {
79 ps_present
= (profile_
== 29);
81 RCHECK(reader
.ReadBits(4, &extension_frequency_index
));
82 if (extension_frequency_index
== 0xf)
83 RCHECK(reader
.ReadBits(24, &extension_frequency_
));
84 RCHECK(reader
.ReadBits(5, &profile_
));
87 RCHECK(SkipDecoderGASpecificConfig(&reader
));
88 RCHECK(SkipErrorSpecificConfig());
90 // Read extension configuration again
91 if (extension_type
!= 5) {
92 uint16 sync_extension_type
;
93 uint8 sbr_present_flag
;
94 uint8 ps_present_flag
;
96 if (reader
.ReadBits(11, &sync_extension_type
) &&
97 sync_extension_type
== 0x2b7) {
98 if (reader
.ReadBits(5, &extension_type
) && extension_type
== 5) {
99 RCHECK(reader
.ReadBits(1, &sbr_present_flag
));
101 if (sbr_present_flag
) {
102 RCHECK(reader
.ReadBits(4, &extension_frequency_index
));
104 if (extension_frequency_index
== 0xf)
105 RCHECK(reader
.ReadBits(24, &extension_frequency_
));
107 RCHECK(reader
.ReadBits(11, &sync_extension_type
));
109 if (sync_extension_type
== 0x548) {
110 RCHECK(reader
.ReadBits(1, &ps_present_flag
));
111 ps_present
= ps_present_flag
!= 0;
118 if (frequency_
== 0) {
119 RCHECK(frequency_index_
< arraysize(kFrequencyMap
));
120 frequency_
= kFrequencyMap
[frequency_index_
];
123 if (extension_frequency_
== 0 && extension_frequency_index
!= 0xff) {
124 RCHECK(extension_frequency_index
< arraysize(kFrequencyMap
));
125 extension_frequency_
= kFrequencyMap
[extension_frequency_index
];
128 // When Parametric Stereo is on, mono will be played as stereo.
129 if (ps_present
&& channel_config_
== 1)
130 channel_layout_
= GetChannelLayout(2);
132 channel_layout_
= GetChannelLayout(channel_config_
);
134 return frequency_
!= 0 && channel_layout_
!= CHANNEL_LAYOUT_UNSUPPORTED
&&
135 profile_
>= 1 && profile_
<= 4 && frequency_index_
!= 0xf &&
136 channel_config_
<= 7;
139 int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype
) const {
140 if (extension_frequency_
> 0)
141 return extension_frequency_
;
143 if (!sbr_in_mimetype
)
146 // The following code is written according to ISO 14496 Part 3 Table 1.11 and
147 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
148 // to SBR doubling the AAC sample rate.)
149 // TODO(acolwell) : Extend sample rate cap to 96kHz for Level 5 content.
150 DCHECK_GT(frequency_
, 0);
151 return std::min(2 * frequency_
, 48000);
154 ChannelLayout
AAC::channel_layout() const {
155 return channel_layout_
;
158 bool AAC::ConvertEsdsToADTS(std::vector
<uint8
>* buffer
) const {
159 size_t size
= buffer
->size() + kADTSHeaderSize
;
161 DCHECK(profile_
>= 1 && profile_
<= 4 && frequency_index_
!= 0xf &&
162 channel_config_
<= 7);
164 // ADTS header uses 13 bits for packet size.
165 if (size
>= (1 << 13))
168 std::vector
<uint8
>& adts
= *buffer
;
170 adts
.insert(buffer
->begin(), kADTSHeaderSize
, 0);
173 adts
[2] = ((profile_
- 1) << 6) + (frequency_index_
<< 2) +
174 (channel_config_
>> 2);
175 adts
[3] = ((channel_config_
& 0x3) << 6) + (size
>> 11);
176 adts
[4] = (size
& 0x7ff) >> 3;
177 adts
[5] = ((size
& 7) << 5) + 0x1f;
183 // Currently this function only support GASpecificConfig defined in
184 // ISO 14496 Part 3 Table 4.1 - Syntax of GASpecificConfig()
185 bool AAC::SkipDecoderGASpecificConfig(BitReader
* bit_reader
) const {
199 return SkipGASpecificConfig(bit_reader
);
207 bool AAC::SkipErrorSpecificConfig() const {
227 // The following code is written according to ISO 14496 part 3 Table 4.1 -
229 bool AAC::SkipGASpecificConfig(BitReader
* bit_reader
) const {
230 uint8 extension_flag
= 0;
231 uint8 depends_on_core_coder
;
234 RCHECK(bit_reader
->ReadBits(1, &dummy
)); // frameLengthFlag
235 RCHECK(bit_reader
->ReadBits(1, &depends_on_core_coder
));
236 if (depends_on_core_coder
== 1)
237 RCHECK(bit_reader
->ReadBits(14, &dummy
)); // coreCoderDelay
239 RCHECK(bit_reader
->ReadBits(1, &extension_flag
));
240 RCHECK(channel_config_
!= 0);
242 if (profile_
== 6 || profile_
== 20)
243 RCHECK(bit_reader
->ReadBits(3, &dummy
)); // layerNr
245 if (extension_flag
) {
246 if (profile_
== 22) {
247 RCHECK(bit_reader
->ReadBits(5, &dummy
)); // numOfSubFrame
248 RCHECK(bit_reader
->ReadBits(11, &dummy
)); // layer_length
251 if (profile_
== 17 || profile_
== 19 || profile_
== 20 || profile_
== 23) {
252 RCHECK(bit_reader
->ReadBits(3, &dummy
)); // resilience flags
255 RCHECK(bit_reader
->ReadBits(1, &dummy
)); // extensionFlag3