Revert 224458 "Enabling MediaStreamInfoBarTest.DenyingCameraDoes..."
[chromium-blink-merge.git] / media / mp3 / mp3_stream_parser.cc
blob0688d99fcc998d89aab6bb115e352a43a75ae1a6
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/mp3/mp3_stream_parser.h"
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 #include "net/http/http_util.h"
16 namespace media {
18 static const uint32 kMP3StartCodeMask = 0xffe00000;
19 static const uint32 kICYStartCode = 0x49435920; // 'ICY '
21 // Arbitrary upper bound on the size of an IceCast header before it
22 // triggers an error.
23 static const int kMaxIcecastHeaderSize = 4096;
25 static const uint32 kID3StartCodeMask = 0xffffff00;
26 static const uint32 kID3v1StartCode = 0x54414700; // 'TAG\0'
27 static const int kID3v1Size = 128;
28 static const int kID3v1ExtendedSize = 227;
29 static const uint32 kID3v2StartCode = 0x49443300; // 'ID3\0'
31 // Map that determines which bitrate_index & channel_mode combinations
32 // are allowed.
33 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
34 static const bool kIsAllowed[17][4] = {
35 { true, true, true, true }, // free
36 { true, false, false, false }, // 32
37 { true, false, false, false }, // 48
38 { true, false, false, false }, // 56
39 { true, true, true, true }, // 64
40 { true, false, false, false }, // 80
41 { true, true, true, true }, // 96
42 { true, true, true, true }, // 112
43 { true, true, true, true }, // 128
44 { true, true, true, true }, // 160
45 { true, true, true, true }, // 192
46 { false, true, true, true }, // 224
47 { false, true, true, true }, // 256
48 { false, true, true, true }, // 320
49 { false, true, true, true }, // 384
50 { false, false, false, false } // bad
53 // Maps version and layer information in the frame header
54 // into an index for the |kBitrateMap|.
55 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
56 static const int kVersionLayerMap[4][4] = {
57 // { reserved, L3, L2, L1 }
58 { 5, 4, 4, 3 }, // MPEG 2.5
59 { 5, 5, 5, 5 }, // reserved
60 { 5, 4, 4, 3 }, // MPEG 2
61 { 5, 2, 1, 0 } // MPEG 1
64 // Maps the bitrate index field in the header and an index
65 // from |kVersionLayerMap| to a frame bitrate.
66 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
67 static const int kBitrateMap[16][6] = {
68 // { V1L1, V1L2, V1L3, V2L1, V2L2 & V2L3, reserved }
69 { 0, 0, 0, 0, 0, 0 },
70 { 32, 32, 32, 32, 8, 0 },
71 { 64, 48, 40, 48, 16, 0 },
72 { 96, 56, 48, 56, 24, 0 },
73 { 128, 64, 56, 64, 32, 0 },
74 { 160, 80, 64, 80, 40, 0 },
75 { 192, 96, 80, 96, 48, 0 },
76 { 224, 112, 96, 112, 56, 0 },
77 { 256, 128, 112, 128, 64, 0 },
78 { 288, 160, 128, 144, 80, 0 },
79 { 320, 192, 160, 160, 96, 0 },
80 { 352, 224, 192, 176, 112, 0 },
81 { 384, 256, 224, 192, 128, 0 },
82 { 416, 320, 256, 224, 144, 0 },
83 { 448, 384, 320, 256, 160, 0 },
84 { 0, 0, 0, 0, 0}
87 // Maps the sample rate index and version fields from the frame header
88 // to a sample rate.
89 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
90 static const int kSampleRateMap[4][4] = {
91 // { V2.5, reserved, V2, V1 }
92 { 11025, 0, 22050, 44100 },
93 { 12000, 0, 24000, 48000 },
94 { 8000, 0, 16000, 32000 },
95 { 0, 0, 0, 0 }
98 // Frame header field constants.
99 static const int kVersion1 = 3;
100 static const int kVersion2 = 2;
101 static const int kVersionReserved = 1;
102 static const int kVersion2_5 = 0;
103 static const int kLayerReserved = 0;
104 static const int kLayer1 = 3;
105 static const int kLayer2 = 2;
106 static const int kLayer3 = 1;
107 static const int kBitrateFree = 0;
108 static const int kBitrateBad = 0xf;
109 static const int kSampleRateReserved = 3;
111 MP3StreamParser::MP3StreamParser()
112 : state_(UNINITIALIZED),
113 in_media_segment_(false) {
116 MP3StreamParser::~MP3StreamParser() {}
118 void MP3StreamParser::Init(const InitCB& init_cb,
119 const NewConfigCB& config_cb,
120 const NewBuffersCB& new_buffers_cb,
121 const NewTextBuffersCB& text_cb,
122 const NeedKeyCB& need_key_cb,
123 const AddTextTrackCB& add_text_track_cb,
124 const NewMediaSegmentCB& new_segment_cb,
125 const base::Closure& end_of_segment_cb,
126 const LogCB& log_cb) {
127 DVLOG(1) << __FUNCTION__;
128 DCHECK_EQ(state_, UNINITIALIZED);
129 init_cb_ = init_cb;
130 config_cb_ = config_cb;
131 new_buffers_cb_ = new_buffers_cb;
132 new_segment_cb_ = new_segment_cb;
133 end_of_segment_cb_ = end_of_segment_cb;
134 log_cb_ = log_cb;
136 ChangeState(INITIALIZED);
139 void MP3StreamParser::Flush() {
140 DVLOG(1) << __FUNCTION__;
141 DCHECK_NE(state_, UNINITIALIZED);
142 queue_.Reset();
143 timestamp_helper_->SetBaseTimestamp(base::TimeDelta());
144 in_media_segment_ = false;
147 bool MP3StreamParser::Parse(const uint8* buf, int size) {
148 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
149 DCHECK(buf);
150 DCHECK_GT(size, 0);
151 DCHECK_NE(state_, UNINITIALIZED);
153 if (state_ == PARSE_ERROR)
154 return false;
156 DCHECK_EQ(state_, INITIALIZED);
158 queue_.Push(buf, size);
160 bool end_of_segment = true;
161 BufferQueue buffers;
162 for (;;) {
163 const uint8* data;
164 int data_size;
165 queue_.Peek(&data, &data_size);
167 if (size < 4)
168 break;
170 uint32 start_code = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
171 int bytes_read = 0;
172 bool parsed_metadata = true;
173 if ((start_code & kMP3StartCodeMask) == kMP3StartCodeMask) {
174 bytes_read = ParseMP3Frame(data, data_size, &buffers);
176 // Only allow the current segment to end if a full frame has been parsed.
177 end_of_segment = bytes_read > 0;
178 parsed_metadata = false;
179 } else if (start_code == kICYStartCode) {
180 bytes_read = ParseIcecastHeader(data, data_size);
181 } else if ((start_code & kID3StartCodeMask) == kID3v1StartCode) {
182 bytes_read = ParseID3v1(data, data_size);
183 } else if ((start_code & kID3StartCodeMask) == kID3v2StartCode) {
184 bytes_read = ParseID3v2(data, data_size);
185 } else {
186 bytes_read = FindNextValidStartCode(data, data_size);
188 if (bytes_read > 0) {
189 DVLOG(1) << "Unexpected start code 0x" << std::hex << start_code;
190 DVLOG(1) << "SKIPPING " << bytes_read << " bytes of garbage.";
194 CHECK_LE(bytes_read, data_size);
196 if (bytes_read < 0) {
197 ChangeState(PARSE_ERROR);
198 return false;
199 } else if (bytes_read == 0) {
200 // Need more data.
201 break;
204 // Send pending buffers if we have encountered metadata.
205 if (parsed_metadata && !buffers.empty() && !SendBuffers(&buffers, true))
206 return false;
208 queue_.Pop(bytes_read);
209 end_of_segment = true;
212 if (buffers.empty())
213 return true;
215 // Send buffers collected in this append that haven't been sent yet.
216 return SendBuffers(&buffers, end_of_segment);
219 void MP3StreamParser::ChangeState(State state) {
220 DVLOG(1) << __FUNCTION__ << "() : " << state_ << " -> " << state;
221 state_ = state;
224 int MP3StreamParser::ParseFrameHeader(const uint8* data, int size,
225 int* frame_size,
226 int* sample_rate,
227 ChannelLayout* channel_layout,
228 int* sample_count) const {
229 DCHECK(data);
230 DCHECK_GE(size, 0);
231 DCHECK(frame_size);
233 if (size < 4)
234 return 0;
236 BitReader reader(data, size);
237 int sync;
238 int version;
239 int layer;
240 int is_protected;
241 int bitrate_index;
242 int sample_rate_index;
243 int has_padding;
244 int is_private;
245 int channel_mode;
246 int other_flags;
248 if (!reader.ReadBits(11, &sync) ||
249 !reader.ReadBits(2, &version) ||
250 !reader.ReadBits(2, &layer) ||
251 !reader.ReadBits(1, &is_protected) ||
252 !reader.ReadBits(4, &bitrate_index) ||
253 !reader.ReadBits(2, &sample_rate_index) ||
254 !reader.ReadBits(1, &has_padding) ||
255 !reader.ReadBits(1, &is_private) ||
256 !reader.ReadBits(2, &channel_mode) ||
257 !reader.ReadBits(6, &other_flags)) {
258 return -1;
261 DVLOG(2) << "Header data :" << std::hex
262 << " sync 0x" << sync
263 << " version 0x" << version
264 << " layer 0x" << layer
265 << " bitrate_index 0x" << bitrate_index
266 << " sample_rate_index 0x" << sample_rate_index
267 << " channel_mode 0x" << channel_mode;
269 if (sync != 0x7ff ||
270 version == kVersionReserved ||
271 layer == kLayerReserved ||
272 bitrate_index == kBitrateFree || bitrate_index == kBitrateBad ||
273 sample_rate_index == kSampleRateReserved) {
274 MEDIA_LOG(log_cb_) << "Invalid header data :" << std::hex
275 << " sync 0x" << sync
276 << " version 0x" << version
277 << " layer 0x" << layer
278 << " bitrate_index 0x" << bitrate_index
279 << " sample_rate_index 0x" << sample_rate_index
280 << " channel_mode 0x" << channel_mode;
281 return -1;
284 if (layer == kLayer2 && kIsAllowed[bitrate_index][channel_mode]) {
285 MEDIA_LOG(log_cb_) << "Invalid (bitrate_index, channel_mode) combination :"
286 << std::hex
287 << " bitrate_index " << bitrate_index
288 << " channel_mode " << channel_mode;
289 return -1;
292 int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]];
294 if (bitrate == 0) {
295 MEDIA_LOG(log_cb_) << "Invalid bitrate :" << std::hex
296 << " version " << version
297 << " layer " << layer
298 << " bitrate_index " << bitrate_index;
299 return -1;
302 DVLOG(2) << " bitrate " << bitrate;
304 int frame_sample_rate = kSampleRateMap[sample_rate_index][version];
305 if (frame_sample_rate == 0) {
306 MEDIA_LOG(log_cb_) << "Invalid sample rate :" << std::hex
307 << " version " << version
308 << " sample_rate_index " << sample_rate_index;
309 return -1;
312 if (sample_rate)
313 *sample_rate = frame_sample_rate;
315 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
316 // Table 2.1.5
317 int samples_per_frame;
318 switch (layer) {
319 case kLayer1:
320 samples_per_frame = 384;
321 break;
323 case kLayer2:
324 samples_per_frame = 1152;
325 break;
327 case kLayer3:
328 if (version == kVersion2 || version == kVersion2_5)
329 samples_per_frame = 576;
330 else
331 samples_per_frame = 1152;
332 break;
334 default:
335 return -1;
338 if (sample_count)
339 *sample_count = samples_per_frame;
341 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
342 // Text just below Table 2.1.5.
343 if (layer == kLayer1) {
344 // This formulation is a slight variation on the equation below,
345 // but has slightly different truncation characteristics to deal
346 // with the fact that Layer 1 has 4 byte "slots" instead of single
347 // byte ones.
348 *frame_size = 4 * (12 * bitrate * 1000 / frame_sample_rate);
349 } else {
350 *frame_size =
351 ((samples_per_frame / 8) * bitrate * 1000) / frame_sample_rate;
354 if (has_padding)
355 *frame_size += (layer == kLayer1) ? 4 : 1;
357 if (channel_layout) {
358 // Map Stereo(0), Joint Stereo(1), and Dual Channel (2) to
359 // CHANNEL_LAYOUT_STEREO and Single Channel (3) to CHANNEL_LAYOUT_MONO.
360 *channel_layout =
361 (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
364 return 4;
367 int MP3StreamParser::ParseMP3Frame(const uint8* data,
368 int size,
369 BufferQueue* buffers) {
370 DVLOG(2) << __FUNCTION__ << "(" << size << ")";
372 int sample_rate;
373 ChannelLayout channel_layout;
374 int frame_size;
375 int sample_count;
376 int bytes_read = ParseFrameHeader(
377 data, size, &frame_size, &sample_rate, &channel_layout, &sample_count);
379 if (bytes_read <= 0)
380 return bytes_read;
382 // Make sure data contains the entire frame.
383 if (size < frame_size)
384 return 0;
386 DVLOG(2) << " sample_rate " << sample_rate
387 << " channel_layout " << channel_layout
388 << " frame_size " << frame_size;
390 if (config_.IsValidConfig() &&
391 (config_.samples_per_second() != sample_rate ||
392 config_.channel_layout() != channel_layout)) {
393 // Clear config data so that a config change is initiated.
394 config_ = AudioDecoderConfig();
396 // Send all buffers associated with the previous config.
397 if (!buffers->empty() && !SendBuffers(buffers, true))
398 return -1;
401 if (!config_.IsValidConfig()) {
402 config_.Initialize(kCodecMP3, kSampleFormatF32, channel_layout,
403 sample_rate, NULL, 0, false, false,
404 base::TimeDelta(), base::TimeDelta());
406 base::TimeDelta base_timestamp;
407 if (timestamp_helper_)
408 base_timestamp = timestamp_helper_->GetTimestamp();
410 timestamp_helper_.reset(new AudioTimestampHelper(sample_rate));
411 timestamp_helper_->SetBaseTimestamp(base_timestamp);
413 VideoDecoderConfig video_config;
414 bool success = config_cb_.Run(config_, video_config);
416 if (!init_cb_.is_null())
417 base::ResetAndReturn(&init_cb_).Run(success, kInfiniteDuration());
419 if (!success)
420 return -1;
423 scoped_refptr<StreamParserBuffer> buffer =
424 StreamParserBuffer::CopyFrom(data, frame_size, true);
425 buffer->set_timestamp(timestamp_helper_->GetTimestamp());
426 buffer->set_duration(timestamp_helper_->GetFrameDuration(sample_count));
427 buffers->push_back(buffer);
429 timestamp_helper_->AddFrames(sample_count);
431 return frame_size;
434 int MP3StreamParser::ParseIcecastHeader(const uint8* data, int size) {
435 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
437 if (size < 4)
438 return 0;
440 if (memcmp("ICY ", data, 4))
441 return -1;
443 int locate_size = std::min(size, kMaxIcecastHeaderSize);
444 int offset = net::HttpUtil::LocateEndOfHeaders(
445 reinterpret_cast<const char*>(data), locate_size, 4);
446 if (offset < 0) {
447 if (locate_size == kMaxIcecastHeaderSize) {
448 MEDIA_LOG(log_cb_) << "Icecast header is too large.";
449 return -1;
452 return 0;
455 return offset;
458 int MP3StreamParser::ParseID3v1(const uint8* data, int size) {
459 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
461 if (size < kID3v1Size)
462 return 0;
464 // TODO(acolwell): Add code to actually validate ID3v1 data and
465 // expose it as a metadata text track.
466 return !memcmp(data, "TAG+", 4) ? kID3v1ExtendedSize : kID3v1Size;
469 int MP3StreamParser::ParseID3v2(const uint8* data, int size) {
470 DVLOG(1) << __FUNCTION__ << "(" << size << ")";
472 if (size < 10)
473 return 0;
475 BitReader reader(data, size);
476 int32 id;
477 int version;
478 uint8 flags;
479 int32 id3_size;
481 if (!reader.ReadBits(24, &id) ||
482 !reader.ReadBits(16, &version) ||
483 !reader.ReadBits(8, &flags) ||
484 !ParseSyncSafeInt(&reader, &id3_size)) {
485 return -1;
488 int32 actual_tag_size = 10 + id3_size;
490 // Increment size if 'Footer present' flag is set.
491 if (flags & 0x10)
492 actual_tag_size += 10;
494 // Make sure we have the entire tag.
495 if (size < actual_tag_size)
496 return 0;
498 // TODO(acolwell): Add code to actually validate ID3v2 data and
499 // expose it as a metadata text track.
500 return actual_tag_size;
503 bool MP3StreamParser::ParseSyncSafeInt(BitReader* reader, int32* value) {
504 *value = 0;
505 for (int i = 0; i < 4; ++i) {
506 uint8 tmp;
507 if (!reader->ReadBits(1, &tmp) || tmp != 0) {
508 MEDIA_LOG(log_cb_) << "ID3 syncsafe integer byte MSb is not 0!";
509 return false;
512 if (!reader->ReadBits(7, &tmp))
513 return false;
515 *value <<= 7;
516 *value += tmp;
519 return true;
522 int MP3StreamParser::FindNextValidStartCode(const uint8* data, int size) const {
523 const uint8* start = data;
524 const uint8* end = data + size;
526 while (start < end) {
527 int bytes_left = end - start;
528 const uint8* candidate_start_code =
529 static_cast<const uint8*>(memchr(start, 0xff, bytes_left));
531 if (!candidate_start_code)
532 return 0;
534 bool parse_header_failed = false;
535 const uint8* sync = candidate_start_code;
536 // Try to find 3 valid frames in a row. 3 was selected to decrease
537 // the probability of false positives.
538 for (int i = 0; i < 3; ++i) {
539 int sync_size = end - sync;
540 int frame_size;
541 int sync_bytes = ParseFrameHeader(
542 sync, sync_size, &frame_size, NULL, NULL, NULL);
544 if (sync_bytes == 0)
545 return 0;
547 if (sync_bytes > 0) {
548 DCHECK_LT(sync_bytes, sync_size);
550 // Skip over this frame so we can check the next one.
551 sync += frame_size;
553 // Make sure the next frame starts inside the buffer.
554 if (sync >= end)
555 return 0;
556 } else {
557 DVLOG(1) << "ParseFrameHeader() " << i << " failed @" << (sync - data);
558 parse_header_failed = true;
559 break;
563 if (parse_header_failed) {
564 // One of the frame header parses failed so |candidate_start_code|
565 // did not point to the start of a real frame. Move |start| forward
566 // so we can find the next candidate.
567 start = candidate_start_code + 1;
568 continue;
571 return candidate_start_code - data;
574 return 0;
577 bool MP3StreamParser::SendBuffers(BufferQueue* buffers, bool end_of_segment) {
578 DCHECK(!buffers->empty());
580 if (!in_media_segment_) {
581 in_media_segment_ = true;
582 new_segment_cb_.Run();
585 BufferQueue empty_video_buffers;
586 if (!new_buffers_cb_.Run(*buffers, empty_video_buffers))
587 return false;
588 buffers->clear();
590 if (end_of_segment) {
591 in_media_segment_ = false;
592 end_of_segment_cb_.Run();
595 return true;
598 } // namespace media