Add integration browser tests for settings hardening.
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_adts.cc
blob433baabe5e674017fcbb2b150fcaefb9006d0a37
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"
7 #include <list>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "media/base/audio_timestamp_helper.h"
13 #include "media/base/bit_reader.h"
14 #include "media/base/buffers.h"
15 #include "media/base/channel_layout.h"
16 #include "media/base/stream_parser_buffer.h"
17 #include "media/formats/common/offset_byte_queue.h"
18 #include "media/formats/mp2t/mp2t_common.h"
19 #include "media/formats/mpeg/adts_constants.h"
21 namespace media {
23 static int ExtractAdtsFrameSize(const uint8* adts_header) {
24 return ((static_cast<int>(adts_header[5]) >> 5) |
25 (static_cast<int>(adts_header[4]) << 3) |
26 ((static_cast<int>(adts_header[3]) & 0x3) << 11));
29 static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) {
30 return ((adts_header[2] >> 2) & 0xf);
33 static size_t ExtractAdtsChannelConfig(const uint8* adts_header) {
34 return (((adts_header[3] >> 6) & 0x3) |
35 ((adts_header[2] & 0x1) << 2));
38 // Return true if buf corresponds to an ADTS syncword.
39 // |buf| size must be at least 2.
40 static bool isAdtsSyncWord(const uint8* buf) {
41 // The first 12 bits must be 1.
42 // The layer field (2 bits) must be set to 0.
43 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
46 namespace mp2t {
48 struct EsParserAdts::AdtsFrame {
49 // Pointer to the ES data.
50 const uint8* data;
52 // Frame size;
53 int size;
55 // Frame offset in the ES queue.
56 int64 queue_offset;
59 bool EsParserAdts::LookForAdtsFrame(AdtsFrame* adts_frame) {
60 int es_size;
61 const uint8* es;
62 es_queue_->Peek(&es, &es_size);
64 int max_offset = es_size - kADTSHeaderMinSize;
65 if (max_offset <= 0)
66 return false;
68 for (int offset = 0; offset < max_offset; offset++) {
69 const uint8* cur_buf = &es[offset];
70 if (!isAdtsSyncWord(cur_buf))
71 continue;
73 int frame_size = ExtractAdtsFrameSize(cur_buf);
74 if (frame_size < kADTSHeaderMinSize) {
75 // Too short to be an ADTS frame.
76 continue;
79 int remaining_size = es_size - offset;
80 if (remaining_size < frame_size) {
81 // Not a full frame: will resume when we have more data.
82 es_queue_->Pop(offset);
83 return false;
86 // Check whether there is another frame
87 // |size| apart from the current one.
88 if (remaining_size >= frame_size + 2 &&
89 !isAdtsSyncWord(&cur_buf[frame_size])) {
90 continue;
93 es_queue_->Pop(offset);
94 es_queue_->Peek(&adts_frame->data, &es_size);
95 adts_frame->queue_offset = es_queue_->head();
96 adts_frame->size = frame_size;
97 DVLOG(LOG_LEVEL_ES)
98 << "ADTS syncword @ pos=" << adts_frame->queue_offset
99 << " frame_size=" << adts_frame->size;
100 DVLOG(LOG_LEVEL_ES)
101 << "ADTS header: "
102 << base::HexEncode(adts_frame->data, kADTSHeaderMinSize);
103 return true;
106 es_queue_->Pop(max_offset);
107 return false;
110 void EsParserAdts::SkipAdtsFrame(const AdtsFrame& adts_frame) {
111 DCHECK_EQ(adts_frame.queue_offset, es_queue_->head());
112 es_queue_->Pop(adts_frame.size);
115 EsParserAdts::EsParserAdts(
116 const NewAudioConfigCB& new_audio_config_cb,
117 const EmitBufferCB& emit_buffer_cb,
118 bool sbr_in_mimetype)
119 : new_audio_config_cb_(new_audio_config_cb),
120 emit_buffer_cb_(emit_buffer_cb),
121 sbr_in_mimetype_(sbr_in_mimetype),
122 es_queue_(new media::OffsetByteQueue()) {
125 EsParserAdts::~EsParserAdts() {
128 bool EsParserAdts::Parse(const uint8* buf, int size,
129 base::TimeDelta pts,
130 base::TimeDelta dts) {
131 // The incoming PTS applies to the access unit that comes just after
132 // the beginning of |buf|.
133 if (pts != kNoTimestamp())
134 pts_list_.push_back(EsPts(es_queue_->tail(), pts));
136 // Copy the input data to the ES buffer.
137 es_queue_->Push(buf, size);
139 // Look for every ADTS frame in the ES buffer.
140 AdtsFrame adts_frame;
141 while (LookForAdtsFrame(&adts_frame)) {
142 // Update the audio configuration if needed.
143 DCHECK_GE(adts_frame.size, kADTSHeaderMinSize);
144 if (!UpdateAudioConfiguration(adts_frame.data))
145 return false;
147 // Get the PTS & the duration of this access unit.
148 while (!pts_list_.empty() &&
149 pts_list_.front().first <= adts_frame.queue_offset) {
150 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
151 pts_list_.pop_front();
154 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
155 base::TimeDelta frame_duration =
156 audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
158 // Emit an audio frame.
159 bool is_key_frame = true;
161 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
162 // type and allow multiple audio tracks. See https://crbug.com/341581.
163 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
164 StreamParserBuffer::CopyFrom(
165 adts_frame.data,
166 adts_frame.size,
167 is_key_frame,
168 DemuxerStream::AUDIO, 0);
169 stream_parser_buffer->SetDecodeTimestamp(current_pts);
170 stream_parser_buffer->set_timestamp(current_pts);
171 stream_parser_buffer->set_duration(frame_duration);
172 emit_buffer_cb_.Run(stream_parser_buffer);
174 // Update the PTS of the next frame.
175 audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
177 // Skip the current frame.
178 SkipAdtsFrame(adts_frame);
181 return true;
184 void EsParserAdts::Flush() {
187 void EsParserAdts::Reset() {
188 es_queue_.reset(new media::OffsetByteQueue());
189 pts_list_.clear();
190 last_audio_decoder_config_ = AudioDecoderConfig();
193 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
194 size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header);
195 if (frequency_index >= kADTSFrequencyTableSize) {
196 // Frequency index 13 & 14 are reserved
197 // while 15 means that the frequency is explicitly written
198 // (not supported).
199 return false;
202 size_t channel_configuration = ExtractAdtsChannelConfig(adts_header);
203 if (channel_configuration == 0 ||
204 channel_configuration >= kADTSChannelLayoutTableSize) {
205 // TODO(damienv): Add support for inband channel configuration.
206 return false;
209 // TODO(damienv): support HE-AAC frequency doubling (SBR)
210 // based on the incoming ADTS profile.
211 int samples_per_second = kADTSFrequencyTable[frequency_index];
212 int adts_profile = (adts_header[2] >> 6) & 0x3;
214 // The following code is written according to ISO 14496 Part 3 Table 1.11 and
215 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
216 // to SBR doubling the AAC sample rate.)
217 // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content.
218 int extended_samples_per_second = sbr_in_mimetype_
219 ? std::min(2 * samples_per_second, 48000)
220 : samples_per_second;
222 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
223 // Syntax of AudioSpecificConfig.
224 uint16 extra_data_int =
225 // Note: adts_profile is in the range [0,3], since the ADTS header only
226 // allows two bits for its value.
227 ((adts_profile + 1) << 11) +
228 (frequency_index << 7) +
229 (channel_configuration << 3);
230 uint8 extra_data[2] = {
231 static_cast<uint8>(extra_data_int >> 8),
232 static_cast<uint8>(extra_data_int & 0xff)
235 AudioDecoderConfig audio_decoder_config(
236 kCodecAAC,
237 kSampleFormatS16,
238 kADTSChannelLayoutTable[channel_configuration],
239 extended_samples_per_second,
240 extra_data,
241 arraysize(extra_data),
242 false);
244 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
245 DVLOG(1) << "Sampling frequency: " << samples_per_second;
246 DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
247 DVLOG(1) << "Channel config: " << channel_configuration;
248 DVLOG(1) << "Adts profile: " << adts_profile;
249 // Reset the timestamp helper to use a new time scale.
250 if (audio_timestamp_helper_) {
251 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
252 audio_timestamp_helper_.reset(
253 new AudioTimestampHelper(samples_per_second));
254 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
255 } else {
256 audio_timestamp_helper_.reset(
257 new AudioTimestampHelper(samples_per_second));
259 // Audio config notification.
260 last_audio_decoder_config_ = audio_decoder_config;
261 new_audio_config_cb_.Run(audio_decoder_config);
264 return true;
267 } // namespace mp2t
268 } // namespace media