Removed unused VideoCaptureCapability parameters.
[chromium-blink-merge.git] / media / mp2t / es_parser_adts.cc
blobb7578360b692339cffe4b8abf2231ed36f2b0975
1 // Copyright 2013 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/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/mp2t/mp2t_common.h"
19 // Adts header is at least 7 bytes (can be 9 bytes).
20 static const int kAdtsHeaderMinSize = 7;
22 static const int adts_frequency_table[16] = {
23 96000,
24 88200,
25 64000,
26 48000,
27 44100,
28 32000,
29 24000,
30 22050,
31 16000,
32 12000,
33 11025,
34 8000,
35 7350,
40 static const int kMaxSupportedFrequencyIndex = 12;
42 static media::ChannelLayout adts_channel_layout[8] = {
43 media::CHANNEL_LAYOUT_NONE,
44 media::CHANNEL_LAYOUT_MONO,
45 media::CHANNEL_LAYOUT_STEREO,
46 media::CHANNEL_LAYOUT_SURROUND,
47 media::CHANNEL_LAYOUT_4_0,
48 media::CHANNEL_LAYOUT_5_0_BACK,
49 media::CHANNEL_LAYOUT_5_1_BACK,
50 media::CHANNEL_LAYOUT_7_1,
53 // Number of samples per frame.
54 static const int kNumberSamplesPerAACFrame = 1024;
56 static int ExtractAdtsFrameSize(const uint8* adts_header) {
57 return ((static_cast<int>(adts_header[5]) >> 5) |
58 (static_cast<int>(adts_header[4]) << 3) |
59 ((static_cast<int>(adts_header[3]) & 0x3) << 11));
62 static int ExtractAdtsFrequencyIndex(const uint8* adts_header) {
63 return ((adts_header[2] >> 2) & 0xf);
66 static int ExtractAdtsChannelConfig(const uint8* adts_header) {
67 return (((adts_header[3] >> 6) & 0x3) |
68 ((adts_header[2] & 0x1) << 2));
71 // Return true if buf corresponds to an ADTS syncword.
72 // |buf| size must be at least 2.
73 static bool isAdtsSyncWord(const uint8* buf) {
74 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
77 // Look for an ADTS syncword.
78 // |new_pos| returns
79 // - either the byte position of the ADTS frame (if found)
80 // - or the byte position of 1st byte that was not processed (if not found).
81 // In every case, the returned value in |new_pos| is such that new_pos >= pos
82 // |frame_sz| returns the size of the ADTS frame (if found).
83 // Return whether a syncword was found.
84 static bool LookForSyncWord(const uint8* raw_es, int raw_es_size,
85 int pos,
86 int* new_pos, int* frame_sz) {
87 DCHECK_GE(pos, 0);
88 DCHECK_LE(pos, raw_es_size);
90 int max_offset = raw_es_size - kAdtsHeaderMinSize;
91 if (pos >= max_offset) {
92 // Do not change the position if:
93 // - max_offset < 0: not enough bytes to get a full header
94 // Since pos >= 0, this is a subcase of the next condition.
95 // - pos >= max_offset: might be the case after reading one full frame,
96 // |pos| is then incremented by the frame size and might then point
97 // to the end of the buffer.
98 *new_pos = pos;
99 return false;
102 for (int offset = pos; offset < max_offset; offset++) {
103 const uint8* cur_buf = &raw_es[offset];
105 if (!isAdtsSyncWord(cur_buf))
106 // The first 12 bits must be 1.
107 // The layer field (2 bits) must be set to 0.
108 continue;
110 int frame_size = ExtractAdtsFrameSize(cur_buf);
111 if (frame_size < kAdtsHeaderMinSize) {
112 // Too short to be an ADTS frame.
113 continue;
116 // Check whether there is another frame
117 // |size| apart from the current one.
118 int remaining_size = raw_es_size - offset;
119 if (remaining_size >= frame_size + 2 &&
120 !isAdtsSyncWord(&cur_buf[frame_size])) {
121 continue;
124 *new_pos = offset;
125 *frame_sz = frame_size;
126 return true;
129 *new_pos = max_offset;
130 return false;
133 namespace media {
134 namespace mp2t {
136 EsParserAdts::EsParserAdts(
137 const NewAudioConfigCB& new_audio_config_cb,
138 const EmitBufferCB& emit_buffer_cb)
139 : new_audio_config_cb_(new_audio_config_cb),
140 emit_buffer_cb_(emit_buffer_cb) {
143 EsParserAdts::~EsParserAdts() {
146 bool EsParserAdts::Parse(const uint8* buf, int size,
147 base::TimeDelta pts,
148 base::TimeDelta dts) {
149 int raw_es_size;
150 const uint8* raw_es;
152 // The incoming PTS applies to the access unit that comes just after
153 // the beginning of |buf|.
154 if (pts != kNoTimestamp()) {
155 es_byte_queue_.Peek(&raw_es, &raw_es_size);
156 pts_list_.push_back(EsPts(raw_es_size, pts));
159 // Copy the input data to the ES buffer.
160 es_byte_queue_.Push(buf, size);
161 es_byte_queue_.Peek(&raw_es, &raw_es_size);
163 // Look for every ADTS frame in the ES buffer starting at offset = 0
164 int es_position = 0;
165 int frame_size;
166 while (LookForSyncWord(raw_es, raw_es_size, es_position,
167 &es_position, &frame_size)) {
168 DVLOG(LOG_LEVEL_ES)
169 << "ADTS syncword @ pos=" << es_position
170 << " frame_size=" << frame_size;
171 DVLOG(LOG_LEVEL_ES)
172 << "ADTS header: "
173 << base::HexEncode(&raw_es[es_position], kAdtsHeaderMinSize);
175 // Do not process the frame if this one is a partial frame.
176 int remaining_size = raw_es_size - es_position;
177 if (frame_size > remaining_size)
178 break;
180 // Update the audio configuration if needed.
181 DCHECK_GE(frame_size, kAdtsHeaderMinSize);
182 if (!UpdateAudioConfiguration(&raw_es[es_position]))
183 return false;
185 // Get the PTS & the duration of this access unit.
186 while (!pts_list_.empty() &&
187 pts_list_.front().first <= es_position) {
188 audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
189 pts_list_.pop_front();
192 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
193 base::TimeDelta frame_duration =
194 audio_timestamp_helper_->GetFrameDuration(kNumberSamplesPerAACFrame);
196 // Emit an audio frame.
197 bool is_key_frame = true;
198 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
199 StreamParserBuffer::CopyFrom(
200 &raw_es[es_position],
201 frame_size,
202 is_key_frame);
203 stream_parser_buffer->SetDecodeTimestamp(current_pts);
204 stream_parser_buffer->set_timestamp(current_pts);
205 stream_parser_buffer->set_duration(frame_duration);
206 emit_buffer_cb_.Run(stream_parser_buffer);
208 // Update the PTS of the next frame.
209 audio_timestamp_helper_->AddFrames(kNumberSamplesPerAACFrame);
211 // Skip the current frame.
212 es_position += frame_size;
215 // Discard all the bytes that have been processed.
216 DiscardEs(es_position);
218 return true;
221 void EsParserAdts::Flush() {
224 void EsParserAdts::Reset() {
225 es_byte_queue_.Reset();
226 pts_list_.clear();
227 last_audio_decoder_config_ = AudioDecoderConfig();
230 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
231 int frequency_index = ExtractAdtsFrequencyIndex(adts_header);
232 if (frequency_index > kMaxSupportedFrequencyIndex) {
233 // Frequency index 13 & 14 are reserved
234 // while 15 means that the frequency is explicitly written
235 // (not supported).
236 return false;
239 int channel_configuration = ExtractAdtsChannelConfig(adts_header);
240 if (channel_configuration == 0) {
241 // TODO(damienv): Add support for inband channel configuration.
242 return false;
245 // TODO(damienv): support HE-AAC frequency doubling (SBR)
246 // based on the incoming ADTS profile.
247 int samples_per_second = adts_frequency_table[frequency_index];
248 int adts_profile = (adts_header[2] >> 6) & 0x3;
250 AudioDecoderConfig audio_decoder_config(
251 kCodecAAC,
252 kSampleFormatS16,
253 adts_channel_layout[channel_configuration],
254 samples_per_second,
255 NULL, 0,
256 false);
258 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
259 DVLOG(1) << "Sampling frequency: " << samples_per_second;
260 DVLOG(1) << "Channel config: " << channel_configuration;
261 DVLOG(1) << "Adts profile: " << adts_profile;
262 // Reset the timestamp helper to use a new time scale.
263 if (audio_timestamp_helper_) {
264 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
265 audio_timestamp_helper_.reset(
266 new AudioTimestampHelper(samples_per_second));
267 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
268 } else {
269 audio_timestamp_helper_.reset(
270 new AudioTimestampHelper(samples_per_second));
272 // Audio config notification.
273 last_audio_decoder_config_ = audio_decoder_config;
274 new_audio_config_cb_.Run(audio_decoder_config);
277 return true;
280 void EsParserAdts::DiscardEs(int nbytes) {
281 DCHECK_GE(nbytes, 0);
282 if (nbytes <= 0)
283 return;
285 // Adjust the ES position of each PTS.
286 for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
287 it->first -= nbytes;
289 // Discard |nbytes| of ES.
290 es_byte_queue_.Pop(nbytes);
293 } // namespace mp2t
294 } // namespace media