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/mpeg1_header.h"
22 #include "packager/media/formats/mp2t/ts_stream_type.h"
36 static bool LookForSyncWord(
const uint8_t* raw_es,
40 AudioHeader* audio_header) {
42 DCHECK_LE(pos, raw_es_size);
44 const int max_offset =
45 raw_es_size -
static_cast<int>(audio_header->GetMinFrameSize());
46 if (pos >= max_offset) {
57 for (
int offset = pos; offset < max_offset; offset++) {
58 const uint8_t* cur_buf = &raw_es[offset];
60 if (!audio_header->IsSyncWord(cur_buf))
63 const size_t remaining_size =
static_cast<size_t>(raw_es_size - offset);
64 const int kSyncWordSize = 2;
65 const size_t frame_size =
66 audio_header->GetFrameSizeWithoutParsing(cur_buf, remaining_size);
67 if (frame_size < audio_header->GetMinFrameSize())
70 if (remaining_size < frame_size)
74 if (remaining_size >= frame_size + kSyncWordSize &&
75 !audio_header->IsSyncWord(&cur_buf[frame_size])) {
79 if (!audio_header->Parse(cur_buf, frame_size))
86 *new_pos = max_offset;
90 EsParserAudio::EsParserAudio(uint32_t pid,
91 TsStreamType stream_type,
92 const NewStreamInfoCB& new_stream_info_cb,
93 const EmitSampleCB& emit_sample_cb,
96 stream_type_(stream_type),
97 new_stream_info_cb_(new_stream_info_cb),
98 emit_sample_cb_(emit_sample_cb),
99 sbr_in_mimetype_(sbr_in_mimetype) {
100 if (stream_type == TsStreamType::kAc3) {
101 audio_header_.reset(
new Ac3Header);
102 }
else if (stream_type == TsStreamType::kMpeg1Audio) {
103 audio_header_.reset(
new Mpeg1Header);
105 DCHECK_EQ(stream_type, TsStreamType::kAdtsAac);
106 audio_header_.reset(
new AdtsHeader);
110 EsParserAudio::~EsParserAudio() {}
112 bool EsParserAudio::Parse(
const uint8_t* buf,
117 const uint8_t* raw_es;
121 if (pts != kNoTimestamp) {
122 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
123 pts_list_.push_back(EsPts(raw_es_size, pts));
127 es_byte_queue_.
Push(buf,
static_cast<int>(size));
128 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
132 while (LookForSyncWord(raw_es, raw_es_size, es_position, &es_position,
133 audio_header_.get())) {
134 const uint8_t* frame_ptr = raw_es + es_position;
135 DVLOG(LOG_LEVEL_ES) <<
"syncword @ pos=" << es_position
136 <<
" frame_size=" << audio_header_->GetFrameSize();
137 DVLOG(LOG_LEVEL_ES) <<
"header: "
138 << base::HexEncode(frame_ptr,
139 audio_header_->GetHeaderSize());
142 int remaining_size = raw_es_size - es_position;
143 if (
static_cast<int>(audio_header_->GetFrameSize()) > remaining_size)
147 if (!UpdateAudioConfiguration(*audio_header_))
151 while (!pts_list_.empty() && pts_list_.front().first <= es_position) {
152 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
153 pts_list_.pop_front();
156 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
157 int64_t frame_duration = audio_timestamp_helper_->GetFrameDuration(
158 audio_header_->GetSamplesPerFrame());
161 bool is_key_frame =
true;
164 frame_ptr + audio_header_->GetHeaderSize(),
165 audio_header_->GetFrameSize() - audio_header_->GetHeaderSize(),
167 sample->set_pts(current_pts);
168 sample->set_dts(current_pts);
169 sample->set_duration(frame_duration);
170 emit_sample_cb_.Run(sample);
173 audio_timestamp_helper_->AddFrames(audio_header_->GetSamplesPerFrame());
176 es_position +=
static_cast<int>(audio_header_->GetFrameSize());
180 DiscardEs(es_position);
185 bool EsParserAudio::Flush() {
189 void EsParserAudio::Reset() {
190 es_byte_queue_.
Reset();
192 last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
195 bool EsParserAudio::UpdateAudioConfiguration(
const AudioHeader& audio_header) {
196 const uint8_t kAacSampleSizeBits(16);
198 std::vector<uint8_t> audio_specific_config;
199 audio_header.GetAudioSpecificConfig(&audio_specific_config);
201 if (last_audio_decoder_config_) {
203 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
207 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
214 int samples_per_second = audio_header.GetSamplingFrequency();
217 int extended_samples_per_second =
218 sbr_in_mimetype_ ? std::min(2 * samples_per_second, 48000)
219 : samples_per_second;
222 stream_type_ == TsStreamType::kAc3
224 : (stream_type_ == TsStreamType::kMpeg1Audio ? kCodecMP3 : kCodecAAC);
225 last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
226 pid(), kMpeg2Timescale, kInfiniteDuration, codec,
228 audio_specific_config.data(), audio_specific_config.size(),
229 kAacSampleSizeBits, audio_header.GetNumChannels(),
230 extended_samples_per_second, 0 , 0 ,
231 0 , 0 , std::string(),
false);
233 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
234 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
235 DVLOG(1) <<
"Channel config: "
236 <<
static_cast<int>(audio_header.GetNumChannels());
237 DVLOG(1) <<
"Object type: " <<
static_cast<int>(audio_header.GetObjectType());
239 if (audio_timestamp_helper_) {
240 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
241 audio_timestamp_helper_.reset(
242 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
243 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
245 audio_timestamp_helper_.reset(
246 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
250 new_stream_info_cb_.Run(last_audio_decoder_config_);
255 void EsParserAudio::DiscardEs(
int nbytes) {
256 DCHECK_GE(nbytes, 0);
261 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
265 es_byte_queue_.
Pop(nbytes);