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 if (!audio_header->Parse(cur_buf, raw_es_size - offset))
66 const size_t remaining_size =
static_cast<size_t>(raw_es_size - offset);
67 const int kSyncWordSize = 2;
68 if (remaining_size >= audio_header->GetFrameSize() + kSyncWordSize &&
69 !audio_header->IsSyncWord(&cur_buf[audio_header->GetFrameSize()])) {
77 *new_pos = max_offset;
81 EsParserAudio::EsParserAudio(uint32_t pid,
82 TsStreamType stream_type,
83 const NewStreamInfoCB& new_stream_info_cb,
84 const EmitSampleCB& emit_sample_cb,
87 stream_type_(stream_type),
88 new_stream_info_cb_(new_stream_info_cb),
89 emit_sample_cb_(emit_sample_cb),
90 sbr_in_mimetype_(sbr_in_mimetype) {
91 if (stream_type == TsStreamType::kAc3) {
92 audio_header_.reset(
new Ac3Header);
94 DCHECK_EQ(stream_type, TsStreamType::kAdtsAac);
95 audio_header_.reset(
new AdtsHeader);
99 EsParserAudio::~EsParserAudio() {}
101 bool EsParserAudio::Parse(
const uint8_t* buf,
106 const uint8_t* raw_es;
110 if (pts != kNoTimestamp) {
111 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
112 pts_list_.push_back(EsPts(raw_es_size, pts));
116 es_byte_queue_.
Push(buf, static_cast<int>(size));
117 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
121 while (LookForSyncWord(raw_es, raw_es_size, es_position, &es_position,
122 audio_header_.get())) {
123 const uint8_t* frame_ptr = raw_es + es_position;
124 DVLOG(LOG_LEVEL_ES) <<
"syncword @ pos=" << es_position
125 <<
" frame_size=" << audio_header_->GetFrameSize();
126 DVLOG(LOG_LEVEL_ES) <<
"header: "
127 << base::HexEncode(frame_ptr,
128 audio_header_->GetHeaderSize());
131 int remaining_size = raw_es_size - es_position;
132 if (static_cast<int>(audio_header_->GetFrameSize()) > remaining_size)
136 if (!UpdateAudioConfiguration(*audio_header_))
140 while (!pts_list_.empty() && pts_list_.front().first <= es_position) {
141 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
142 pts_list_.pop_front();
145 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
146 int64_t frame_duration = audio_timestamp_helper_->GetFrameDuration(
147 audio_header_->GetSamplesPerFrame());
150 bool is_key_frame =
true;
153 frame_ptr + audio_header_->GetHeaderSize(),
154 audio_header_->GetFrameSize() - audio_header_->GetHeaderSize(),
156 sample->set_pts(current_pts);
157 sample->set_dts(current_pts);
158 sample->set_duration(frame_duration);
159 emit_sample_cb_.Run(pid(), sample);
162 audio_timestamp_helper_->AddFrames(audio_header_->GetSamplesPerFrame());
165 es_position +=
static_cast<int>(audio_header_->GetFrameSize());
169 DiscardEs(es_position);
174 void EsParserAudio::Flush() {}
176 void EsParserAudio::Reset() {
177 es_byte_queue_.
Reset();
179 last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
182 bool EsParserAudio::UpdateAudioConfiguration(
const AudioHeader& audio_header) {
183 const uint8_t kAacSampleSizeBits(16);
185 std::vector<uint8_t> audio_specific_config;
186 audio_header.GetAudioSpecificConfig(&audio_specific_config);
188 if (last_audio_decoder_config_) {
190 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
194 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
201 int samples_per_second = audio_header.GetSamplingFrequency();
204 int extended_samples_per_second =
205 sbr_in_mimetype_ ? std::min(2 * samples_per_second, 48000)
206 : samples_per_second;
209 stream_type_ == TsStreamType::kAc3 ? kCodecAC3 : kCodecAAC;
210 last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
211 pid(), kMpeg2Timescale, kInfiniteDuration, codec,
213 audio_specific_config.data(), audio_specific_config.size(),
214 kAacSampleSizeBits, audio_header.GetNumChannels(),
215 extended_samples_per_second, 0 , 0 ,
216 0 , 0 , std::string(),
false);
218 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
219 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
220 DVLOG(1) <<
"Channel config: "
221 <<
static_cast<int>(audio_header.GetNumChannels());
222 DVLOG(1) <<
"Object type: " <<
static_cast<int>(audio_header.GetObjectType());
224 if (audio_timestamp_helper_) {
225 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
226 audio_timestamp_helper_.reset(
227 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
228 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
230 audio_timestamp_helper_.reset(
231 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
235 new_stream_info_cb_.Run(last_audio_decoder_config_);
240 void EsParserAudio::DiscardEs(
int nbytes) {
241 DCHECK_GE(nbytes, 0);
246 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
250 es_byte_queue_.
Pop(nbytes);