Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: media/mp4/aac.cc

Issue 10710002: Add HE AAC support to ISO BMFF. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add unittest for 5.1 channel. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "media/mp4/aac.h"
6
7 #include "base/logging.h"
8 #include "media/base/bit_reader.h"
9 #include "media/mp4/rcheck.h"
10
11 namespace {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Remove. The media code does not use anonymous name
12
13 // The following conversion table is extracted from ISO 14496 Part 3
14 // "Table 1.16 — Sampling Frequency Index".
15 static const uint32_t kFrequencyMap[] = {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Change to uint32
16 96000, 88200, 64000, 48000, 44100, 32000, 24000,
17 22050, 16000, 12000, 11025, 8000, 7350
18 };
19
20 // The elementary stream size is specific by up to 4 bytes.
21 // The MSB of a byte indicates if there are more bytes for the size.
22 bool ReadESSize(const std::vector<uint8>& esds, size_t* offset, uint32* size) {
23 uint8 byte;
24
25 *size = 0;
26
27 for (size_t i = 0; i < 4; ++i) {
28 RCHECK(*offset < esds.size());
29
30 byte = esds[*offset];
31 ++*offset;
32 *size = *size * 128 + (byte & 0x7f);
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Use << here
33
34 if ((byte & 0x80) == 0)
35 break;
36 }
37
38 return true;
39 }
40
41 ChannelLayout GetChannelLayout(uint8 channel_config) {
42 switch (channel_config) {
43 case 1:
44 return CHANNEL_LAYOUT_MONO;
45 case 2:
46 return CHANNEL_LAYOUT_STEREO;
47 case 3:
48 return CHANNEL_LAYOUT_SURROUND;
49 case 4:
50 return CHANNEL_LAYOUT_4_0;
51 case 5:
52 return CHANNEL_LAYOUT_5_0;
53 case 6:
54 return CHANNEL_LAYOUT_5_1;
55 case 8:
56 return CHANNEL_LAYOUT_7_1;
57 default:
58 break;
59 }
60
61 return CHANNEL_LAYOUT_UNSUPPORTED;
62 }
63
64 // This functions parses the binary data from esds box to get elementary
65 // stream descriptor.
66 bool ExtractDescriptor(const std::vector<uint8>& esds,
67 std::vector<uint8>* descriptor) {
68 uint8 tag;
69 uint32 size;
70 size_t offset = 0;
71
72 while (offset < esds.size()) {
73 tag = esds[offset];
74 ++offset;
75
76 RCHECK(ReadESSize(esds, &offset, &size));
77
78 if (tag == 3) {
79 offset += 3;
80 } else if (tag == 4) {
81 offset += 13;
82 } else if (tag == 5) {
83 RCHECK(offset + size <= esds.size());
84 descriptor->assign(esds.begin() + offset, esds.begin() + offset + size);
85 offset += size;
86 } else if (tag == 6) {
87 offset += 1;
88 } else {
89 break;
90 }
91 }
92
93 return !descriptor->empty();
94 }
95
96 } // anonymous namespace
97
98 namespace media {
99
100 namespace mp4 {
101
102 AAC::AAC()
103 : profile_(0), frequency_index_(0), channel_config_(0),
104 frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED) {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Add 2 indent spaces.
105 }
106
107 AAC::~AAC() {
108 }
109
110 bool AAC::Initialize(const std::vector<uint8>& esds) {
111 BitReader reader;
112 uint8 extension_type = 0;
113 bool ps_present = false;
114 uint8 extension_frequency_index;
115 std::vector<uint8> descriptor;
116
117 profile_ = 0;
118 frequency_index_ = 0;
119 frequency_ = 0;
120 channel_config_ = 0;
121
122 RCHECK(ExtractDescriptor(esds, &descriptor));
123 reader.Initialize(&descriptor[0], descriptor.size());
124
125 // The following code is written according to ISO 14496 Part 3, Table 1.13 -
126 // Syntax of AudioSpecificConfig.
127
128 // Read base configuration
129 RCHECK(reader.ReadBits(5, &profile_));
130 RCHECK(reader.ReadBits(4, &frequency_index_));
131 if (frequency_index_ == 0xf)
132 RCHECK(reader.ReadBits(24, &frequency_));
133 RCHECK(reader.ReadBits(4, &channel_config_));
134
135 extension_frequency_index = frequency_index_;
136
137 // Read extension configuration
138 if (profile_ == 5 || profile_ == 29) {
139 ps_present = (profile_ == 29);
140 extension_type = 5;
141 RCHECK(reader.ReadBits(4, &extension_frequency_index));
142 if (extension_frequency_index == 0xf)
143 RCHECK(reader.ReadBits(24, &frequency_));
144 RCHECK(reader.ReadBits(5, &profile_));
145 }
146
147 RCHECK(SkipDecoderGASpecificConfig(&reader));
148 RCHECK(SkipErrorSpecificConfig(&reader));
149
150 // Read extension configuration again
151 if (extension_type != 5 && reader.NumBitsLeft() >= 16) {
152 uint16 sync_extension_type;
153 uint8 sbr_present_flag;
154 uint8 ps_present_flag;
155
156 RCHECK(reader.ReadBits(11, &sync_extension_type));
157
158 if (sync_extension_type == 0x2b7) {
159 RCHECK(reader.ReadBits(5, &extension_type));
160
161 if (extension_type == 5) {
162 RCHECK(reader.ReadBits(1, &sbr_present_flag));
163
164 if (sbr_present_flag) {
165 RCHECK(reader.ReadBits(4, &extension_frequency_index));
166
167 if (extension_frequency_index == 0xf)
168 RCHECK(reader.ReadBits(24, &frequency_));
169
170 RCHECK(reader.ReadBits(11, &sync_extension_type));
171
172 if (sync_extension_type == 0x548) {
173 RCHECK(reader.ReadBits(1, &ps_present_flag));
174 ps_present = ps_present_flag != 0;
175 }
176 }
177 }
178 }
179 }
180
181 if (frequency_ == 0) {
182 RCHECK(extension_frequency_index <
183 sizeof(kFrequencyMap) / sizeof(*kFrequencyMap));
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 used arraysize(kFrequencyMap) here instead of size
184 frequency_ = kFrequencyMap[extension_frequency_index];
185 }
186
187 // When Parametric Stereo is on, mono will be played as stereo.
188 if (ps_present && channel_config_ == 1)
189 channel_layout_ = GetChannelLayout(channel_config_ + 1);
190 else
191 channel_layout_ = GetChannelLayout(channel_config_);
192
193 return frequency_ != 0 && channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED &&
194 profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf &&
195 channel_config_ <= 7;
196 }
197
198 uint32 AAC::frequency() const {
199 return frequency_;
200 }
201
202 ChannelLayout AAC::channel_layout() const {
203 return channel_layout_;
204 }
205
206 bool AAC::ConvertEsdsToADTS(std::vector<uint8>* buffer) const {
207 size_t size = buffer->size() + 7;
208
209 DCHECK(profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf &&
210 channel_config_ <= 7);
211
212 if (size <= 8192) {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 reverse condition and return early so that you don
213 std::vector<uint8>& adts = *buffer;
214
215 adts.insert(buffer->begin(), 7, 0);
216 adts[0] = 0xff;
217 adts[1] = 0xf1;
218 adts[2] = (profile_ - 1) * 64 + frequency_index_ * 4 + channel_config_ / 4;
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Please use << instead of * for all shifting and us
219 adts[3] = channel_config_ % 4 * 64 + size / 2048;
220 adts[4] = size % 2048 / 8;
221 adts[5] = size % 8 * 32 + 0x1f;
222 adts[6] = 0xfc;
223
224 return true;
225 }
226
227 return false;
228 }
229
230 // Currently this function only support GASpecificConfig defined in
231 // ISO 14496 Part 3 "Table 4.1 – Syntax of GASpecificConfig()"
232 bool AAC::SkipDecoderGASpecificConfig(BitReader* bit_reader) const {
233 switch (profile_) {
234 case 1:
235 case 2:
236 case 3:
237 case 4:
238 case 6:
239 case 7:
240 case 17:
241 case 19:
242 case 20:
243 case 21:
244 case 22:
245 case 23:
246 return SkipGASpecificConfig(bit_reader);
247 default:
248 break;
249 }
250
251 return false;
252 }
253
254 bool AAC::SkipErrorSpecificConfig(BitReader* bit_reader) const {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Remove bit_reader parameter. It isn't used by this
255 switch (profile_) {
256 case 17:
257 case 19:
258 case 20:
259 case 21:
260 case 22:
261 case 23:
262 case 24:
263 case 25:
264 case 26:
265 case 27:
266 return false;
267 default:
268 break;
269 }
270
271 return true;
272 }
273
274 // The following code is written according to ISO 14496 part 3 Table 4.1 -
275 // GASpecificConfig.
276 bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const {
277 uint8 extension_flag = 0;
278 uint8 depends_on_core_coder;
279
280 RCHECK(bit_reader->SkipBits(1)); // frameLengthFlag
281 RCHECK(bit_reader->ReadBits(1, &depends_on_core_coder));
282 if (depends_on_core_coder == 1)
283 RCHECK(bit_reader->SkipBits(14)); // coreCoderDelay
284
285 RCHECK(bit_reader->ReadBits(1, &extension_flag));
286 RCHECK(channel_config_ != 0);
287
288 if (profile_ == 6 || profile_ == 20)
289 RCHECK(bit_reader->SkipBits(3));
290
291 if (extension_flag) {
292 if (profile_ == 22) {
293 RCHECK(bit_reader->SkipBits(5));
294 RCHECK(bit_reader->SkipBits(11));
295 }
296
297 if (profile_ == 17 || profile_ == 19 || profile_ == 20 || profile_ == 23) {
298 RCHECK(bit_reader->SkipBits(1));
299 RCHECK(bit_reader->SkipBits(1));
300 RCHECK(bit_reader->SkipBits(1));
301 }
302
303 RCHECK(bit_reader->SkipBits(1));
304 }
305
306 return true;
307 }
308
309 } // namespace mp4
310
311 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698