Added histogram for v2 app launch source
[chromium-blink-merge.git] / media / mp4 / aac.cc
blob0214eb3c64ae7b18de013798af490ccd23b5d9e8
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"
7 #include <algorithm>
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
20 namespace media {
22 static ChannelLayout GetChannelLayout(uint8 channel_config) {
23 switch (channel_config) {
24 case 1:
25 return CHANNEL_LAYOUT_MONO;
26 case 2:
27 return CHANNEL_LAYOUT_STEREO;
28 case 3:
29 return CHANNEL_LAYOUT_SURROUND;
30 case 4:
31 return CHANNEL_LAYOUT_4_0;
32 case 5:
33 return CHANNEL_LAYOUT_5_0;
34 case 6:
35 return CHANNEL_LAYOUT_5_1;
36 case 8:
37 return CHANNEL_LAYOUT_7_1;
38 default:
39 break;
42 return CHANNEL_LAYOUT_UNSUPPORTED;
45 namespace mp4 {
47 AAC::AAC()
48 : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0),
49 extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED) {
52 AAC::~AAC() {
55 bool AAC::Parse(const std::vector<uint8>& data) {
56 if (data.empty())
57 return false;
59 BitReader reader(&data[0], data.size());
60 uint8 extension_type = 0;
61 bool ps_present = false;
62 uint8 extension_frequency_index = 0xff;
64 frequency_ = 0;
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);
80 extension_type = 5;
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);
131 else
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)
144 return frequency_;
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))
166 return false;
168 std::vector<uint8>& adts = *buffer;
170 adts.insert(buffer->begin(), kADTSHeaderSize, 0);
171 adts[0] = 0xff;
172 adts[1] = 0xf1;
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;
178 adts[6] = 0xfc;
180 return true;
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 {
186 switch (profile_) {
187 case 1:
188 case 2:
189 case 3:
190 case 4:
191 case 6:
192 case 7:
193 case 17:
194 case 19:
195 case 20:
196 case 21:
197 case 22:
198 case 23:
199 return SkipGASpecificConfig(bit_reader);
200 default:
201 break;
204 return false;
207 bool AAC::SkipErrorSpecificConfig() const {
208 switch (profile_) {
209 case 17:
210 case 19:
211 case 20:
212 case 21:
213 case 22:
214 case 23:
215 case 24:
216 case 25:
217 case 26:
218 case 27:
219 return false;
220 default:
221 break;
224 return true;
227 // The following code is written according to ISO 14496 part 3 Table 4.1 -
228 // GASpecificConfig.
229 bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const {
230 uint8 extension_flag = 0;
231 uint8 depends_on_core_coder;
232 uint16 dummy;
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
258 return true;
261 } // namespace mp4
263 } // namespace media