ATSParser.cpp revision 85f12e9b9062402d6110df3f7099707912040edb
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "ATSParser.h" 18 19#include "AnotherPacketSource.h" 20#include "include/avc_utils.h" 21 22#include <media/stagefright/foundation/ABitReader.h> 23#include <media/stagefright/foundation/ABuffer.h> 24#include <media/stagefright/foundation/ADebug.h> 25#include <media/stagefright/foundation/AMessage.h> 26#include <media/stagefright/foundation/hexdump.h> 27#include <media/stagefright/MediaDefs.h> 28#include <media/stagefright/MediaErrors.h> 29#include <media/stagefright/MetaData.h> 30#include <utils/KeyedVector.h> 31 32namespace android { 33 34static const size_t kTSPacketSize = 188; 35 36struct ATSParser::Program : public RefBase { 37 Program(unsigned programMapPID); 38 39 bool parsePID( 40 unsigned pid, unsigned payload_unit_start_indicator, 41 ABitReader *br); 42 43 sp<MediaSource> getSource(SourceType type); 44 45private: 46 unsigned mProgramMapPID; 47 KeyedVector<unsigned, sp<Stream> > mStreams; 48 49 void parseProgramMap(ABitReader *br); 50 51 DISALLOW_EVIL_CONSTRUCTORS(Program); 52}; 53 54struct ATSParser::Stream : public RefBase { 55 Stream(unsigned elementaryPID, unsigned streamType); 56 57 void parse( 58 unsigned payload_unit_start_indicator, 59 ABitReader *br); 60 61 sp<MediaSource> getSource(SourceType type); 62 63protected: 64 virtual ~Stream(); 65 66private: 67 unsigned mElementaryPID; 68 unsigned mStreamType; 69 70 sp<ABuffer> mBuffer; 71 sp<AnotherPacketSource> mSource; 72 bool mPayloadStarted; 73 74 void flush(); 75 void parsePES(ABitReader *br); 76 77 void onPayloadData( 78 unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS, 79 const uint8_t *data, size_t size); 80 81 DISALLOW_EVIL_CONSTRUCTORS(Stream); 82}; 83 84//////////////////////////////////////////////////////////////////////////////// 85 86ATSParser::Program::Program(unsigned programMapPID) 87 : mProgramMapPID(programMapPID) { 88} 89 90bool ATSParser::Program::parsePID( 91 unsigned pid, unsigned payload_unit_start_indicator, 92 ABitReader *br) { 93 if (pid == mProgramMapPID) { 94 if (payload_unit_start_indicator) { 95 unsigned skip = br->getBits(8); 96 br->skipBits(skip * 8); 97 } 98 99 parseProgramMap(br); 100 return true; 101 } 102 103 ssize_t index = mStreams.indexOfKey(pid); 104 if (index < 0) { 105 return false; 106 } 107 108 mStreams.editValueAt(index)->parse( 109 payload_unit_start_indicator, br); 110 111 return true; 112} 113 114void ATSParser::Program::parseProgramMap(ABitReader *br) { 115 unsigned table_id = br->getBits(8); 116 LOG(VERBOSE) << " table_id = " << table_id; 117 CHECK_EQ(table_id, 0x02u); 118 119 unsigned section_syntax_indictor = br->getBits(1); 120 LOG(VERBOSE) << " section_syntax_indictor = " << section_syntax_indictor; 121 CHECK_EQ(section_syntax_indictor, 1u); 122 123 CHECK_EQ(br->getBits(1), 0u); 124 LOG(VERBOSE) << " reserved = " << br->getBits(2); 125 126 unsigned section_length = br->getBits(12); 127 LOG(VERBOSE) << " section_length = " << section_length; 128 CHECK((section_length & 0xc00) == 0); 129 CHECK_LE(section_length, 1021u); 130 131 LOG(VERBOSE) << " program_number = " << br->getBits(16); 132 LOG(VERBOSE) << " reserved = " << br->getBits(2); 133 LOG(VERBOSE) << " version_number = " << br->getBits(5); 134 LOG(VERBOSE) << " current_next_indicator = " << br->getBits(1); 135 LOG(VERBOSE) << " section_number = " << br->getBits(8); 136 LOG(VERBOSE) << " last_section_number = " << br->getBits(8); 137 LOG(VERBOSE) << " reserved = " << br->getBits(3); 138 139 LOG(VERBOSE) << " PCR_PID = " 140 << StringPrintf("0x%04x", br->getBits(13)); 141 142 LOG(VERBOSE) << " reserved = " << br->getBits(4); 143 144 unsigned program_info_length = br->getBits(12); 145 LOG(VERBOSE) << " program_info_length = " << program_info_length; 146 CHECK((program_info_length & 0xc00) == 0); 147 148 br->skipBits(program_info_length * 8); // skip descriptors 149 150 // infoBytesRemaining is the number of bytes that make up the 151 // variable length section of ES_infos. It does not include the 152 // final CRC. 153 size_t infoBytesRemaining = section_length - 9 - program_info_length - 4; 154 155 while (infoBytesRemaining > 0) { 156 CHECK_GE(infoBytesRemaining, 5u); 157 158 unsigned streamType = br->getBits(8); 159 LOG(VERBOSE) << " stream_type = " 160 << StringPrintf("0x%02x", streamType); 161 162 LOG(VERBOSE) << " reserved = " << br->getBits(3); 163 164 unsigned elementaryPID = br->getBits(13); 165 LOG(VERBOSE) << " elementary_PID = " 166 << StringPrintf("0x%04x", elementaryPID); 167 168 LOG(VERBOSE) << " reserved = " << br->getBits(4); 169 170 unsigned ES_info_length = br->getBits(12); 171 LOG(VERBOSE) << " ES_info_length = " << ES_info_length; 172 CHECK((ES_info_length & 0xc00) == 0); 173 174 CHECK_GE(infoBytesRemaining - 5, ES_info_length); 175 176#if 0 177 br->skipBits(ES_info_length * 8); // skip descriptors 178#else 179 unsigned info_bytes_remaining = ES_info_length; 180 while (info_bytes_remaining >= 2) { 181 LOG(VERBOSE) << " tag = " << StringPrintf("0x%02x", br->getBits(8)); 182 183 unsigned descLength = br->getBits(8); 184 LOG(VERBOSE) << " len = " << descLength; 185 186 CHECK_GE(info_bytes_remaining, 2 + descLength); 187 188 br->skipBits(descLength * 8); 189 190 info_bytes_remaining -= descLength + 2; 191 } 192 CHECK_EQ(info_bytes_remaining, 0u); 193#endif 194 195 ssize_t index = mStreams.indexOfKey(elementaryPID); 196#if 0 // XXX revisit 197 CHECK_LT(index, 0); 198 mStreams.add(elementaryPID, new Stream(elementaryPID, streamType)); 199#else 200 if (index < 0) { 201 mStreams.add(elementaryPID, new Stream(elementaryPID, streamType)); 202 } 203#endif 204 205 infoBytesRemaining -= 5 + ES_info_length; 206 } 207 208 CHECK_EQ(infoBytesRemaining, 0u); 209 210 LOG(VERBOSE) << " CRC = " << StringPrintf("0x%08x", br->getBits(32)); 211} 212 213sp<MediaSource> ATSParser::Program::getSource(SourceType type) { 214 for (size_t i = 0; i < mStreams.size(); ++i) { 215 sp<MediaSource> source = mStreams.editValueAt(i)->getSource(type); 216 if (source != NULL) { 217 return source; 218 } 219 } 220 221 return NULL; 222} 223 224//////////////////////////////////////////////////////////////////////////////// 225 226ATSParser::Stream::Stream(unsigned elementaryPID, unsigned streamType) 227 : mElementaryPID(elementaryPID), 228 mStreamType(streamType), 229 mBuffer(new ABuffer(65536)), 230 mPayloadStarted(false) { 231 mBuffer->setRange(0, 0); 232} 233 234ATSParser::Stream::~Stream() { 235} 236 237void ATSParser::Stream::parse( 238 unsigned payload_unit_start_indicator, ABitReader *br) { 239 if (payload_unit_start_indicator) { 240 if (mPayloadStarted) { 241 // Otherwise we run the danger of receiving the trailing bytes 242 // of a PES packet that we never saw the start of and assuming 243 // we have a a complete PES packet. 244 245 flush(); 246 } 247 248 mPayloadStarted = true; 249 } 250 251 if (!mPayloadStarted) { 252 return; 253 } 254 255 size_t payloadSizeBits = br->numBitsLeft(); 256 CHECK_EQ(payloadSizeBits % 8, 0u); 257 258 CHECK_LE(mBuffer->size() + payloadSizeBits / 8, mBuffer->capacity()); 259 260 memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8); 261 mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8); 262} 263 264void ATSParser::Stream::parsePES(ABitReader *br) { 265 unsigned packet_startcode_prefix = br->getBits(24); 266 267 LOG(VERBOSE) << "packet_startcode_prefix = " 268 << StringPrintf("0x%08x", packet_startcode_prefix); 269 270 CHECK_EQ(packet_startcode_prefix, 0x000001u); 271 272 unsigned stream_id = br->getBits(8); 273 LOG(VERBOSE) << "stream_id = " << StringPrintf("0x%02x", stream_id); 274 275 unsigned PES_packet_length = br->getBits(16); 276 LOG(VERBOSE) << "PES_packet_length = " << PES_packet_length; 277 278 if (stream_id != 0xbc // program_stream_map 279 && stream_id != 0xbe // padding_stream 280 && stream_id != 0xbf // private_stream_2 281 && stream_id != 0xf0 // ECM 282 && stream_id != 0xf1 // EMM 283 && stream_id != 0xff // program_stream_directory 284 && stream_id != 0xf2 // DSMCC 285 && stream_id != 0xf8) { // H.222.1 type E 286 CHECK_EQ(br->getBits(2), 2u); 287 288 LOG(VERBOSE) << "PES_scrambling_control = " << br->getBits(2); 289 LOG(VERBOSE) << "PES_priority = " << br->getBits(1); 290 LOG(VERBOSE) << "data_alignment_indicator = " << br->getBits(1); 291 LOG(VERBOSE) << "copyright = " << br->getBits(1); 292 LOG(VERBOSE) << "original_or_copy = " << br->getBits(1); 293 294 unsigned PTS_DTS_flags = br->getBits(2); 295 LOG(VERBOSE) << "PTS_DTS_flags = " << PTS_DTS_flags; 296 297 unsigned ESCR_flag = br->getBits(1); 298 LOG(VERBOSE) << "ESCR_flag = " << ESCR_flag; 299 300 unsigned ES_rate_flag = br->getBits(1); 301 LOG(VERBOSE) << "ES_rate_flag = " << ES_rate_flag; 302 303 unsigned DSM_trick_mode_flag = br->getBits(1); 304 LOG(VERBOSE) << "DSM_trick_mode_flag = " << DSM_trick_mode_flag; 305 306 unsigned additional_copy_info_flag = br->getBits(1); 307 LOG(VERBOSE) << "additional_copy_info_flag = " 308 << additional_copy_info_flag; 309 310 LOG(VERBOSE) << "PES_CRC_flag = " << br->getBits(1); 311 LOG(VERBOSE) << "PES_extension_flag = " << br->getBits(1); 312 313 unsigned PES_header_data_length = br->getBits(8); 314 LOG(VERBOSE) << "PES_header_data_length = " << PES_header_data_length; 315 316 unsigned optional_bytes_remaining = PES_header_data_length; 317 318 uint64_t PTS = 0, DTS = 0; 319 320 if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) { 321 CHECK_GE(optional_bytes_remaining, 5u); 322 323 CHECK_EQ(br->getBits(4), PTS_DTS_flags); 324 325 PTS = ((uint64_t)br->getBits(3)) << 30; 326 CHECK_EQ(br->getBits(1), 1u); 327 PTS |= ((uint64_t)br->getBits(15)) << 15; 328 CHECK_EQ(br->getBits(1), 1u); 329 PTS |= br->getBits(15); 330 CHECK_EQ(br->getBits(1), 1u); 331 332 LOG(VERBOSE) << "PTS = " << PTS; 333 // LOG(INFO) << "PTS = " << PTS / 90000.0f << " secs"; 334 335 optional_bytes_remaining -= 5; 336 337 if (PTS_DTS_flags == 3) { 338 CHECK_GE(optional_bytes_remaining, 5u); 339 340 CHECK_EQ(br->getBits(4), 1u); 341 342 DTS = ((uint64_t)br->getBits(3)) << 30; 343 CHECK_EQ(br->getBits(1), 1u); 344 DTS |= ((uint64_t)br->getBits(15)) << 15; 345 CHECK_EQ(br->getBits(1), 1u); 346 DTS |= br->getBits(15); 347 CHECK_EQ(br->getBits(1), 1u); 348 349 LOG(VERBOSE) << "DTS = " << DTS; 350 351 optional_bytes_remaining -= 5; 352 } 353 } 354 355 if (ESCR_flag) { 356 CHECK_GE(optional_bytes_remaining, 6u); 357 358 br->getBits(2); 359 360 uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30; 361 CHECK_EQ(br->getBits(1), 1u); 362 ESCR |= ((uint64_t)br->getBits(15)) << 15; 363 CHECK_EQ(br->getBits(1), 1u); 364 ESCR |= br->getBits(15); 365 CHECK_EQ(br->getBits(1), 1u); 366 367 LOG(VERBOSE) << "ESCR = " << ESCR; 368 LOG(VERBOSE) << "ESCR_extension = " << br->getBits(9); 369 370 CHECK_EQ(br->getBits(1), 1u); 371 372 optional_bytes_remaining -= 6; 373 } 374 375 if (ES_rate_flag) { 376 CHECK_GE(optional_bytes_remaining, 3u); 377 378 CHECK_EQ(br->getBits(1), 1u); 379 LOG(VERBOSE) << "ES_rate = " << br->getBits(22); 380 CHECK_EQ(br->getBits(1), 1u); 381 382 optional_bytes_remaining -= 3; 383 } 384 385 br->skipBits(optional_bytes_remaining * 8); 386 387 // ES data follows. 388 389 onPayloadData( 390 PTS_DTS_flags, PTS, DTS, 391 br->data(), br->numBitsLeft() / 8); 392 393 if (PES_packet_length != 0) { 394 CHECK_GE(PES_packet_length, PES_header_data_length + 3); 395 396 unsigned dataLength = 397 PES_packet_length - 3 - PES_header_data_length; 398 399 CHECK_EQ(br->numBitsLeft(), dataLength * 8); 400 401 br->skipBits(dataLength * 8); 402 } else { 403 size_t payloadSizeBits = br->numBitsLeft(); 404 CHECK((payloadSizeBits % 8) == 0); 405 406 LOG(VERBOSE) << "There's " << (payloadSizeBits / 8) 407 << " bytes of payload."; 408 } 409 } else if (stream_id == 0xbe) { // padding_stream 410 CHECK_NE(PES_packet_length, 0u); 411 br->skipBits(PES_packet_length * 8); 412 } else { 413 CHECK_NE(PES_packet_length, 0u); 414 br->skipBits(PES_packet_length * 8); 415 } 416} 417 418void ATSParser::Stream::flush() { 419 if (mBuffer->size() == 0) { 420 return; 421 } 422 423 LOG(VERBOSE) << "flushing stream " 424 << StringPrintf("0x%04x", mElementaryPID) 425 << " size = " << mBuffer->size(); 426 427 ABitReader br(mBuffer->data(), mBuffer->size()); 428 parsePES(&br); 429 430 mBuffer->setRange(0, 0); 431} 432 433static sp<ABuffer> FindNAL( 434 const uint8_t *data, size_t size, unsigned nalType, 435 size_t *stopOffset) { 436 bool foundStart = false; 437 size_t startOffset = 0; 438 439 size_t offset = 0; 440 for (;;) { 441 while (offset + 3 < size 442 && memcmp("\x00\x00\x00\x01", &data[offset], 4)) { 443 ++offset; 444 } 445 446 if (foundStart) { 447 size_t nalSize; 448 if (offset + 3 >= size) { 449 nalSize = size - startOffset; 450 } else { 451 nalSize = offset - startOffset; 452 } 453 454 sp<ABuffer> nal = new ABuffer(nalSize); 455 memcpy(nal->data(), &data[startOffset], nalSize); 456 457 if (stopOffset != NULL) { 458 *stopOffset = startOffset + nalSize; 459 } 460 461 return nal; 462 } 463 464 if (offset + 4 >= size) { 465 return NULL; 466 } 467 468 if ((data[offset + 4] & 0x1f) == nalType) { 469 foundStart = true; 470 startOffset = offset + 4; 471 } 472 473 offset += 4; 474 } 475} 476 477static sp<ABuffer> MakeAVCCodecSpecificData( 478 const sp<ABuffer> &buffer, int32_t *width, int32_t *height) { 479 const uint8_t *data = buffer->data(); 480 size_t size = buffer->size(); 481 482 sp<ABuffer> seqParamSet = FindNAL(data, size, 7, NULL); 483 if (seqParamSet == NULL) { 484 return NULL; 485 } 486 487 FindAVCDimensions(seqParamSet, width, height); 488 489 size_t stopOffset; 490 sp<ABuffer> picParamSet = FindNAL(data, size, 8, &stopOffset); 491 CHECK(picParamSet != NULL); 492 493 buffer->setRange(stopOffset, size - stopOffset); 494 LOG(INFO) << "buffer has " << buffer->size() << " bytes left."; 495 496 size_t csdSize = 497 1 + 3 + 1 + 1 498 + 2 * 1 + seqParamSet->size() 499 + 1 + 2 * 1 + picParamSet->size(); 500 501 sp<ABuffer> csd = new ABuffer(csdSize); 502 uint8_t *out = csd->data(); 503 504 *out++ = 0x01; // configurationVersion 505 memcpy(out, seqParamSet->data() + 1, 3); // profile/level... 506 out += 3; 507 *out++ = (0x3f << 2) | 1; // lengthSize == 2 bytes 508 *out++ = 0xe0 | 1; 509 510 *out++ = seqParamSet->size() >> 8; 511 *out++ = seqParamSet->size() & 0xff; 512 memcpy(out, seqParamSet->data(), seqParamSet->size()); 513 out += seqParamSet->size(); 514 515 *out++ = 1; 516 517 *out++ = picParamSet->size() >> 8; 518 *out++ = picParamSet->size() & 0xff; 519 memcpy(out, picParamSet->data(), picParamSet->size()); 520 521 return csd; 522} 523 524static bool getNextNALUnit( 525 const uint8_t **_data, size_t *_size, 526 const uint8_t **nalStart, size_t *nalSize) { 527 const uint8_t *data = *_data; 528 size_t size = *_size; 529 530 *nalStart = NULL; 531 *nalSize = 0; 532 533 if (size == 0) { 534 return false; 535 } 536 537 size_t offset = 0; 538 for (;;) { 539 CHECK_LT(offset + 2, size); 540 541 if (!memcmp("\x00\x00\x01", &data[offset], 3)) { 542 break; 543 } 544 545 CHECK_EQ((unsigned)data[offset], 0x00u); 546 ++offset; 547 } 548 549 offset += 3; 550 size_t startOffset = offset; 551 552 while (offset + 2 < size 553 && memcmp("\x00\x00\x00", &data[offset], 3) 554 && memcmp("\x00\x00\x01", &data[offset], 3)) { 555 ++offset; 556 } 557 558 if (offset + 2 >= size) { 559 *nalStart = &data[startOffset]; 560 *nalSize = size - startOffset; 561 562 *_data = NULL; 563 *_size = 0; 564 565 return true; 566 } 567 568 size_t endOffset = offset; 569 570 while (offset + 2 < size && memcmp("\x00\x00\x01", &data[offset], 3)) { 571 CHECK_EQ((unsigned)data[offset], 0x00u); 572 ++offset; 573 } 574 575 CHECK_LT(offset + 2, size); 576 577 *nalStart = &data[startOffset]; 578 *nalSize = endOffset - startOffset; 579 580 *_data = &data[offset]; 581 *_size = size - offset; 582 583 return true; 584} 585 586sp<ABuffer> MakeCleanAVCData(const uint8_t *data, size_t size) { 587 const uint8_t *tmpData = data; 588 size_t tmpSize = size; 589 590 size_t totalSize = 0; 591 const uint8_t *nalStart; 592 size_t nalSize; 593 while (getNextNALUnit(&tmpData, &tmpSize, &nalStart, &nalSize)) { 594 totalSize += 4 + nalSize; 595 } 596 597 sp<ABuffer> buffer = new ABuffer(totalSize); 598 size_t offset = 0; 599 while (getNextNALUnit(&data, &size, &nalStart, &nalSize)) { 600 memcpy(buffer->data() + offset, "\x00\x00\x00\x01", 4); 601 memcpy(buffer->data() + offset + 4, nalStart, nalSize); 602 603 offset += 4 + nalSize; 604 } 605 606 return buffer; 607} 608 609static sp<ABuffer> FindMPEG2ADTSConfig( 610 const sp<ABuffer> &buffer, int32_t *sampleRate, int32_t *channelCount) { 611 ABitReader br(buffer->data(), buffer->size()); 612 613 CHECK_EQ(br.getBits(12), 0xfffu); 614 CHECK_EQ(br.getBits(1), 0u); 615 CHECK_EQ(br.getBits(2), 0u); 616 br.getBits(1); // protection_absent 617 unsigned profile = br.getBits(2); 618 LOG(INFO) << "profile = " << profile; 619 CHECK_NE(profile, 3u); 620 unsigned sampling_freq_index = br.getBits(4); 621 br.getBits(1); // private_bit 622 unsigned channel_configuration = br.getBits(3); 623 CHECK_NE(channel_configuration, 0u); 624 625 LOG(INFO) << "sampling_freq_index = " << sampling_freq_index; 626 LOG(INFO) << "channel_configuration = " << channel_configuration; 627 628 CHECK_LE(sampling_freq_index, 11u); 629 static const int32_t kSamplingFreq[] = { 630 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 631 16000, 12000, 11025, 8000 632 }; 633 *sampleRate = kSamplingFreq[sampling_freq_index]; 634 635 *channelCount = channel_configuration; 636 637 static const uint8_t kStaticESDS[] = { 638 0x03, 22, 639 0x00, 0x00, // ES_ID 640 0x00, // streamDependenceFlag, URL_Flag, OCRstreamFlag 641 642 0x04, 17, 643 0x40, // Audio ISO/IEC 14496-3 644 0x00, 0x00, 0x00, 0x00, 645 0x00, 0x00, 0x00, 0x00, 646 0x00, 0x00, 0x00, 0x00, 647 648 0x05, 2, 649 // AudioSpecificInfo follows 650 651 // oooo offf fccc c000 652 // o - audioObjectType 653 // f - samplingFreqIndex 654 // c - channelConfig 655 }; 656 sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2); 657 memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS)); 658 659 csd->data()[sizeof(kStaticESDS)] = 660 ((profile + 1) << 3) | (sampling_freq_index >> 1); 661 662 csd->data()[sizeof(kStaticESDS) + 1] = 663 ((sampling_freq_index << 7) & 0x80) | (channel_configuration << 3); 664 665 hexdump(csd->data(), csd->size()); 666 return csd; 667} 668 669void ATSParser::Stream::onPayloadData( 670 unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS, 671 const uint8_t *data, size_t size) { 672 LOG(VERBOSE) << "onPayloadData mStreamType=" 673 << StringPrintf("0x%02x", mStreamType); 674 675 sp<ABuffer> buffer; 676 677 if (mStreamType == 0x1b) { 678 buffer = MakeCleanAVCData(data, size); 679 } else { 680 // hexdump(data, size); 681 682 buffer = new ABuffer(size); 683 memcpy(buffer->data(), data, size); 684 } 685 686 if (mSource == NULL) { 687 sp<MetaData> meta = new MetaData; 688 689 if (mStreamType == 0x1b) { 690 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 691 692 int32_t width, height; 693 sp<ABuffer> csd = MakeAVCCodecSpecificData(buffer, &width, &height); 694 695 if (csd == NULL) { 696 return; 697 } 698 699 meta->setData(kKeyAVCC, 0, csd->data(), csd->size()); 700 meta->setInt32(kKeyWidth, width); 701 meta->setInt32(kKeyHeight, height); 702 } else { 703 CHECK_EQ(mStreamType, 0x0fu); 704 705 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); 706 707 int32_t sampleRate, channelCount; 708 sp<ABuffer> csd = 709 FindMPEG2ADTSConfig(buffer, &sampleRate, &channelCount); 710 711 LOG(INFO) << "sampleRate = " << sampleRate; 712 LOG(INFO) << "channelCount = " << channelCount; 713 714 meta->setInt32(kKeySampleRate, sampleRate); 715 meta->setInt32(kKeyChannelCount, channelCount); 716 717 meta->setData(kKeyESDS, 0, csd->data(), csd->size()); 718 } 719 720 LOG(INFO) << "created source!"; 721 mSource = new AnotherPacketSource(meta); 722 723 // fall through 724 } 725 726 CHECK(PTS_DTS_flags == 2 || PTS_DTS_flags == 3); 727 buffer->meta()->setInt64("time", (PTS * 100) / 9); 728 729 if (mStreamType == 0x0f) { 730 // WHY??? 731 buffer->setRange(7, buffer->size() - 7); 732 } 733 734 mSource->queueAccessUnit(buffer); 735} 736 737sp<MediaSource> ATSParser::Stream::getSource(SourceType type) { 738 if ((type == AVC_VIDEO && mStreamType == 0x1b) 739 || (type == MPEG2ADTS_AUDIO && mStreamType == 0x0f)) { 740 return mSource; 741 } 742 743 return NULL; 744} 745 746//////////////////////////////////////////////////////////////////////////////// 747 748ATSParser::ATSParser() { 749} 750 751ATSParser::~ATSParser() { 752} 753 754void ATSParser::feedTSPacket(const void *data, size_t size) { 755 CHECK_EQ(size, kTSPacketSize); 756 757 ABitReader br((const uint8_t *)data, kTSPacketSize); 758 parseTS(&br); 759} 760 761void ATSParser::parseProgramAssociationTable(ABitReader *br) { 762 unsigned table_id = br->getBits(8); 763 LOG(VERBOSE) << " table_id = " << table_id; 764 CHECK_EQ(table_id, 0x00u); 765 766 unsigned section_syntax_indictor = br->getBits(1); 767 LOG(VERBOSE) << " section_syntax_indictor = " << section_syntax_indictor; 768 CHECK_EQ(section_syntax_indictor, 1u); 769 770 CHECK_EQ(br->getBits(1), 0u); 771 LOG(VERBOSE) << " reserved = " << br->getBits(2); 772 773 unsigned section_length = br->getBits(12); 774 LOG(VERBOSE) << " section_length = " << section_length; 775 CHECK((section_length & 0xc00) == 0); 776 777 LOG(VERBOSE) << " transport_stream_id = " << br->getBits(16); 778 LOG(VERBOSE) << " reserved = " << br->getBits(2); 779 LOG(VERBOSE) << " version_number = " << br->getBits(5); 780 LOG(VERBOSE) << " current_next_indicator = " << br->getBits(1); 781 LOG(VERBOSE) << " section_number = " << br->getBits(8); 782 LOG(VERBOSE) << " last_section_number = " << br->getBits(8); 783 784 size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */); 785 CHECK_EQ((numProgramBytes % 4), 0u); 786 787 for (size_t i = 0; i < numProgramBytes / 4; ++i) { 788 unsigned program_number = br->getBits(16); 789 LOG(VERBOSE) << " program_number = " << program_number; 790 791 LOG(VERBOSE) << " reserved = " << br->getBits(3); 792 793 if (program_number == 0) { 794 LOG(VERBOSE) << " network_PID = " 795 << StringPrintf("0x%04x", br->getBits(13)); 796 } else { 797 unsigned programMapPID = br->getBits(13); 798 799 LOG(VERBOSE) << " program_map_PID = " 800 << StringPrintf("0x%04x", programMapPID); 801 802 mPrograms.push(new Program(programMapPID)); 803 } 804 } 805 806 LOG(VERBOSE) << " CRC = " << StringPrintf("0x%08x", br->getBits(32)); 807} 808 809void ATSParser::parsePID( 810 ABitReader *br, unsigned PID, 811 unsigned payload_unit_start_indicator) { 812 if (PID == 0) { 813 if (payload_unit_start_indicator) { 814 unsigned skip = br->getBits(8); 815 br->skipBits(skip * 8); 816 } 817 parseProgramAssociationTable(br); 818 return; 819 } 820 821 bool handled = false; 822 for (size_t i = 0; i < mPrograms.size(); ++i) { 823 if (mPrograms.editItemAt(i)->parsePID( 824 PID, payload_unit_start_indicator, br)) { 825 handled = true; 826 break; 827 } 828 } 829 830 if (!handled) { 831 LOG(WARNING) << "PID " << StringPrintf("0x%04x", PID) 832 << " not handled."; 833 } 834} 835 836void ATSParser::parseAdaptationField(ABitReader *br) { 837 unsigned adaptation_field_length = br->getBits(8); 838 if (adaptation_field_length > 0) { 839 br->skipBits(adaptation_field_length * 8); // XXX 840 } 841} 842 843void ATSParser::parseTS(ABitReader *br) { 844 LOG(VERBOSE) << "---"; 845 846 unsigned sync_byte = br->getBits(8); 847 CHECK_EQ(sync_byte, 0x47u); 848 849 LOG(VERBOSE) << "transport_error_indicator = " << br->getBits(1); 850 851 unsigned payload_unit_start_indicator = br->getBits(1); 852 LOG(VERBOSE) << "payload_unit_start_indicator = " 853 << payload_unit_start_indicator; 854 855 LOG(VERBOSE) << "transport_priority = " << br->getBits(1); 856 857 unsigned PID = br->getBits(13); 858 LOG(VERBOSE) << "PID = " << StringPrintf("0x%04x", PID); 859 860 LOG(VERBOSE) << "transport_scrambling_control = " << br->getBits(2); 861 862 unsigned adaptation_field_control = br->getBits(2); 863 LOG(VERBOSE) << "adaptation_field_control = " << adaptation_field_control; 864 865 LOG(VERBOSE) << "continuity_counter = " << br->getBits(4); 866 867 if (adaptation_field_control == 2 || adaptation_field_control == 3) { 868 parseAdaptationField(br); 869 } 870 871 if (adaptation_field_control == 1 || adaptation_field_control == 3) { 872 parsePID(br, PID, payload_unit_start_indicator); 873 } 874} 875 876sp<MediaSource> ATSParser::getSource(SourceType type) { 877 for (size_t i = 0; i < mPrograms.size(); ++i) { 878 sp<MediaSource> source = mPrograms.editItemAt(i)->getSource(type); 879 880 if (source != NULL) { 881 return source; 882 } 883 } 884 885 return NULL; 886} 887 888} // namespace android 889