5 #include "packager/media/formats/mp2t/es_parser_audio.h" 12 #include "packager/base/logging.h" 13 #include "packager/base/strings/string_number_conversions.h" 14 #include "packager/media/base/audio_timestamp_helper.h" 15 #include "packager/media/base/bit_reader.h" 16 #include "packager/media/base/media_sample.h" 17 #include "packager/media/base/timestamp.h" 18 #include "packager/media/formats/mp2t/ac3_header.h" 19 #include "packager/media/formats/mp2t/adts_header.h" 20 #include "packager/media/formats/mp2t/mp2t_common.h" 21 #include "packager/media/formats/mp2t/ts_stream_type.h" 35 static bool LookForSyncWord(
const uint8_t* raw_es,
39 AudioHeader* audio_header) {
41 DCHECK_LE(pos, raw_es_size);
43 const int max_offset =
44 raw_es_size -
static_cast<int>(audio_header->GetMinFrameSize());
45 if (pos >= max_offset) {
56 for (
int offset = pos; offset < max_offset; offset++) {
57 const uint8_t* cur_buf = &raw_es[offset];
59 if (!audio_header->IsSyncWord(cur_buf))
62 const size_t remaining_size =
static_cast<size_t>(raw_es_size - offset);
63 const int kSyncWordSize = 2;
64 const size_t frame_size =
65 audio_header->GetFrameSizeWithoutParsing(cur_buf, remaining_size);
66 if (frame_size < audio_header->GetMinFrameSize())
69 if (remaining_size < frame_size)
73 if (remaining_size >= frame_size + kSyncWordSize &&
74 !audio_header->IsSyncWord(&cur_buf[frame_size])) {
78 if (!audio_header->Parse(cur_buf, frame_size))
85 *new_pos = max_offset;
89 EsParserAudio::EsParserAudio(uint32_t pid,
90 TsStreamType stream_type,
91 const NewStreamInfoCB& new_stream_info_cb,
92 const EmitSampleCB& emit_sample_cb,
95 stream_type_(stream_type),
96 new_stream_info_cb_(new_stream_info_cb),
97 emit_sample_cb_(emit_sample_cb),
98 sbr_in_mimetype_(sbr_in_mimetype) {
99 if (stream_type == TsStreamType::kAc3) {
100 audio_header_.reset(
new Ac3Header);
102 DCHECK_EQ(stream_type, TsStreamType::kAdtsAac);
103 audio_header_.reset(
new AdtsHeader);
107 EsParserAudio::~EsParserAudio() {}
109 bool EsParserAudio::Parse(
const uint8_t* buf,
114 const uint8_t* raw_es;
118 if (pts != kNoTimestamp) {
119 es_byte_queue_.Peek(&raw_es, &raw_es_size);
120 pts_list_.push_back(EsPts(raw_es_size, pts));
124 es_byte_queue_.Push(buf, static_cast<int>(size));
125 es_byte_queue_.Peek(&raw_es, &raw_es_size);
129 while (LookForSyncWord(raw_es, raw_es_size, es_position, &es_position,
130 audio_header_.get())) {
131 const uint8_t* frame_ptr = raw_es + es_position;
132 DVLOG(LOG_LEVEL_ES) <<
"syncword @ pos=" << es_position
133 <<
" frame_size=" << audio_header_->GetFrameSize();
134 DVLOG(LOG_LEVEL_ES) <<
"header: " 135 << base::HexEncode(frame_ptr,
136 audio_header_->GetHeaderSize());
139 int remaining_size = raw_es_size - es_position;
140 if (static_cast<int>(audio_header_->GetFrameSize()) > remaining_size)
144 if (!UpdateAudioConfiguration(*audio_header_))
148 while (!pts_list_.empty() && pts_list_.front().first <= es_position) {
149 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
150 pts_list_.pop_front();
153 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
154 int64_t frame_duration = audio_timestamp_helper_->GetFrameDuration(
155 audio_header_->GetSamplesPerFrame());
158 bool is_key_frame =
true;
161 frame_ptr + audio_header_->GetHeaderSize(),
162 audio_header_->GetFrameSize() - audio_header_->GetHeaderSize(),
164 sample->set_pts(current_pts);
165 sample->set_dts(current_pts);
166 sample->set_duration(frame_duration);
167 emit_sample_cb_.Run(pid(), sample);
170 audio_timestamp_helper_->AddFrames(audio_header_->GetSamplesPerFrame());
173 es_position +=
static_cast<int>(audio_header_->GetFrameSize());
177 DiscardEs(es_position);
182 void EsParserAudio::Flush() {}
184 void EsParserAudio::Reset() {
185 es_byte_queue_.Reset();
187 last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
190 bool EsParserAudio::UpdateAudioConfiguration(
const AudioHeader& audio_header) {
191 const uint8_t kAacSampleSizeBits(16);
193 std::vector<uint8_t> audio_specific_config;
194 audio_header.GetAudioSpecificConfig(&audio_specific_config);
196 if (last_audio_decoder_config_) {
198 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
202 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
209 int samples_per_second = audio_header.GetSamplingFrequency();
212 int extended_samples_per_second =
213 sbr_in_mimetype_ ? std::min(2 * samples_per_second, 48000)
214 : samples_per_second;
217 stream_type_ == TsStreamType::kAc3 ? kCodecAC3 : kCodecAAC;
218 last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
219 pid(), kMpeg2Timescale, kInfiniteDuration, codec,
221 audio_specific_config.data(), audio_specific_config.size(),
222 kAacSampleSizeBits, audio_header.GetNumChannels(),
223 extended_samples_per_second, 0 , 0 ,
224 0 , 0 , std::string(),
false);
226 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
227 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
228 DVLOG(1) <<
"Channel config: " 229 <<
static_cast<int>(audio_header.GetNumChannels());
230 DVLOG(1) <<
"Object type: " <<
static_cast<int>(audio_header.GetObjectType());
232 if (audio_timestamp_helper_) {
233 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
234 audio_timestamp_helper_.reset(
235 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
236 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
238 audio_timestamp_helper_.reset(
239 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
243 new_stream_info_cb_.Run(last_audio_decoder_config_);
248 void EsParserAudio::DiscardEs(
int nbytes) {
249 DCHECK_GE(nbytes, 0);
254 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
258 es_byte_queue_.Pop(nbytes);
All the methods that are virtual are virtual for mocking.