5#include <packager/media/formats/mp2t/es_parser_audio.h>
15#include <absl/log/check.h>
16#include <absl/log/log.h>
17#include <absl/strings/escaping.h>
18#include <absl/strings/string_view.h>
20#include <packager/macros/logging.h>
21#include <packager/media/base/audio_stream_info.h>
22#include <packager/media/base/audio_timestamp_helper.h>
23#include <packager/media/base/media_sample.h>
24#include <packager/media/base/stream_info.h>
25#include <packager/media/base/timestamp.h>
26#include <packager/media/formats/mp2t/ac3_header.h>
27#include <packager/media/formats/mp2t/adts_header.h>
28#include <packager/media/formats/mp2t/es_parser.h>
29#include <packager/media/formats/mp2t/mp2t_common.h>
30#include <packager/media/formats/mp2t/mpeg1_header.h>
31#include <packager/media/formats/mp2t/ts_stream_type.h>
45static bool LookForSyncWord(
const uint8_t* raw_es,
49 AudioHeader* audio_header) {
51 DCHECK_LE(pos, raw_es_size);
53 const int max_offset =
54 raw_es_size -
static_cast<int>(audio_header->GetMinFrameSize());
55 if (pos >= max_offset) {
66 for (
int offset = pos; offset < max_offset; offset++) {
67 const uint8_t* cur_buf = &raw_es[offset];
69 if (!audio_header->IsSyncWord(cur_buf))
72 const size_t remaining_size =
static_cast<size_t>(raw_es_size - offset);
73 const int kSyncWordSize = 2;
74 const size_t frame_size =
75 audio_header->GetFrameSizeWithoutParsing(cur_buf, remaining_size);
76 if (frame_size < audio_header->GetMinFrameSize())
79 if (remaining_size < frame_size)
83 if (remaining_size >= frame_size + kSyncWordSize &&
84 !audio_header->IsSyncWord(&cur_buf[frame_size])) {
88 if (!audio_header->Parse(cur_buf, frame_size))
95 *new_pos = max_offset;
99EsParserAudio::EsParserAudio(uint32_t pid,
100 TsStreamType stream_type,
101 const NewStreamInfoCB& new_stream_info_cb,
102 const EmitSampleCB& emit_sample_cb,
103 bool sbr_in_mimetype)
105 stream_type_(stream_type),
106 new_stream_info_cb_(new_stream_info_cb),
107 emit_sample_cb_(emit_sample_cb),
108 sbr_in_mimetype_(sbr_in_mimetype) {
109 if (stream_type == TsStreamType::kAc3) {
110 audio_header_.reset(
new Ac3Header);
111 }
else if (stream_type == TsStreamType::kMpeg1Audio) {
112 audio_header_.reset(
new Mpeg1Header);
114 DCHECK_EQ(
static_cast<int>(stream_type),
115 static_cast<int>(TsStreamType::kAdtsAac));
116 audio_header_.reset(
new AdtsHeader);
120EsParserAudio::~EsParserAudio() {}
122bool EsParserAudio::Parse(
const uint8_t* buf,
127 const uint8_t* raw_es;
131 if (pts != kNoTimestamp) {
132 es_byte_queue_.Peek(&raw_es, &raw_es_size);
133 pts_list_.push_back(EsPts(raw_es_size, pts));
137 es_byte_queue_.Push(buf,
static_cast<int>(size));
138 es_byte_queue_.Peek(&raw_es, &raw_es_size);
142 while (LookForSyncWord(raw_es, raw_es_size, es_position, &es_position,
143 audio_header_.get())) {
144 const uint8_t* frame_ptr = raw_es + es_position;
145 DVLOG(LOG_LEVEL_ES) <<
"syncword @ pos=" << es_position
146 <<
" frame_size=" << audio_header_->GetFrameSize();
147 DVLOG(LOG_LEVEL_ES) <<
"header: "
148 << absl::BytesToHexString(absl::string_view(
149 reinterpret_cast<const char*
>(frame_ptr),
150 audio_header_->GetHeaderSize()));
153 int remaining_size = raw_es_size - es_position;
154 if (
static_cast<int>(audio_header_->GetFrameSize()) > remaining_size)
158 if (!UpdateAudioConfiguration(*audio_header_))
162 while (!pts_list_.empty() && pts_list_.front().first <= es_position) {
163 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
164 pts_list_.pop_front();
167 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
168 int64_t frame_duration = audio_timestamp_helper_->GetFrameDuration(
169 audio_header_->GetSamplesPerFrame());
172 bool is_key_frame =
true;
174 std::shared_ptr<MediaSample> sample = MediaSample::CopyFrom(
175 frame_ptr + audio_header_->GetHeaderSize(),
176 audio_header_->GetFrameSize() - audio_header_->GetHeaderSize(),
178 sample->set_pts(current_pts);
179 sample->set_dts(current_pts);
180 sample->set_duration(frame_duration);
181 emit_sample_cb_(sample);
184 audio_timestamp_helper_->AddFrames(audio_header_->GetSamplesPerFrame());
187 es_position +=
static_cast<int>(audio_header_->GetFrameSize());
191 DiscardEs(es_position);
196bool EsParserAudio::Flush() {
200void EsParserAudio::Reset() {
201 es_byte_queue_.Reset();
203 last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
206bool EsParserAudio::UpdateAudioConfiguration(
const AudioHeader& audio_header) {
207 const uint8_t kAacSampleSizeBits(16);
209 std::vector<uint8_t> audio_specific_config;
210 audio_header.GetAudioSpecificConfig(&audio_specific_config);
212 if (last_audio_decoder_config_) {
214 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
218 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
225 int samples_per_second = audio_header.GetSamplingFrequency();
228 int extended_samples_per_second =
229 sbr_in_mimetype_ ? std::min(2 * samples_per_second, 48000)
230 : samples_per_second;
233 stream_type_ == TsStreamType::kAc3
235 : (stream_type_ == TsStreamType::kMpeg1Audio ? kCodecMP3 : kCodecAAC);
236 last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
237 pid(), kMpeg2Timescale, kInfiniteDuration, codec,
238 AudioStreamInfo::GetCodecString(codec, audio_header.GetObjectType()),
239 audio_specific_config.data(), audio_specific_config.size(),
240 kAacSampleSizeBits, audio_header.GetNumChannels(),
241 extended_samples_per_second, 0 , 0 ,
242 0 , 0 , std::string(),
false);
244 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
245 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
246 DVLOG(1) <<
"Channel config: "
247 <<
static_cast<int>(audio_header.GetNumChannels());
248 DVLOG(1) <<
"Object type: " <<
static_cast<int>(audio_header.GetObjectType());
250 if (audio_timestamp_helper_) {
251 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
252 audio_timestamp_helper_.reset(
253 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
254 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
256 audio_timestamp_helper_.reset(
257 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
261 new_stream_info_cb_(last_audio_decoder_config_);
266void EsParserAudio::DiscardEs(
int nbytes) {
267 DCHECK_GE(nbytes, 0);
272 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
276 es_byte_queue_.Pop(nbytes);
All the methods that are virtual are virtual for mocking.