2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "media/mp4/aac.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "media/base/bit_reader.h"
|
|
|
|
#include "media/mp4/rcheck.h"
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Sampling Frequency Index table, from ISO 14496-3 Table 1.16
|
2013-10-14 20:55:48 +00:00
|
|
|
static const uint32 kSampleRates[] = {
|
2013-09-24 01:35:40 +00:00
|
|
|
96000, 88200, 64000, 48000, 44100, 32000, 24000,
|
|
|
|
22050, 16000, 12000, 11025, 8000, 7350
|
|
|
|
};
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
// Channel Configuration table, from ISO 14496-3 Table 1.17
|
2013-10-14 20:55:48 +00:00
|
|
|
const uint8 kChannelConfigs[] = {0, 1, 2, 3, 4, 5, 6, 8};
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
} // namespace
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
namespace media {
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
namespace mp4 {
|
|
|
|
|
|
|
|
AAC::AAC()
|
2013-10-14 20:55:48 +00:00
|
|
|
: audio_object_type_(0), frequency_index_(0), channel_config_(0),
|
|
|
|
ps_present_(false), frequency_(0), extension_frequency_(0),
|
|
|
|
num_channels_(0) {
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AAC::~AAC() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AAC::Parse(const std::vector<uint8>& data) {
|
|
|
|
codec_specific_data_ = data;
|
2013-10-14 20:55:48 +00:00
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
if (data.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BitReader reader(&data[0], data.size());
|
|
|
|
uint8 extension_type = 0;
|
|
|
|
uint8 extension_frequency_index = 0xff;
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
ps_present_ = false;
|
2013-09-24 01:35:40 +00:00
|
|
|
frequency_ = 0;
|
|
|
|
extension_frequency_ = 0;
|
|
|
|
|
|
|
|
// The following code is written according to ISO 14496 Part 3 Table 1.13 -
|
|
|
|
// Syntax of AudioSpecificConfig.
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
// Read base configuration.
|
|
|
|
// Audio Object Types specified in ISO 14496-3, Table 1.15.
|
|
|
|
RCHECK(reader.ReadBits(5, &audio_object_type_));
|
|
|
|
// Audio objects type >=31 is not supported yet.
|
|
|
|
RCHECK(audio_object_type_ < 31);
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader.ReadBits(4, &frequency_index_));
|
|
|
|
if (frequency_index_ == 0xf)
|
|
|
|
RCHECK(reader.ReadBits(24, &frequency_));
|
|
|
|
RCHECK(reader.ReadBits(4, &channel_config_));
|
|
|
|
|
|
|
|
// Read extension configuration.
|
2013-10-14 20:55:48 +00:00
|
|
|
if (audio_object_type_ == 5 || audio_object_type_ == 29) {
|
|
|
|
ps_present_ = (audio_object_type_ == 29);
|
2013-09-24 01:35:40 +00:00
|
|
|
extension_type = 5;
|
|
|
|
RCHECK(reader.ReadBits(4, &extension_frequency_index));
|
|
|
|
if (extension_frequency_index == 0xf)
|
|
|
|
RCHECK(reader.ReadBits(24, &extension_frequency_));
|
2013-10-14 20:55:48 +00:00
|
|
|
RCHECK(reader.ReadBits(5, &audio_object_type_));
|
|
|
|
// Audio objects type >=31 is not supported yet.
|
|
|
|
RCHECK(audio_object_type_ < 31);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCHECK(SkipDecoderGASpecificConfig(&reader));
|
|
|
|
RCHECK(SkipErrorSpecificConfig());
|
|
|
|
|
|
|
|
// Read extension configuration again
|
|
|
|
// Note: The check for 16 available bits comes from the AAC spec.
|
|
|
|
if (extension_type != 5 && reader.bits_available() >= 16) {
|
|
|
|
uint16 sync_extension_type;
|
|
|
|
uint8 sbr_present_flag;
|
|
|
|
uint8 ps_present_flag;
|
|
|
|
|
|
|
|
if (reader.ReadBits(11, &sync_extension_type) &&
|
|
|
|
sync_extension_type == 0x2b7) {
|
|
|
|
if (reader.ReadBits(5, &extension_type) && extension_type == 5) {
|
|
|
|
RCHECK(reader.ReadBits(1, &sbr_present_flag));
|
|
|
|
|
|
|
|
if (sbr_present_flag) {
|
|
|
|
RCHECK(reader.ReadBits(4, &extension_frequency_index));
|
|
|
|
|
|
|
|
if (extension_frequency_index == 0xf)
|
|
|
|
RCHECK(reader.ReadBits(24, &extension_frequency_));
|
|
|
|
|
|
|
|
// Note: The check for 12 available bits comes from the AAC spec.
|
|
|
|
if (reader.bits_available() >= 12) {
|
|
|
|
RCHECK(reader.ReadBits(11, &sync_extension_type));
|
|
|
|
if (sync_extension_type == 0x548) {
|
|
|
|
RCHECK(reader.ReadBits(1, &ps_present_flag));
|
2013-10-14 20:55:48 +00:00
|
|
|
ps_present_ = ps_present_flag != 0;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frequency_ == 0) {
|
2013-09-24 04:17:12 +00:00
|
|
|
RCHECK(frequency_index_ < arraysize(kSampleRates));
|
|
|
|
frequency_ = kSampleRates[frequency_index_];
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (extension_frequency_ == 0 && extension_frequency_index != 0xff) {
|
2013-09-24 04:17:12 +00:00
|
|
|
RCHECK(extension_frequency_index < arraysize(kSampleRates));
|
|
|
|
extension_frequency_ = kSampleRates[extension_frequency_index];
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
RCHECK(channel_config_ < arraysize(kChannelConfigs));
|
|
|
|
num_channels_ = kChannelConfigs[channel_config_];
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
return frequency_ != 0 && num_channels_ != 0 && audio_object_type_ >= 1 &&
|
|
|
|
audio_object_type_ <= 4 && frequency_index_ != 0xf &&
|
|
|
|
channel_config_ <= 7;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
uint32 AAC::GetOutputSamplesPerSecond(bool sbr_in_mimetype) const {
|
2013-09-24 01:35:40 +00:00
|
|
|
if (extension_frequency_ > 0)
|
|
|
|
return extension_frequency_;
|
|
|
|
|
|
|
|
if (!sbr_in_mimetype)
|
|
|
|
return frequency_;
|
|
|
|
|
|
|
|
// The following code is written according to ISO 14496 Part 3 Table 1.11 and
|
|
|
|
// Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
|
|
|
|
// to SBR doubling the AAC sample rate.)
|
|
|
|
// TODO(acolwell) : Extend sample rate cap to 96kHz for Level 5 content.
|
|
|
|
DCHECK_GT(frequency_, 0);
|
2013-10-14 20:55:48 +00:00
|
|
|
return std::min(2 * frequency_, 48000u);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
uint8 AAC::GetNumChannels(bool sbr_in_mimetype) const {
|
2013-09-24 01:35:40 +00:00
|
|
|
// Check for implicit signalling of HE-AAC and indicate stereo output
|
|
|
|
// if the mono channel configuration is signalled.
|
|
|
|
// See ISO-14496-3 Section 1.6.6.1.2 for details about this special casing.
|
|
|
|
if (sbr_in_mimetype && channel_config_ == 1)
|
2013-09-24 04:17:12 +00:00
|
|
|
return 2; // CHANNEL_LAYOUT_STEREO
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
// When Parametric Stereo is on, mono will be played as stereo.
|
|
|
|
if (ps_present_ && channel_config_ == 1)
|
|
|
|
return 2; // CHANNEL_LAYOUT_STEREO
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
return num_channels_;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
bool AAC::ConvertToADTS(std::vector<uint8>* buffer) const {
|
2013-09-24 01:35:40 +00:00
|
|
|
size_t size = buffer->size() + kADTSHeaderSize;
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
DCHECK(audio_object_type_ >= 1 && audio_object_type_ <= 4 &&
|
|
|
|
frequency_index_ != 0xf && channel_config_ <= 7);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// ADTS header uses 13 bits for packet size.
|
|
|
|
if (size >= (1 << 13))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::vector<uint8>& adts = *buffer;
|
|
|
|
|
|
|
|
adts.insert(buffer->begin(), kADTSHeaderSize, 0);
|
|
|
|
adts[0] = 0xff;
|
|
|
|
adts[1] = 0xf1;
|
2013-10-14 20:55:48 +00:00
|
|
|
adts[2] = ((audio_object_type_ - 1) << 6) + (frequency_index_ << 2) +
|
2013-09-24 01:35:40 +00:00
|
|
|
(channel_config_ >> 2);
|
|
|
|
adts[3] = ((channel_config_ & 0x3) << 6) + (size >> 11);
|
|
|
|
adts[4] = (size & 0x7ff) >> 3;
|
|
|
|
adts[5] = ((size & 7) << 5) + 0x1f;
|
|
|
|
adts[6] = 0xfc;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently this function only support GASpecificConfig defined in
|
|
|
|
// ISO 14496 Part 3 Table 4.1 - Syntax of GASpecificConfig()
|
|
|
|
bool AAC::SkipDecoderGASpecificConfig(BitReader* bit_reader) const {
|
2013-10-14 20:55:48 +00:00
|
|
|
switch (audio_object_type_) {
|
2013-09-24 01:35:40 +00:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 17:
|
|
|
|
case 19:
|
|
|
|
case 20:
|
|
|
|
case 21:
|
|
|
|
case 22:
|
|
|
|
case 23:
|
|
|
|
return SkipGASpecificConfig(bit_reader);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AAC::SkipErrorSpecificConfig() const {
|
2013-10-14 20:55:48 +00:00
|
|
|
switch (audio_object_type_) {
|
2013-09-24 01:35:40 +00:00
|
|
|
case 17:
|
|
|
|
case 19:
|
|
|
|
case 20:
|
|
|
|
case 21:
|
|
|
|
case 22:
|
|
|
|
case 23:
|
|
|
|
case 24:
|
|
|
|
case 25:
|
|
|
|
case 26:
|
|
|
|
case 27:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following code is written according to ISO 14496 part 3 Table 4.1 -
|
|
|
|
// GASpecificConfig.
|
|
|
|
bool AAC::SkipGASpecificConfig(BitReader* bit_reader) const {
|
|
|
|
uint8 extension_flag = 0;
|
|
|
|
uint8 depends_on_core_coder;
|
|
|
|
uint16 dummy;
|
|
|
|
|
|
|
|
RCHECK(bit_reader->ReadBits(1, &dummy)); // frameLengthFlag
|
|
|
|
RCHECK(bit_reader->ReadBits(1, &depends_on_core_coder));
|
|
|
|
if (depends_on_core_coder == 1)
|
|
|
|
RCHECK(bit_reader->ReadBits(14, &dummy)); // coreCoderDelay
|
|
|
|
|
|
|
|
RCHECK(bit_reader->ReadBits(1, &extension_flag));
|
|
|
|
RCHECK(channel_config_ != 0);
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
if (audio_object_type_ == 6 || audio_object_type_ == 20)
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(bit_reader->ReadBits(3, &dummy)); // layerNr
|
|
|
|
|
|
|
|
if (extension_flag) {
|
2013-10-14 20:55:48 +00:00
|
|
|
if (audio_object_type_ == 22) {
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(bit_reader->ReadBits(5, &dummy)); // numOfSubFrame
|
|
|
|
RCHECK(bit_reader->ReadBits(11, &dummy)); // layer_length
|
|
|
|
}
|
|
|
|
|
2013-10-14 20:55:48 +00:00
|
|
|
if (audio_object_type_ == 17 || audio_object_type_ == 19 ||
|
|
|
|
audio_object_type_ == 20 || audio_object_type_ == 23) {
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(bit_reader->ReadBits(3, &dummy)); // resilience flags
|
|
|
|
}
|
|
|
|
|
|
|
|
RCHECK(bit_reader->ReadBits(1, &dummy)); // extensionFlag3
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
|
|
|
|
} // namespace media
|