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))
66 int frame_size =
static_cast<int>(
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, static_cast<int>(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;
163 frame_ptr + header_size, frame_size - header_size, is_key_frame);
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(kSamplesPerAACFrame);
173 es_position += frame_size;
177 DiscardEs(es_position);
182 void EsParserAdts::Flush() {
185 void EsParserAdts::Reset() {
186 es_byte_queue_.
Reset();
188 last_audio_decoder_config_ = std::shared_ptr<AudioStreamInfo>();
191 bool EsParserAdts::UpdateAudioConfiguration(
const uint8_t* adts_frame,
192 size_t adts_frame_size) {
193 const uint8_t kAacSampleSizeBits(16);
195 AdtsHeader adts_header;
196 if (!adts_header.Parse(adts_frame, adts_frame_size)) {
197 LOG(ERROR) <<
"Error parsing ADTS frame header.";
200 std::vector<uint8_t> audio_specific_config;
201 if (!adts_header.GetAudioSpecificConfig(&audio_specific_config))
204 if (last_audio_decoder_config_) {
206 if (last_audio_decoder_config_->codec_config() == audio_specific_config) {
210 NOTIMPLEMENTED() <<
"Varying audio configurations are not supported.";
217 int samples_per_second = adts_header.GetSamplingFrequency();
218 int extended_samples_per_second = sbr_in_mimetype_
219 ? std::min(2 * samples_per_second, 48000)
220 : samples_per_second;
222 last_audio_decoder_config_ = std::make_shared<AudioStreamInfo>(
223 pid(), kMpeg2Timescale, kInfiniteDuration, kCodecAAC,
225 audio_specific_config.data(), audio_specific_config.size(),
226 kAacSampleSizeBits, adts_header.GetNumChannels(),
227 extended_samples_per_second, 0 , 0 ,
228 0 , 0 , std::string(),
false);
230 DVLOG(1) <<
"Sampling frequency: " << samples_per_second;
231 DVLOG(1) <<
"Extended sampling frequency: " << extended_samples_per_second;
232 DVLOG(1) <<
"Channel config: " << adts_header.GetNumChannels();
233 DVLOG(1) <<
"Object type: " << adts_header.GetObjectType();
235 if (audio_timestamp_helper_) {
236 int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
237 audio_timestamp_helper_.reset(
238 new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
239 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
241 audio_timestamp_helper_.reset(
242 new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
246 new_stream_info_cb_.Run(last_audio_decoder_config_);
251 void EsParserAdts::DiscardEs(
int nbytes) {
252 DCHECK_GE(nbytes, 0);
257 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
261 es_byte_queue_.
Pop(nbytes);