Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_adts.cc
blob9c57de196117197964a9efaa15b59a3ba9f8859d
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 "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "media/base/audio_timestamp_helper.h"
11 #include "media/base/bit_reader.h"
12 #include "media/base/channel_layout.h"
13 #include "media/base/stream_parser_buffer.h"
14 #include "media/base/timestamp_constants.h"
15 #include "media/formats/common/offset_byte_queue.h"
16 #include "media/formats/mp2t/mp2t_common.h"
17 #include "media/formats/mpeg/adts_constants.h"
19 namespace media {
21 static int ExtractAdtsFrameSize(const uint8* adts_header) {
22 return ((static_cast<int>(adts_header[5]) >> 5) |
23 (static_cast<int>(adts_header[4]) << 3) |
24 ((static_cast<int>(adts_header[3]) & 0x3) << 11));
27 static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) {
28 return ((adts_header[2] >> 2) & 0xf);
31 static size_t ExtractAdtsChannelConfig(const uint8* adts_header) {
32 return (((adts_header[3] >> 6) & 0x3) |
33 ((adts_header[2] & 0x1) << 2));
36 // Return true if buf corresponds to an ADTS syncword.
37 // |buf| size must be at least 2.
38 static bool isAdtsSyncWord(const uint8* buf) {
39 // The first 12 bits must be 1.
40 // The layer field (2 bits) must be set to 0.
41 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
44 namespace mp2t {
46 struct EsParserAdts::AdtsFrame {
47 // Pointer to the ES data.
48 const uint8* data;
50 // Frame size;
51 int size;
53 // Frame offset in the ES queue.
54 int64 queue_offset;
57 bool EsParserAdts::LookForAdtsFrame(AdtsFrame* adts_frame) {
58 int es_size;
59 const uint8* es;
60 es_queue_->Peek(&es, &es_size);
62 int max_offset = es_size - kADTSHeaderMinSize;
63 if (max_offset <= 0)
64 return false;
66 for (int offset = 0; offset < max_offset; offset++) {
67 const uint8* cur_buf = &es[offset];
68 if (!isAdtsSyncWord(cur_buf))
69 continue;
71 int frame_size = ExtractAdtsFrameSize(cur_buf);
72 if (frame_size < kADTSHeaderMinSize) {
73 // Too short to be an ADTS frame.
74 continue;
77 int remaining_size = es_size - offset;
78 if (remaining_size < frame_size) {
79 // Not a full frame: will resume when we have more data.
80 es_queue_->Pop(offset);
81 return false;
84 // Check whether there is another frame
85 // |size| apart from the current one.
86 if (remaining_size >= frame_size + 2 &&
87 !isAdtsSyncWord(&cur_buf[frame_size])) {
88 continue;
91 es_queue_->Pop(offset);
92 es_queue_->Peek(&adts_frame->data, &es_size);
93 adts_frame->queue_offset = es_queue_->head();
94 adts_frame->size = frame_size;
95 DVLOG(LOG_LEVEL_ES)
96 << "ADTS syncword @ pos=" << adts_frame->queue_offset
97 << " frame_size=" << adts_frame->size;
98 DVLOG(LOG_LEVEL_ES)
99 << "ADTS header: "
100 << base::HexEncode(adts_frame->data, kADTSHeaderMinSize);
101 return true;
104 es_queue_->Pop(max_offset);
105 return false;
108 void EsParserAdts::SkipAdtsFrame(const AdtsFrame& adts_frame) {
109 DCHECK_EQ(adts_frame.queue_offset, es_queue_->head());
110 es_queue_->Pop(adts_frame.size);
113 EsParserAdts::EsParserAdts(
114 const NewAudioConfigCB& new_audio_config_cb,
115 const EmitBufferCB& emit_buffer_cb,
116 bool sbr_in_mimetype)
117 : new_audio_config_cb_(new_audio_config_cb),
118 emit_buffer_cb_(emit_buffer_cb),
119 sbr_in_mimetype_(sbr_in_mimetype) {
122 EsParserAdts::~EsParserAdts() {
125 bool EsParserAdts::ParseFromEsQueue() {
126 // Look for every ADTS frame in the ES buffer.
127 AdtsFrame adts_frame;
128 while (LookForAdtsFrame(&adts_frame)) {
129 // Update the audio configuration if needed.
130 DCHECK_GE(adts_frame.size, kADTSHeaderMinSize);
131 if (!UpdateAudioConfiguration(adts_frame.data))
132 return false;
134 // Get the PTS & the duration of this access unit.
135 TimingDesc current_timing_desc =
136 GetTimingDescriptor(adts_frame.queue_offset);
137 if (current_timing_desc.pts != kNoTimestamp())
138 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
140 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
141 DVLOG(1) << "Skipping audio frame with unknown timestamp";
142 SkipAdtsFrame(adts_frame);
143 continue;
145 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
146 base::TimeDelta frame_duration =
147 audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
149 // Emit an audio frame.
150 bool is_key_frame = true;
152 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
153 // type and allow multiple audio tracks. See https://crbug.com/341581.
154 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
155 StreamParserBuffer::CopyFrom(
156 adts_frame.data,
157 adts_frame.size,
158 is_key_frame,
159 DemuxerStream::AUDIO, 0);
160 stream_parser_buffer->set_timestamp(current_pts);
161 stream_parser_buffer->SetDecodeTimestamp(
162 DecodeTimestamp::FromPresentationTime(current_pts));
163 stream_parser_buffer->set_duration(frame_duration);
164 emit_buffer_cb_.Run(stream_parser_buffer);
166 // Update the PTS of the next frame.
167 audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
169 // Skip the current frame.
170 SkipAdtsFrame(adts_frame);
173 return true;
176 void EsParserAdts::Flush() {
179 void EsParserAdts::ResetInternal() {
180 last_audio_decoder_config_ = AudioDecoderConfig();
183 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
184 size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header);
185 if (frequency_index >= kADTSFrequencyTableSize) {
186 // Frequency index 13 & 14 are reserved
187 // while 15 means that the frequency is explicitly written
188 // (not supported).
189 return false;
192 size_t channel_configuration = ExtractAdtsChannelConfig(adts_header);
193 if (channel_configuration == 0 ||
194 channel_configuration >= kADTSChannelLayoutTableSize) {
195 // TODO(damienv): Add support for inband channel configuration.
196 return false;
199 // TODO(damienv): support HE-AAC frequency doubling (SBR)
200 // based on the incoming ADTS profile.
201 int samples_per_second = kADTSFrequencyTable[frequency_index];
202 int adts_profile = (adts_header[2] >> 6) & 0x3;
204 // The following code is written according to ISO 14496 Part 3 Table 1.11 and
205 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
206 // to SBR doubling the AAC sample rate.)
207 // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content.
208 int extended_samples_per_second = sbr_in_mimetype_
209 ? std::min(2 * samples_per_second, 48000)
210 : samples_per_second;
212 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
213 // Syntax of AudioSpecificConfig.
214 uint16 extra_data_int = static_cast<uint16>(
215 // Note: adts_profile is in the range [0,3], since the ADTS header only
216 // allows two bits for its value.
217 ((adts_profile + 1) << 11) +
218 // frequency_index is [0..13], per early out above.
219 (frequency_index << 7) +
220 // channel_configuration is [0..7], per early out above.
221 (channel_configuration << 3));
222 uint8 extra_data[2] = {
223 static_cast<uint8>(extra_data_int >> 8),
224 static_cast<uint8>(extra_data_int & 0xff)
227 AudioDecoderConfig audio_decoder_config(
228 kCodecAAC,
229 kSampleFormatS16,
230 kADTSChannelLayoutTable[channel_configuration],
231 extended_samples_per_second,
232 extra_data,
233 arraysize(extra_data),
234 false);
236 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
237 DVLOG(1) << "Sampling frequency: " << samples_per_second;
238 DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
239 DVLOG(1) << "Channel config: " << channel_configuration;
240 DVLOG(1) << "Adts profile: " << adts_profile;
241 // Reset the timestamp helper to use a new time scale.
242 if (audio_timestamp_helper_ &&
243 audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
244 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
245 audio_timestamp_helper_.reset(
246 new AudioTimestampHelper(samples_per_second));
247 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
248 } else {
249 audio_timestamp_helper_.reset(
250 new AudioTimestampHelper(samples_per_second));
252 // Audio config notification.
253 last_audio_decoder_config_ = audio_decoder_config;
254 new_audio_config_cb_.Run(audio_decoder_config);
257 return true;
260 } // namespace mp2t
261 } // namespace media