2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-04-15 23:51:32 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/adts_header.h"
|
2014-04-15 23:51:32 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/bit_reader.h"
|
2017-10-23 05:27:27 +00:00
|
|
|
#include "packager/media/base/bit_writer.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/mp2t_common.h"
|
2017-11-06 23:45:44 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const size_t kAdtsHeaderMinSize = 7;
|
|
|
|
|
|
|
|
// The following conversion table is extracted from ISO 14496 Part 3 -
|
|
|
|
// Table 1.16 - Sampling Frequency Index.
|
|
|
|
const int kAdtsFrequencyTable[] = {96000, 88200, 64000, 48000, 44100,
|
|
|
|
32000, 24000, 22050, 16000, 12000,
|
|
|
|
11025, 8000, 7350};
|
|
|
|
const size_t kAdtsFrequencyTableSize = arraysize(kAdtsFrequencyTable);
|
|
|
|
|
|
|
|
// The following conversion table is extracted from ISO 14496 Part 3 -
|
|
|
|
// Table 1.17 - Channel Configuration.
|
|
|
|
const int kAdtsNumChannelsTable[] = {0, 1, 2, 3, 4, 5, 6, 8};
|
|
|
|
const size_t kAdtsNumChannelsTableSize = arraysize(kAdtsNumChannelsTable);
|
|
|
|
} // namespace
|
2014-04-15 23:51:32 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-15 23:51:32 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
bool AdtsHeader::IsSyncWord(const uint8_t* buf) const {
|
|
|
|
return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
|
2014-04-15 23:51:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
size_t AdtsHeader::GetMinFrameSize() const {
|
|
|
|
return kAdtsHeaderMinSize + 1;
|
2014-04-15 23:51:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 23:45:44 +00:00
|
|
|
size_t AdtsHeader::GetSamplesPerFrame() const {
|
|
|
|
const size_t kSamplesPerAacFrame = 1024;
|
|
|
|
return kSamplesPerAacFrame;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool AdtsHeader::Parse(const uint8_t* adts_frame, size_t adts_frame_size) {
|
2014-04-15 23:51:32 +00:00
|
|
|
CHECK(adts_frame);
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
if (adts_frame_size < kAdtsHeaderMinSize)
|
|
|
|
return false;
|
2014-04-15 23:51:32 +00:00
|
|
|
|
|
|
|
BitReader frame(adts_frame, adts_frame_size);
|
|
|
|
// Verify frame starts with sync bits (0xfff).
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t sync;
|
2014-04-15 23:51:32 +00:00
|
|
|
RCHECK(frame.ReadBits(12, &sync));
|
|
|
|
RCHECK(sync == 0xfff);
|
|
|
|
// Skip MPEG version and layer.
|
|
|
|
RCHECK(frame.SkipBits(3));
|
2017-10-23 05:27:27 +00:00
|
|
|
RCHECK(frame.ReadBits(1, &protection_absent_));
|
2014-04-15 23:51:32 +00:00
|
|
|
RCHECK(frame.ReadBits(2, &profile_));
|
|
|
|
RCHECK(frame.ReadBits(4, &sampling_frequency_index_));
|
|
|
|
RCHECK(sampling_frequency_index_ < kAdtsFrequencyTableSize);
|
|
|
|
// Skip private stream bit.
|
|
|
|
RCHECK(frame.SkipBits(1));
|
|
|
|
RCHECK(frame.ReadBits(3, &channel_configuration_));
|
2019-10-01 17:15:58 +00:00
|
|
|
RCHECK(channel_configuration_ < kAdtsNumChannelsTableSize);
|
2014-04-15 23:51:32 +00:00
|
|
|
// Skip originality, home and copyright info.
|
|
|
|
RCHECK(frame.SkipBits(4));
|
2017-10-23 05:27:27 +00:00
|
|
|
RCHECK(frame.ReadBits(13, &frame_size_));
|
2014-04-15 23:51:32 +00:00
|
|
|
// Skip buffer fullness indicator.
|
|
|
|
RCHECK(frame.SkipBits(11));
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t num_blocks_minus_1;
|
2014-04-15 23:51:32 +00:00
|
|
|
RCHECK(frame.ReadBits(2, &num_blocks_minus_1));
|
|
|
|
if (num_blocks_minus_1) {
|
|
|
|
NOTIMPLEMENTED() << "ADTS frames with more than one data block "
|
|
|
|
"not supported.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
size_t AdtsHeader::GetHeaderSize() const {
|
|
|
|
const size_t kCrcSize = sizeof(uint16_t);
|
|
|
|
return kAdtsHeaderMinSize + (protection_absent_ ? 0 : kCrcSize);
|
|
|
|
}
|
2014-04-15 23:51:32 +00:00
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
size_t AdtsHeader::GetFrameSize() const {
|
|
|
|
return frame_size_;
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:15:58 +00:00
|
|
|
size_t AdtsHeader::GetFrameSizeWithoutParsing(const uint8_t* data,
|
|
|
|
size_t num_bytes) const {
|
|
|
|
DCHECK_GT(num_bytes, static_cast<size_t>(5));
|
|
|
|
return ((static_cast<int>(data[5]) >> 5) | (static_cast<int>(data[4]) << 3) |
|
|
|
|
((static_cast<int>(data[3]) & 0x3) << 11));
|
|
|
|
}
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
void AdtsHeader::GetAudioSpecificConfig(std::vector<uint8_t>* buffer) const {
|
|
|
|
DCHECK(buffer);
|
|
|
|
buffer->clear();
|
|
|
|
BitWriter config(buffer);
|
|
|
|
config.WriteBits(GetObjectType(), 5);
|
|
|
|
config.WriteBits(sampling_frequency_index_, 4);
|
|
|
|
config.WriteBits(channel_configuration_, 4);
|
|
|
|
config.Flush();
|
2014-04-15 23:51:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t AdtsHeader::GetObjectType() const {
|
2014-04-15 23:51:32 +00:00
|
|
|
return profile_ + 1;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t AdtsHeader::GetSamplingFrequency() const {
|
2014-04-15 23:51:32 +00:00
|
|
|
DCHECK_LT(sampling_frequency_index_, kAdtsFrequencyTableSize);
|
|
|
|
return kAdtsFrequencyTable[sampling_frequency_index_];
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t AdtsHeader::GetNumChannels() const {
|
2014-04-15 23:51:32 +00:00
|
|
|
DCHECK_LT(channel_configuration_, kAdtsNumChannelsTableSize);
|
|
|
|
return kAdtsNumChannelsTable[channel_configuration_];
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|