Chromium Code Reviews| 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/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 #include "net/http/http_util.h" | |
| 15 | |
| 16 namespace media { | |
| 17 | |
| 18 static const uint32 kMP3StartCodeMask = 0xffe00000; | |
| 19 static const uint32 kICYStartCode = 0x49435920; // 'ICY ' | |
| 20 | |
| 21 // Arbitrary upper bound on the size of an IceCast header before it | |
| 22 // triggers an error. | |
| 23 static const int kMaxIcecastHeaderSize = 4096; | |
| 24 | |
| 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' | |
| 30 | |
| 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 | |
| 51 }; | |
| 52 | |
| 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 | |
| 62 }; | |
| 63 | |
| 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} | |
| 85 }; | |
| 86 | |
| 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 } | |
| 96 }; | |
| 97 | |
| 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 kVersion25 = 0; | |
|
scherkus (not reviewing)
2013/09/04 01:25:37
nit: typically I've seen dot versions use _ in pla
acolwell GONE FROM CHROMIUM
2013/09/04 01:36:01
Done.
| |
| 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; | |
| 110 | |
| 111 MP3StreamParser::MP3StreamParser() | |
| 112 : state_(UNINITIALIZED), | |
| 113 in_media_segment_(false) { | |
| 114 } | |
| 115 | |
| 116 MP3StreamParser::~MP3StreamParser() {} | |
| 117 | |
| 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; | |
| 135 | |
| 136 ChangeState(INITIALIZED); | |
| 137 } | |
| 138 | |
| 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; | |
| 145 } | |
| 146 | |
| 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); | |
| 152 | |
| 153 if (state_ == PARSE_ERROR) | |
| 154 return false; | |
| 155 | |
| 156 DCHECK_EQ(state_, INITIALIZED); | |
| 157 | |
| 158 queue_.Push(buf, size); | |
| 159 | |
| 160 for (;;) { | |
| 161 const uint8* data; | |
| 162 int data_size; | |
| 163 queue_.Peek(&data, &data_size); | |
| 164 | |
| 165 if (size < 4) | |
| 166 return true; | |
| 167 | |
| 168 uint32 start_code = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; | |
| 169 int bytes_read = 0; | |
| 170 if ((start_code & kMP3StartCodeMask) == kMP3StartCodeMask) { | |
| 171 bytes_read = ParseMP3Frame(data, data_size); | |
| 172 } else if (start_code == kICYStartCode) { | |
| 173 bytes_read = ParseIcecastHeader(data, data_size); | |
| 174 } else if ((start_code & kID3StartCodeMask) == kID3v1StartCode) { | |
| 175 bytes_read = ParseID3v1(data, data_size); | |
| 176 } else if ((start_code & kID3StartCodeMask) == kID3v2StartCode) { | |
| 177 bytes_read = ParseID3v2(data, data_size); | |
| 178 } else { | |
| 179 bytes_read = FindNextValidStartCode(data, data_size); | |
| 180 | |
| 181 if (bytes_read > 0) { | |
| 182 DVLOG(1) << "Unexpected start code 0x" << std::hex << start_code; | |
| 183 DVLOG(1) << "SKIPPING " << bytes_read << " bytes of garbage."; | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 CHECK_LE(bytes_read, data_size); | |
| 188 | |
| 189 if (bytes_read < 0) { | |
| 190 ChangeState(PARSE_ERROR); | |
| 191 return false; | |
| 192 } else if (bytes_read == 0) { | |
| 193 // Need more data. | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 queue_.Pop(bytes_read); | |
| 198 } | |
| 199 | |
| 200 return true; | |
| 201 } | |
| 202 | |
| 203 void MP3StreamParser::ChangeState(State state) { | |
| 204 DVLOG(1) << __FUNCTION__ << "() : " << state_ << " -> " << state; | |
| 205 state_ = state; | |
| 206 } | |
| 207 | |
| 208 int MP3StreamParser::ParseFrameHeader(const uint8* data, int size, | |
| 209 int* frame_size, | |
| 210 int* sample_rate, | |
| 211 ChannelLayout* channel_layout, | |
| 212 int* sample_count) const { | |
| 213 DCHECK(data); | |
| 214 DCHECK_GE(size, 0); | |
| 215 DCHECK(frame_size); | |
| 216 | |
| 217 if (size < 4) | |
| 218 return 0; | |
| 219 | |
| 220 BitReader reader(data, size); | |
| 221 int sync; | |
| 222 int version; | |
| 223 int layer; | |
| 224 int is_protected; | |
| 225 int bitrate_index; | |
| 226 int sample_rate_index; | |
| 227 int has_padding; | |
| 228 int is_private; | |
| 229 int channel_mode; | |
| 230 int other_flags; | |
| 231 | |
| 232 if (!reader.ReadBits(11, &sync) || | |
| 233 !reader.ReadBits(2, &version) || | |
| 234 !reader.ReadBits(2, &layer) || | |
| 235 !reader.ReadBits(1, &is_protected) || | |
| 236 !reader.ReadBits(4, &bitrate_index) || | |
| 237 !reader.ReadBits(2, &sample_rate_index) || | |
| 238 !reader.ReadBits(1, &has_padding) || | |
| 239 !reader.ReadBits(1, &is_private) || | |
| 240 !reader.ReadBits(2, &channel_mode) || | |
| 241 !reader.ReadBits(6, &other_flags)) { | |
| 242 return -1; | |
| 243 } | |
| 244 | |
| 245 DVLOG(2) << "Header data :" << std::hex | |
| 246 << " sync 0x" << sync | |
| 247 << " version 0x" << version | |
| 248 << " layer 0x" << layer | |
| 249 << " bitrate_index 0x" << bitrate_index | |
| 250 << " sample_rate_index 0x" << sample_rate_index | |
| 251 << " channel_mode 0x" << channel_mode; | |
| 252 | |
| 253 if (sync != 0x7ff || | |
| 254 version == kVersionReserved || | |
| 255 layer == kLayerReserved || | |
| 256 bitrate_index == kBitrateFree || bitrate_index == kBitrateBad || | |
| 257 sample_rate_index == kSampleRateReserved) { | |
| 258 MEDIA_LOG(log_cb_) << "Invalid header data :" << std::hex | |
| 259 << " sync 0x" << sync | |
| 260 << " version 0x" << version | |
| 261 << " layer 0x" << layer | |
| 262 << " bitrate_index 0x" << bitrate_index | |
| 263 << " sample_rate_index 0x" << sample_rate_index | |
| 264 << " channel_mode 0x" << channel_mode; | |
| 265 return -1; | |
| 266 } | |
| 267 | |
| 268 if (layer == kLayer2 && kIsAllowed[bitrate_index][channel_mode]) { | |
| 269 MEDIA_LOG(log_cb_) << "Invalid (bitrate_index, channel_mode) combination :" | |
| 270 << std::hex | |
| 271 << " bitrate_index " << bitrate_index | |
| 272 << " channel_mode " << channel_mode; | |
| 273 return -1; | |
| 274 } | |
| 275 | |
| 276 int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]]; | |
| 277 | |
| 278 if (bitrate == 0) { | |
| 279 MEDIA_LOG(log_cb_) << "Invalid bitrate :" << std::hex | |
| 280 << " version " << version | |
| 281 << " layer " << layer | |
| 282 << " bitrate_index " << bitrate_index; | |
| 283 return -1; | |
| 284 } | |
| 285 | |
| 286 DVLOG(2) << " bitrate " << bitrate; | |
| 287 | |
| 288 int frame_sample_rate = kSampleRateMap[sample_rate_index][version]; | |
| 289 if (frame_sample_rate == 0) { | |
| 290 MEDIA_LOG(log_cb_) << "Invalid sample rate :" << std::hex | |
| 291 << " version " << version | |
| 292 << " sample_rate_index " << sample_rate_index; | |
| 293 return -1; | |
| 294 } | |
| 295 | |
| 296 if (sample_rate) | |
| 297 *sample_rate = frame_sample_rate; | |
| 298 | |
| 299 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf | |
| 300 // Table 2.1.5 | |
| 301 int samples_per_frame; | |
| 302 switch (layer) { | |
| 303 case kLayer1: | |
| 304 samples_per_frame = 384; | |
| 305 break; | |
| 306 | |
| 307 case kLayer2: | |
| 308 samples_per_frame = 1152; | |
| 309 break; | |
| 310 | |
| 311 case kLayer3: | |
| 312 if (version == kVersion2 || version == kVersion25) | |
| 313 samples_per_frame = 576; | |
| 314 else | |
| 315 samples_per_frame = 1152; | |
| 316 break; | |
| 317 default: | |
|
scherkus (not reviewing)
2013/09/04 01:25:37
nit: add blank line before
acolwell GONE FROM CHROMIUM
2013/09/04 01:36:01
Done.
| |
| 318 return -1; | |
| 319 } | |
| 320 | |
| 321 if (sample_count) | |
| 322 *sample_count = samples_per_frame; | |
| 323 | |
| 324 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf | |
| 325 // Text just below Table 2.1.5. | |
| 326 if (layer == kLayer1) { | |
| 327 // This formulation is a slight variation on the equation below, | |
| 328 // but has slightly different truncation characteristics to deal | |
| 329 // with the fact that Layer 1 has 4 byte "slots" instead of single | |
| 330 // byte ones. | |
| 331 *frame_size = 4 * (12 * bitrate * 1000 / frame_sample_rate); | |
| 332 } else { | |
| 333 *frame_size = | |
| 334 ((samples_per_frame / 8) * bitrate * 1000) / frame_sample_rate; | |
| 335 } | |
| 336 | |
| 337 if (has_padding) | |
| 338 *frame_size += (layer == kLayer1) ? 4 : 1; | |
| 339 | |
| 340 if (channel_layout) { | |
| 341 // Map Stereo(0), Joint Stereo(1), and Dual Channel (2) to | |
| 342 // CHANNEL_LAYOUT_STEREO and Single Channel (3) to CHANNEL_LAYOUT_MONO. | |
| 343 *channel_layout = | |
| 344 (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
| 345 } | |
| 346 | |
| 347 return 4; | |
| 348 } | |
| 349 | |
| 350 int MP3StreamParser::ParseMP3Frame(const uint8* data, int size) { | |
| 351 DVLOG(2) << __FUNCTION__ << "(" << size << ")"; | |
| 352 | |
| 353 int sample_rate; | |
| 354 ChannelLayout channel_layout; | |
| 355 int frame_size; | |
| 356 int sample_count; | |
| 357 int bytes_read = ParseFrameHeader( | |
| 358 data, size, &frame_size, &sample_rate, &channel_layout, &sample_count); | |
| 359 | |
| 360 if (bytes_read <= 0) | |
| 361 return bytes_read; | |
| 362 | |
| 363 // Make sure data contains the entire frame. | |
| 364 if (size < frame_size) | |
| 365 return 0; | |
| 366 | |
| 367 DVLOG(2) << " sample_rate " << sample_rate | |
| 368 << " channel_layout " << channel_layout | |
| 369 << " frame_size " << frame_size; | |
| 370 | |
| 371 if (config_.IsValidConfig() && | |
| 372 (config_.samples_per_second() != sample_rate || | |
| 373 config_.channel_layout() != channel_layout)) { | |
| 374 // Clear config data so that a config change is initiated. | |
| 375 config_ = AudioDecoderConfig(); | |
| 376 } | |
| 377 | |
| 378 if (!config_.IsValidConfig()) { | |
| 379 config_.Initialize(kCodecMP3, kSampleFormatF32, channel_layout, | |
| 380 sample_rate, NULL, 0, false, false); | |
| 381 | |
| 382 base::TimeDelta base_timestamp; | |
| 383 if (timestamp_helper_) | |
| 384 base_timestamp = timestamp_helper_->GetTimestamp(); | |
| 385 | |
| 386 timestamp_helper_.reset(new AudioTimestampHelper(sample_rate)); | |
| 387 timestamp_helper_->SetBaseTimestamp(base_timestamp); | |
| 388 | |
| 389 VideoDecoderConfig video_config; | |
| 390 bool success = config_cb_.Run(config_, video_config); | |
| 391 | |
| 392 if (!init_cb_.is_null()) | |
| 393 base::ResetAndReturn(&init_cb_).Run(success, kInfiniteDuration()); | |
| 394 | |
| 395 if (!success) | |
| 396 return -1; | |
| 397 } | |
| 398 | |
| 399 if (!in_media_segment_) { | |
| 400 in_media_segment_ = true; | |
| 401 new_segment_cb_.Run(); | |
| 402 } | |
| 403 | |
| 404 BufferQueue audio_buffers; | |
| 405 BufferQueue video_buffers; | |
| 406 | |
| 407 // TODO(acolwell): Change this code to parse as many frames as | |
| 408 // possible before calling |new_buffers_cb_|. | |
| 409 scoped_refptr<StreamParserBuffer> buffer = | |
| 410 StreamParserBuffer::CopyFrom(data, frame_size, true); | |
| 411 audio_buffers.push_back(buffer); | |
| 412 | |
| 413 if (!new_buffers_cb_.Run(audio_buffers, video_buffers)) | |
| 414 return -1; | |
| 415 | |
| 416 timestamp_helper_->AddFrames(sample_count); | |
| 417 | |
| 418 return frame_size; | |
| 419 } | |
| 420 | |
| 421 int MP3StreamParser::ParseIcecastHeader(const uint8* data, int size) { | |
| 422 DVLOG(1) << __FUNCTION__ << "(" << size << ")"; | |
| 423 | |
| 424 if (size < 4) | |
| 425 return 0; | |
| 426 | |
| 427 if (memcmp("ICY ", data, 4)) | |
| 428 return -1; | |
| 429 | |
| 430 int offset = net::HttpUtil::LocateEndOfHeaders( | |
| 431 reinterpret_cast<const char*>(data), size, 4); | |
| 432 if (offset < 0) | |
| 433 return 0; | |
| 434 | |
| 435 if (size > kMaxIcecastHeaderSize) { | |
|
scherkus (not reviewing)
2013/09/04 01:25:37
isn't this checking that ParseIcecastHeader() is n
acolwell GONE FROM CHROMIUM
2013/09/04 01:36:01
Yeah. That was one bug with this code. I've fixed
| |
| 436 MEDIA_LOG(log_cb_) << "Icecast header is too large."; | |
| 437 return -1; | |
| 438 } | |
| 439 | |
| 440 return offset; | |
| 441 } | |
| 442 | |
| 443 int MP3StreamParser::ParseID3v1(const uint8* data, int size) { | |
| 444 DVLOG(1) << __FUNCTION__ << "(" << size << ")"; | |
| 445 | |
| 446 if (size < kID3v1Size) | |
| 447 return 0; | |
| 448 | |
| 449 // TODO(acolwell): Add code to actually validate ID3v1 data and | |
| 450 // expose it as a metadata text track. | |
| 451 return !memcmp(data, "TAG+", 4) ? kID3v1ExtendedSize : kID3v1Size; | |
| 452 } | |
| 453 | |
| 454 int MP3StreamParser::ParseID3v2(const uint8* data, int size) { | |
| 455 DVLOG(1) << __FUNCTION__ << "(" << size << ")"; | |
| 456 | |
| 457 if (size < 10) | |
| 458 return 0; | |
| 459 | |
| 460 BitReader reader(data, size); | |
| 461 int32 id; | |
| 462 int version; | |
| 463 uint8 flags; | |
| 464 int32 id3_size; | |
| 465 | |
| 466 if (!reader.ReadBits(24, &id) || | |
| 467 !reader.ReadBits(16, &version) || | |
| 468 !reader.ReadBits(8, &flags) || | |
| 469 !ParseSyncSafeInt(&reader, &id3_size)) { | |
| 470 return -1; | |
| 471 } | |
| 472 | |
| 473 int32 actual_tag_size = 10 + id3_size; | |
| 474 | |
| 475 // Increment size if 'Footer present' flag is set. | |
| 476 if (flags & 0x10) | |
| 477 actual_tag_size += 10; | |
| 478 | |
| 479 // Make sure we have the entire tag. | |
| 480 if (size < actual_tag_size) | |
| 481 return 0; | |
| 482 | |
| 483 // TODO(acolwell): Add code to actually validate ID3v2 data and | |
| 484 // expose it as a metadata text track. | |
| 485 return actual_tag_size; | |
| 486 } | |
| 487 | |
| 488 bool MP3StreamParser::ParseSyncSafeInt(BitReader* reader, int32* value) { | |
| 489 *value = 0; | |
| 490 for (int i = 0; i < 4; ++i) { | |
| 491 uint8 tmp; | |
| 492 if (!reader->ReadBits(1, &tmp) || tmp != 0) { | |
| 493 MEDIA_LOG(log_cb_) << "ID3 syncsafe integer byte MSb is not 0!"; | |
| 494 return false; | |
| 495 } | |
| 496 | |
| 497 if (!reader->ReadBits(7, &tmp)) | |
| 498 return false; | |
| 499 | |
| 500 *value <<= 7; | |
| 501 *value += tmp; | |
| 502 } | |
| 503 | |
| 504 return true; | |
| 505 } | |
| 506 | |
| 507 int MP3StreamParser::FindNextValidStartCode(const uint8* data, int size) const { | |
| 508 const uint8* start = data; | |
| 509 const uint8* end = data + size; | |
| 510 | |
| 511 while (start < end) { | |
| 512 int bytes_left = end - start; | |
| 513 const uint8* candidate_start_code = | |
| 514 static_cast<const uint8*>(memchr(start, 0xff, bytes_left)); | |
| 515 | |
| 516 if (!candidate_start_code) | |
| 517 return 0; | |
| 518 | |
| 519 bool parse_header_failed = false; | |
| 520 const uint8* sync = candidate_start_code; | |
| 521 // Try to find 3 valid frames in a row. 3 was selected to decrease | |
| 522 // the probability of false positives. | |
| 523 for (int i = 0; i < 3; ++i) { | |
| 524 int sync_size = end - sync; | |
| 525 int frame_size; | |
| 526 int sync_bytes = ParseFrameHeader( | |
| 527 sync, sync_size, &frame_size, NULL, NULL, NULL); | |
| 528 | |
| 529 if (sync_bytes == 0) | |
| 530 return 0; | |
| 531 | |
| 532 if (sync_bytes > 0) { | |
| 533 DCHECK_LT(sync_bytes, sync_size); | |
| 534 | |
| 535 // Skip over this frame so we can check the next one. | |
| 536 sync += frame_size; | |
| 537 | |
| 538 // Make sure the next frame starts inside the buffer. | |
| 539 if (sync >= end) | |
| 540 return 0; | |
| 541 } else { | |
| 542 DVLOG(1) << "ParseFrameHeader() " << i << " failed @" << (sync - data); | |
| 543 parse_header_failed = true; | |
| 544 break; | |
| 545 } | |
| 546 } | |
| 547 | |
| 548 if (parse_header_failed) { | |
| 549 // One of the frame header parses failed so |candidate_start_code| | |
| 550 // did not point to the start of a real frame. Move |start| forward | |
| 551 // so we can find the next candidate. | |
| 552 start = candidate_start_code + 1; | |
| 553 continue; | |
| 554 } | |
| 555 | |
| 556 return candidate_start_code - data; | |
| 557 } | |
| 558 | |
| 559 return 0; | |
| 560 } | |
| 561 | |
| 562 } // namespace media | |
| OLD | NEW |