1 // Copyright 2014 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/formats/mp4/aac.h"
9 #include "base/logging.h"
10 #include "media/base/bit_reader.h"
11 #include "media/formats/mp4/rcheck.h"
12 #include "media/formats/mpeg/adts_constants.h"
18 : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0),
19 extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED
) {
25 bool AAC::Parse(const std::vector
<uint8
>& data
,
26 const scoped_refptr
<MediaLog
>& media_log
) {
27 #if defined(OS_ANDROID)
28 codec_specific_data_
= data
;
33 BitReader
reader(&data
[0], data
.size());
34 uint8 extension_type
= 0;
35 bool ps_present
= false;
36 uint8 extension_frequency_index
= 0xff;
39 extension_frequency_
= 0;
41 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
42 // Syntax of AudioSpecificConfig.
44 // Read base configuration
45 RCHECK(reader
.ReadBits(5, &profile_
));
46 RCHECK(reader
.ReadBits(4, &frequency_index_
));
47 if (frequency_index_
== 0xf)
48 RCHECK(reader
.ReadBits(24, &frequency_
));
49 RCHECK(reader
.ReadBits(4, &channel_config_
));
51 // Read extension configuration.
52 if (profile_
== 5 || profile_
== 29) {
53 ps_present
= (profile_
== 29);
55 RCHECK(reader
.ReadBits(4, &extension_frequency_index
));
56 if (extension_frequency_index
== 0xf)
57 RCHECK(reader
.ReadBits(24, &extension_frequency_
));
58 RCHECK(reader
.ReadBits(5, &profile_
));
61 MEDIA_LOG(INFO
, media_log
) << "Audio codec: mp4a.40." << std::hex
62 << static_cast<int>(profile_
);
64 RCHECK(SkipDecoderGASpecificConfig(&reader
));
65 RCHECK(SkipErrorSpecificConfig());
67 // Read extension configuration again
68 // Note: The check for 16 available bits comes from the AAC spec.
69 if (extension_type
!= 5 && reader
.bits_available() >= 16) {
70 uint16 sync_extension_type
;
71 uint8 sbr_present_flag
;
72 uint8 ps_present_flag
;
74 if (reader
.ReadBits(11, &sync_extension_type
) &&
75 sync_extension_type
== 0x2b7) {
76 if (reader
.ReadBits(5, &extension_type
) && extension_type
== 5) {
77 RCHECK(reader
.ReadBits(1, &sbr_present_flag
));
79 if (sbr_present_flag
) {
80 RCHECK(reader
.ReadBits(4, &extension_frequency_index
));
82 if (extension_frequency_index
== 0xf)
83 RCHECK(reader
.ReadBits(24, &extension_frequency_
));
85 // Note: The check for 12 available bits comes from the AAC spec.
86 if (reader
.bits_available() >= 12) {
87 RCHECK(reader
.ReadBits(11, &sync_extension_type
));
88 if (sync_extension_type
== 0x548) {
89 RCHECK(reader
.ReadBits(1, &ps_present_flag
));
90 ps_present
= ps_present_flag
!= 0;
98 if (frequency_
== 0) {
99 RCHECK(frequency_index_
< kADTSFrequencyTableSize
);
100 frequency_
= kADTSFrequencyTable
[frequency_index_
];
103 if (extension_frequency_
== 0 && extension_frequency_index
!= 0xff) {
104 RCHECK(extension_frequency_index
< kADTSFrequencyTableSize
);
105 extension_frequency_
= kADTSFrequencyTable
[extension_frequency_index
];
108 // When Parametric Stereo is on, mono will be played as stereo.
109 if (ps_present
&& channel_config_
== 1) {
110 channel_layout_
= CHANNEL_LAYOUT_STEREO
;
112 RCHECK(channel_config_
< kADTSChannelLayoutTableSize
);
113 channel_layout_
= kADTSChannelLayoutTable
[channel_config_
];
116 return frequency_
!= 0 && channel_layout_
!= CHANNEL_LAYOUT_NONE
&&
117 profile_
>= 1 && profile_
<= 4;
120 int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype
) const {
121 if (extension_frequency_
> 0)
122 return extension_frequency_
;
124 if (!sbr_in_mimetype
)
127 // The following code is written according to ISO 14496 Part 3 Table 1.11 and
128 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
129 // to SBR doubling the AAC sample rate.)
130 // TODO(acolwell) : Extend sample rate cap to 96kHz for Level 5 content.
131 DCHECK_GT(frequency_
, 0);
132 return std::min(2 * frequency_
, 48000);
135 ChannelLayout
AAC::GetChannelLayout(bool sbr_in_mimetype
) const {
136 // Check for implicit signalling of HE-AAC and indicate stereo output
137 // if the mono channel configuration is signalled.
138 // See ISO-14496-3 Section 1.6.6.1.2 for details about this special casing.
139 if (sbr_in_mimetype
&& channel_config_
== 1)
140 return CHANNEL_LAYOUT_STEREO
;
142 return channel_layout_
;
145 bool AAC::ConvertEsdsToADTS(std::vector
<uint8
>* buffer
) const {
146 size_t size
= buffer
->size() + kADTSHeaderMinSize
;
148 DCHECK(profile_
>= 1 && profile_
<= 4 && frequency_index_
!= 0xf &&
149 channel_config_
<= 7);
151 // ADTS header uses 13 bits for packet size.
152 if (size
>= (1 << 13))
155 std::vector
<uint8
>& adts
= *buffer
;
157 adts
.insert(buffer
->begin(), kADTSHeaderMinSize
, 0);
160 adts
[2] = ((profile_
- 1) << 6) + (frequency_index_
<< 2) +
161 (channel_config_
>> 2);
162 adts
[3] = static_cast<uint8
>(((channel_config_
& 0x3) << 6) + (size
>> 11));
163 adts
[4] = static_cast<uint8
>((size
& 0x7ff) >> 3);
164 adts
[5] = ((size
& 7) << 5) + 0x1f;
170 // Currently this function only support GASpecificConfig defined in
171 // ISO 14496 Part 3 Table 4.1 - Syntax of GASpecificConfig()
172 bool AAC::SkipDecoderGASpecificConfig(BitReader
* bit_reader
) const {
186 return SkipGASpecificConfig(bit_reader
);
194 bool AAC::SkipErrorSpecificConfig() const {
214 // The following code is written according to ISO 14496 part 3 Table 4.1 -
216 bool AAC::SkipGASpecificConfig(BitReader
* bit_reader
) const {
217 uint8 extension_flag
= 0;
218 uint8 depends_on_core_coder
;
221 RCHECK(bit_reader
->ReadBits(1, &dummy
)); // frameLengthFlag
222 RCHECK(bit_reader
->ReadBits(1, &depends_on_core_coder
));
223 if (depends_on_core_coder
== 1)
224 RCHECK(bit_reader
->ReadBits(14, &dummy
)); // coreCoderDelay
226 RCHECK(bit_reader
->ReadBits(1, &extension_flag
));
227 RCHECK(channel_config_
!= 0);
229 if (profile_
== 6 || profile_
== 20)
230 RCHECK(bit_reader
->ReadBits(3, &dummy
)); // layerNr
232 if (extension_flag
) {
233 if (profile_
== 22) {
234 RCHECK(bit_reader
->ReadBits(5, &dummy
)); // numOfSubFrame
235 RCHECK(bit_reader
->ReadBits(11, &dummy
)); // layer_length
238 if (profile_
== 17 || profile_
== 19 || profile_
== 20 || profile_
== 23) {
239 RCHECK(bit_reader
->ReadBits(3, &dummy
)); // resilience flags
242 RCHECK(bit_reader
->ReadBits(1, &dummy
)); // extensionFlag3