5 #include <packager/media/codecs/aac_audio_specific_config.h>
9 #include <absl/log/check.h>
10 #include <absl/log/log.h>
12 #include <packager/media/base/bit_reader.h>
13 #include <packager/media/base/rcheck.h>
20 static const uint32_t kSampleRates[] = {96000, 88200, 64000, 48000, 44100,
21 32000, 24000, 22050, 16000, 12000,
25 const uint8_t kChannelConfigs[] = {0, 1, 2, 3, 4, 5, 6, 8};
32 bool CountChannels(uint8_t num_elements,
33 uint8_t* num_channels,
34 BitReader* bit_reader) {
35 for (uint8_t i = 0; i < num_elements; ++i) {
37 RCHECK(bit_reader->ReadBits(1, &is_pair));
38 *num_channels += is_pair ? 2 : 1;
39 RCHECK(bit_reader->SkipBits(4));
46 AACAudioSpecificConfig::AACAudioSpecificConfig() {}
48 AACAudioSpecificConfig::~AACAudioSpecificConfig() {}
55 uint8_t extension_type = AOT_NULL;
56 uint8_t extension_frequency_index = 0xff;
61 extension_frequency_ = 0;
68 RCHECK(ParseAudioObjectType(&reader));
70 RCHECK(reader.
ReadBits(4, &frequency_index_));
71 if (frequency_index_ == 0xf)
72 RCHECK(reader.
ReadBits(24, &frequency_));
73 RCHECK(reader.
ReadBits(4, &channel_config_));
75 RCHECK(channel_config_ < std::size(kChannelConfigs));
76 num_channels_ = kChannelConfigs[channel_config_];
79 if (audio_object_type_ == AOT_SBR || audio_object_type_ == AOT_PS) {
80 sbr_present_ = audio_object_type_ == AOT_SBR;
81 ps_present_ = audio_object_type_ == AOT_PS;
82 extension_type = AOT_SBR;
83 RCHECK(reader.
ReadBits(4, &extension_frequency_index));
84 if (extension_frequency_index == 0xf)
85 RCHECK(reader.
ReadBits(24, &extension_frequency_));
86 RCHECK(ParseAudioObjectType(&reader));
89 RCHECK(ParseDecoderGASpecificConfig(&reader));
90 RCHECK(SkipErrorSpecificConfig());
95 uint16_t sync_extension_type;
96 uint8_t sbr_present_flag;
97 uint8_t ps_present_flag;
99 if (reader.
ReadBits(11, &sync_extension_type) &&
100 sync_extension_type == 0x2b7) {
101 if (reader.
ReadBits(5, &extension_type) && extension_type == 5) {
102 RCHECK(reader.
ReadBits(1, &sbr_present_flag));
103 sbr_present_ = sbr_present_flag != 0;
105 if (sbr_present_flag) {
106 RCHECK(reader.
ReadBits(4, &extension_frequency_index));
108 if (extension_frequency_index == 0xf)
109 RCHECK(reader.
ReadBits(24, &extension_frequency_));
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;
124 if (frequency_ == 0) {
125 RCHECK(frequency_index_ < std::size(kSampleRates));
126 frequency_ = kSampleRates[frequency_index_];
129 if (extension_frequency_ == 0 && extension_frequency_index != 0xff) {
130 RCHECK(extension_frequency_index < std::size(kSampleRates));
131 extension_frequency_ = kSampleRates[extension_frequency_index];
134 if (audio_object_type_ == AOT_USAC) {
135 return frequency_ != 0 && num_channels_ != 0 && channel_config_ <= 7;
137 return frequency_ != 0 && num_channels_ != 0 && audio_object_type_ >= 1 &&
138 audio_object_type_ <= 4 && frequency_index_ != 0xf &&
139 channel_config_ <= 7;
146 std::vector<uint8_t>* audio_frame)
const {
147 DCHECK(audio_object_type_ >= 1 && audio_object_type_ <= 4 &&
148 frequency_index_ != 0xf && channel_config_ <= 7);
153 if (size >= (1 << 13))
156 audio_frame->reserve(size);
159 audio_frame->at(0) = 0xff;
160 audio_frame->at(1) = 0xf1;
161 audio_frame->at(2) = ((audio_object_type_ - 1) << 6) +
162 (frequency_index_ << 2) + (channel_config_ >> 2);
164 ((channel_config_ & 0x3) << 6) +
static_cast<uint8_t
>(size >> 11);
165 audio_frame->at(4) =
static_cast<uint8_t
>((size & 0x7ff) >> 3);
166 audio_frame->at(5) =
static_cast<uint8_t
>(((size & 7) << 5) + 0x1f);
167 audio_frame->at(6) = 0xfc;
169 audio_frame->insert(audio_frame->end(), data, data + data_size);
174 AACAudioSpecificConfig::AudioObjectType
180 return audio_object_type_;
184 if (extension_frequency_ > 0)
185 return extension_frequency_;
193 DCHECK_GT(frequency_, 0u);
194 return std::min(2 * frequency_, 48000u);
201 if (sbr_present_ && channel_config_ == 1)
205 if (ps_present_ && channel_config_ == 1)
208 return num_channels_;
211 bool AACAudioSpecificConfig::ParseAudioObjectType(
BitReader* bit_reader) {
212 RCHECK(bit_reader->
ReadBits(5, &audio_object_type_));
214 if (audio_object_type_ == AOT_ESCAPE) {
215 uint8_t audioObjectTypeExt;
216 RCHECK(bit_reader->
ReadBits(6, &audioObjectTypeExt));
217 audio_object_type_ =
static_cast<AudioObjectType
>(32 + audioObjectTypeExt);
225 bool AACAudioSpecificConfig::ParseDecoderGASpecificConfig(
226 BitReader* bit_reader) {
227 switch (audio_object_type_) {
240 return ParseGASpecificConfig(bit_reader);
243 RCHECK(bit_reader->SkipBits(bit_reader->bits_available()));
252 bool AACAudioSpecificConfig::SkipErrorSpecificConfig()
const {
253 switch (audio_object_type_) {
274 bool AACAudioSpecificConfig::ParseGASpecificConfig(BitReader* bit_reader) {
275 uint8_t extension_flag = 0;
276 uint8_t depends_on_core_coder;
279 RCHECK(bit_reader->ReadBits(1, &dummy));
280 RCHECK(bit_reader->ReadBits(1, &depends_on_core_coder));
281 if (depends_on_core_coder == 1)
282 RCHECK(bit_reader->ReadBits(14, &dummy));
284 RCHECK(bit_reader->ReadBits(1, &extension_flag));
285 if (channel_config_ == 0)
286 RCHECK(ParseProgramConfigElement(bit_reader));
288 if (audio_object_type_ == 6 || audio_object_type_ == 20)
289 RCHECK(bit_reader->ReadBits(3, &dummy));
291 if (extension_flag) {
292 if (audio_object_type_ == 22) {
293 RCHECK(bit_reader->ReadBits(5, &dummy));
294 RCHECK(bit_reader->ReadBits(11, &dummy));
297 if (audio_object_type_ == 17 || audio_object_type_ == 19 ||
298 audio_object_type_ == 20 || audio_object_type_ == 23) {
299 RCHECK(bit_reader->ReadBits(3, &dummy));
302 RCHECK(bit_reader->ReadBits(1, &dummy));
358 bool AACAudioSpecificConfig::ParseProgramConfigElement(BitReader* bit_reader) {
360 RCHECK(bit_reader->SkipBits(4 + 2 + 4));
362 uint8_t num_front_channel_elements = 0;
363 uint8_t num_side_channel_elements = 0;
364 uint8_t num_back_channel_elements = 0;
365 uint8_t num_lfe_channel_elements = 0;
366 RCHECK(bit_reader->ReadBits(4, &num_front_channel_elements));
367 RCHECK(bit_reader->ReadBits(4, &num_side_channel_elements));
368 RCHECK(bit_reader->ReadBits(4, &num_back_channel_elements));
369 RCHECK(bit_reader->ReadBits(2, &num_lfe_channel_elements));
371 uint8_t num_assoc_data_elements = 0;
372 RCHECK(bit_reader->ReadBits(3, &num_assoc_data_elements));
373 uint8_t num_valid_cc_elements = 0;
374 RCHECK(bit_reader->ReadBits(4, &num_valid_cc_elements));
376 RCHECK(bit_reader->SkipBitsConditional(
true, 4));
377 RCHECK(bit_reader->SkipBitsConditional(
true, 4));
378 RCHECK(bit_reader->SkipBitsConditional(
true, 3));
381 RCHECK(CountChannels(num_front_channel_elements, &num_channels_, bit_reader));
382 RCHECK(CountChannels(num_side_channel_elements, &num_channels_, bit_reader));
383 RCHECK(CountChannels(num_back_channel_elements, &num_channels_, bit_reader));
384 num_channels_ += num_lfe_channel_elements;
386 RCHECK(bit_reader->SkipBits(4 * num_lfe_channel_elements));
387 RCHECK(bit_reader->SkipBits(4 * num_assoc_data_elements));
388 RCHECK(bit_reader->SkipBits(5 * num_valid_cc_elements));
390 bit_reader->SkipToNextByte();
392 uint8_t comment_field_bytes = 0;
393 RCHECK(bit_reader->ReadBits(8, &comment_field_bytes));
394 RCHECK(bit_reader->SkipBytes(comment_field_bytes));
All the methods that are virtual are virtual for mocking.