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

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: Fix build busters 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) 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/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 '
scherkus (not reviewing) 2013/09/03 19:34:22 does this permit appending of icecast streams? sh
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Yes.
20 static const int kMaxIcecastHeaderSize = 4096;
scherkus (not reviewing) 2013/09/03 19:34:22 is this arbitrary? if so, I'd mention it is
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
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
scherkus (not reviewing) 2013/09/03 19:34:22 s/determined/determines/?
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
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 DVLOG(1) << __FUNCTION__;
121 DCHECK_NE(state_, UNINITIALIZED);
122 queue_.Reset();
123 timestamp_helper_->SetBaseTimestamp(base::TimeDelta());
124 in_media_segment_ = false;
125 }
126
127 bool MP3StreamParser::Parse(const uint8* buf, int size) {
128 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
129 DCHECK(buf);
130 DCHECK_GT(size, 0);
131 DCHECK_NE(state_, UNINITIALIZED);
132
133 if (state_ == PARSE_ERROR)
134 return false;
135
136 DCHECK_EQ(state_, INITIALIZED);
137
138 queue_.Push(buf, size);
139
140 for (;;) {
141 const uint8* data;
142 int data_size;
143 queue_.Peek(&data, &data_size);
144
145 if (size < 4)
146 return true;
147
148 uint32 start_code = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
149 int bytes_read = 0;
150 if ((start_code & kMP3StartCodeMask) == kMP3StartCodeMask) {
151 bytes_read = ParseMP3Frame(data, data_size);
152 } else if (start_code == kICYStartCode) {
153 bytes_read = ParseIcecastHeader(data, data_size);
154 } else if ((start_code & kID3StartCodeMask) == kID3v1StartCode) {
155 bytes_read = ParseID3v1(data, data_size);
156 } else if ((start_code & kID3StartCodeMask) == kID3v2StartCode) {
157 bytes_read = ParseID3v2(data, data_size);
158 } else {
159 bytes_read = FindNextValidStartCode(data, data_size);
160
161 if (bytes_read > 0) {
162 DVLOG(1) << "Unexpected start code 0x" << std::hex << start_code;
163 DVLOG(1) << "SKIPPING " << bytes_read << " bytes of garbage.";
164 }
165 }
166
167 CHECK_LE(bytes_read, data_size);
168
169 if (bytes_read < 0) {
170 ChangeState(PARSE_ERROR);
171 return false;
172 } else if (bytes_read == 0) {
173 // Need more data.
174 return true;
175 }
176
177 queue_.Pop(bytes_read);
178 }
179
180 return true;
181 }
182
183 void MP3StreamParser::ChangeState(State state) {
184 DVLOG(1) << __FUNCTION__ << "() : " << state_ << " -> " << state;
185 state_ = state;
186 }
187
188 int MP3StreamParser::ParseFrameHeader(const uint8* data, int size,
scherkus (not reviewing) 2013/09/03 19:34:22 there's a lot of magic constants in here that migh
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
189 int* frame_size,
190 int* sample_rate,
191 ChannelLayout* channel_layout,
192 int* sample_count) const {
193 DCHECK(data);
194 DCHECK_GE(size, 0);
195 DCHECK(frame_size);
196
197 if (size < 4)
198 return 0;
199
200 BitReader reader(data, size);
201 int sync;
202 int version;
203 int layer;
204 int is_protected;
205 int bitrate_index;
206 int sample_rate_index;
207 int has_padding;
208 int is_private;
209 int channel_mode;
210 int other_flags;
211
212 if (!reader.ReadBits(11, &sync) ||
213 !reader.ReadBits(2, &version) ||
214 !reader.ReadBits(2, &layer) ||
215 !reader.ReadBits(1, &is_protected) ||
216 !reader.ReadBits(4, &bitrate_index) ||
217 !reader.ReadBits(2, &sample_rate_index) ||
218 !reader.ReadBits(1, &has_padding) ||
219 !reader.ReadBits(1, &is_private) ||
220 !reader.ReadBits(2, &channel_mode) ||
221 !reader.ReadBits(6, &other_flags)) {
222 return -1;
223 }
224
225 DVLOG(2) << "Header data :" << std::hex
226 << " sync 0x" << sync
227 << " version 0x" << version
228 << " layer 0x" << layer
229 << " bitrate_index 0x" << bitrate_index
230 << " sample_rate_index 0x" << sample_rate_index
231 << " channel_mode 0x" << channel_mode;
232
233 if (sync != 0x7ff ||
234 version == 0 ||
235 layer == 0 ||
236 bitrate_index == 0x0 || bitrate_index == 0xf ||
237 sample_rate_index == 0x3) {
238 MEDIA_LOG(log_cb_) << "Invalid header data :" << std::hex
239 << " sync 0x" << sync
240 << " version 0x" << version
241 << " layer 0x" << layer
242 << " bitrate_index 0x" << bitrate_index
243 << " sample_rate_index 0x" << sample_rate_index
244 << " channel_mode 0x" << channel_mode;
245 return -1;
246 }
247
248 if (layer == 2 && kIsAllowed[bitrate_index][channel_mode]) {
249 MEDIA_LOG(log_cb_) << "Invalid (bitrate_index, channel_mode) combination :"
250 << std::hex
251 << " bitrate_index " << bitrate_index
252 << " channel_mode " << channel_mode;
253 return -1;
254 }
255
256 int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]];
257
258 if (bitrate == 0){
259 MEDIA_LOG(log_cb_) << "Invalid bitrate :" << std::hex
260 << " version " << version
261 << " layer " << layer
262 << " bitrate_index " << bitrate_index;
263 return -1;
264 }
265
266 DVLOG(2) << " bitrate " << bitrate;
267
268 int frame_sample_rate = kSampleRateMap[sample_rate_index][version];
269 if (frame_sample_rate == 0) {
270 MEDIA_LOG(log_cb_) << "Invalid sample rate :" << std::hex
271 << " version " << version
272 << " sample_rate_index " << sample_rate_index;
273 return -1;
274 }
275
276 if (sample_rate)
277 *sample_rate = frame_sample_rate;
278
279 *frame_size = 144 * bitrate * 1000 / frame_sample_rate;
280
281 if (has_padding)
282 *frame_size += (layer == 0x3) ? 4 : 1;
283
284 if (channel_layout) {
285 *channel_layout =
286 (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
scherkus (not reviewing) 2013/09/03 19:34:22 for this one it might be nice to call out that ste
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
287 }
288
289 if (sample_count)
290 *sample_count = (version == 3) ? 1152 : 576;
291
292 return 4;
293 }
294
295 int MP3StreamParser::ParseMP3Frame(const uint8* data, int size) {
296 DVLOG(2) << __FUNCTION__ << "(" << size << ")";
297
298 int sample_rate;
299 ChannelLayout channel_layout;
300 int frame_size;
301 int sample_count;
302 int bytes_read = ParseFrameHeader(
303 data, size, &frame_size, &sample_rate, &channel_layout, &sample_count);
304
305 if (bytes_read <= 0)
306 return bytes_read;
307
308 // Make sure data contains the entire frame.
309 if (size < frame_size)
310 return 0;
311
312 DVLOG(2) << " sample_rate " << sample_rate
313 << " channel_layout " << channel_layout
314 << " frame_size " << frame_size;
315
316 if (config_.IsValidConfig() &&
317 (config_.samples_per_second() != sample_rate ||
scherkus (not reviewing) 2013/09/03 19:34:22 I thought MP3s maintained the same sample rate / c
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 I believe this is only by convention. I believe th
318 config_.channel_layout() != channel_layout)) {
319 // Clear config data so that a config change is initiated.
320 config_ = AudioDecoderConfig();
321 }
322
323 if (!config_.IsValidConfig()) {
324 config_.Initialize(kCodecMP3, kSampleFormatF32, channel_layout,
325 sample_rate, NULL, 0, false, false);
326
327 base::TimeDelta base_timestamp;
328 if (timestamp_helper_)
329 base_timestamp = timestamp_helper_->GetTimestamp();
330
331 timestamp_helper_.reset(new AudioTimestampHelper(sample_rate));
332 timestamp_helper_->SetBaseTimestamp(base_timestamp);
333
334 VideoDecoderConfig video_config;
335 bool success = config_cb_.Run(config_, video_config);
336
337 if (!init_cb_.is_null())
338 base::ResetAndReturn(&init_cb_).Run(success, kInfiniteDuration());
339
340 if (!success)
341 return -1;
342 }
343
344 if (!in_media_segment_) {
345 in_media_segment_ = true;
346 new_segment_cb_.Run();
347 }
348
349 BufferQueue audio_buffers;
350 BufferQueue video_buffers;
351
352 // TODO(acolwell): Change this code to parse as many frames as
353 // possible before calling new_buffers_cb_.
scherkus (not reviewing) 2013/09/03 19:34:22 |new_buffers_cb_|
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
354 scoped_refptr<StreamParserBuffer> buffer =
355 StreamParserBuffer::CopyFrom(data, frame_size, true);
356 audio_buffers.push_back(buffer);
357
358 if (!new_buffers_cb_.Run(audio_buffers, video_buffers))
359 return -1;
360
361 timestamp_helper_->AddFrames(sample_count);
362
363 return frame_size;
364 }
365
366 int MP3StreamParser::ParseIcecastHeader(const uint8* data, int size) {
367 DVLOG(1) << "ParseIcecastHeader(" << size << ")";
scherkus (not reviewing) 2013/09/03 19:34:22 __FUNCTION__
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
368
369 if (size < 4)
370 return 0;
371
372 if (memcmp("ICY ", data, 4))
scherkus (not reviewing) 2013/09/03 19:34:22 after checking for ICY I believe you can use HttpU
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
373 return -1;
374
375 const uint8* end = data + size;
scherkus (not reviewing) 2013/09/03 19:34:22 would it be preferable to cast start/end to const
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 The str functions would treat \0 in a special way.
376 for (const uint8* start = data; start < end; ++start) {
scherkus (not reviewing) 2013/09/03 19:34:22 s/start/pos/? my initial scan through this code m
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
377 int bytes_left = end - start;
378 const uint8* end_of_line =
379 static_cast<const uint8*>(memchr(start, '\r', bytes_left));
380 if (end_of_line == NULL)
381 break;
382
383 // TODO(acolwell): Add code to actually parse header lines.
scherkus (not reviewing) 2013/09/03 19:34:22 OOC what sort of information can we get out of the
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Title, author, copyright, bitrate... and whether t
384
385 start = end_of_line;
386 bytes_left = end - start;
387
388 if (bytes_left < 4)
389 return 0;
390
391 if (!memcmp("\r\n\r\n", start, 4)) {
392 start += 4;
393 return start - data;
394 }
395 }
396
397 if (size > kMaxIcecastHeaderSize) {
398 MEDIA_LOG(log_cb_) << "Icecast header is too large. ";
scherkus (not reviewing) 2013/09/03 19:34:22 remove extra trailing space in " "
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
399 return -1;
400 }
401
402 return 0;
403 }
404
405 int MP3StreamParser::ParseID3v1(const uint8* data, int size) {
406 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
407
408 if (size < kID3v1Size)
409 return 0;
410
411 // TODO(acolwell): Add code to actually validate ID3v1 data and
412 // expose it as a metadata text track.
413 return (!memcmp(data, "TAG+", 4)) ? kID3v1ExtendedSize : kID3v1Size;
scherkus (not reviewing) 2013/09/03 19:34:22 nit: remove the extra () in first clause?
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Done.
414 }
415
416 int MP3StreamParser::ParseID3v2(const uint8* data, int size) {
417 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
418
419 if (size < 10)
420 return 0;
421
422 BitReader reader(data, size);
423 int32 id;
424 int version;
425 uint8 flags;
426 int32 id3_size;
427
428 if (!reader.ReadBits(24, &id) ||
429 !reader.ReadBits(16, &version) ||
430 !reader.ReadBits(8, &flags) ||
431 !ParseSyncSafeInt(&reader, &id3_size)) {
432 return -1;
433 }
434
435 int32 actual_tag_size = 10 + id3_size;
436
437 // Increment size if 'Footer present' flag is set.
438 if (flags & 0x10)
439 actual_tag_size += 10;
440
441 // Make sure we have the entire tag.
442 if (size < actual_tag_size)
443 return 0;
444
445 // TODO(acolwell): Add code to actually validate ID3v2 data and
446 // expose it as a metadata text track.
447 return actual_tag_size;
448 }
449
450 bool MP3StreamParser::ParseSyncSafeInt(BitReader* reader, int32* value) {
451 *value = 0;
452 for (int i = 0; i < 4; ++i) {
453 uint8 tmp;
454 if (!reader->ReadBits(1, &tmp) || tmp != 0) {
455 MEDIA_LOG(log_cb_) << "ID3 syncsafe integer byte MSb is not 0!";
456 return false;
457 }
458
459 if (!reader->ReadBits(7, &tmp))
460 return false;
461
462 *value <<= 7;
463 *value += tmp;
464 }
465
466 return true;
467 }
468
469 int MP3StreamParser::FindNextValidStartCode(const uint8* data, int size) const {
470 const uint8* start = data;
471 const uint8* end = data + size;
472
473 while (start < end) {
474 int bytes_left = end - start;
475 const uint8* candidate_start_code =
476 static_cast<const uint8*>(memchr(start, 0xff, bytes_left));
477
478 if (!candidate_start_code)
479 return 0;
480
481 bool parse_header_failed = false;
482 const uint8* sync = candidate_start_code;
483 // Try to find 3 valid frames in a row.
scherkus (not reviewing) 2013/09/03 19:34:22 OOC why 3?
acolwell GONE FROM CHROMIUM 2013/09/04 00:48:13 Updated the comment. Checking for only 2 headers i
484 for (int i = 0; i < 3; ++i) {
485 int sync_size = end - sync;
486 int frame_size;
487 int sync_bytes = ParseFrameHeader(
488 sync, sync_size, &frame_size, NULL, NULL, NULL);
489
490 if (sync_bytes == 0)
491 return 0;
492
493 if (sync_bytes > 0) {
494 DCHECK_LT(sync_bytes, sync_size);
495
496 // Skip over this frame so we can check the next one.
497 sync += frame_size;
498
499 // Make sure the next frame starts inside the buffer.
500 if (sync >= end)
501 return 0;
502 } else {
503 DVLOG(1) << "ParseFrameHeader() " << i << " failed @" << (sync - data);
504 parse_header_failed = true;
505 break;
506 }
507 }
508
509 if (parse_header_failed) {
510 // One of the frame header parses failed so |candidate_start_code|
511 // did not point to the start of a real frame. Move |start| forward
512 // so we can find the next candidate.
513 start = candidate_start_code + 1;
514 continue;
515 }
516
517 return candidate_start_code - data;
518 }
519
520 return 0;
521 }
522
523 } // namespace mp3
524 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698