5 #include "packager/media/formats/mp2t/es_parser_adts.h"
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/string_number_conversions.h"
13 #include "packager/media/base/audio_timestamp_helper.h"
14 #include "packager/media/base/bit_reader.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/base/timestamp.h"
17 #include "packager/media/formats/mp2t/adts_header.h"
18 #include "packager/media/formats/mp2t/mp2t_common.h"
19 #include "packager/media/formats/mpeg/adts_constants.h"
21 namespace edash_packager {
26 static bool isAdtsSyncWord(
const uint8_t* buf) {
27 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
37 static bool LookForSyncWord(
const uint8_t* raw_es,
43 DCHECK_LE(pos, raw_es_size);
45 int max_offset = raw_es_size - kAdtsHeaderMinSize;
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 (!isAdtsSyncWord(cur_buf))
67 if (frame_size < kAdtsHeaderMinSize) {
74 int remaining_size = raw_es_size - offset;
75 if (remaining_size >= frame_size + 2 &&
76 !isAdtsSyncWord(&cur_buf[frame_size])) {
81 *frame_sz = frame_size;
85 *new_pos = max_offset;
91 EsParserAdts::EsParserAdts(uint32_t pid,
92 const NewStreamInfoCB& new_stream_info_cb,
93 const EmitSampleCB& emit_sample_cb,
96 new_stream_info_cb_(new_stream_info_cb),
97 emit_sample_cb_(emit_sample_cb),
98 sbr_in_mimetype_(sbr_in_mimetype) {
101 EsParserAdts::~EsParserAdts() {
104 bool EsParserAdts::Parse(
const uint8_t* buf,
109 const uint8_t* raw_es;
113 if (pts != kNoTimestamp) {
114 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
115 pts_list_.push_back(EsPts(raw_es_size, pts));
119 es_byte_queue_.
Push(buf, size);
120 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
125 while (LookForSyncWord(raw_es, raw_es_size, es_position,
126 &es_position, &frame_size)) {
127 const uint8_t* frame_ptr = raw_es + es_position;
129 <<
"ADTS syncword @ pos=" << es_position
130 <<
" frame_size=" << frame_size;
133 << base::HexEncode(frame_ptr, kAdtsHeaderMinSize);
136 int remaining_size = raw_es_size - es_position;
137 if (frame_size > remaining_size)
143 DCHECK_GE(frame_size, kAdtsHeaderMinSize);
144 if (!UpdateAudioConfiguration(frame_ptr, frame_size))
148 while (!pts_list_.empty() &&
149 pts_list_.front().first <= es_position) {
150 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
151 pts_list_.pop_front();
154 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
155 int64_t frame_duration =
156 audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
159 bool is_key_frame =
true;
161 scoped_refptr<MediaSample> sample =
163 frame_ptr + header_size,
164 frame_size - header_size,
166 sample->set_pts(current_pts);
167 sample->set_dts(current_pts);
168 sample->set_duration(frame_duration);
169 emit_sample_cb_.Run(pid(), sample);
172 audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
175 es_position += frame_size;
179 DiscardEs(es_position);
184 void EsParserAdts::Flush() {
187 void EsParserAdts::Reset() {
188 es_byte_queue_.
Reset();
190 last_audio_decoder_config_ = scoped_refptr<AudioStreamInfo>();
193 bool EsParserAdts::UpdateAudioConfiguration(
const uint8_t* adts_frame,
194 size_t adts_frame_size) {
195 const uint8_t kAacSampleSizeBits(16);
197 AdtsHeader adts_header;
198 if (!adts_header.Parse(adts_frame, adts_frame_size)) {
199 LOG(ERROR) <<
"Error parsing ADTS frame header.";
202 std::vector<uint8_t> audio_specific_config;
203 if (!adts_header.GetAudioSpecificConfig(&audio_specific_config))
206 if (last_audio_decoder_config_) {
208 if (last_audio_decoder_config_->extra_data() == audio_specific_config) {
212 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
219 int samples_per_second = adts_header.GetSamplingFrequency();
220 int extended_samples_per_second = sbr_in_mimetype_
221 ? std::min(2 * samples_per_second, 48000)
222 : samples_per_second;
224 last_audio_decoder_config_ = scoped_refptr<StreamInfo>(
231 adts_header.GetObjectType()),
234 adts_header.GetNumChannels(),
235 extended_samples_per_second,
236 audio_specific_config.data(),
237 audio_specific_config.size(),
240 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
241 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
242 DVLOG(1) <<
"Channel config: " << adts_header.GetNumChannels();
243 DVLOG(1) <<
"Object type: " << adts_header.GetObjectType();
245 if (audio_timestamp_helper_) {
246 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
247 audio_timestamp_helper_.reset(
248 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
249 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
251 audio_timestamp_helper_.reset(
252 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
256 new_stream_info_cb_.Run(last_audio_decoder_config_);
261 void EsParserAdts::DiscardEs(
int nbytes) {
262 DCHECK_GE(nbytes, 0);
267 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
271 es_byte_queue_.
Pop(nbytes);