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