2014-04-01 01:34:59 +00:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
#include "packager/media/formats/mp2t/es_parser_audio.h"
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-08-19 22:32:27 +00:00
|
|
|
#include <algorithm>
|
2014-04-01 01:34:59 +00:00
|
|
|
#include <list>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/media/base/audio_timestamp_helper.h"
|
|
|
|
#include "packager/media/base/bit_reader.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/timestamp.h"
|
2017-10-23 05:38:55 +00:00
|
|
|
#include "packager/media/formats/mp2t/ac3_header.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/adts_header.h"
|
|
|
|
#include "packager/media/formats/mp2t/mp2t_common.h"
|
2020-06-03 00:32:19 +00:00
|
|
|
#include "packager/media/formats/mp2t/mpeg1_header.h"
|
2017-10-23 05:38:55 +00:00
|
|
|
#include "packager/media/formats/mp2t/ts_stream_type.h"
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-01 01:34:59 +00:00
|
|
|
namespace media {
|
2017-10-23 05:27:27 +00:00
|
|
|
namespace mp2t {
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
// Look for a syncword.
|
2014-04-01 01:34:59 +00:00
|
|
|
// |new_pos| returns
|
2017-10-23 05:27:27 +00:00
|
|
|
// - either the byte position of the frame (if found)
|
2014-04-01 01:34:59 +00:00
|
|
|
// - or the byte position of 1st byte that was not processed (if not found).
|
|
|
|
// In every case, the returned value in |new_pos| is such that new_pos >= pos
|
2017-10-23 05:27:27 +00:00
|
|
|
// |audio_header| is updated with the new audio frame info if a syncword is
|
|
|
|
// found.
|
2014-04-01 01:34:59 +00:00
|
|
|
// Return whether a syncword was found.
|
2014-09-30 21:52:21 +00:00
|
|
|
static bool LookForSyncWord(const uint8_t* raw_es,
|
|
|
|
int raw_es_size,
|
2014-04-01 01:34:59 +00:00
|
|
|
int pos,
|
2014-09-30 21:52:21 +00:00
|
|
|
int* new_pos,
|
2017-10-23 05:27:27 +00:00
|
|
|
AudioHeader* audio_header) {
|
2014-04-01 01:34:59 +00:00
|
|
|
DCHECK_GE(pos, 0);
|
|
|
|
DCHECK_LE(pos, raw_es_size);
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
const int max_offset =
|
|
|
|
raw_es_size - static_cast<int>(audio_header->GetMinFrameSize());
|
2014-04-01 01:34:59 +00:00
|
|
|
if (pos >= max_offset) {
|
|
|
|
// Do not change the position if:
|
|
|
|
// - max_offset < 0: not enough bytes to get a full header
|
|
|
|
// Since pos >= 0, this is a subcase of the next condition.
|
|
|
|
// - pos >= max_offset: might be the case after reading one full frame,
|
|
|
|
// |pos| is then incremented by the frame size and might then point
|
|
|
|
// to the end of the buffer.
|
|
|
|
*new_pos = pos;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int offset = pos; offset < max_offset; offset++) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* cur_buf = &raw_es[offset];
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
if (!audio_header->IsSyncWord(cur_buf))
|
2014-04-01 01:34:59 +00:00
|
|
|
continue;
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
const size_t remaining_size = static_cast<size_t>(raw_es_size - offset);
|
|
|
|
const int kSyncWordSize = 2;
|
2019-10-01 17:15:58 +00:00
|
|
|
const size_t frame_size =
|
|
|
|
audio_header->GetFrameSizeWithoutParsing(cur_buf, remaining_size);
|
|
|
|
if (frame_size < audio_header->GetMinFrameSize())
|
|
|
|
// Too short to be a valid frame.
|
|
|
|
continue;
|
|
|
|
if (remaining_size < frame_size)
|
|
|
|
// Not a full frame: will resume when we have more data.
|
|
|
|
return false;
|
|
|
|
// Check whether there is another frame |size| apart from the current one.
|
|
|
|
if (remaining_size >= frame_size + kSyncWordSize &&
|
|
|
|
!audio_header->IsSyncWord(&cur_buf[frame_size])) {
|
2014-04-01 01:34:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:15:58 +00:00
|
|
|
if (!audio_header->Parse(cur_buf, frame_size))
|
|
|
|
continue;
|
|
|
|
|
2014-04-01 01:34:59 +00:00
|
|
|
*new_pos = offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*new_pos = max_offset;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
EsParserAudio::EsParserAudio(uint32_t pid,
|
2017-10-23 05:38:55 +00:00
|
|
|
TsStreamType stream_type,
|
2017-10-23 05:58:52 +00:00
|
|
|
const NewStreamInfoCB& new_stream_info_cb,
|
|
|
|
const EmitSampleCB& emit_sample_cb,
|
|
|
|
bool sbr_in_mimetype)
|
2014-04-10 19:57:10 +00:00
|
|
|
: EsParser(pid),
|
2017-10-23 05:38:55 +00:00
|
|
|
stream_type_(stream_type),
|
2014-04-10 19:57:10 +00:00
|
|
|
new_stream_info_cb_(new_stream_info_cb),
|
2014-04-07 17:48:25 +00:00
|
|
|
emit_sample_cb_(emit_sample_cb),
|
2017-10-23 05:38:55 +00:00
|
|
|
sbr_in_mimetype_(sbr_in_mimetype) {
|
|
|
|
if (stream_type == TsStreamType::kAc3) {
|
|
|
|
audio_header_.reset(new Ac3Header);
|
2020-06-03 00:32:19 +00:00
|
|
|
} else if (stream_type == TsStreamType::kMpeg1Audio) {
|
|
|
|
audio_header_.reset(new Mpeg1Header);
|
2017-10-23 05:38:55 +00:00
|
|
|
} else {
|
|
|
|
DCHECK_EQ(stream_type, TsStreamType::kAdtsAac);
|
|
|
|
audio_header_.reset(new AdtsHeader);
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
EsParserAudio::~EsParserAudio() {}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
bool EsParserAudio::Parse(const uint8_t* buf,
|
|
|
|
int size,
|
|
|
|
int64_t pts,
|
|
|
|
int64_t dts) {
|
2014-04-01 01:34:59 +00:00
|
|
|
int raw_es_size;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* raw_es;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// The incoming PTS applies to the access unit that comes just after
|
|
|
|
// the beginning of |buf|.
|
2014-04-07 17:48:25 +00:00
|
|
|
if (pts != kNoTimestamp) {
|
2014-04-01 01:34:59 +00:00
|
|
|
es_byte_queue_.Peek(&raw_es, &raw_es_size);
|
|
|
|
pts_list_.push_back(EsPts(raw_es_size, pts));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the input data to the ES buffer.
|
2016-11-09 02:11:13 +00:00
|
|
|
es_byte_queue_.Push(buf, static_cast<int>(size));
|
2014-04-01 01:34:59 +00:00
|
|
|
es_byte_queue_.Peek(&raw_es, &raw_es_size);
|
|
|
|
|
2017-10-23 05:27:27 +00:00
|
|
|
// Look for every frame in the ES buffer starting at offset = 0
|
2014-04-01 01:34:59 +00:00
|
|
|
int es_position = 0;
|
2017-10-23 05:27:27 +00:00
|
|
|
while (LookForSyncWord(raw_es, raw_es_size, es_position, &es_position,
|
|
|
|
audio_header_.get())) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* frame_ptr = raw_es + es_position;
|
2017-10-23 05:27:27 +00:00
|
|
|
DVLOG(LOG_LEVEL_ES) << "syncword @ pos=" << es_position
|
|
|
|
<< " frame_size=" << audio_header_->GetFrameSize();
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "header: "
|
|
|
|
<< base::HexEncode(frame_ptr,
|
|
|
|
audio_header_->GetHeaderSize());
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Do not process the frame if this one is a partial frame.
|
|
|
|
int remaining_size = raw_es_size - es_position;
|
2017-10-23 05:27:27 +00:00
|
|
|
if (static_cast<int>(audio_header_->GetFrameSize()) > remaining_size)
|
2014-04-01 01:34:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Update the audio configuration if needed.
|
2017-10-23 05:27:27 +00:00
|
|
|
if (!UpdateAudioConfiguration(*audio_header_))
|
2014-04-01 01:34:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the PTS & the duration of this access unit.
|
2017-10-23 05:58:52 +00:00
|
|
|
while (!pts_list_.empty() && pts_list_.front().first <= es_position) {
|
2014-04-01 01:34:59 +00:00
|
|
|
audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
|
|
|
|
pts_list_.pop_front();
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
|
2017-11-06 23:45:44 +00:00
|
|
|
int64_t frame_duration = audio_timestamp_helper_->GetFrameDuration(
|
|
|
|
audio_header_->GetSamplesPerFrame());
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Emit an audio frame.
|
|
|
|
bool is_key_frame = true;
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample = MediaSample::CopyFrom(
|
2017-10-23 05:27:27 +00:00
|
|
|
frame_ptr + audio_header_->GetHeaderSize(),
|
|
|
|
audio_header_->GetFrameSize() - audio_header_->GetHeaderSize(),
|
|
|
|
is_key_frame);
|
2014-04-07 17:48:25 +00:00
|
|
|
sample->set_pts(current_pts);
|
|
|
|
sample->set_dts(current_pts);
|
|
|
|
sample->set_duration(frame_duration);
|
2020-10-16 21:18:35 +00:00
|
|
|
emit_sample_cb_.Run(sample);
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Update the PTS of the next frame.
|
2017-11-06 23:45:44 +00:00
|
|
|
audio_timestamp_helper_->AddFrames(audio_header_->GetSamplesPerFrame());
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Skip the current frame.
|
2017-10-23 05:27:27 +00:00
|
|
|
es_position += static_cast<int>(audio_header_->GetFrameSize());
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discard all the bytes that have been processed.
|
|
|
|
DiscardEs(es_position);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-10 23:03:41 +00:00
|
|
|
bool EsParserAudio::Flush() {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
void EsParserAudio::Reset() {
|
2014-04-01 01:34:59 +00:00
|
|
|
es_byte_queue_.Reset();
|
|
|
|
pts_list_.clear();
|
2017-01-24 00:55:02 +00:00
|
|
|
last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
bool EsParserAudio::UpdateAudioConfiguration(const AudioHeader& audio_header) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t kAacSampleSizeBits(16);
|
2014-04-15 23:51:32 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> audio_specific_config;
|
2017-10-23 05:27:27 +00:00
|
|
|
audio_header.GetAudioSpecificConfig(&audio_specific_config);
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2014-04-15 23:51:32 +00:00
|
|
|
if (last_audio_decoder_config_) {
|
|
|
|
// Verify that the audio decoder config has not changed.
|
2016-06-27 19:30:32 +00:00
|
|
|
if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
|
2014-04-15 23:51:32 +00:00
|
|
|
// Audio configuration has not changed.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NOTIMPLEMENTED() << "Varying audio configurations are not supported.";
|
2014-04-01 01:34:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.)
|
2017-10-23 05:27:27 +00:00
|
|
|
int samples_per_second = audio_header.GetSamplingFrequency();
|
|
|
|
// TODO(kqyang): Review if it makes sense to have |sbr_in_mimetype_| in
|
|
|
|
// es_parser.
|
2017-10-23 05:58:52 +00:00
|
|
|
int extended_samples_per_second =
|
|
|
|
sbr_in_mimetype_ ? std::min(2 * samples_per_second, 48000)
|
|
|
|
: samples_per_second;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-10-23 05:38:55 +00:00
|
|
|
const Codec codec =
|
2020-06-03 00:32:19 +00:00
|
|
|
stream_type_ == TsStreamType::kAc3
|
|
|
|
? kCodecAC3
|
|
|
|
: (stream_type_ == TsStreamType::kMpeg1Audio ? kCodecMP3 : kCodecAAC);
|
2017-01-24 00:55:02 +00:00
|
|
|
last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
|
2017-10-23 05:27:27 +00:00
|
|
|
pid(), kMpeg2Timescale, kInfiniteDuration, codec,
|
|
|
|
AudioStreamInfo::GetCodecString(codec, audio_header.GetObjectType()),
|
2016-07-27 00:51:08 +00:00
|
|
|
audio_specific_config.data(), audio_specific_config.size(),
|
2017-10-23 05:27:27 +00:00
|
|
|
kAacSampleSizeBits, audio_header.GetNumChannels(),
|
2016-07-27 00:51:08 +00:00
|
|
|
extended_samples_per_second, 0 /* seek preroll */, 0 /* codec delay */,
|
2017-01-24 00:55:02 +00:00
|
|
|
0 /* max bitrate */, 0 /* avg bitrate */, std::string(), false);
|
2014-04-07 17:48:25 +00:00
|
|
|
|
|
|
|
DVLOG(1) << "Sampling frequency: " << samples_per_second;
|
|
|
|
DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
|
2017-10-23 05:27:27 +00:00
|
|
|
DVLOG(1) << "Channel config: "
|
|
|
|
<< static_cast<int>(audio_header.GetNumChannels());
|
|
|
|
DVLOG(1) << "Object type: " << static_cast<int>(audio_header.GetObjectType());
|
2014-04-07 17:48:25 +00:00
|
|
|
// Reset the timestamp helper to use a new sampling frequency.
|
|
|
|
if (audio_timestamp_helper_) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
|
2014-04-07 17:48:25 +00:00
|
|
|
audio_timestamp_helper_.reset(
|
|
|
|
new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
|
|
|
|
audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
|
|
|
|
} else {
|
|
|
|
audio_timestamp_helper_.reset(
|
2014-04-15 23:51:32 +00:00
|
|
|
new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 17:48:25 +00:00
|
|
|
// Audio config notification.
|
2014-04-10 19:57:10 +00:00
|
|
|
new_stream_info_cb_.Run(last_audio_decoder_config_);
|
2014-04-07 17:48:25 +00:00
|
|
|
|
2014-04-01 01:34:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-23 05:58:52 +00:00
|
|
|
void EsParserAudio::DiscardEs(int nbytes) {
|
2014-04-01 01:34:59 +00:00
|
|
|
DCHECK_GE(nbytes, 0);
|
|
|
|
if (nbytes <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Adjust the ES position of each PTS.
|
|
|
|
for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
|
|
|
|
it->first -= nbytes;
|
|
|
|
|
|
|
|
// Discard |nbytes| of ES.
|
|
|
|
es_byte_queue_.Pop(nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|