This source file includes following definitions.
- ExtractAdtsFrameSize
- ExtractAdtsFrequencyIndex
- ExtractAdtsChannelConfig
- isAdtsSyncWord
- LookForSyncWord
- sbr_in_mimetype_
- Flush
- Reset
- UpdateAudioConfiguration
- DiscardEs
#include "media/formats/mp2t/es_parser_adts.h"
#include <list>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/bit_reader.h"
#include "media/base/buffers.h"
#include "media/base/channel_layout.h"
#include "media/base/stream_parser_buffer.h"
#include "media/formats/mp2t/mp2t_common.h"
#include "media/formats/mpeg/adts_constants.h"
namespace media {
static int ExtractAdtsFrameSize(const uint8* adts_header) {
return ((static_cast<int>(adts_header[5]) >> 5) |
(static_cast<int>(adts_header[4]) << 3) |
((static_cast<int>(adts_header[3]) & 0x3) << 11));
}
static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) {
return ((adts_header[2] >> 2) & 0xf);
}
static size_t ExtractAdtsChannelConfig(const uint8* adts_header) {
return (((adts_header[3] >> 6) & 0x3) |
((adts_header[2] & 0x1) << 2));
}
static bool isAdtsSyncWord(const uint8* buf) {
return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
}
static bool LookForSyncWord(const uint8* raw_es, int raw_es_size,
int pos,
int* new_pos, int* frame_sz) {
DCHECK_GE(pos, 0);
DCHECK_LE(pos, raw_es_size);
int max_offset = raw_es_size - kADTSHeaderMinSize;
if (pos >= max_offset) {
*new_pos = pos;
return false;
}
for (int offset = pos; offset < max_offset; offset++) {
const uint8* cur_buf = &raw_es[offset];
if (!isAdtsSyncWord(cur_buf))
continue;
int frame_size = ExtractAdtsFrameSize(cur_buf);
if (frame_size < kADTSHeaderMinSize) {
continue;
}
int remaining_size = raw_es_size - offset;
if (remaining_size >= frame_size + 2 &&
!isAdtsSyncWord(&cur_buf[frame_size])) {
continue;
}
*new_pos = offset;
*frame_sz = frame_size;
return true;
}
*new_pos = max_offset;
return false;
}
namespace mp2t {
EsParserAdts::EsParserAdts(
const NewAudioConfigCB& new_audio_config_cb,
const EmitBufferCB& emit_buffer_cb,
bool sbr_in_mimetype)
: new_audio_config_cb_(new_audio_config_cb),
emit_buffer_cb_(emit_buffer_cb),
sbr_in_mimetype_(sbr_in_mimetype) {
}
EsParserAdts::~EsParserAdts() {
}
bool EsParserAdts::Parse(const uint8* buf, int size,
base::TimeDelta pts,
base::TimeDelta dts) {
int raw_es_size;
const uint8* raw_es;
if (pts != kNoTimestamp()) {
es_byte_queue_.Peek(&raw_es, &raw_es_size);
pts_list_.push_back(EsPts(raw_es_size, pts));
}
es_byte_queue_.Push(buf, size);
es_byte_queue_.Peek(&raw_es, &raw_es_size);
int es_position = 0;
int frame_size;
while (LookForSyncWord(raw_es, raw_es_size, es_position,
&es_position, &frame_size)) {
DVLOG(LOG_LEVEL_ES)
<< "ADTS syncword @ pos=" << es_position
<< " frame_size=" << frame_size;
DVLOG(LOG_LEVEL_ES)
<< "ADTS header: "
<< base::HexEncode(&raw_es[es_position], kADTSHeaderMinSize);
int remaining_size = raw_es_size - es_position;
if (frame_size > remaining_size)
break;
DCHECK_GE(frame_size, kADTSHeaderMinSize);
if (!UpdateAudioConfiguration(&raw_es[es_position]))
return false;
while (!pts_list_.empty() &&
pts_list_.front().first <= es_position) {
audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
pts_list_.pop_front();
}
base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
base::TimeDelta frame_duration =
audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
bool is_key_frame = true;
scoped_refptr<StreamParserBuffer> stream_parser_buffer =
StreamParserBuffer::CopyFrom(
&raw_es[es_position],
frame_size,
is_key_frame,
DemuxerStream::AUDIO, 0);
stream_parser_buffer->SetDecodeTimestamp(current_pts);
stream_parser_buffer->set_timestamp(current_pts);
stream_parser_buffer->set_duration(frame_duration);
emit_buffer_cb_.Run(stream_parser_buffer);
audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
es_position += frame_size;
}
DiscardEs(es_position);
return true;
}
void EsParserAdts::Flush() {
}
void EsParserAdts::Reset() {
es_byte_queue_.Reset();
pts_list_.clear();
last_audio_decoder_config_ = AudioDecoderConfig();
}
bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header);
if (frequency_index >= kADTSFrequencyTableSize) {
return false;
}
size_t channel_configuration = ExtractAdtsChannelConfig(adts_header);
if (channel_configuration == 0 ||
channel_configuration >= kADTSChannelLayoutTableSize) {
return false;
}
int samples_per_second = kADTSFrequencyTable[frequency_index];
int adts_profile = (adts_header[2] >> 6) & 0x3;
int extended_samples_per_second = sbr_in_mimetype_
? std::min(2 * samples_per_second, 48000)
: samples_per_second;
uint16 extra_data_int =
((adts_profile + 1) << 11) +
(frequency_index << 7) +
(channel_configuration << 3);
uint8 extra_data[2] = {
static_cast<uint8>(extra_data_int >> 8),
static_cast<uint8>(extra_data_int & 0xff)
};
AudioDecoderConfig audio_decoder_config(
kCodecAAC,
kSampleFormatS16,
kADTSChannelLayoutTable[channel_configuration],
extended_samples_per_second,
extra_data,
arraysize(extra_data),
false);
if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
DVLOG(1) << "Sampling frequency: " << samples_per_second;
DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
DVLOG(1) << "Channel config: " << channel_configuration;
DVLOG(1) << "Adts profile: " << adts_profile;
if (audio_timestamp_helper_) {
base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
audio_timestamp_helper_.reset(
new AudioTimestampHelper(samples_per_second));
audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
} else {
audio_timestamp_helper_.reset(
new AudioTimestampHelper(samples_per_second));
}
last_audio_decoder_config_ = audio_decoder_config;
new_audio_config_cb_.Run(audio_decoder_config);
}
return true;
}
void EsParserAdts::DiscardEs(int nbytes) {
DCHECK_GE(nbytes, 0);
if (nbytes <= 0)
return;
for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
it->first -= nbytes;
es_byte_queue_.Pop(nbytes);
}
}
}