Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "mp4_demuxer/aac.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <algorithm> |
michael@0 | 8 | |
michael@0 | 9 | #include "mp4_demuxer/bit_reader.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace mp4_demuxer { |
michael@0 | 12 | |
michael@0 | 13 | // The following conversion table is extracted from ISO 14496 Part 3 - |
michael@0 | 14 | // Table 1.16 - Sampling Frequency Index. |
michael@0 | 15 | static const int kFrequencyMap[] = { |
michael@0 | 16 | 96000, 88200, 64000, 48000, 44100, 32000, 24000, |
michael@0 | 17 | 22050, 16000, 12000, 11025, 8000, 7350 |
michael@0 | 18 | }; |
michael@0 | 19 | |
michael@0 | 20 | static ChannelLayout ConvertChannelConfigToLayout(uint8_t channel_config) { |
michael@0 | 21 | switch (channel_config) { |
michael@0 | 22 | case 1: |
michael@0 | 23 | return CHANNEL_LAYOUT_MONO; |
michael@0 | 24 | case 2: |
michael@0 | 25 | return CHANNEL_LAYOUT_STEREO; |
michael@0 | 26 | case 3: |
michael@0 | 27 | return CHANNEL_LAYOUT_SURROUND; |
michael@0 | 28 | case 4: |
michael@0 | 29 | return CHANNEL_LAYOUT_4_0; |
michael@0 | 30 | case 5: |
michael@0 | 31 | return CHANNEL_LAYOUT_5_0; |
michael@0 | 32 | case 6: |
michael@0 | 33 | return CHANNEL_LAYOUT_5_1; |
michael@0 | 34 | case 8: |
michael@0 | 35 | return CHANNEL_LAYOUT_7_1; |
michael@0 | 36 | default: |
michael@0 | 37 | break; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | return CHANNEL_LAYOUT_UNSUPPORTED; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | AAC::AAC() |
michael@0 | 44 | : profile_(0), frequency_index_(0), channel_config_(0), frequency_(0), |
michael@0 | 45 | extension_frequency_(0), channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED) { |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | AAC::~AAC() { |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | bool AAC::Parse(const std::vector<uint8_t>& data) { |
michael@0 | 52 | if (data.empty()) |
michael@0 | 53 | return false; |
michael@0 | 54 | |
michael@0 | 55 | BitReader reader(&data[0], data.size()); |
michael@0 | 56 | uint8_t extension_type = 0; |
michael@0 | 57 | bool ps_present = false; |
michael@0 | 58 | uint8_t extension_frequency_index = 0xff; |
michael@0 | 59 | |
michael@0 | 60 | frequency_ = 0; |
michael@0 | 61 | extension_frequency_ = 0; |
michael@0 | 62 | |
michael@0 | 63 | // The following code is written according to ISO 14496 Part 3 Table 1.13 - |
michael@0 | 64 | // Syntax of AudioSpecificConfig. |
michael@0 | 65 | |
michael@0 | 66 | // Read base configuration |
michael@0 | 67 | RCHECK(reader.ReadBits(5, &profile_)); |
michael@0 | 68 | RCHECK(reader.ReadBits(4, &frequency_index_)); |
michael@0 | 69 | if (frequency_index_ == 0xf) |
michael@0 | 70 | RCHECK(reader.ReadBits(24, &frequency_)); |
michael@0 | 71 | RCHECK(reader.ReadBits(4, &channel_config_)); |
michael@0 | 72 | |
michael@0 | 73 | // Read extension configuration. |
michael@0 | 74 | if (profile_ == 5 || profile_ == 29) { |
michael@0 | 75 | ps_present = (profile_ == 29); |
michael@0 | 76 | extension_type = 5; |
michael@0 | 77 | RCHECK(reader.ReadBits(4, &extension_frequency_index)); |
michael@0 | 78 | if (extension_frequency_index == 0xf) |
michael@0 | 79 | RCHECK(reader.ReadBits(24, &extension_frequency_)); |
michael@0 | 80 | RCHECK(reader.ReadBits(5, &profile_)); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | RCHECK(SkipDecoderGASpecificConfig(&reader)); |
michael@0 | 84 | RCHECK(SkipErrorSpecificConfig()); |
michael@0 | 85 | |
michael@0 | 86 | // Read extension configuration again |
michael@0 | 87 | // Note: The check for 16 available bits comes from the AAC spec. |
michael@0 | 88 | if (extension_type != 5 && reader.bits_available() >= 16) { |
michael@0 | 89 | uint16_t sync_extension_type; |
michael@0 | 90 | uint8_t sbr_present_flag; |
michael@0 | 91 | uint8_t ps_present_flag; |
michael@0 | 92 | |
michael@0 | 93 | if (reader.ReadBits(11, &sync_extension_type) && |
michael@0 | 94 | sync_extension_type == 0x2b7) { |
michael@0 | 95 | if (reader.ReadBits(5, &extension_type) && extension_type == 5) { |
michael@0 | 96 | RCHECK(reader.ReadBits(1, &sbr_present_flag)); |
michael@0 | 97 | |
michael@0 | 98 | if (sbr_present_flag) { |
michael@0 | 99 | RCHECK(reader.ReadBits(4, &extension_frequency_index)); |
michael@0 | 100 | |
michael@0 | 101 | if (extension_frequency_index == 0xf) |
michael@0 | 102 | RCHECK(reader.ReadBits(24, &extension_frequency_)); |
michael@0 | 103 | |
michael@0 | 104 | // Note: The check for 12 available bits comes from the AAC spec. |
michael@0 | 105 | if (reader.bits_available() >= 12) { |
michael@0 | 106 | RCHECK(reader.ReadBits(11, &sync_extension_type)); |
michael@0 | 107 | if (sync_extension_type == 0x548) { |
michael@0 | 108 | RCHECK(reader.ReadBits(1, &ps_present_flag)); |
michael@0 | 109 | ps_present = ps_present_flag != 0; |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | if (frequency_ == 0) { |
michael@0 | 118 | RCHECK(frequency_index_ < arraysize(kFrequencyMap)); |
michael@0 | 119 | frequency_ = kFrequencyMap[frequency_index_]; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | if (extension_frequency_ == 0 && extension_frequency_index != 0xff) { |
michael@0 | 123 | RCHECK(extension_frequency_index < arraysize(kFrequencyMap)); |
michael@0 | 124 | extension_frequency_ = kFrequencyMap[extension_frequency_index]; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | // When Parametric Stereo is on, mono will be played as stereo. |
michael@0 | 128 | if (ps_present && channel_config_ == 1) |
michael@0 | 129 | channel_layout_ = CHANNEL_LAYOUT_STEREO; |
michael@0 | 130 | else |
michael@0 | 131 | channel_layout_ = ConvertChannelConfigToLayout(channel_config_); |
michael@0 | 132 | |
michael@0 | 133 | audio_specific_config_.insert(audio_specific_config_.begin(), data.begin(), data.end()); |
michael@0 | 134 | |
michael@0 | 135 | return frequency_ != 0 && channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED && |
michael@0 | 136 | profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf && |
michael@0 | 137 | channel_config_ <= 7; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | const std::vector<uint8_t>& AAC::AudioSpecificConfig() const |
michael@0 | 141 | { |
michael@0 | 142 | return audio_specific_config_; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | int AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype) const { |
michael@0 | 146 | if (extension_frequency_ > 0) |
michael@0 | 147 | return extension_frequency_; |
michael@0 | 148 | |
michael@0 | 149 | if (!sbr_in_mimetype) |
michael@0 | 150 | return frequency_; |
michael@0 | 151 | |
michael@0 | 152 | // The following code is written according to ISO 14496 Part 3 Table 1.11 and |
michael@0 | 153 | // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers |
michael@0 | 154 | // to SBR doubling the AAC sample rate.) |
michael@0 | 155 | // TODO(acolwell) : Extend sample rate cap to 96kHz for Level 5 content. |
michael@0 | 156 | DCHECK_GT(frequency_, 0); |
michael@0 | 157 | return std::min(2 * frequency_, 48000); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | ChannelLayout AAC::GetChannelLayout(bool sbr_in_mimetype) const { |
michael@0 | 161 | // Check for implicit signalling of HE-AAC and indicate stereo output |
michael@0 | 162 | // if the mono channel configuration is signalled. |
michael@0 | 163 | // See ISO-14496-3 Section 1.6.6.1.2 for details about this special casing. |
michael@0 | 164 | if (sbr_in_mimetype && channel_config_ == 1) |
michael@0 | 165 | return CHANNEL_LAYOUT_STEREO; |
michael@0 | 166 | |
michael@0 | 167 | return channel_layout_; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | bool AAC::ConvertEsdsToADTS(std::vector<uint8_t>* buffer) const { |
michael@0 | 171 | size_t size = buffer->size() + kADTSHeaderSize; |
michael@0 | 172 | |
michael@0 | 173 | DCHECK(profile_ >= 1 && profile_ <= 4 && frequency_index_ != 0xf && |
michael@0 | 174 | channel_config_ <= 7); |
michael@0 | 175 | |
michael@0 | 176 | // ADTS header uses 13 bits for packet size. |
michael@0 | 177 | if (size >= (1 << 13)) |
michael@0 | 178 | return false; |
michael@0 | 179 | |
michael@0 | 180 | std::vector<uint8_t>& adts = *buffer; |
michael@0 | 181 | |
michael@0 | 182 | adts.insert(buffer->begin(), kADTSHeaderSize, 0); |
michael@0 | 183 | adts[0] = 0xff; |
michael@0 | 184 | adts[1] = 0xf1; |
michael@0 | 185 | adts[2] = ((profile_ - 1) << 6) + (frequency_index_ << 2) + |
michael@0 | 186 | (channel_config_ >> 2); |
michael@0 | 187 | adts[3] = ((channel_config_ & 0x3) << 6) + (size >> 11); |
michael@0 | 188 | adts[4] = (size & 0x7ff) >> 3; |
michael@0 | 189 | adts[5] = ((size & 7) << 5) + 0x1f; |
michael@0 | 190 | adts[6] = 0xfc; |
michael@0 | 191 | |
michael@0 | 192 | return true; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | // Currently this function only support GASpecificConfig defined in |
michael@0 | 196 | // ISO 14496 Part 3 Table 4.1 - Syntax of GASpecificConfig() |
michael@0 | 197 | bool AAC::SkipDecoderGASpecificConfig(BitReader* bit_reader) const { |
michael@0 | 198 | switch (profile_) { |
michael@0 | 199 | case 1: |
michael@0 | 200 | case 2: |
michael@0 | 201 | case 3: |
michael@0 | 202 | case 4: |
michael@0 | 203 | case 6: |
michael@0 | 204 | case 7: |
michael@0 | 205 | case 17: |
michael@0 | 206 | case 19: |
michael@0 | 207 | case 20: |
michael@0 | 208 | case 21: |
michael@0 | 209 | case 22: |
michael@0 | 210 | case 23: |
michael@0 | 211 | return SkipGASpecificConfig(bit_reader); |
michael@0 | 212 | default: |
michael@0 | 213 | break; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | return false; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | bool AAC::SkipErrorSpecificConfig() const { |
michael@0 | 220 | switch (profile_) { |
michael@0 | 221 | case 17: |
michael@0 | 222 | case 19: |
michael@0 | 223 | case 20: |
michael@0 | 224 | case 21: |
michael@0 | 225 | case 22: |
michael@0 | 226 | case 23: |
michael@0 | 227 | case 24: |
michael@0 | 228 | case 25: |
michael@0 | 229 | case 26: |
michael@0 | 230 | case 27: |
michael@0 | 231 | return false; |
michael@0 | 232 | default: |
michael@0 | 233 | break; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | return true; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | // The following code is written according to ISO 14496 part 3 Table 4.1 - |
michael@0 | 240 | // GASpecificConfig. |
michael@0 | 241 | bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const { |
michael@0 | 242 | uint8_t extension_flag = 0; |
michael@0 | 243 | uint8_t depends_on_core_coder; |
michael@0 | 244 | uint16_t dummy; |
michael@0 | 245 | |
michael@0 | 246 | RCHECK(bit_reader->ReadBits(1, &dummy)); // frameLengthFlag |
michael@0 | 247 | RCHECK(bit_reader->ReadBits(1, &depends_on_core_coder)); |
michael@0 | 248 | if (depends_on_core_coder == 1) |
michael@0 | 249 | RCHECK(bit_reader->ReadBits(14, &dummy)); // coreCoderDelay |
michael@0 | 250 | |
michael@0 | 251 | RCHECK(bit_reader->ReadBits(1, &extension_flag)); |
michael@0 | 252 | RCHECK(channel_config_ != 0); |
michael@0 | 253 | |
michael@0 | 254 | if (profile_ == 6 || profile_ == 20) |
michael@0 | 255 | RCHECK(bit_reader->ReadBits(3, &dummy)); // layerNr |
michael@0 | 256 | |
michael@0 | 257 | if (extension_flag) { |
michael@0 | 258 | if (profile_ == 22) { |
michael@0 | 259 | RCHECK(bit_reader->ReadBits(5, &dummy)); // numOfSubFrame |
michael@0 | 260 | RCHECK(bit_reader->ReadBits(11, &dummy)); // layer_length |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | if (profile_ == 17 || profile_ == 19 || profile_ == 20 || profile_ == 23) { |
michael@0 | 264 | RCHECK(bit_reader->ReadBits(3, &dummy)); // resilience flags |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | RCHECK(bit_reader->ReadBits(1, &dummy)); // extensionFlag3 |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | return true; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | } // namespace mp4_demuxer |