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"
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] = {
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.
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
,
86 int* new_pos
, int* frame_sz
) {
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.
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.
110 int frame_size
= ExtractAdtsFrameSize(cur_buf
);
111 if (frame_size
< kAdtsHeaderMinSize
) {
112 // Too short to be an ADTS frame.
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
])) {
125 *frame_sz
= frame_size
;
129 *new_pos
= max_offset
;
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
,
148 base::TimeDelta dts
) {
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
166 while (LookForSyncWord(raw_es
, raw_es_size
, es_position
,
167 &es_position
, &frame_size
)) {
169 << "ADTS syncword @ pos=" << es_position
170 << " frame_size=" << frame_size
;
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
)
180 // Update the audio configuration if needed.
181 DCHECK_GE(frame_size
, kAdtsHeaderMinSize
);
182 if (!UpdateAudioConfiguration(&raw_es
[es_position
]))
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
],
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
);
221 void EsParserAdts::Flush() {
224 void EsParserAdts::Reset() {
225 es_byte_queue_
.Reset();
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
239 int channel_configuration
= ExtractAdtsChannelConfig(adts_header
);
240 if (channel_configuration
== 0) {
241 // TODO(damienv): Add support for inband channel configuration.
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(
253 adts_channel_layout
[channel_configuration
],
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
);
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
);
280 void EsParserAdts::DiscardEs(int nbytes
) {
281 DCHECK_GE(nbytes
, 0);
285 // Adjust the ES position of each PTS.
286 for (EsPtsList::iterator it
= pts_list_
.begin(); it
!= pts_list_
.end(); ++it
)
289 // Discard |nbytes| of ES.
290 es_byte_queue_
.Pop(nbytes
);