5 #include "packager/media/formats/mp2t/es_parser_adts.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/adts_header.h"
19 #include "packager/media/formats/mp2t/mp2t_common.h"
20 #include "packager/media/formats/mpeg/adts_constants.h"
27 static bool isAdtsSyncWord(
const uint8_t* buf) {
28 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
38 static bool LookForSyncWord(
const uint8_t* raw_es,
44 DCHECK_LE(pos, raw_es_size);
46 int max_offset = raw_es_size - kAdtsHeaderMinSize;
47 if (pos >= max_offset) {
58 for (
int offset = pos; offset < max_offset; offset++) {
59 const uint8_t* cur_buf = &raw_es[offset];
61 if (!isAdtsSyncWord(cur_buf))
68 if (frame_size < kAdtsHeaderMinSize) {
75 int remaining_size = raw_es_size - offset;
76 if (remaining_size >= frame_size + 2 &&
77 !isAdtsSyncWord(&cur_buf[frame_size])) {
82 *frame_sz = frame_size;
86 *new_pos = max_offset;
92 EsParserAdts::EsParserAdts(uint32_t pid,
93 const NewStreamInfoCB& new_stream_info_cb,
94 const EmitSampleCB& emit_sample_cb,
97 new_stream_info_cb_(new_stream_info_cb),
98 emit_sample_cb_(emit_sample_cb),
99 sbr_in_mimetype_(sbr_in_mimetype) {
102 EsParserAdts::~EsParserAdts() {
105 bool EsParserAdts::Parse(
const uint8_t* buf,
110 const uint8_t* raw_es;
114 if (pts != kNoTimestamp) {
115 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
116 pts_list_.push_back(EsPts(raw_es_size, pts));
120 es_byte_queue_.
Push(buf, size);
121 es_byte_queue_.
Peek(&raw_es, &raw_es_size);
126 while (LookForSyncWord(raw_es, raw_es_size, es_position,
127 &es_position, &frame_size)) {
128 const uint8_t* frame_ptr = raw_es + es_position;
130 <<
"ADTS syncword @ pos=" << es_position
131 <<
" frame_size=" << frame_size;
134 << base::HexEncode(frame_ptr, kAdtsHeaderMinSize);
137 int remaining_size = raw_es_size - es_position;
138 if (frame_size > remaining_size)
144 DCHECK_GE(frame_size, kAdtsHeaderMinSize);
145 if (!UpdateAudioConfiguration(frame_ptr, frame_size))
149 while (!pts_list_.empty() &&
150 pts_list_.front().first <= es_position) {
151 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
152 pts_list_.pop_front();
155 int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
156 int64_t frame_duration =
157 audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
160 bool is_key_frame =
true;
162 scoped_refptr<MediaSample> sample =
164 frame_ptr + header_size,
165 frame_size - header_size,
167 sample->set_pts(current_pts);
168 sample->set_dts(current_pts);
169 sample->set_duration(frame_duration);
170 emit_sample_cb_.Run(pid(), sample);
173 audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
176 es_position += frame_size;
180 DiscardEs(es_position);
185 void EsParserAdts::Flush() {
188 void EsParserAdts::Reset() {
189 es_byte_queue_.
Reset();
191 last_audio_decoder_config_ = scoped_refptr<AudioStreamInfo>();
194 bool EsParserAdts::UpdateAudioConfiguration(
const uint8_t* adts_frame,
195 size_t adts_frame_size) {
196 const uint8_t kAacSampleSizeBits(16);
198 AdtsHeader adts_header;
199 if (!adts_header.Parse(adts_frame, adts_frame_size)) {
200 LOG(ERROR) <<
"Error parsing ADTS frame header.";
203 std::vector<uint8_t> audio_specific_config;
204 if (!adts_header.GetAudioSpecificConfig(&audio_specific_config))
207 if (last_audio_decoder_config_) {
209 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
213 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
220 int samples_per_second = adts_header.GetSamplingFrequency();
221 int extended_samples_per_second = sbr_in_mimetype_
222 ? std::min(2 * samples_per_second, 48000)
223 : samples_per_second;
225 last_audio_decoder_config_ = scoped_refptr<StreamInfo>(
new AudioStreamInfo(
226 pid(), kMpeg2Timescale, kInfiniteDuration, kCodecAAC,
228 audio_specific_config.data(), audio_specific_config.size(),
229 kAacSampleSizeBits, adts_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: " << adts_header.GetNumChannels();
236 DVLOG(1) <<
"Object type: " << adts_header.GetObjectType();
238 if (audio_timestamp_helper_) {
239 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
240 audio_timestamp_helper_.reset(
241 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
242 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
244 audio_timestamp_helper_.reset(
245 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
249 new_stream_info_cb_.Run(last_audio_decoder_config_);
254 void EsParserAdts::DiscardEs(
int nbytes) {
255 DCHECK_GE(nbytes, 0);
260 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
264 es_byte_queue_.
Pop(nbytes);