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/base/media_log.h" 12 #include "media/formats/mp4/rcheck.h" 13 #include "media/formats/mpeg/adts_constants.h" 14 15 namespace media { 16 namespace mp4 { 17 18 AAC::AAC() 19 : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0), 20 extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED) { 21 } 22 23 AAC::~AAC() { 24 } 25 26 bool AAC::Parse(const std::vector<uint8>& data, const LogCB& log_cb) { 27 #if defined(OS_ANDROID) 28 codec_specific_data_ = data; 29 #endif 30 if (data.empty()) 31 return false; 32 33 BitReader reader(&data[0], data.size()); 34 uint8 extension_type = 0; 35 bool ps_present = false; 36 uint8 extension_frequency_index = 0xff; 37 38 frequency_ = 0; 39 extension_frequency_ = 0; 40 41 // The following code is written according to ISO 14496 Part 3 Table 1.13 - 42 // Syntax of AudioSpecificConfig. 43 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_)); 50 51 // Read extension configuration. 52 if (profile_ == 5 || profile_ == 29) { 53 ps_present = (profile_ == 29); 54 extension_type = 5; 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_)); 59 } 60 61 MEDIA_LOG(log_cb) << "Audio codec: mp4a.40." 62 << std::hex << static_cast<int>(profile_); 63 64 RCHECK(SkipDecoderGASpecificConfig(&reader)); 65 RCHECK(SkipErrorSpecificConfig()); 66 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; 73 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)); 78 79 if (sbr_present_flag) { 80 RCHECK(reader.ReadBits(4, &extension_frequency_index)); 81 82 if (extension_frequency_index == 0xf) 83 RCHECK(reader.ReadBits(24, &extension_frequency_)); 84 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; 91 } 92 } 93 } 94 } 95 } 96 } 97 98 if (frequency_ == 0) { 99 RCHECK(frequency_index_ < kADTSFrequencyTableSize); 100 frequency_ = kADTSFrequencyTable[frequency_index_]; 101 } 102 103 if (extension_frequency_ == 0 && extension_frequency_index != 0xff) { 104 RCHECK(extension_frequency_index < kADTSFrequencyTableSize); 105 extension_frequency_ = kADTSFrequencyTable[extension_frequency_index]; 106 } 107 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; 111 } else { 112 RCHECK(channel_config_ < kADTSChannelLayoutTableSize); 113 channel_layout_ = kADTSChannelLayoutTable[channel_config_]; 114 } 115 116 return frequency_ != 0 && channel_layout_ != CHANNEL_LAYOUT_NONE && 117 profile_ >= 1 && profile_ <= 4; 118 } 119 120 int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype) const { 121 if (extension_frequency_ > 0) 122 return extension_frequency_; 123 124 if (!sbr_in_mimetype) 125 return frequency_; 126 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); 133 } 134 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; 141 142 return channel_layout_; 143 } 144 145 bool AAC::ConvertEsdsToADTS(std::vector<uint8>* buffer) const { 146 size_t size = buffer->size() + kADTSHeaderMinSize; 147 148 DCHECK(profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf && 149 channel_config_ <= 7); 150 151 // ADTS header uses 13 bits for packet size. 152 if (size >= (1 << 13)) 153 return false; 154 155 std::vector<uint8>& adts = *buffer; 156 157 adts.insert(buffer->begin(), kADTSHeaderMinSize, 0); 158 adts[0] = 0xff; 159 adts[1] = 0xf1; 160 adts[2] = ((profile_ - 1) << 6) + (frequency_index_ << 2) + 161 (channel_config_ >> 2); 162 adts[3] = ((channel_config_ & 0x3) << 6) + (size >> 11); 163 adts[4] = (size & 0x7ff) >> 3; 164 adts[5] = ((size & 7) << 5) + 0x1f; 165 adts[6] = 0xfc; 166 167 return true; 168 } 169 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 { 173 switch (profile_) { 174 case 1: 175 case 2: 176 case 3: 177 case 4: 178 case 6: 179 case 7: 180 case 17: 181 case 19: 182 case 20: 183 case 21: 184 case 22: 185 case 23: 186 return SkipGASpecificConfig(bit_reader); 187 default: 188 break; 189 } 190 191 return false; 192 } 193 194 bool AAC::SkipErrorSpecificConfig() const { 195 switch (profile_) { 196 case 17: 197 case 19: 198 case 20: 199 case 21: 200 case 22: 201 case 23: 202 case 24: 203 case 25: 204 case 26: 205 case 27: 206 return false; 207 default: 208 break; 209 } 210 211 return true; 212 } 213 214 // The following code is written according to ISO 14496 part 3 Table 4.1 - 215 // GASpecificConfig. 216 bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const { 217 uint8 extension_flag = 0; 218 uint8 depends_on_core_coder; 219 uint16 dummy; 220 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 225 226 RCHECK(bit_reader->ReadBits(1, &extension_flag)); 227 RCHECK(channel_config_ != 0); 228 229 if (profile_ == 6 || profile_ == 20) 230 RCHECK(bit_reader->ReadBits(3, &dummy)); // layerNr 231 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 236 } 237 238 if (profile_ == 17 || profile_ == 19 || profile_ == 20 || profile_ == 23) { 239 RCHECK(bit_reader->ReadBits(3, &dummy)); // resilience flags 240 } 241 242 RCHECK(bit_reader->ReadBits(1, &dummy)); // extensionFlag3 243 } 244 245 return true; 246 } 247 248 } // namespace mp4 249 250 } // namespace media 251