[chromedriver] Explicitly set page loading state when document.readyState != "complete".
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_adts.cc
blob8699e20ccb5ffc2c417de396e5673ad7026b413d
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/formats/mp2t/es_parser_adts.h"
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "media/base/audio_timestamp_helper.h"
12 #include "media/base/bit_reader.h"
13 #include "media/base/buffers.h"
14 #include "media/base/channel_layout.h"
15 #include "media/base/stream_parser_buffer.h"
16 #include "media/formats/common/offset_byte_queue.h"
17 #include "media/formats/mp2t/mp2t_common.h"
18 #include "media/formats/mpeg/adts_constants.h"
20 namespace media {
22 static int ExtractAdtsFrameSize(const uint8* adts_header) {
23 return ((static_cast<int>(adts_header[5]) >> 5) |
24 (static_cast<int>(adts_header[4]) << 3) |
25 ((static_cast<int>(adts_header[3]) & 0x3) << 11));
28 static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) {
29 return ((adts_header[2] >> 2) & 0xf);
32 static size_t ExtractAdtsChannelConfig(const uint8* adts_header) {
33 return (((adts_header[3] >> 6) & 0x3) |
34 ((adts_header[2] & 0x1) << 2));
37 // Return true if buf corresponds to an ADTS syncword.
38 // |buf| size must be at least 2.
39 static bool isAdtsSyncWord(const uint8* buf) {
40 // The first 12 bits must be 1.
41 // The layer field (2 bits) must be set to 0.
42 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
45 namespace mp2t {
47 struct EsParserAdts::AdtsFrame {
48 // Pointer to the ES data.
49 const uint8* data;
51 // Frame size;
52 int size;
54 // Frame offset in the ES queue.
55 int64 queue_offset;
58 bool EsParserAdts::LookForAdtsFrame(AdtsFrame* adts_frame) {
59 int es_size;
60 const uint8* es;
61 es_queue_->Peek(&es, &es_size);
63 int max_offset = es_size - kADTSHeaderMinSize;
64 if (max_offset <= 0)
65 return false;
67 for (int offset = 0; offset < max_offset; offset++) {
68 const uint8* cur_buf = &es[offset];
69 if (!isAdtsSyncWord(cur_buf))
70 continue;
72 int frame_size = ExtractAdtsFrameSize(cur_buf);
73 if (frame_size < kADTSHeaderMinSize) {
74 // Too short to be an ADTS frame.
75 continue;
78 int remaining_size = es_size - offset;
79 if (remaining_size < frame_size) {
80 // Not a full frame: will resume when we have more data.
81 es_queue_->Pop(offset);
82 return false;
85 // Check whether there is another frame
86 // |size| apart from the current one.
87 if (remaining_size >= frame_size + 2 &&
88 !isAdtsSyncWord(&cur_buf[frame_size])) {
89 continue;
92 es_queue_->Pop(offset);
93 es_queue_->Peek(&adts_frame->data, &es_size);
94 adts_frame->queue_offset = es_queue_->head();
95 adts_frame->size = frame_size;
96 DVLOG(LOG_LEVEL_ES)
97 << "ADTS syncword @ pos=" << adts_frame->queue_offset
98 << " frame_size=" << adts_frame->size;
99 DVLOG(LOG_LEVEL_ES)
100 << "ADTS header: "
101 << base::HexEncode(adts_frame->data, kADTSHeaderMinSize);
102 return true;
105 es_queue_->Pop(max_offset);
106 return false;
109 void EsParserAdts::SkipAdtsFrame(const AdtsFrame& adts_frame) {
110 DCHECK_EQ(adts_frame.queue_offset, es_queue_->head());
111 es_queue_->Pop(adts_frame.size);
114 EsParserAdts::EsParserAdts(
115 const NewAudioConfigCB& new_audio_config_cb,
116 const EmitBufferCB& emit_buffer_cb,
117 bool sbr_in_mimetype)
118 : new_audio_config_cb_(new_audio_config_cb),
119 emit_buffer_cb_(emit_buffer_cb),
120 sbr_in_mimetype_(sbr_in_mimetype) {
123 EsParserAdts::~EsParserAdts() {
126 bool EsParserAdts::ParseFromEsQueue() {
127 // Look for every ADTS frame in the ES buffer.
128 AdtsFrame adts_frame;
129 while (LookForAdtsFrame(&adts_frame)) {
130 // Update the audio configuration if needed.
131 DCHECK_GE(adts_frame.size, kADTSHeaderMinSize);
132 if (!UpdateAudioConfiguration(adts_frame.data))
133 return false;
135 // Get the PTS & the duration of this access unit.
136 TimingDesc current_timing_desc =
137 GetTimingDescriptor(adts_frame.queue_offset);
138 if (current_timing_desc.pts != kNoTimestamp())
139 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
141 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
142 DVLOG(1) << "Skipping audio frame with unknown timestamp";
143 SkipAdtsFrame(adts_frame);
144 continue;
146 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
147 base::TimeDelta frame_duration =
148 audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
150 // Emit an audio frame.
151 bool is_key_frame = true;
153 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
154 // type and allow multiple audio tracks. See https://crbug.com/341581.
155 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
156 StreamParserBuffer::CopyFrom(
157 adts_frame.data,
158 adts_frame.size,
159 is_key_frame,
160 DemuxerStream::AUDIO, 0);
161 stream_parser_buffer->set_timestamp(current_pts);
162 stream_parser_buffer->SetDecodeTimestamp(
163 DecodeTimestamp::FromPresentationTime(current_pts));
164 stream_parser_buffer->set_duration(frame_duration);
165 emit_buffer_cb_.Run(stream_parser_buffer);
167 // Update the PTS of the next frame.
168 audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
170 // Skip the current frame.
171 SkipAdtsFrame(adts_frame);
174 return true;
177 void EsParserAdts::Flush() {
180 void EsParserAdts::ResetInternal() {
181 last_audio_decoder_config_ = AudioDecoderConfig();
184 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
185 size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header);
186 if (frequency_index >= kADTSFrequencyTableSize) {
187 // Frequency index 13 & 14 are reserved
188 // while 15 means that the frequency is explicitly written
189 // (not supported).
190 return false;
193 size_t channel_configuration = ExtractAdtsChannelConfig(adts_header);
194 if (channel_configuration == 0 ||
195 channel_configuration >= kADTSChannelLayoutTableSize) {
196 // TODO(damienv): Add support for inband channel configuration.
197 return false;
200 // TODO(damienv): support HE-AAC frequency doubling (SBR)
201 // based on the incoming ADTS profile.
202 int samples_per_second = kADTSFrequencyTable[frequency_index];
203 int adts_profile = (adts_header[2] >> 6) & 0x3;
205 // The following code is written according to ISO 14496 Part 3 Table 1.11 and
206 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
207 // to SBR doubling the AAC sample rate.)
208 // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content.
209 int extended_samples_per_second = sbr_in_mimetype_
210 ? std::min(2 * samples_per_second, 48000)
211 : samples_per_second;
213 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
214 // Syntax of AudioSpecificConfig.
215 uint16 extra_data_int = static_cast<uint16>(
216 // Note: adts_profile is in the range [0,3], since the ADTS header only
217 // allows two bits for its value.
218 ((adts_profile + 1) << 11) +
219 // frequency_index is [0..13], per early out above.
220 (frequency_index << 7) +
221 // channel_configuration is [0..7], per early out above.
222 (channel_configuration << 3));
223 uint8 extra_data[2] = {
224 static_cast<uint8>(extra_data_int >> 8),
225 static_cast<uint8>(extra_data_int & 0xff)
228 AudioDecoderConfig audio_decoder_config(
229 kCodecAAC,
230 kSampleFormatS16,
231 kADTSChannelLayoutTable[channel_configuration],
232 extended_samples_per_second,
233 extra_data,
234 arraysize(extra_data),
235 false);
237 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
238 DVLOG(1) << "Sampling frequency: " << samples_per_second;
239 DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
240 DVLOG(1) << "Channel config: " << channel_configuration;
241 DVLOG(1) << "Adts profile: " << adts_profile;
242 // Reset the timestamp helper to use a new time scale.
243 if (audio_timestamp_helper_ &&
244 audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
245 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
246 audio_timestamp_helper_.reset(
247 new AudioTimestampHelper(samples_per_second));
248 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
249 } else {
250 audio_timestamp_helper_.reset(
251 new AudioTimestampHelper(samples_per_second));
253 // Audio config notification.
254 last_audio_decoder_config_ = audio_decoder_config;
255 new_audio_config_cb_.Run(audio_decoder_config);
258 return true;
261 } // namespace mp2t
262 } // namespace media