aac.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.
4
5#include "media/formats/mp4/aac.h"
6
7#include <algorithm>
8
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"
13
14namespace media {
15namespace mp4 {
16
17AAC::AAC()
18    : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0),
19      extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED) {
20}
21
22AAC::~AAC() {
23}
24
25bool AAC::Parse(const std::vector<uint8>& data) {
26#if defined(OS_ANDROID)
27  codec_specific_data_ = data;
28#endif
29  if (data.empty())
30    return false;
31
32  BitReader reader(&data[0], data.size());
33  uint8 extension_type = 0;
34  bool ps_present = false;
35  uint8 extension_frequency_index = 0xff;
36
37  frequency_ = 0;
38  extension_frequency_ = 0;
39
40  // The following code is written according to ISO 14496 Part 3 Table 1.13 -
41  // Syntax of AudioSpecificConfig.
42
43  // Read base configuration
44  RCHECK(reader.ReadBits(5, &profile_));
45  RCHECK(reader.ReadBits(4, &frequency_index_));
46  if (frequency_index_ == 0xf)
47    RCHECK(reader.ReadBits(24, &frequency_));
48  RCHECK(reader.ReadBits(4, &channel_config_));
49
50  // Read extension configuration.
51  if (profile_ == 5 || profile_ == 29) {
52    ps_present = (profile_ == 29);
53    extension_type = 5;
54    RCHECK(reader.ReadBits(4, &extension_frequency_index));
55    if (extension_frequency_index == 0xf)
56      RCHECK(reader.ReadBits(24, &extension_frequency_));
57    RCHECK(reader.ReadBits(5, &profile_));
58  }
59
60  RCHECK(SkipDecoderGASpecificConfig(&reader));
61  RCHECK(SkipErrorSpecificConfig());
62
63  // Read extension configuration again
64  // Note: The check for 16 available bits comes from the AAC spec.
65  if (extension_type != 5 && reader.bits_available() >= 16) {
66    uint16 sync_extension_type;
67    uint8 sbr_present_flag;
68    uint8 ps_present_flag;
69
70    if (reader.ReadBits(11, &sync_extension_type) &&
71        sync_extension_type == 0x2b7) {
72      if (reader.ReadBits(5, &extension_type) && extension_type == 5) {
73        RCHECK(reader.ReadBits(1, &sbr_present_flag));
74
75        if (sbr_present_flag) {
76          RCHECK(reader.ReadBits(4, &extension_frequency_index));
77
78          if (extension_frequency_index == 0xf)
79            RCHECK(reader.ReadBits(24, &extension_frequency_));
80
81          // Note: The check for 12 available bits comes from the AAC spec.
82          if (reader.bits_available() >= 12) {
83            RCHECK(reader.ReadBits(11, &sync_extension_type));
84            if (sync_extension_type == 0x548) {
85              RCHECK(reader.ReadBits(1, &ps_present_flag));
86              ps_present = ps_present_flag != 0;
87            }
88          }
89        }
90      }
91    }
92  }
93
94  if (frequency_ == 0) {
95    RCHECK(frequency_index_ < kADTSFrequencyTableSize);
96    frequency_ = kADTSFrequencyTable[frequency_index_];
97  }
98
99  if (extension_frequency_ == 0 && extension_frequency_index != 0xff) {
100    RCHECK(extension_frequency_index < kADTSFrequencyTableSize);
101    extension_frequency_ = kADTSFrequencyTable[extension_frequency_index];
102  }
103
104  // When Parametric Stereo is on, mono will be played as stereo.
105  if (ps_present && channel_config_ == 1) {
106    channel_layout_ = CHANNEL_LAYOUT_STEREO;
107  } else {
108    RCHECK(channel_config_ < kADTSChannelLayoutTableSize);
109    channel_layout_ = kADTSChannelLayoutTable[channel_config_];
110  }
111
112  return frequency_ != 0 && channel_layout_ != CHANNEL_LAYOUT_NONE &&
113         profile_ >= 1 && profile_ <= 4;
114}
115
116int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype) const {
117  if (extension_frequency_ > 0)
118    return extension_frequency_;
119
120  if (!sbr_in_mimetype)
121    return frequency_;
122
123  // The following code is written according to ISO 14496 Part 3 Table 1.11 and
124  // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
125  // to SBR doubling the AAC sample rate.)
126  // TODO(acolwell) : Extend sample rate cap to 96kHz for Level 5 content.
127  DCHECK_GT(frequency_, 0);
128  return std::min(2 * frequency_, 48000);
129}
130
131ChannelLayout AAC::GetChannelLayout(bool sbr_in_mimetype) const {
132  // Check for implicit signalling of HE-AAC and indicate stereo output
133  // if the mono channel configuration is signalled.
134  // See ISO-14496-3 Section 1.6.6.1.2 for details about this special casing.
135  if (sbr_in_mimetype && channel_config_ == 1)
136    return CHANNEL_LAYOUT_STEREO;
137
138  return channel_layout_;
139}
140
141bool AAC::ConvertEsdsToADTS(std::vector<uint8>* buffer) const {
142  size_t size = buffer->size() + kADTSHeaderMinSize;
143
144  DCHECK(profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf &&
145         channel_config_ <= 7);
146
147  // ADTS header uses 13 bits for packet size.
148  if (size >= (1 << 13))
149    return false;
150
151  std::vector<uint8>& adts = *buffer;
152
153  adts.insert(buffer->begin(), kADTSHeaderMinSize, 0);
154  adts[0] = 0xff;
155  adts[1] = 0xf1;
156  adts[2] = ((profile_ - 1) << 6) + (frequency_index_ << 2) +
157      (channel_config_ >> 2);
158  adts[3] = ((channel_config_ & 0x3) << 6) + (size >> 11);
159  adts[4] = (size & 0x7ff) >> 3;
160  adts[5] = ((size & 7) << 5) + 0x1f;
161  adts[6] = 0xfc;
162
163  return true;
164}
165
166// Currently this function only support GASpecificConfig defined in
167// ISO 14496 Part 3 Table 4.1 - Syntax of GASpecificConfig()
168bool AAC::SkipDecoderGASpecificConfig(BitReader* bit_reader) const {
169  switch (profile_) {
170    case 1:
171    case 2:
172    case 3:
173    case 4:
174    case 6:
175    case 7:
176    case 17:
177    case 19:
178    case 20:
179    case 21:
180    case 22:
181    case 23:
182      return SkipGASpecificConfig(bit_reader);
183    default:
184      break;
185  }
186
187  return false;
188}
189
190bool AAC::SkipErrorSpecificConfig() const {
191  switch (profile_) {
192    case 17:
193    case 19:
194    case 20:
195    case 21:
196    case 22:
197    case 23:
198    case 24:
199    case 25:
200    case 26:
201    case 27:
202      return false;
203    default:
204      break;
205  }
206
207  return true;
208}
209
210// The following code is written according to ISO 14496 part 3 Table 4.1 -
211// GASpecificConfig.
212bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const {
213  uint8 extension_flag = 0;
214  uint8 depends_on_core_coder;
215  uint16 dummy;
216
217  RCHECK(bit_reader->ReadBits(1, &dummy));  // frameLengthFlag
218  RCHECK(bit_reader->ReadBits(1, &depends_on_core_coder));
219  if (depends_on_core_coder == 1)
220    RCHECK(bit_reader->ReadBits(14, &dummy));  // coreCoderDelay
221
222  RCHECK(bit_reader->ReadBits(1, &extension_flag));
223  RCHECK(channel_config_ != 0);
224
225  if (profile_ == 6 || profile_ == 20)
226    RCHECK(bit_reader->ReadBits(3, &dummy));  // layerNr
227
228  if (extension_flag) {
229    if (profile_ == 22) {
230      RCHECK(bit_reader->ReadBits(5, &dummy));  // numOfSubFrame
231      RCHECK(bit_reader->ReadBits(11, &dummy));  // layer_length
232    }
233
234    if (profile_ == 17 || profile_ == 19 || profile_ == 20 || profile_ == 23) {
235      RCHECK(bit_reader->ReadBits(3, &dummy));  // resilience flags
236    }
237
238    RCHECK(bit_reader->ReadBits(1, &dummy));  // extensionFlag3
239  }
240
241  return true;
242}
243
244}  // namespace mp4
245
246}  // namespace media
247