OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "media/mpeg2/es_parser_adts.h" | |
6 | |
7 #include <list> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "media/base/audio_decoder_config.h" | |
13 #include "media/base/bit_reader.h" | |
14 #include "media/base/channel_layout.h" | |
15 #include "media/base/stream_parser_buffer.h" | |
16 #include "media/mpeg2/mpeg2ts_common.h" | |
17 | |
18 namespace { | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Move these into the mpeg2 namespace and use s
damienv1
2013/09/04 01:37:14
According to http://www.chromium.org/developers/co
| |
19 // Adts header is at least 7 bytes (can be 9 bytes). | |
20 const int kAdtsHeaderMinSize = 7; | |
21 | |
22 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, | |
36 0, | |
37 0, | |
38 0, | |
39 }; | |
40 const int kExplicitFrequencyIndex = 15; | |
41 | |
42 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, | |
51 }; | |
52 | |
53 // Number of samples per frame. | |
54 const int kNumberSamplesPerAACFrame = 1024; | |
55 const int kNumberSamplesPerHeAACFrame = 2048; | |
56 const int kNumberSamplesPerAACLcFrame = 960; | |
57 | |
58 int ExtractAdtsFrameSize(const uint8* adts_header) { | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: move these into the mpeg2 namespace and make
damienv1
2013/09/04 01:37:14
ditto.
If you have an updated coding guideline and
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
The majority of the media code uses static instead
| |
59 int frame_size = | |
60 (static_cast<int>(adts_header[5]) >> 5) | | |
61 (static_cast<int>(adts_header[4]) << 3) | | |
62 ((static_cast<int>(adts_header[3]) & 0x3) << 11); | |
63 return frame_size; | |
64 } | |
65 | |
66 int ExtractAdtsFrequencyIndex(const uint8* adts_header) { | |
67 int frequency_index = | |
68 (adts_header[2] >> 2) & 0xf; | |
69 return frequency_index; | |
70 } | |
71 | |
72 int ExtractAdtsChannelConfig(const uint8* adts_header) { | |
73 int channel_config = | |
74 ((adts_header[3] >> 6) & 0x3) | | |
75 ((adts_header[2] & 0x1) << 2); | |
76 return channel_config; | |
77 } | |
78 | |
79 // Look for an ADTS syncword. | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Please document the parameters and return val
damienv1
2013/09/04 01:37:14
Done.
| |
80 bool LookForSyncWord(const std::vector<uint8>& buf, | |
81 int pos, | |
82 int* new_pos, int* frame_sz) { | |
83 int max_offset = buf.size() - kAdtsHeaderMinSize; | |
84 if (max_offset < 0) { | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: remove {} for single line bodies here and eve
damienv1
2013/09/04 01:37:14
Done.
| |
85 max_offset = 0; | |
86 } | |
87 | |
88 for (int offset = pos; offset < max_offset; offset++) { | |
89 const uint8* cur_buf = &buf[offset]; | |
90 | |
91 if ((cur_buf[0] != 0xff) || ((cur_buf[1] & 0xf6) != 0xf0)) { | |
92 // The first 12 bits must be 1. | |
93 // The layer field (2 bits) must be set to 0. | |
94 continue; | |
95 } | |
96 | |
97 int frequency_index = ExtractAdtsFrequencyIndex(cur_buf); | |
98 if (frequency_index == kExplicitFrequencyIndex) { | |
99 // 15 is a forbidden value. | |
100 continue; | |
101 } | |
102 | |
103 int frame_size = ExtractAdtsFrameSize(cur_buf); | |
104 if (frame_size < kAdtsHeaderMinSize) { | |
105 // Too short to be an ADTS frame. | |
106 continue; | |
107 } | |
108 | |
109 // Check whether there is another frame | |
110 // |size| apart from the current one. | |
111 int remaining_size = buf.size() - offset; | |
112 if (remaining_size >= frame_size + 2) { | |
113 if ((cur_buf[frame_size] != 0xff) || | |
114 (cur_buf[frame_size + 1] & 0xf6) != 0xf0) { | |
115 continue; | |
116 } | |
117 } | |
118 | |
119 *new_pos = offset; | |
120 *frame_sz = frame_size; | |
121 return true; | |
122 } | |
123 | |
124 *new_pos = max_offset; | |
125 return false; | |
126 } | |
127 | |
128 } // namespace | |
129 | |
130 namespace media { | |
131 namespace mpeg2ts { | |
132 | |
133 EsParserAdts::EsParserAdts( | |
134 NewAudioConfigCB new_audio_config_cb, | |
135 EmitBufferCB emit_buffer_cb) | |
136 : first_frame_(true), | |
137 new_audio_config_cb_(new_audio_config_cb), | |
138 emit_buffer_cb_(emit_buffer_cb), | |
139 is_audio_config_known_(false), | |
140 sampling_frequency_(0), | |
141 channel_configuration_(0) { | |
142 } | |
143 | |
144 EsParserAdts::~EsParserAdts() { | |
145 } | |
146 | |
147 void EsParserAdts::Parse(const uint8* buf, int size, | |
148 bool is_pts_valid, base::TimeDelta pts, | |
149 bool is_dts_valid, base::TimeDelta dts) { | |
150 // The incoming PTS applies to the access unit that comes just after | |
151 // the beginning of |buf|. | |
152 if (is_pts_valid) { | |
153 pts_list_.push_back(EsPts(raw_es_.size(), pts)); | |
154 } | |
155 | |
156 // Copy the input data to the ES buffer. | |
157 int old_size = raw_es_.size(); | |
158 raw_es_.resize(old_size + size); | |
159 memcpy(&raw_es_[old_size], buf, size); | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: use media::ByteQueue to avoid doing this stuf
damienv1
2013/09/04 01:37:14
Done.
| |
160 | |
161 // Look for every ADTS frame in the ES buffer starting at offset = 0 | |
162 int es_position = 0; | |
163 int frame_size; | |
164 while (LookForSyncWord(raw_es_, es_position, | |
165 &es_position, &frame_size)) { | |
166 VLOG(LOG_LEVEL_ES) << "ADTS syncword @ pos=" << es_position | |
167 << " frame_size=" << frame_size; | |
168 VLOG(LOG_LEVEL_ES) << "ADTS header: " | |
169 << base::HexEncode(&raw_es_[es_position], 7); | |
170 | |
171 // Do not process the frame if this one is a partial frame. | |
172 int remaining_size = raw_es_.size() - es_position; | |
173 if (frame_size > remaining_size) { | |
174 break; | |
175 } | |
176 | |
177 // Update the audio configuration if needed. | |
178 DCHECK_GE(frame_size, kAdtsHeaderMinSize); | |
179 UpdateAudioConfiguration(&raw_es_[es_position]); | |
180 | |
181 // Get the PTS of this access unit. | |
182 base::TimeDelta current_pts = estimated_pts_; | |
183 while (!pts_list_.empty() && | |
184 pts_list_.front().first <= es_position) { | |
185 current_pts = pts_list_.front().second; | |
186 pts_list_.pop_front(); | |
187 } | |
188 VLOG(LOG_LEVEL_ES) | |
189 << "Current PTS: " << current_pts.InMilliseconds() | |
190 << " Estimated PTS: " << estimated_pts_.InMilliseconds(); | |
191 | |
192 // Verify that PTS is increasing. | |
193 if (!first_frame_ && current_pts < last_frame_pts_) { | |
194 LOG(WARNING) << "ADTS: pts not monotonic"; | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
This seems like it should be a DCHECK or at least
damienv1
2013/09/04 01:37:14
At the ES level, nothing is preventing an audio fr
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
My concern is that having code here makes me think
| |
195 } | |
196 first_frame_ = false; | |
197 last_frame_pts_ = current_pts; | |
198 | |
199 // Emit an audio frame. | |
200 bool is_key_frame = true; | |
201 scoped_refptr<StreamParserBuffer> stream_parser_buffer = | |
202 StreamParserBuffer::CopyFrom( | |
203 &raw_es_[es_position], | |
204 frame_size, | |
205 is_key_frame); | |
206 stream_parser_buffer->SetDecodeTimestamp(current_pts); | |
207 stream_parser_buffer->set_timestamp(current_pts); | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Set the duration of the buffer too since it i
damienv1
2013/09/04 01:37:14
Done.
| |
208 emit_buffer_cb_.Run(stream_parser_buffer); | |
209 | |
210 // Update the PTS of the next frame. | |
211 base::TimeDelta frame_duration = | |
212 base::TimeDelta::FromMicroseconds( | |
213 (1000000 * kNumberSamplesPerAACFrame) / sampling_frequency_); | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
Use media::AudioTimestampHelper for this type of c
| |
214 estimated_pts_ = current_pts + frame_duration; | |
215 | |
216 // Skip the current frame. | |
217 es_position += frame_size; | |
218 } | |
219 | |
220 // Discard all the bytes that have been processed. | |
221 DiscardEs(es_position); | |
222 } | |
223 | |
224 void EsParserAdts::Flush() { | |
225 // All the complete frames have been emitted, | |
226 // so just clear the ES buffer. | |
227 raw_es_.clear(); | |
228 pts_list_.clear(); | |
229 } | |
230 | |
231 void EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) { | |
232 int frequency_index = ExtractAdtsFrequencyIndex(adts_header); | |
233 if (frequency_index > 12) { | |
234 // Frequency index 13 & 14 are reserved | |
235 // while 15 means that the frequency is explicitly written | |
236 // (not supported). | |
237 return; | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
This should probably cause a parse error and print
damienv1
2013/09/04 01:37:14
I slightly changed the behavior.
Now, ADTS syncwor
| |
238 } | |
239 int samples_per_second = adts_frequency_table[frequency_index]; | |
240 | |
241 int channel_configuration = ExtractAdtsChannelConfig(adts_header); | |
242 int adts_profile = (adts_header[2] >> 6) & 0x3; | |
243 | |
244 #if 0 | |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Remove if this isn't going to be turned on in
damienv1
2013/09/04 01:37:14
Done.
| |
245 // TODO(damienv): support HE-AAC frequency doubling (SBR) | |
246 if (adts_profile == kAdtsProfileHeAAC) { | |
247 samples_per_second *= 2; | |
248 } | |
249 #endif | |
250 | |
251 if (!is_audio_config_known_ || | |
252 sampling_frequency_ != samples_per_second || | |
253 channel_configuration_ != channel_configuration) { | |
254 is_audio_config_known_ = true; | |
255 sampling_frequency_ = samples_per_second; | |
256 channel_configuration_ = channel_configuration; | |
257 | |
258 LOG(INFO) << "Sampling frequency: " << samples_per_second; | |
259 LOG(INFO) << "Channel config: " << channel_configuration; | |
260 LOG(INFO) << "Adts profile: " << adts_profile; | |
261 AudioDecoderConfig audio_decoder_config( | |
262 kCodecAAC, | |
263 kSampleFormatS16, | |
264 adts_channel_layout[channel_configuration], | |
265 samples_per_second, | |
266 NULL, 0, | |
267 false); | |
268 new_audio_config_cb_.Run(audio_decoder_config); | |
269 } | |
270 } | |
271 | |
272 void EsParserAdts::DiscardEs(int nbytes) { | |
273 if (nbytes <= 0) { | |
274 return; | |
275 } | |
276 | |
277 // Adjust the ES position of each PTS. | |
278 EsPtsList::iterator it = pts_list_.begin(); | |
279 for (; it != pts_list_.end(); ++it) { | |
280 it->first -= nbytes; | |
281 } | |
282 | |
283 // Discard |nbytes| of ES. | |
284 int old_size = raw_es_.size(); | |
285 int new_size = old_size - nbytes; | |
286 CHECK_LE(nbytes, old_size); | |
287 if (new_size > 0) { | |
288 memmove(&raw_es_[0], &raw_es_[nbytes], new_size); | |
289 } | |
290 raw_es_.resize(new_size); | |
291 } | |
292 | |
293 } // namespace mpeg2ts | |
294 } // namespace media | |
OLD | NEW |