5 #include <packager/media/formats/mp2t/es_parser_audio.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
13 #include <absl/strings/escaping.h>
14 #include <absl/strings/numbers.h>
16 #include <packager/macros/logging.h>
17 #include <packager/media/base/audio_timestamp_helper.h>
18 #include <packager/media/base/bit_reader.h>
19 #include <packager/media/base/media_sample.h>
20 #include <packager/media/base/timestamp.h>
21 #include <packager/media/formats/mp2t/ac3_header.h>
22 #include <packager/media/formats/mp2t/adts_header.h>
23 #include <packager/media/formats/mp2t/mp2t_common.h>
24 #include <packager/media/formats/mp2t/mpeg1_header.h>
25 #include <packager/media/formats/mp2t/ts_stream_type.h>
39 static bool LookForSyncWord(
const uint8_t* raw_es,
43 AudioHeader* audio_header) {
45 DCHECK_LE(pos, raw_es_size);
47 const int max_offset =
48 raw_es_size -
static_cast<int>(audio_header->GetMinFrameSize());
49 if (pos >= max_offset) {
60 for (
int offset = pos; offset < max_offset; offset++) {
61 const uint8_t* cur_buf = &raw_es[offset];
63 if (!audio_header->IsSyncWord(cur_buf))
66 const size_t remaining_size =
static_cast<size_t>(raw_es_size - offset);
67 const int kSyncWordSize = 2;
68 const size_t frame_size =
69 audio_header->GetFrameSizeWithoutParsing(cur_buf, remaining_size);
70 if (frame_size < audio_header->GetMinFrameSize())
73 if (remaining_size < frame_size)
77 if (remaining_size >= frame_size + kSyncWordSize &&
78 !audio_header->IsSyncWord(&cur_buf[frame_size])) {
82 if (!audio_header->Parse(cur_buf, frame_size))
89 *new_pos = max_offset;
93 EsParserAudio::EsParserAudio(uint32_t pid,
94 TsStreamType stream_type,
95 const NewStreamInfoCB& new_stream_info_cb,
96 const EmitSampleCB& emit_sample_cb,
99 stream_type_(stream_type),
100 new_stream_info_cb_(new_stream_info_cb),
101 emit_sample_cb_(emit_sample_cb),
102 sbr_in_mimetype_(sbr_in_mimetype) {
103 if (stream_type == TsStreamType::kAc3) {
104 audio_header_.reset(
new Ac3Header);
105 }
else if (stream_type == TsStreamType::kMpeg1Audio) {
106 audio_header_.reset(
new Mpeg1Header);
108 DCHECK_EQ(
static_cast<int>(stream_type),
109 static_cast<int>(TsStreamType::kAdtsAac));
110 audio_header_.reset(
new AdtsHeader);
114 EsParserAudio::~EsParserAudio() {}
116 bool EsParserAudio::Parse(
const uint8_t* buf,
121 const uint8_t* raw_es;
125 if (pts != kNoTimestamp) {
126 es_byte_queue_.Peek(&raw_es, &raw_es_size);
127 pts_list_.push_back(EsPts(raw_es_size, pts));
131 es_byte_queue_.Push(buf,
static_cast<int>(size));
132 es_byte_queue_.Peek(&raw_es, &raw_es_size);
136 while (LookForSyncWord(raw_es, raw_es_size, es_position, &es_position,
137 audio_header_.get())) {
138 const uint8_t* frame_ptr = raw_es + es_position;
139 DVLOG(LOG_LEVEL_ES) <<
"syncword @ pos=" << es_position
140 <<
" frame_size=" << audio_header_->GetFrameSize();
141 DVLOG(LOG_LEVEL_ES) <<
"header: "
142 << absl::BytesToHexString(absl::string_view(
143 reinterpret_cast<const char*
>(frame_ptr),
144 audio_header_->GetHeaderSize()));
147 int remaining_size = raw_es_size - es_position;
148 if (
static_cast<int>(audio_header_->GetFrameSize()) > remaining_size)
152 if (!UpdateAudioConfiguration(*audio_header_))
156 while (!pts_list_.empty() && pts_list_.front().first <= es_position) {
157 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
158 pts_list_.pop_front();
161 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
162 int64_t frame_duration = audio_timestamp_helper_->GetFrameDuration(
163 audio_header_->GetSamplesPerFrame());
166 bool is_key_frame =
true;
168 std::shared_ptr<MediaSample> sample = MediaSample::CopyFrom(
169 frame_ptr + audio_header_->GetHeaderSize(),
170 audio_header_->GetFrameSize() - audio_header_->GetHeaderSize(),
172 sample->set_pts(current_pts);
173 sample->set_dts(current_pts);
174 sample->set_duration(frame_duration);
175 emit_sample_cb_(sample);
178 audio_timestamp_helper_->AddFrames(audio_header_->GetSamplesPerFrame());
181 es_position +=
static_cast<int>(audio_header_->GetFrameSize());
185 DiscardEs(es_position);
190 bool EsParserAudio::Flush() {
194 void EsParserAudio::Reset() {
195 es_byte_queue_.Reset();
197 last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
200 bool EsParserAudio::UpdateAudioConfiguration(
const AudioHeader& audio_header) {
201 const uint8_t kAacSampleSizeBits(16);
203 std::vector<uint8_t> audio_specific_config;
204 audio_header.GetAudioSpecificConfig(&audio_specific_config);
206 if (last_audio_decoder_config_) {
208 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
212 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
219 int samples_per_second = audio_header.GetSamplingFrequency();
222 int extended_samples_per_second =
223 sbr_in_mimetype_ ? std::min(2 * samples_per_second, 48000)
224 : samples_per_second;
227 stream_type_ == TsStreamType::kAc3
229 : (stream_type_ == TsStreamType::kMpeg1Audio ? kCodecMP3 : kCodecAAC);
230 last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
231 pid(), kMpeg2Timescale, kInfiniteDuration, codec,
232 AudioStreamInfo::GetCodecString(codec, audio_header.GetObjectType()),
233 audio_specific_config.data(), audio_specific_config.size(),
234 kAacSampleSizeBits, audio_header.GetNumChannels(),
235 extended_samples_per_second, 0 , 0 ,
236 0 , 0 , std::string(),
false);
238 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
239 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
240 DVLOG(1) <<
"Channel config: "
241 <<
static_cast<int>(audio_header.GetNumChannels());
242 DVLOG(1) <<
"Object type: " <<
static_cast<int>(audio_header.GetObjectType());
244 if (audio_timestamp_helper_) {
245 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
246 audio_timestamp_helper_.reset(
247 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
248 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
250 audio_timestamp_helper_.reset(
251 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
255 new_stream_info_cb_(last_audio_decoder_config_);
260 void EsParserAudio::DiscardEs(
int nbytes) {
261 DCHECK_GE(nbytes, 0);
266 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
270 es_byte_queue_.Pop(nbytes);
All the methods that are virtual are virtual for mocking.