- add sources.
[platform/framework/web/crosswalk.git] / src / media / mp4 / aac.cc
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 <algorithm>
8
9 #include "base/logging.h"
10 #include "media/base/bit_reader.h"
11 #include "media/mp4/rcheck.h"
12
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
18 };
19
20 namespace media {
21
22 static ChannelLayout ConvertChannelConfigToLayout(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;
40   }
41
42   return CHANNEL_LAYOUT_UNSUPPORTED;
43 }
44
45 namespace mp4 {
46
47 AAC::AAC()
48     : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0),
49       extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED) {
50 }
51
52 AAC::~AAC() {
53 }
54
55 bool AAC::Parse(const std::vector<uint8>& data) {
56 #if defined(OS_ANDROID)
57   codec_specific_data_ = data;
58 #endif
59   if (data.empty())
60     return false;
61
62   BitReader reader(&data[0], data.size());
63   uint8 extension_type = 0;
64   bool ps_present = false;
65   uint8 extension_frequency_index = 0xff;
66
67   frequency_ = 0;
68   extension_frequency_ = 0;
69
70   // The following code is written according to ISO 14496 Part 3 Table 1.13 -
71   // Syntax of AudioSpecificConfig.
72
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_));
79
80   // Read extension configuration.
81   if (profile_ == 5 || profile_ == 29) {
82     ps_present = (profile_ == 29);
83     extension_type = 5;
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_));
88   }
89
90   RCHECK(SkipDecoderGASpecificConfig(&reader));
91   RCHECK(SkipErrorSpecificConfig());
92
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;
99
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));
104
105         if (sbr_present_flag) {
106           RCHECK(reader.ReadBits(4, &extension_frequency_index));
107
108           if (extension_frequency_index == 0xf)
109             RCHECK(reader.ReadBits(24, &extension_frequency_));
110
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;
117             }
118           }
119         }
120       }
121     }
122   }
123
124   if (frequency_ == 0) {
125     RCHECK(frequency_index_ < arraysize(kFrequencyMap));
126     frequency_ = kFrequencyMap[frequency_index_];
127   }
128
129   if (extension_frequency_ == 0 && extension_frequency_index != 0xff) {
130     RCHECK(extension_frequency_index < arraysize(kFrequencyMap));
131     extension_frequency_ = kFrequencyMap[extension_frequency_index];
132   }
133
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;
137   else
138     channel_layout_ = ConvertChannelConfigToLayout(channel_config_);
139
140   return frequency_ != 0 && channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED &&
141       profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf &&
142       channel_config_ <= 7;
143 }
144
145 int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype) const {
146   if (extension_frequency_ > 0)
147     return extension_frequency_;
148
149   if (!sbr_in_mimetype)
150     return frequency_;
151
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);
158 }
159
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;
166
167   return channel_layout_;
168 }
169
170 bool AAC::ConvertEsdsToADTS(std::vector<uint8>* buffer) const {
171   size_t size = buffer->size() + kADTSHeaderSize;
172
173   DCHECK(profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf &&
174          channel_config_ <= 7);
175
176   // ADTS header uses 13 bits for packet size.
177   if (size >= (1 << 13))
178     return false;
179
180   std::vector<uint8>& adts = *buffer;
181
182   adts.insert(buffer->begin(), kADTSHeaderSize, 0);
183   adts[0] = 0xff;
184   adts[1] = 0xf1;
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;
190   adts[6] = 0xfc;
191
192   return true;
193 }
194
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 {
198   switch (profile_) {
199     case 1:
200     case 2:
201     case 3:
202     case 4:
203     case 6:
204     case 7:
205     case 17:
206     case 19:
207     case 20:
208     case 21:
209     case 22:
210     case 23:
211       return SkipGASpecificConfig(bit_reader);
212     default:
213       break;
214   }
215
216   return false;
217 }
218
219 bool AAC::SkipErrorSpecificConfig() const {
220   switch (profile_) {
221     case 17:
222     case 19:
223     case 20:
224     case 21:
225     case 22:
226     case 23:
227     case 24:
228     case 25:
229     case 26:
230     case 27:
231       return false;
232     default:
233       break;
234   }
235
236   return true;
237 }
238
239 // The following code is written according to ISO 14496 part 3 Table 4.1 -
240 // GASpecificConfig.
241 bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const {
242   uint8 extension_flag = 0;
243   uint8 depends_on_core_coder;
244   uint16 dummy;
245
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
250
251   RCHECK(bit_reader->ReadBits(1, &extension_flag));
252   RCHECK(channel_config_ != 0);
253
254   if (profile_ == 6 || profile_ == 20)
255     RCHECK(bit_reader->ReadBits(3, &dummy));  // layerNr
256
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
261     }
262
263     if (profile_ == 17 || profile_ == 19 || profile_ == 20 || profile_ == 23) {
264       RCHECK(bit_reader->ReadBits(3, &dummy));  // resilience flags
265     }
266
267     RCHECK(bit_reader->ReadBits(1, &dummy));  // extensionFlag3
268   }
269
270   return true;
271 }
272
273 }  // namespace mp4
274
275 }  // namespace media