Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: media/mp3/mp3_stream_parser.cc

Issue 23454006: Implement experimental MP3 support for Media Source API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
scherkus (not reviewing) 2013/08/30 00:06:11 2013!
acolwell GONE FROM CHROMIUM 2013/08/30 01:42:22 Done. How embarrassing. :)
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/mp3/mp3_stream_parser.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "media/base/bit_reader.h"
11 #include "media/base/buffers.h"
12 #include "media/base/stream_parser_buffer.h"
13 #include "media/base/video_decoder_config.h"
14
15 namespace media {
16 namespace mp3 {
17
18 static const uint32 kMP3StartCodeMask = 0xffe00000;
19 static const uint32 kICYStartCode = 0x49435920; // 'ICY '
20 static const int kMaxIcecastHeaderSize = 4096;
21
22 static const uint32 kID3StartCodeMask = 0xffffff00;
23 static const uint32 kID3v1StartCode = 0x54414700; // 'TAG\0'
24 static const int kID3v1Size = 128;
25 static const int kID3v1ExtendedSize = 227;
26 static const uint32 kID3v2StartCode = 0x49443300; // 'ID3\0'
27
28 // Map that determined which bitrate_index & channel_mode combinations
29 // are allowed.
30 static const bool kIsAllowed[17][4] = {
31 { true, true, true, true }, // free
32 { true, false, false, false }, // 32
33 { true, false, false, false }, // 48
34 { true, false, false, false }, // 56
35 { true, true, true, true }, // 64
36 { true, false, false, false }, // 80
37 { true, true, true, true }, // 96
38 { true, true, true, true }, // 112
39 { true, true, true, true }, // 128
40 { true, true, true, true }, // 160
41 { true, true, true, true }, // 192
42 { false, true, true, true }, // 224
43 { false, true, true, true }, // 256
44 { false, true, true, true }, // 320
45 { false, true, true, true }, // 384
46 { false, false, false, false } // bad
47 };
48
49 // Maps version and layer information in the frame header
50 // into an index for the |kBitrateMap|.
51 static const int kVersionLayerMap[4][4] = {
52 // { reserved, L3, L2, L1 }
53 { 5, 4, 4, 3 }, // MPEG 2.5
54 { 5, 5, 5, 5 }, // reserved
55 { 5, 4, 4, 3 }, // MPEG 2
56 { 5, 2, 1, 0 } // MPEG 1
57 };
58
59 // Maps the bitrate index field in the header and an index
60 // from |kVersionLayerMap| to a frame bitrate.
61 static const int kBitrateMap[16][6] = {
62 // { V1L1, V1L2, V1L3, V2L1, V2L2 & V2L3, reserved }
63 { 0, 0, 0, 0, 0, 0 },
64 { 32, 32, 32, 32, 8, 0 },
65 { 64, 48, 40, 48, 16, 0 },
66 { 96, 56, 48, 56, 24, 0 },
67 { 128, 64, 56, 64, 32, 0 },
68 { 160, 80, 64, 80, 40, 0 },
69 { 192, 96, 80, 96, 48, 0 },
70 { 224, 112, 96, 112, 56, 0 },
71 { 256, 128, 112, 128, 64, 0 },
72 { 288, 160, 128, 144, 80, 0 },
73 { 320, 192, 160, 160, 96, 0 },
74 { 352, 224, 192, 176, 112, 0 },
75 { 384, 256, 224, 192, 128, 0 },
76 { 416, 320, 256, 224, 144, 0 },
77 { 448, 384, 320, 256, 160, 0 },
78 { 0, 0, 0, 0, 0}
79 };
80
81 // Maps the sample rate index and version fields from the frame header
82 // to a sample rate.
83 static const int kSampleRateMap[4][4] {
84 // { V2.5, reserved, V2, V1 }
85 { 11025, 0, 22050, 44100 },
86 { 12000, 0, 24000, 48000 },
87 { 8000, 0, 16000, 32000 },
88 { 0, 0, 0, 0 }
89 };
90
91 MP3StreamParser::MP3StreamParser()
92 : state_(UNINITIALIZED),
93 in_media_segment_(false) {
94 }
95
96 MP3StreamParser::~MP3StreamParser() {}
97
98 void MP3StreamParser::Init(const InitCB& init_cb,
99 const NewConfigCB& config_cb,
100 const NewBuffersCB& new_buffers_cb,
101 const NewTextBuffersCB& text_cb,
102 const NeedKeyCB& need_key_cb,
103 const AddTextTrackCB& add_text_track_cb,
104 const NewMediaSegmentCB& new_segment_cb,
105 const base::Closure& end_of_segment_cb,
106 const LogCB& log_cb) {
107 DVLOG(1) << __FUNCTION__;
108 DCHECK_EQ(state_, UNINITIALIZED);
109 init_cb_ = init_cb;
110 config_cb_ = config_cb;
111 new_buffers_cb_ = new_buffers_cb;
112 new_segment_cb_ = new_segment_cb;
113 end_of_segment_cb_ = end_of_segment_cb;
114 log_cb_ = log_cb;
115
116 ChangeState(INITIALIZED);
117 }
118
119 void MP3StreamParser::Flush()
120 {
scherkus (not reviewing) 2013/08/30 00:06:11 move to prev line
acolwell GONE FROM CHROMIUM 2013/08/30 01:42:22 Done.
121 DVLOG(1) << __FUNCTION__;
122 DCHECK_NE(state_, UNINITIALIZED);
123 queue_.Reset();
124 timestamp_helper_->SetBaseTimestamp(base::TimeDelta());
125 in_media_segment_ = false;
126 }
127
128 bool MP3StreamParser::Parse(const uint8* buf, int size)
129 {
scherkus (not reviewing) 2013/08/30 00:06:11 move to prev line
acolwell GONE FROM CHROMIUM 2013/08/30 01:42:22 Done.
130 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
131 DCHECK(buf);
132 DCHECK_GT(size, 0);
133 DCHECK_NE(state_, UNINITIALIZED);
134
135 if (state_ == ERROR)
136 return false;
137
138 DCHECK_EQ(state_, INITIALIZED);
139
140 queue_.Push(buf, size);
141
142 for (;;) {
143 const uint8* data;
144 int data_size;
145 queue_.Peek(&data, &data_size);
146
147 if (size < 4)
148 return true;
149
150 uint32 start_code = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
151 int bytes_read = 0;
152 if ((start_code & kMP3StartCodeMask) == kMP3StartCodeMask) {
153 bytes_read = ParseMP3Frame(data, data_size);
154 } else if (start_code == kICYStartCode) {
155 bytes_read = ParseIcecastHeader(data, data_size);
156 } else if ((start_code & kID3StartCodeMask) == kID3v1StartCode) {
157 bytes_read = ParseID3v1(data, data_size);
158 } else if ((start_code & kID3StartCodeMask) == kID3v2StartCode) {
159 bytes_read = ParseID3v2(data, data_size);
160 } else {
161 bytes_read = FindNextValidStartCode(data, data_size);
162
163 if (bytes_read > 0) {
164 DVLOG(1) << "Unexpected start code 0x" << std::hex << start_code;
165 DVLOG(1) << "SKIPPING " << bytes_read << " bytes of garbage.";
166 }
167 }
168
169 CHECK_LE(bytes_read, data_size);
170
171 if (bytes_read < 0) {
172 ChangeState(ERROR);
173 return false;
174 } else if (bytes_read == 0) {
175 // Need more data.
176 return true;
177 }
178
179 queue_.Pop(bytes_read);
180 }
181
182 return true;
183 }
184
185 void MP3StreamParser::ChangeState(State state) {
186 DVLOG(1) << __FUNCTION__ << "() : " << state_ << " -> " << state;
187 state_ = state;
188 }
189
190 int MP3StreamParser::ParseFrameHeader(const uint8* data, int size,
191 int* frame_size,
192 int* sample_rate,
193 ChannelLayout* channel_layout,
194 int* sample_count) const {
195 DCHECK(data);
196 DCHECK_GE(size, 0);
197 DCHECK(frame_size);
198
199 if (size < 4)
200 return 0;
201
202 BitReader reader(data, size);
203 int sync;
204 int version;
205 int layer;
206 int is_protected;
207 int bitrate_index;
208 int sample_rate_index;
209 int has_padding;
210 int is_private;
211 int channel_mode;
212 int other_flags;
213
214 if (!reader.ReadBits(11, &sync) ||
215 !reader.ReadBits(2, &version) ||
216 !reader.ReadBits(2, &layer) ||
217 !reader.ReadBits(1, &is_protected) ||
218 !reader.ReadBits(4, &bitrate_index) ||
219 !reader.ReadBits(2, &sample_rate_index) ||
220 !reader.ReadBits(1, &has_padding) ||
221 !reader.ReadBits(1, &is_private) ||
222 !reader.ReadBits(2, &channel_mode) ||
223 !reader.ReadBits(6, &other_flags)) {
224 return -1;
225 }
226
227 DVLOG(2) << "Header data :" << std::hex
228 << " sync 0x" << sync
229 << " version 0x" << version
230 << " layer 0x" << layer
231 << " bitrate_index 0x" << bitrate_index
232 << " sample_rate_index 0x" << sample_rate_index
233 << " channel_mode 0x" << channel_mode;
234
235 if (sync != 0x7ff ||
236 version == 0 ||
237 layer == 0 ||
238 bitrate_index == 0x0 || bitrate_index == 0xf ||
239 sample_rate_index == 0x3) {
240 MEDIA_LOG(log_cb_) << "Invalid header data :" << std::hex
241 << " sync 0x" << sync
242 << " version 0x" << version
243 << " layer 0x" << layer
244 << " bitrate_index 0x" << bitrate_index
245 << " sample_rate_index 0x" << sample_rate_index
246 << " channel_mode 0x" << channel_mode;
247 return -1;
248 }
249
250 if (layer == 2 && kIsAllowed[bitrate_index][channel_mode]) {
251 MEDIA_LOG(log_cb_) << "Invalid (bitrate_index, channel_mode) combination :"
252 << std::hex
253 << " bitrate_index " << bitrate_index
254 << " channel_mode " << channel_mode;
255 return -1;
256 }
257
258 int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]];
259
260 if (bitrate == 0){
261 MEDIA_LOG(log_cb_) << "Invalid bitrate :" << std::hex
262 << " version " << version
263 << " layer " << layer
264 << " bitrate_index " << bitrate_index;
265 return -1;
266 }
267
268 DVLOG(2) << " bitrate " << bitrate;
269
270 int frame_sample_rate = kSampleRateMap[sample_rate_index][version];
271 if (frame_sample_rate == 0) {
272 MEDIA_LOG(log_cb_) << "Invalid sample rate :" << std::hex
273 << " version " << version
274 << " sample_rate_index " << sample_rate_index;
275 return -1;
276 }
277
278 if (sample_rate)
279 *sample_rate = frame_sample_rate;
280
281 *frame_size = 144 * bitrate * 1000 / frame_sample_rate;
282
283 if (has_padding)
284 *frame_size += (layer == 0x3) ? 4 : 1;
285
286 if (channel_layout) {
287 *channel_layout =
288 (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
289 }
290
291 if (sample_count)
292 *sample_count = (version == 3) ? 1152 : 576;
293
294 return 4;
295 }
296
297 int MP3StreamParser::ParseMP3Frame(const uint8* data, int size) {
298 DVLOG(2) << __FUNCTION__ << "(" << size << ")";
299
300 int sample_rate;
301 ChannelLayout channel_layout;
302 int frame_size;
303 int sample_count;
304 int bytes_read = ParseFrameHeader(
305 data, size, &frame_size, &sample_rate, &channel_layout, &sample_count);
306
307 if (bytes_read <= 0)
308 return bytes_read;
309
310 // Make sure data contains the entire frame.
311 if (size < frame_size)
312 return 0;
313
314 DVLOG(2) << " sample_rate " << sample_rate
315 << " channel_layout " << channel_layout
316 << " frame_size " << frame_size;
317
318 if (config_.IsValidConfig() &&
319 (config_.samples_per_second() != sample_rate ||
320 config_.channel_layout() != channel_layout)) {
321 // Clear config data so that a config change is initiated.
322 config_ = AudioDecoderConfig();
323 }
324
325 if (!config_.IsValidConfig()) {
326 config_.Initialize(kCodecMP3, kSampleFormatF32, channel_layout,
327 sample_rate, NULL, 0, false, false);
328
329 base::TimeDelta base_timestamp;
330 if (timestamp_helper_)
331 base_timestamp = timestamp_helper_->GetTimestamp();
332
333 timestamp_helper_.reset(new AudioTimestampHelper(sample_rate));
334 timestamp_helper_->SetBaseTimestamp(base_timestamp);
335
336 VideoDecoderConfig video_config;
337 bool success = config_cb_.Run(config_, video_config);
338
339 if (!init_cb_.is_null())
340 base::ResetAndReturn(&init_cb_).Run(success, kInfiniteDuration());
341
342 if (!success)
343 return -1;
344 }
345
346 if (!in_media_segment_) {
347 in_media_segment_ = true;
348 new_segment_cb_.Run();
349 }
350
351 BufferQueue audio_buffers;
352 BufferQueue video_buffers;
353
354 // TODO(acolwell): Change this code to parse as many frames as
355 // possible before calling new_buffers_cb_.
356 scoped_refptr<StreamParserBuffer> buffer =
357 StreamParserBuffer::CopyFrom(data, frame_size, true);
358 audio_buffers.push_back(buffer);
359
360 if (!new_buffers_cb_.Run(audio_buffers, video_buffers))
361 return -1;
362
363 timestamp_helper_->AddFrames(sample_count);
364
365 return frame_size;
366 }
367
368 int MP3StreamParser::ParseIcecastHeader(const uint8* data, int size){
scherkus (not reviewing) 2013/08/30 00:06:11 space before {
acolwell GONE FROM CHROMIUM 2013/08/30 01:42:22 Done.
369 DVLOG(1) << "ParseIcecastHeader(" << size << ")";
370
371 if (size < 4)
372 return 0;
373
374 if (memcmp("ICY ", data, 4))
375 return -1;
376
377 const uint8* end = data + size;
378 for (const uint8* start = data; start < end; ++start) {
379 int bytes_left = end - start;
380 const uint8* end_of_line =
381 static_cast<const uint8*>(memchr(start, '\r', bytes_left));
382 if (end_of_line == NULL)
383 break;
384
385 // TODO(acolwell): Add code to actually parse header lines.
386
387 start = end_of_line;
388 bytes_left = end - start;
389
390 if (bytes_left < 4)
391 return 0;
392
393 if (!memcmp("\r\n\r\n", start, 4)) {
394 start += 4;
395 return start - data;
396 }
397 }
398
399 if (size > kMaxIcecastHeaderSize) {
400 MEDIA_LOG(log_cb_) << "Icecast header is too large. ";
401 return -1;
402 }
403
404 return 0;
405 }
406
407 int MP3StreamParser::ParseID3v1(const uint8* data, int size) {
408 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
409
410 if (size < kID3v1Size)
411 return 0;
412
413 // TODO(acolwell): Add code to actually validate ID3v1 data and
414 // expose it as a metadata text track.
415 return (!memcmp(data, "TAG+", 4)) ? kID3v1ExtendedSize : kID3v1Size;
416 }
417
418 int MP3StreamParser::ParseID3v2(const uint8* data, int size) {
419 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
420
421 if (size < 10)
422 return 0;
423
424 BitReader reader(data, size);
425 int32 id;
426 int version;
427 uint8 flags;
428 int32 id3_size;
429
430 if (!reader.ReadBits(24, &id) ||
431 !reader.ReadBits(16, &version) ||
432 !reader.ReadBits(8, &flags) ||
433 !ParseSyncSafeInt(&reader, &id3_size)) {
434 return -1;
435 }
436
437 int32 actual_tag_size = 10 + id3_size;
438
439 // Increment size if 'Footer present' flag is set.
440 if (flags & 0x10)
441 actual_tag_size += 10;
442
443 // Make sure we have the entire tag.
444 if (size < actual_tag_size)
445 return 0;
446
447 // TODO(acolwell): Add code to actually validate ID3v2 data and
448 // expose it as a metadata text track.
449 return actual_tag_size;
450 }
451
452 bool MP3StreamParser::ParseSyncSafeInt(BitReader* reader, int32* value) {
453 *value = 0;
454 for (int i = 0; i < 4; ++i) {
455 uint8 tmp;
456 if (!reader->ReadBits(1, &tmp) || tmp != 0) {
457 MEDIA_LOG(log_cb_) << "ID3 syncsafe integer byte MSb is not 0!";
458 return false;
459 }
460
461 if (!reader->ReadBits(7, &tmp))
462 return false;
463
464 *value <<= 7;
465 *value += tmp;
466 }
467
468 return true;
469 }
470
471 int MP3StreamParser::FindNextValidStartCode(const uint8* data, int size) const {
472 const uint8* start = data;
473 const uint8* end = data + size;
474
475 while (start < end) {
476 int bytes_left = end - start;
477 const uint8* candidate_start_code =
478 static_cast<uint8*>(memchr(start, 0xff, bytes_left));
479
480 if (!candidate_start_code)
481 return 0;
482
483 bool parse_header_failed = false;
484 const uint8* sync = candidate_start_code;
485 // Try to find 3 valid frames in a row.
486 for (int i = 0; i < 3; ++i) {
487 int sync_size = end - sync;
488 int frame_size;
489 int sync_bytes = ParseFrameHeader(
490 sync, sync_size, &frame_size, NULL, NULL, NULL);
491
492 if (sync_bytes == 0)
493 return 0;
494
495 if (sync_bytes > 0) {
496 DCHECK_LT(sync_bytes, sync_size);
497
498 // Skip over this frame so we can check the next one.
499 sync += frame_size;
500
501 // Make sure the next frame starts inside the buffer.
502 if (sync >= end)
503 return 0;
504 } else {
505 DVLOG(1) << "ParseFrameHeader() " << i << " failed @" << (sync - data);
506 parse_header_failed = true;
507 break;
508 }
509 }
510
511 if (parse_header_failed) {
512 // One of the frame header parses failed so |candidate_start_code|
513 // did not point to the start of a real frame. Move |start| forward
514 // so we can find the next candidate.
515 start = candidate_start_code + 1;
516 continue;
517 }
518
519 return candidate_start_code - data;
520 }
521
522 return 0;
523 }
524
525 } // namespace mp3
526 } // namespace media
OLDNEW
« media/filters/stream_parser_factory.cc ('K') | « media/mp3/mp3_stream_parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698