1 /* 2 * Copyright (C) 2009 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_TAG "MPEG4Extractor" 18 #include <utils/Log.h> 19 20 #include "include/MPEG4Extractor.h" 21 #include "include/SampleTable.h" 22 #include "include/ESDS.h" 23 #include "timedtext/TimedTextPlayer.h" 24 25 #include <arpa/inet.h> 26 27 #include <ctype.h> 28 #include <stdint.h> 29 #include <stdlib.h> 30 #include <string.h> 31 32 #include <media/stagefright/foundation/ADebug.h> 33 #include <media/stagefright/DataSource.h> 34 #include <media/stagefright/MediaBuffer.h> 35 #include <media/stagefright/MediaBufferGroup.h> 36 #include <media/stagefright/MediaDefs.h> 37 #include <media/stagefright/MediaSource.h> 38 #include <media/stagefright/MetaData.h> 39 #include <media/stagefright/Utils.h> 40 #include <utils/String8.h> 41 42 namespace android { 43 44 class MPEG4Source : public MediaSource { 45 public: 46 // Caller retains ownership of both "dataSource" and "sampleTable". 47 MPEG4Source(const sp<MetaData> &format, 48 const sp<DataSource> &dataSource, 49 int32_t timeScale, 50 const sp<SampleTable> &sampleTable); 51 52 virtual status_t start(MetaData *params = NULL); 53 virtual status_t stop(); 54 55 virtual sp<MetaData> getFormat(); 56 57 virtual status_t read( 58 MediaBuffer **buffer, const ReadOptions *options = NULL); 59 60 protected: 61 virtual ~MPEG4Source(); 62 63 private: 64 Mutex mLock; 65 66 sp<MetaData> mFormat; 67 sp<DataSource> mDataSource; 68 int32_t mTimescale; 69 sp<SampleTable> mSampleTable; 70 uint32_t mCurrentSampleIndex; 71 72 bool mIsAVC; 73 size_t mNALLengthSize; 74 75 bool mStarted; 76 77 MediaBufferGroup *mGroup; 78 79 MediaBuffer *mBuffer; 80 81 bool mWantsNALFragments; 82 83 uint8_t *mSrcBuffer; 84 85 size_t parseNALSize(const uint8_t *data) const; 86 87 MPEG4Source(const MPEG4Source &); 88 MPEG4Source &operator=(const MPEG4Source &); 89 }; 90 91 // This custom data source wraps an existing one and satisfies requests 92 // falling entirely within a cached range from the cache while forwarding 93 // all remaining requests to the wrapped datasource. 94 // This is used to cache the full sampletable metadata for a single track, 95 // possibly wrapping multiple times to cover all tracks, i.e. 96 // Each MPEG4DataSource caches the sampletable metadata for a single track. 97 98 struct MPEG4DataSource : public DataSource { 99 MPEG4DataSource(const sp<DataSource> &source); 100 101 virtual status_t initCheck() const; 102 virtual ssize_t readAt(off64_t offset, void *data, size_t size); 103 virtual status_t getSize(off64_t *size); 104 virtual uint32_t flags(); 105 106 status_t setCachedRange(off64_t offset, size_t size); 107 108 protected: 109 virtual ~MPEG4DataSource(); 110 111 private: 112 Mutex mLock; 113 114 sp<DataSource> mSource; 115 off64_t mCachedOffset; 116 size_t mCachedSize; 117 uint8_t *mCache; 118 119 void clearCache(); 120 121 MPEG4DataSource(const MPEG4DataSource &); 122 MPEG4DataSource &operator=(const MPEG4DataSource &); 123 }; 124 125 MPEG4DataSource::MPEG4DataSource(const sp<DataSource> &source) 126 : mSource(source), 127 mCachedOffset(0), 128 mCachedSize(0), 129 mCache(NULL) { 130 } 131 132 MPEG4DataSource::~MPEG4DataSource() { 133 clearCache(); 134 } 135 136 void MPEG4DataSource::clearCache() { 137 if (mCache) { 138 free(mCache); 139 mCache = NULL; 140 } 141 142 mCachedOffset = 0; 143 mCachedSize = 0; 144 } 145 146 status_t MPEG4DataSource::initCheck() const { 147 return mSource->initCheck(); 148 } 149 150 ssize_t MPEG4DataSource::readAt(off64_t offset, void *data, size_t size) { 151 Mutex::Autolock autoLock(mLock); 152 153 if (offset >= mCachedOffset 154 && offset + size <= mCachedOffset + mCachedSize) { 155 memcpy(data, &mCache[offset - mCachedOffset], size); 156 return size; 157 } 158 159 return mSource->readAt(offset, data, size); 160 } 161 162 status_t MPEG4DataSource::getSize(off64_t *size) { 163 return mSource->getSize(size); 164 } 165 166 uint32_t MPEG4DataSource::flags() { 167 return mSource->flags(); 168 } 169 170 status_t MPEG4DataSource::setCachedRange(off64_t offset, size_t size) { 171 Mutex::Autolock autoLock(mLock); 172 173 clearCache(); 174 175 mCache = (uint8_t *)malloc(size); 176 177 if (mCache == NULL) { 178 return -ENOMEM; 179 } 180 181 mCachedOffset = offset; 182 mCachedSize = size; 183 184 ssize_t err = mSource->readAt(mCachedOffset, mCache, mCachedSize); 185 186 if (err < (ssize_t)size) { 187 clearCache(); 188 189 return ERROR_IO; 190 } 191 192 return OK; 193 } 194 195 //////////////////////////////////////////////////////////////////////////////// 196 197 static void hexdump(const void *_data, size_t size) { 198 const uint8_t *data = (const uint8_t *)_data; 199 size_t offset = 0; 200 while (offset < size) { 201 printf("0x%04x ", offset); 202 203 size_t n = size - offset; 204 if (n > 16) { 205 n = 16; 206 } 207 208 for (size_t i = 0; i < 16; ++i) { 209 if (i == 8) { 210 printf(" "); 211 } 212 213 if (offset + i < size) { 214 printf("%02x ", data[offset + i]); 215 } else { 216 printf(" "); 217 } 218 } 219 220 printf(" "); 221 222 for (size_t i = 0; i < n; ++i) { 223 if (isprint(data[offset + i])) { 224 printf("%c", data[offset + i]); 225 } else { 226 printf("."); 227 } 228 } 229 230 printf("\n"); 231 232 offset += 16; 233 } 234 } 235 236 static const char *FourCC2MIME(uint32_t fourcc) { 237 switch (fourcc) { 238 case FOURCC('m', 'p', '4', 'a'): 239 return MEDIA_MIMETYPE_AUDIO_AAC; 240 241 case FOURCC('s', 'a', 'm', 'r'): 242 return MEDIA_MIMETYPE_AUDIO_AMR_NB; 243 244 case FOURCC('s', 'a', 'w', 'b'): 245 return MEDIA_MIMETYPE_AUDIO_AMR_WB; 246 247 case FOURCC('m', 'p', '4', 'v'): 248 return MEDIA_MIMETYPE_VIDEO_MPEG4; 249 250 case FOURCC('s', '2', '6', '3'): 251 case FOURCC('h', '2', '6', '3'): 252 case FOURCC('H', '2', '6', '3'): 253 return MEDIA_MIMETYPE_VIDEO_H263; 254 255 case FOURCC('a', 'v', 'c', '1'): 256 return MEDIA_MIMETYPE_VIDEO_AVC; 257 258 default: 259 CHECK(!"should not be here."); 260 return NULL; 261 } 262 } 263 264 MPEG4Extractor::MPEG4Extractor(const sp<DataSource> &source) 265 : mDataSource(source), 266 mInitCheck(NO_INIT), 267 mHasVideo(false), 268 mFirstTrack(NULL), 269 mLastTrack(NULL), 270 mFileMetaData(new MetaData), 271 mFirstSINF(NULL), 272 mIsDrm(false) { 273 } 274 275 MPEG4Extractor::~MPEG4Extractor() { 276 Track *track = mFirstTrack; 277 while (track) { 278 Track *next = track->next; 279 280 delete track; 281 track = next; 282 } 283 mFirstTrack = mLastTrack = NULL; 284 285 SINF *sinf = mFirstSINF; 286 while (sinf) { 287 SINF *next = sinf->next; 288 delete sinf->IPMPData; 289 delete sinf; 290 sinf = next; 291 } 292 mFirstSINF = NULL; 293 } 294 295 sp<MetaData> MPEG4Extractor::getMetaData() { 296 status_t err; 297 if ((err = readMetaData()) != OK) { 298 return new MetaData; 299 } 300 301 return mFileMetaData; 302 } 303 304 size_t MPEG4Extractor::countTracks() { 305 status_t err; 306 if ((err = readMetaData()) != OK) { 307 return 0; 308 } 309 310 size_t n = 0; 311 Track *track = mFirstTrack; 312 while (track) { 313 ++n; 314 track = track->next; 315 } 316 317 return n; 318 } 319 320 sp<MetaData> MPEG4Extractor::getTrackMetaData( 321 size_t index, uint32_t flags) { 322 status_t err; 323 if ((err = readMetaData()) != OK) { 324 return NULL; 325 } 326 327 Track *track = mFirstTrack; 328 while (index > 0) { 329 if (track == NULL) { 330 return NULL; 331 } 332 333 track = track->next; 334 --index; 335 } 336 337 if (track == NULL) { 338 return NULL; 339 } 340 341 if ((flags & kIncludeExtensiveMetaData) 342 && !track->includes_expensive_metadata) { 343 track->includes_expensive_metadata = true; 344 345 const char *mime; 346 CHECK(track->meta->findCString(kKeyMIMEType, &mime)); 347 if (!strncasecmp("video/", mime, 6)) { 348 uint32_t sampleIndex; 349 uint32_t sampleTime; 350 if (track->sampleTable->findThumbnailSample(&sampleIndex) == OK 351 && track->sampleTable->getMetaDataForSample( 352 sampleIndex, NULL /* offset */, NULL /* size */, 353 &sampleTime) == OK) { 354 track->meta->setInt64( 355 kKeyThumbnailTime, 356 ((int64_t)sampleTime * 1000000) / track->timescale); 357 } 358 } 359 } 360 361 return track->meta; 362 } 363 364 status_t MPEG4Extractor::readMetaData() { 365 if (mInitCheck != NO_INIT) { 366 return mInitCheck; 367 } 368 369 off64_t offset = 0; 370 status_t err; 371 while ((err = parseChunk(&offset, 0)) == OK) { 372 } 373 374 if (mInitCheck == OK) { 375 if (mHasVideo) { 376 mFileMetaData->setCString(kKeyMIMEType, "video/mp4"); 377 } else { 378 mFileMetaData->setCString(kKeyMIMEType, "audio/mp4"); 379 } 380 381 mInitCheck = OK; 382 } else { 383 mInitCheck = err; 384 } 385 386 CHECK_NE(err, (status_t)NO_INIT); 387 return mInitCheck; 388 } 389 390 char* MPEG4Extractor::getDrmTrackInfo(size_t trackID, int *len) { 391 if (mFirstSINF == NULL) { 392 return NULL; 393 } 394 395 SINF *sinf = mFirstSINF; 396 while (sinf && (trackID != sinf->trackID)) { 397 sinf = sinf->next; 398 } 399 400 if (sinf == NULL) { 401 return NULL; 402 } 403 404 *len = sinf->len; 405 return sinf->IPMPData; 406 } 407 408 // Reads an encoded integer 7 bits at a time until it encounters the high bit clear. 409 int32_t readSize(off64_t offset, 410 const sp<DataSource> DataSource, uint8_t *numOfBytes) { 411 uint32_t size = 0; 412 uint8_t data; 413 bool moreData = true; 414 *numOfBytes = 0; 415 416 while (moreData) { 417 if (DataSource->readAt(offset, &data, 1) < 1) { 418 return -1; 419 } 420 offset ++; 421 moreData = (data >= 128) ? true : false; 422 size = (size << 7) | (data & 0x7f); // Take last 7 bits 423 (*numOfBytes) ++; 424 } 425 426 return size; 427 } 428 429 status_t MPEG4Extractor::parseDrmSINF(off64_t *offset, off64_t data_offset) { 430 uint8_t updateIdTag; 431 if (mDataSource->readAt(data_offset, &updateIdTag, 1) < 1) { 432 return ERROR_IO; 433 } 434 data_offset ++; 435 436 if (0x01/*OBJECT_DESCRIPTOR_UPDATE_ID_TAG*/ != updateIdTag) { 437 return ERROR_MALFORMED; 438 } 439 440 uint8_t numOfBytes; 441 int32_t size = readSize(data_offset, mDataSource, &numOfBytes); 442 if (size < 0) { 443 return ERROR_IO; 444 } 445 int32_t classSize = size; 446 data_offset += numOfBytes; 447 448 while(size >= 11 ) { 449 uint8_t descriptorTag; 450 if (mDataSource->readAt(data_offset, &descriptorTag, 1) < 1) { 451 return ERROR_IO; 452 } 453 data_offset ++; 454 455 if (0x11/*OBJECT_DESCRIPTOR_ID_TAG*/ != descriptorTag) { 456 return ERROR_MALFORMED; 457 } 458 459 uint8_t buffer[8]; 460 //ObjectDescriptorID and ObjectDescriptor url flag 461 if (mDataSource->readAt(data_offset, buffer, 2) < 2) { 462 return ERROR_IO; 463 } 464 data_offset += 2; 465 466 if ((buffer[1] >> 5) & 0x0001) { //url flag is set 467 return ERROR_MALFORMED; 468 } 469 470 if (mDataSource->readAt(data_offset, buffer, 8) < 8) { 471 return ERROR_IO; 472 } 473 data_offset += 8; 474 475 if ((0x0F/*ES_ID_REF_TAG*/ != buffer[1]) 476 || ( 0x0A/*IPMP_DESCRIPTOR_POINTER_ID_TAG*/ != buffer[5])) { 477 return ERROR_MALFORMED; 478 } 479 480 SINF *sinf = new SINF; 481 sinf->trackID = U16_AT(&buffer[3]); 482 sinf->IPMPDescriptorID = buffer[7]; 483 sinf->next = mFirstSINF; 484 mFirstSINF = sinf; 485 486 size -= (8 + 2 + 1); 487 } 488 489 if (size != 0) { 490 return ERROR_MALFORMED; 491 } 492 493 if (mDataSource->readAt(data_offset, &updateIdTag, 1) < 1) { 494 return ERROR_IO; 495 } 496 data_offset ++; 497 498 if(0x05/*IPMP_DESCRIPTOR_UPDATE_ID_TAG*/ != updateIdTag) { 499 return ERROR_MALFORMED; 500 } 501 502 size = readSize(data_offset, mDataSource, &numOfBytes); 503 if (size < 0) { 504 return ERROR_IO; 505 } 506 classSize = size; 507 data_offset += numOfBytes; 508 509 while (size > 0) { 510 uint8_t tag; 511 int32_t dataLen; 512 if (mDataSource->readAt(data_offset, &tag, 1) < 1) { 513 return ERROR_IO; 514 } 515 data_offset ++; 516 517 if (0x0B/*IPMP_DESCRIPTOR_ID_TAG*/ == tag) { 518 uint8_t id; 519 dataLen = readSize(data_offset, mDataSource, &numOfBytes); 520 if (dataLen < 0) { 521 return ERROR_IO; 522 } else if (dataLen < 4) { 523 return ERROR_MALFORMED; 524 } 525 data_offset += numOfBytes; 526 527 if (mDataSource->readAt(data_offset, &id, 1) < 1) { 528 return ERROR_IO; 529 } 530 data_offset ++; 531 532 SINF *sinf = mFirstSINF; 533 while (sinf && (sinf->IPMPDescriptorID != id)) { 534 sinf = sinf->next; 535 } 536 if (sinf == NULL) { 537 return ERROR_MALFORMED; 538 } 539 sinf->len = dataLen - 3; 540 sinf->IPMPData = new char[sinf->len]; 541 542 if (mDataSource->readAt(data_offset + 2, sinf->IPMPData, sinf->len) < sinf->len) { 543 return ERROR_IO; 544 } 545 data_offset += sinf->len; 546 547 size -= (dataLen + numOfBytes + 1); 548 } 549 } 550 551 if (size != 0) { 552 return ERROR_MALFORMED; 553 } 554 555 return UNKNOWN_ERROR; // Return a dummy error. 556 } 557 558 static void MakeFourCCString(uint32_t x, char *s) { 559 s[0] = x >> 24; 560 s[1] = (x >> 16) & 0xff; 561 s[2] = (x >> 8) & 0xff; 562 s[3] = x & 0xff; 563 s[4] = '\0'; 564 } 565 566 struct PathAdder { 567 PathAdder(Vector<uint32_t> *path, uint32_t chunkType) 568 : mPath(path) { 569 mPath->push(chunkType); 570 } 571 572 ~PathAdder() { 573 mPath->pop(); 574 } 575 576 private: 577 Vector<uint32_t> *mPath; 578 579 PathAdder(const PathAdder &); 580 PathAdder &operator=(const PathAdder &); 581 }; 582 583 static bool underMetaDataPath(const Vector<uint32_t> &path) { 584 return path.size() >= 5 585 && path[0] == FOURCC('m', 'o', 'o', 'v') 586 && path[1] == FOURCC('u', 'd', 't', 'a') 587 && path[2] == FOURCC('m', 'e', 't', 'a') 588 && path[3] == FOURCC('i', 'l', 's', 't'); 589 } 590 591 // Given a time in seconds since Jan 1 1904, produce a human-readable string. 592 static void convertTimeToDate(int64_t time_1904, String8 *s) { 593 time_t time_1970 = time_1904 - (((66 * 365 + 17) * 24) * 3600); 594 595 char tmp[32]; 596 strftime(tmp, sizeof(tmp), "%Y%m%dT%H%M%S.000Z", gmtime(&time_1970)); 597 598 s->setTo(tmp); 599 } 600 601 status_t MPEG4Extractor::parseChunk(off64_t *offset, int depth) { 602 uint32_t hdr[2]; 603 if (mDataSource->readAt(*offset, hdr, 8) < 8) { 604 return ERROR_IO; 605 } 606 uint64_t chunk_size = ntohl(hdr[0]); 607 uint32_t chunk_type = ntohl(hdr[1]); 608 off64_t data_offset = *offset + 8; 609 610 if (chunk_size == 1) { 611 if (mDataSource->readAt(*offset + 8, &chunk_size, 8) < 8) { 612 return ERROR_IO; 613 } 614 chunk_size = ntoh64(chunk_size); 615 data_offset += 8; 616 617 if (chunk_size < 16) { 618 // The smallest valid chunk is 16 bytes long in this case. 619 return ERROR_MALFORMED; 620 } 621 } else if (chunk_size < 8) { 622 // The smallest valid chunk is 8 bytes long. 623 return ERROR_MALFORMED; 624 } 625 626 char chunk[5]; 627 MakeFourCCString(chunk_type, chunk); 628 629 #if 0 630 static const char kWhitespace[] = " "; 631 const char *indent = &kWhitespace[sizeof(kWhitespace) - 1 - 2 * depth]; 632 printf("%sfound chunk '%s' of size %lld\n", indent, chunk, chunk_size); 633 634 char buffer[256]; 635 size_t n = chunk_size; 636 if (n > sizeof(buffer)) { 637 n = sizeof(buffer); 638 } 639 if (mDataSource->readAt(*offset, buffer, n) 640 < (ssize_t)n) { 641 return ERROR_IO; 642 } 643 644 hexdump(buffer, n); 645 #endif 646 647 PathAdder autoAdder(&mPath, chunk_type); 648 649 off64_t chunk_data_size = *offset + chunk_size - data_offset; 650 651 if (chunk_type != FOURCC('c', 'p', 'r', 't') 652 && chunk_type != FOURCC('c', 'o', 'v', 'r') 653 && mPath.size() == 5 && underMetaDataPath(mPath)) { 654 off64_t stop_offset = *offset + chunk_size; 655 *offset = data_offset; 656 while (*offset < stop_offset) { 657 status_t err = parseChunk(offset, depth + 1); 658 if (err != OK) { 659 return err; 660 } 661 } 662 663 if (*offset != stop_offset) { 664 return ERROR_MALFORMED; 665 } 666 667 return OK; 668 } 669 670 switch(chunk_type) { 671 case FOURCC('m', 'o', 'o', 'v'): 672 case FOURCC('t', 'r', 'a', 'k'): 673 case FOURCC('m', 'd', 'i', 'a'): 674 case FOURCC('m', 'i', 'n', 'f'): 675 case FOURCC('d', 'i', 'n', 'f'): 676 case FOURCC('s', 't', 'b', 'l'): 677 case FOURCC('m', 'v', 'e', 'x'): 678 case FOURCC('m', 'o', 'o', 'f'): 679 case FOURCC('t', 'r', 'a', 'f'): 680 case FOURCC('m', 'f', 'r', 'a'): 681 case FOURCC('u', 'd', 't', 'a'): 682 case FOURCC('i', 'l', 's', 't'): 683 { 684 if (chunk_type == FOURCC('s', 't', 'b', 'l')) { 685 LOGV("sampleTable chunk is %d bytes long.", (size_t)chunk_size); 686 687 if (mDataSource->flags() 688 & (DataSource::kWantsPrefetching 689 | DataSource::kIsCachingDataSource)) { 690 sp<MPEG4DataSource> cachedSource = 691 new MPEG4DataSource(mDataSource); 692 693 if (cachedSource->setCachedRange(*offset, chunk_size) == OK) { 694 mDataSource = cachedSource; 695 } 696 } 697 698 mLastTrack->sampleTable = new SampleTable(mDataSource); 699 } 700 701 bool isTrack = false; 702 if (chunk_type == FOURCC('t', 'r', 'a', 'k')) { 703 isTrack = true; 704 705 Track *track = new Track; 706 track->next = NULL; 707 if (mLastTrack) { 708 mLastTrack->next = track; 709 } else { 710 mFirstTrack = track; 711 } 712 mLastTrack = track; 713 714 track->meta = new MetaData; 715 track->includes_expensive_metadata = false; 716 track->skipTrack = false; 717 track->timescale = 0; 718 track->meta->setCString(kKeyMIMEType, "application/octet-stream"); 719 } 720 721 off64_t stop_offset = *offset + chunk_size; 722 *offset = data_offset; 723 while (*offset < stop_offset) { 724 status_t err = parseChunk(offset, depth + 1); 725 if (err != OK) { 726 return err; 727 } 728 } 729 730 if (*offset != stop_offset) { 731 return ERROR_MALFORMED; 732 } 733 734 if (isTrack) { 735 if (mLastTrack->skipTrack) { 736 Track *cur = mFirstTrack; 737 738 if (cur == mLastTrack) { 739 delete cur; 740 mFirstTrack = mLastTrack = NULL; 741 } else { 742 while (cur && cur->next != mLastTrack) { 743 cur = cur->next; 744 } 745 cur->next = NULL; 746 delete mLastTrack; 747 mLastTrack = cur; 748 } 749 750 return OK; 751 } 752 753 status_t err = verifyTrack(mLastTrack); 754 755 if (err != OK) { 756 return err; 757 } 758 } else if (chunk_type == FOURCC('m', 'o', 'o', 'v')) { 759 mInitCheck = OK; 760 761 if (!mIsDrm) { 762 return UNKNOWN_ERROR; // Return a dummy error. 763 } else { 764 return OK; 765 } 766 } 767 break; 768 } 769 770 case FOURCC('t', 'k', 'h', 'd'): 771 { 772 status_t err; 773 if ((err = parseTrackHeader(data_offset, chunk_data_size)) != OK) { 774 return err; 775 } 776 777 *offset += chunk_size; 778 break; 779 } 780 781 case FOURCC('m', 'd', 'h', 'd'): 782 { 783 if (chunk_data_size < 4) { 784 return ERROR_MALFORMED; 785 } 786 787 uint8_t version; 788 if (mDataSource->readAt( 789 data_offset, &version, sizeof(version)) 790 < (ssize_t)sizeof(version)) { 791 return ERROR_IO; 792 } 793 794 off64_t timescale_offset; 795 796 if (version == 1) { 797 timescale_offset = data_offset + 4 + 16; 798 } else if (version == 0) { 799 timescale_offset = data_offset + 4 + 8; 800 } else { 801 return ERROR_IO; 802 } 803 804 uint32_t timescale; 805 if (mDataSource->readAt( 806 timescale_offset, ×cale, sizeof(timescale)) 807 < (ssize_t)sizeof(timescale)) { 808 return ERROR_IO; 809 } 810 811 mLastTrack->timescale = ntohl(timescale); 812 813 int64_t duration; 814 if (version == 1) { 815 if (mDataSource->readAt( 816 timescale_offset + 4, &duration, sizeof(duration)) 817 < (ssize_t)sizeof(duration)) { 818 return ERROR_IO; 819 } 820 duration = ntoh64(duration); 821 } else { 822 int32_t duration32; 823 if (mDataSource->readAt( 824 timescale_offset + 4, &duration32, sizeof(duration32)) 825 < (ssize_t)sizeof(duration32)) { 826 return ERROR_IO; 827 } 828 duration = ntohl(duration32); 829 } 830 mLastTrack->meta->setInt64( 831 kKeyDuration, (duration * 1000000) / mLastTrack->timescale); 832 833 uint8_t lang[2]; 834 off64_t lang_offset; 835 if (version == 1) { 836 lang_offset = timescale_offset + 4 + 8; 837 } else if (version == 0) { 838 lang_offset = timescale_offset + 4 + 4; 839 } else { 840 return ERROR_IO; 841 } 842 843 if (mDataSource->readAt(lang_offset, &lang, sizeof(lang)) 844 < (ssize_t)sizeof(lang)) { 845 return ERROR_IO; 846 } 847 848 // To get the ISO-639-2/T three character language code 849 // 1 bit pad followed by 3 5-bits characters. Each character 850 // is packed as the difference between its ASCII value and 0x60. 851 char lang_code[4]; 852 lang_code[0] = ((lang[0] >> 2) & 0x1f) + 0x60; 853 lang_code[1] = ((lang[0] & 0x3) << 3 | (lang[1] >> 5)) + 0x60; 854 lang_code[2] = (lang[1] & 0x1f) + 0x60; 855 lang_code[3] = '\0'; 856 857 mLastTrack->meta->setCString( 858 kKeyMediaLanguage, lang_code); 859 860 *offset += chunk_size; 861 break; 862 } 863 864 case FOURCC('s', 't', 's', 'd'): 865 { 866 if (chunk_data_size < 8) { 867 return ERROR_MALFORMED; 868 } 869 870 uint8_t buffer[8]; 871 if (chunk_data_size < (off64_t)sizeof(buffer)) { 872 return ERROR_MALFORMED; 873 } 874 875 if (mDataSource->readAt( 876 data_offset, buffer, 8) < 8) { 877 return ERROR_IO; 878 } 879 880 if (U32_AT(buffer) != 0) { 881 // Should be version 0, flags 0. 882 return ERROR_MALFORMED; 883 } 884 885 uint32_t entry_count = U32_AT(&buffer[4]); 886 887 if (entry_count > 1) { 888 // For 3GPP timed text, there could be multiple tx3g boxes contain 889 // multiple text display formats. These formats will be used to 890 // display the timed text. 891 const char *mime; 892 CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); 893 if (strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) { 894 // For now we only support a single type of media per track. 895 mLastTrack->skipTrack = true; 896 *offset += chunk_size; 897 break; 898 } 899 } 900 901 off64_t stop_offset = *offset + chunk_size; 902 *offset = data_offset + 8; 903 for (uint32_t i = 0; i < entry_count; ++i) { 904 status_t err = parseChunk(offset, depth + 1); 905 if (err != OK) { 906 return err; 907 } 908 } 909 910 if (*offset != stop_offset) { 911 return ERROR_MALFORMED; 912 } 913 break; 914 } 915 916 case FOURCC('m', 'p', '4', 'a'): 917 case FOURCC('s', 'a', 'm', 'r'): 918 case FOURCC('s', 'a', 'w', 'b'): 919 { 920 uint8_t buffer[8 + 20]; 921 if (chunk_data_size < (ssize_t)sizeof(buffer)) { 922 // Basic AudioSampleEntry size. 923 return ERROR_MALFORMED; 924 } 925 926 if (mDataSource->readAt( 927 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 928 return ERROR_IO; 929 } 930 931 uint16_t data_ref_index = U16_AT(&buffer[6]); 932 uint16_t num_channels = U16_AT(&buffer[16]); 933 934 uint16_t sample_size = U16_AT(&buffer[18]); 935 uint32_t sample_rate = U32_AT(&buffer[24]) >> 16; 936 937 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, 938 FourCC2MIME(chunk_type))) { 939 // AMR NB audio is always mono, 8kHz 940 num_channels = 1; 941 sample_rate = 8000; 942 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, 943 FourCC2MIME(chunk_type))) { 944 // AMR WB audio is always mono, 16kHz 945 num_channels = 1; 946 sample_rate = 16000; 947 } 948 949 #if 0 950 printf("*** coding='%s' %d channels, size %d, rate %d\n", 951 chunk, num_channels, sample_size, sample_rate); 952 #endif 953 954 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); 955 mLastTrack->meta->setInt32(kKeyChannelCount, num_channels); 956 mLastTrack->meta->setInt32(kKeySampleRate, sample_rate); 957 958 off64_t stop_offset = *offset + chunk_size; 959 *offset = data_offset + sizeof(buffer); 960 while (*offset < stop_offset) { 961 status_t err = parseChunk(offset, depth + 1); 962 if (err != OK) { 963 return err; 964 } 965 } 966 967 if (*offset != stop_offset) { 968 return ERROR_MALFORMED; 969 } 970 break; 971 } 972 973 case FOURCC('m', 'p', '4', 'v'): 974 case FOURCC('s', '2', '6', '3'): 975 case FOURCC('H', '2', '6', '3'): 976 case FOURCC('h', '2', '6', '3'): 977 case FOURCC('a', 'v', 'c', '1'): 978 { 979 mHasVideo = true; 980 981 uint8_t buffer[78]; 982 if (chunk_data_size < (ssize_t)sizeof(buffer)) { 983 // Basic VideoSampleEntry size. 984 return ERROR_MALFORMED; 985 } 986 987 if (mDataSource->readAt( 988 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 989 return ERROR_IO; 990 } 991 992 uint16_t data_ref_index = U16_AT(&buffer[6]); 993 uint16_t width = U16_AT(&buffer[6 + 18]); 994 uint16_t height = U16_AT(&buffer[6 + 20]); 995 996 // The video sample is not stand-compliant if it has invalid dimension. 997 // Use some default width and height value, and 998 // let the decoder figure out the actual width and height (and thus 999 // be prepared for INFO_FOMRAT_CHANGED event). 1000 if (width == 0) width = 352; 1001 if (height == 0) height = 288; 1002 1003 // printf("*** coding='%s' width=%d height=%d\n", 1004 // chunk, width, height); 1005 1006 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); 1007 mLastTrack->meta->setInt32(kKeyWidth, width); 1008 mLastTrack->meta->setInt32(kKeyHeight, height); 1009 1010 off64_t stop_offset = *offset + chunk_size; 1011 *offset = data_offset + sizeof(buffer); 1012 while (*offset < stop_offset) { 1013 status_t err = parseChunk(offset, depth + 1); 1014 if (err != OK) { 1015 return err; 1016 } 1017 } 1018 1019 if (*offset != stop_offset) { 1020 return ERROR_MALFORMED; 1021 } 1022 break; 1023 } 1024 1025 case FOURCC('s', 't', 'c', 'o'): 1026 case FOURCC('c', 'o', '6', '4'): 1027 { 1028 status_t err = 1029 mLastTrack->sampleTable->setChunkOffsetParams( 1030 chunk_type, data_offset, chunk_data_size); 1031 1032 if (err != OK) { 1033 return err; 1034 } 1035 1036 *offset += chunk_size; 1037 break; 1038 } 1039 1040 case FOURCC('s', 't', 's', 'c'): 1041 { 1042 status_t err = 1043 mLastTrack->sampleTable->setSampleToChunkParams( 1044 data_offset, chunk_data_size); 1045 1046 if (err != OK) { 1047 return err; 1048 } 1049 1050 *offset += chunk_size; 1051 break; 1052 } 1053 1054 case FOURCC('s', 't', 's', 'z'): 1055 case FOURCC('s', 't', 'z', '2'): 1056 { 1057 status_t err = 1058 mLastTrack->sampleTable->setSampleSizeParams( 1059 chunk_type, data_offset, chunk_data_size); 1060 1061 if (err != OK) { 1062 return err; 1063 } 1064 1065 size_t max_size; 1066 err = mLastTrack->sampleTable->getMaxSampleSize(&max_size); 1067 1068 if (err != OK) { 1069 return err; 1070 } 1071 1072 // Assume that a given buffer only contains at most 10 fragments, 1073 // each fragment originally prefixed with a 2 byte length will 1074 // have a 4 byte header (0x00 0x00 0x00 0x01) after conversion, 1075 // and thus will grow by 2 bytes per fragment. 1076 mLastTrack->meta->setInt32(kKeyMaxInputSize, max_size + 10 * 2); 1077 *offset += chunk_size; 1078 1079 // Calculate average frame rate. 1080 const char *mime; 1081 CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); 1082 if (!strncasecmp("video/", mime, 6)) { 1083 size_t nSamples = mLastTrack->sampleTable->countSamples(); 1084 int64_t durationUs; 1085 if (mLastTrack->meta->findInt64(kKeyDuration, &durationUs)) { 1086 if (durationUs > 0) { 1087 int32_t frameRate = (nSamples * 1000000LL + 1088 (durationUs >> 1)) / durationUs; 1089 mLastTrack->meta->setInt32(kKeyFrameRate, frameRate); 1090 } 1091 } 1092 } 1093 1094 break; 1095 } 1096 1097 case FOURCC('s', 't', 't', 's'): 1098 { 1099 status_t err = 1100 mLastTrack->sampleTable->setTimeToSampleParams( 1101 data_offset, chunk_data_size); 1102 1103 if (err != OK) { 1104 return err; 1105 } 1106 1107 *offset += chunk_size; 1108 break; 1109 } 1110 1111 case FOURCC('c', 't', 't', 's'): 1112 { 1113 status_t err = 1114 mLastTrack->sampleTable->setCompositionTimeToSampleParams( 1115 data_offset, chunk_data_size); 1116 1117 if (err != OK) { 1118 return err; 1119 } 1120 1121 *offset += chunk_size; 1122 break; 1123 } 1124 1125 case FOURCC('s', 't', 's', 's'): 1126 { 1127 status_t err = 1128 mLastTrack->sampleTable->setSyncSampleParams( 1129 data_offset, chunk_data_size); 1130 1131 if (err != OK) { 1132 return err; 1133 } 1134 1135 *offset += chunk_size; 1136 break; 1137 } 1138 1139 case FOURCC('e', 's', 'd', 's'): 1140 { 1141 if (chunk_data_size < 4) { 1142 return ERROR_MALFORMED; 1143 } 1144 1145 uint8_t buffer[256]; 1146 if (chunk_data_size > (off64_t)sizeof(buffer)) { 1147 return ERROR_BUFFER_TOO_SMALL; 1148 } 1149 1150 if (mDataSource->readAt( 1151 data_offset, buffer, chunk_data_size) < chunk_data_size) { 1152 return ERROR_IO; 1153 } 1154 1155 if (U32_AT(buffer) != 0) { 1156 // Should be version 0, flags 0. 1157 return ERROR_MALFORMED; 1158 } 1159 1160 mLastTrack->meta->setData( 1161 kKeyESDS, kTypeESDS, &buffer[4], chunk_data_size - 4); 1162 1163 if (mPath.size() >= 2 1164 && mPath[mPath.size() - 2] == FOURCC('m', 'p', '4', 'a')) { 1165 // Information from the ESDS must be relied on for proper 1166 // setup of sample rate and channel count for MPEG4 Audio. 1167 // The generic header appears to only contain generic 1168 // information... 1169 1170 status_t err = updateAudioTrackInfoFromESDS_MPEG4Audio( 1171 &buffer[4], chunk_data_size - 4); 1172 1173 if (err != OK) { 1174 return err; 1175 } 1176 } 1177 1178 *offset += chunk_size; 1179 break; 1180 } 1181 1182 case FOURCC('a', 'v', 'c', 'C'): 1183 { 1184 char buffer[256]; 1185 if (chunk_data_size > (off64_t)sizeof(buffer)) { 1186 return ERROR_BUFFER_TOO_SMALL; 1187 } 1188 1189 if (mDataSource->readAt( 1190 data_offset, buffer, chunk_data_size) < chunk_data_size) { 1191 return ERROR_IO; 1192 } 1193 1194 mLastTrack->meta->setData( 1195 kKeyAVCC, kTypeAVCC, buffer, chunk_data_size); 1196 1197 *offset += chunk_size; 1198 break; 1199 } 1200 1201 case FOURCC('d', '2', '6', '3'): 1202 { 1203 /* 1204 * d263 contains a fixed 7 bytes part: 1205 * vendor - 4 bytes 1206 * version - 1 byte 1207 * level - 1 byte 1208 * profile - 1 byte 1209 * optionally, "d263" box itself may contain a 16-byte 1210 * bit rate box (bitr) 1211 * average bit rate - 4 bytes 1212 * max bit rate - 4 bytes 1213 */ 1214 char buffer[23]; 1215 if (chunk_data_size != 7 && 1216 chunk_data_size != 23) { 1217 LOGE("Incorrect D263 box size %lld", chunk_data_size); 1218 return ERROR_MALFORMED; 1219 } 1220 1221 if (mDataSource->readAt( 1222 data_offset, buffer, chunk_data_size) < chunk_data_size) { 1223 return ERROR_IO; 1224 } 1225 1226 mLastTrack->meta->setData(kKeyD263, kTypeD263, buffer, chunk_data_size); 1227 1228 *offset += chunk_size; 1229 break; 1230 } 1231 1232 case FOURCC('m', 'e', 't', 'a'): 1233 { 1234 uint8_t buffer[4]; 1235 if (chunk_data_size < (off64_t)sizeof(buffer)) { 1236 return ERROR_MALFORMED; 1237 } 1238 1239 if (mDataSource->readAt( 1240 data_offset, buffer, 4) < 4) { 1241 return ERROR_IO; 1242 } 1243 1244 if (U32_AT(buffer) != 0) { 1245 // Should be version 0, flags 0. 1246 1247 // If it's not, let's assume this is one of those 1248 // apparently malformed chunks that don't have flags 1249 // and completely different semantics than what's 1250 // in the MPEG4 specs and skip it. 1251 *offset += chunk_size; 1252 return OK; 1253 } 1254 1255 off64_t stop_offset = *offset + chunk_size; 1256 *offset = data_offset + sizeof(buffer); 1257 while (*offset < stop_offset) { 1258 status_t err = parseChunk(offset, depth + 1); 1259 if (err != OK) { 1260 return err; 1261 } 1262 } 1263 1264 if (*offset != stop_offset) { 1265 return ERROR_MALFORMED; 1266 } 1267 break; 1268 } 1269 1270 case FOURCC('d', 'a', 't', 'a'): 1271 { 1272 if (mPath.size() == 6 && underMetaDataPath(mPath)) { 1273 status_t err = parseMetaData(data_offset, chunk_data_size); 1274 1275 if (err != OK) { 1276 return err; 1277 } 1278 } 1279 1280 *offset += chunk_size; 1281 break; 1282 } 1283 1284 case FOURCC('m', 'v', 'h', 'd'): 1285 { 1286 if (chunk_data_size < 12) { 1287 return ERROR_MALFORMED; 1288 } 1289 1290 uint8_t header[12]; 1291 if (mDataSource->readAt( 1292 data_offset, header, sizeof(header)) 1293 < (ssize_t)sizeof(header)) { 1294 return ERROR_IO; 1295 } 1296 1297 int64_t creationTime; 1298 if (header[0] == 1) { 1299 creationTime = U64_AT(&header[4]); 1300 } else if (header[0] != 0) { 1301 return ERROR_MALFORMED; 1302 } else { 1303 creationTime = U32_AT(&header[4]); 1304 } 1305 1306 String8 s; 1307 convertTimeToDate(creationTime, &s); 1308 1309 mFileMetaData->setCString(kKeyDate, s.string()); 1310 1311 *offset += chunk_size; 1312 break; 1313 } 1314 1315 case FOURCC('m', 'd', 'a', 't'): 1316 { 1317 if (!mIsDrm) { 1318 *offset += chunk_size; 1319 break; 1320 } 1321 1322 if (chunk_size < 8) { 1323 return ERROR_MALFORMED; 1324 } 1325 1326 return parseDrmSINF(offset, data_offset); 1327 } 1328 1329 case FOURCC('h', 'd', 'l', 'r'): 1330 { 1331 uint32_t buffer; 1332 if (mDataSource->readAt( 1333 data_offset + 8, &buffer, 4) < 4) { 1334 return ERROR_IO; 1335 } 1336 1337 uint32_t type = ntohl(buffer); 1338 // For the 3GPP file format, the handler-type within the 'hdlr' box 1339 // shall be 'text' 1340 if (type == FOURCC('t', 'e', 'x', 't')) { 1341 mLastTrack->meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_TEXT_3GPP); 1342 } 1343 1344 *offset += chunk_size; 1345 break; 1346 } 1347 1348 case FOURCC('t', 'x', '3', 'g'): 1349 { 1350 uint32_t type; 1351 const void *data; 1352 size_t size = 0; 1353 if (!mLastTrack->meta->findData( 1354 kKeyTextFormatData, &type, &data, &size)) { 1355 size = 0; 1356 } 1357 1358 uint8_t *buffer = new uint8_t[size + chunk_size]; 1359 1360 if (size > 0) { 1361 memcpy(buffer, data, size); 1362 } 1363 1364 if ((size_t)(mDataSource->readAt(*offset, buffer + size, chunk_size)) 1365 < chunk_size) { 1366 delete[] buffer; 1367 buffer = NULL; 1368 1369 return ERROR_IO; 1370 } 1371 1372 mLastTrack->meta->setData( 1373 kKeyTextFormatData, 0, buffer, size + chunk_size); 1374 1375 delete[] buffer; 1376 1377 *offset += chunk_size; 1378 break; 1379 } 1380 1381 case FOURCC('c', 'o', 'v', 'r'): 1382 { 1383 if (mFileMetaData != NULL) { 1384 LOGV("chunk_data_size = %lld and data_offset = %lld", 1385 chunk_data_size, data_offset); 1386 uint8_t *buffer = new uint8_t[chunk_data_size + 1]; 1387 if (mDataSource->readAt( 1388 data_offset, buffer, chunk_data_size) != (ssize_t)chunk_data_size) { 1389 delete[] buffer; 1390 buffer = NULL; 1391 1392 return ERROR_IO; 1393 } 1394 const int kSkipBytesOfDataBox = 16; 1395 mFileMetaData->setData( 1396 kKeyAlbumArt, MetaData::TYPE_NONE, 1397 buffer + kSkipBytesOfDataBox, chunk_data_size - kSkipBytesOfDataBox); 1398 } 1399 1400 *offset += chunk_size; 1401 break; 1402 } 1403 1404 default: 1405 { 1406 *offset += chunk_size; 1407 break; 1408 } 1409 } 1410 1411 return OK; 1412 } 1413 1414 status_t MPEG4Extractor::parseTrackHeader( 1415 off64_t data_offset, off64_t data_size) { 1416 if (data_size < 4) { 1417 return ERROR_MALFORMED; 1418 } 1419 1420 uint8_t version; 1421 if (mDataSource->readAt(data_offset, &version, 1) < 1) { 1422 return ERROR_IO; 1423 } 1424 1425 size_t dynSize = (version == 1) ? 36 : 24; 1426 1427 uint8_t buffer[36 + 60]; 1428 1429 if (data_size != (off64_t)dynSize + 60) { 1430 return ERROR_MALFORMED; 1431 } 1432 1433 if (mDataSource->readAt( 1434 data_offset, buffer, data_size) < (ssize_t)data_size) { 1435 return ERROR_IO; 1436 } 1437 1438 uint64_t ctime, mtime, duration; 1439 int32_t id; 1440 1441 if (version == 1) { 1442 ctime = U64_AT(&buffer[4]); 1443 mtime = U64_AT(&buffer[12]); 1444 id = U32_AT(&buffer[20]); 1445 duration = U64_AT(&buffer[28]); 1446 } else { 1447 CHECK_EQ((unsigned)version, 0u); 1448 1449 ctime = U32_AT(&buffer[4]); 1450 mtime = U32_AT(&buffer[8]); 1451 id = U32_AT(&buffer[12]); 1452 duration = U32_AT(&buffer[20]); 1453 } 1454 1455 mLastTrack->meta->setInt32(kKeyTrackID, id); 1456 1457 size_t matrixOffset = dynSize + 16; 1458 int32_t a00 = U32_AT(&buffer[matrixOffset]); 1459 int32_t a01 = U32_AT(&buffer[matrixOffset + 4]); 1460 int32_t dx = U32_AT(&buffer[matrixOffset + 8]); 1461 int32_t a10 = U32_AT(&buffer[matrixOffset + 12]); 1462 int32_t a11 = U32_AT(&buffer[matrixOffset + 16]); 1463 int32_t dy = U32_AT(&buffer[matrixOffset + 20]); 1464 1465 #if 0 1466 LOGI("x' = %.2f * x + %.2f * y + %.2f", 1467 a00 / 65536.0f, a01 / 65536.0f, dx / 65536.0f); 1468 LOGI("y' = %.2f * x + %.2f * y + %.2f", 1469 a10 / 65536.0f, a11 / 65536.0f, dy / 65536.0f); 1470 #endif 1471 1472 uint32_t rotationDegrees; 1473 1474 static const int32_t kFixedOne = 0x10000; 1475 if (a00 == kFixedOne && a01 == 0 && a10 == 0 && a11 == kFixedOne) { 1476 // Identity, no rotation 1477 rotationDegrees = 0; 1478 } else if (a00 == 0 && a01 == kFixedOne && a10 == -kFixedOne && a11 == 0) { 1479 rotationDegrees = 90; 1480 } else if (a00 == 0 && a01 == -kFixedOne && a10 == kFixedOne && a11 == 0) { 1481 rotationDegrees = 270; 1482 } else if (a00 == -kFixedOne && a01 == 0 && a10 == 0 && a11 == -kFixedOne) { 1483 rotationDegrees = 180; 1484 } else { 1485 LOGW("We only support 0,90,180,270 degree rotation matrices"); 1486 rotationDegrees = 0; 1487 } 1488 1489 if (rotationDegrees != 0) { 1490 mLastTrack->meta->setInt32(kKeyRotation, rotationDegrees); 1491 } 1492 1493 // Handle presentation display size, which could be different 1494 // from the image size indicated by kKeyWidth and kKeyHeight. 1495 uint32_t width = U32_AT(&buffer[dynSize + 52]); 1496 uint32_t height = U32_AT(&buffer[dynSize + 56]); 1497 mLastTrack->meta->setInt32(kKeyDisplayWidth, width >> 16); 1498 mLastTrack->meta->setInt32(kKeyDisplayHeight, height >> 16); 1499 1500 return OK; 1501 } 1502 1503 status_t MPEG4Extractor::parseMetaData(off64_t offset, size_t size) { 1504 if (size < 4) { 1505 return ERROR_MALFORMED; 1506 } 1507 1508 uint8_t *buffer = new uint8_t[size + 1]; 1509 if (mDataSource->readAt( 1510 offset, buffer, size) != (ssize_t)size) { 1511 delete[] buffer; 1512 buffer = NULL; 1513 1514 return ERROR_IO; 1515 } 1516 1517 uint32_t flags = U32_AT(buffer); 1518 1519 uint32_t metadataKey = 0; 1520 switch (mPath[4]) { 1521 case FOURCC(0xa9, 'a', 'l', 'b'): 1522 { 1523 metadataKey = kKeyAlbum; 1524 break; 1525 } 1526 case FOURCC(0xa9, 'A', 'R', 'T'): 1527 { 1528 metadataKey = kKeyArtist; 1529 break; 1530 } 1531 case FOURCC('a', 'A', 'R', 'T'): 1532 { 1533 metadataKey = kKeyAlbumArtist; 1534 break; 1535 } 1536 case FOURCC(0xa9, 'd', 'a', 'y'): 1537 { 1538 metadataKey = kKeyYear; 1539 break; 1540 } 1541 case FOURCC(0xa9, 'n', 'a', 'm'): 1542 { 1543 metadataKey = kKeyTitle; 1544 break; 1545 } 1546 case FOURCC(0xa9, 'w', 'r', 't'): 1547 { 1548 metadataKey = kKeyWriter; 1549 break; 1550 } 1551 case FOURCC('c', 'o', 'v', 'r'): 1552 { 1553 metadataKey = kKeyAlbumArt; 1554 break; 1555 } 1556 case FOURCC('g', 'n', 'r', 'e'): 1557 { 1558 metadataKey = kKeyGenre; 1559 break; 1560 } 1561 case FOURCC(0xa9, 'g', 'e', 'n'): 1562 { 1563 metadataKey = kKeyGenre; 1564 break; 1565 } 1566 case FOURCC('c', 'p', 'i', 'l'): 1567 { 1568 if (size == 9 && flags == 21) { 1569 char tmp[16]; 1570 sprintf(tmp, "%d", 1571 (int)buffer[size - 1]); 1572 1573 mFileMetaData->setCString(kKeyCompilation, tmp); 1574 } 1575 break; 1576 } 1577 case FOURCC('t', 'r', 'k', 'n'): 1578 { 1579 if (size == 16 && flags == 0) { 1580 char tmp[16]; 1581 sprintf(tmp, "%d/%d", 1582 (int)buffer[size - 5], (int)buffer[size - 3]); 1583 1584 mFileMetaData->setCString(kKeyCDTrackNumber, tmp); 1585 } 1586 break; 1587 } 1588 case FOURCC('d', 'i', 's', 'k'): 1589 { 1590 if (size == 14 && flags == 0) { 1591 char tmp[16]; 1592 sprintf(tmp, "%d/%d", 1593 (int)buffer[size - 3], (int)buffer[size - 1]); 1594 1595 mFileMetaData->setCString(kKeyDiscNumber, tmp); 1596 } 1597 break; 1598 } 1599 1600 default: 1601 break; 1602 } 1603 1604 if (size >= 8 && metadataKey) { 1605 if (metadataKey == kKeyAlbumArt) { 1606 mFileMetaData->setData( 1607 kKeyAlbumArt, MetaData::TYPE_NONE, 1608 buffer + 8, size - 8); 1609 } else if (metadataKey == kKeyGenre) { 1610 if (flags == 0) { 1611 // uint8_t genre code, iTunes genre codes are 1612 // the standard id3 codes, except they start 1613 // at 1 instead of 0 (e.g. Pop is 14, not 13) 1614 // We use standard id3 numbering, so subtract 1. 1615 int genrecode = (int)buffer[size - 1]; 1616 genrecode--; 1617 if (genrecode < 0) { 1618 genrecode = 255; // reserved for 'unknown genre' 1619 } 1620 char genre[10]; 1621 sprintf(genre, "%d", genrecode); 1622 1623 mFileMetaData->setCString(metadataKey, genre); 1624 } else if (flags == 1) { 1625 // custom genre string 1626 buffer[size] = '\0'; 1627 1628 mFileMetaData->setCString( 1629 metadataKey, (const char *)buffer + 8); 1630 } 1631 } else { 1632 buffer[size] = '\0'; 1633 1634 mFileMetaData->setCString( 1635 metadataKey, (const char *)buffer + 8); 1636 } 1637 } 1638 1639 delete[] buffer; 1640 buffer = NULL; 1641 1642 return OK; 1643 } 1644 1645 sp<MediaSource> MPEG4Extractor::getTrack(size_t index) { 1646 status_t err; 1647 if ((err = readMetaData()) != OK) { 1648 return NULL; 1649 } 1650 1651 Track *track = mFirstTrack; 1652 while (index > 0) { 1653 if (track == NULL) { 1654 return NULL; 1655 } 1656 1657 track = track->next; 1658 --index; 1659 } 1660 1661 if (track == NULL) { 1662 return NULL; 1663 } 1664 1665 return new MPEG4Source( 1666 track->meta, mDataSource, track->timescale, track->sampleTable); 1667 } 1668 1669 // static 1670 status_t MPEG4Extractor::verifyTrack(Track *track) { 1671 const char *mime; 1672 CHECK(track->meta->findCString(kKeyMIMEType, &mime)); 1673 1674 uint32_t type; 1675 const void *data; 1676 size_t size; 1677 if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { 1678 if (!track->meta->findData(kKeyAVCC, &type, &data, &size) 1679 || type != kTypeAVCC) { 1680 return ERROR_MALFORMED; 1681 } 1682 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) 1683 || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { 1684 if (!track->meta->findData(kKeyESDS, &type, &data, &size) 1685 || type != kTypeESDS) { 1686 return ERROR_MALFORMED; 1687 } 1688 } 1689 1690 if (!track->sampleTable->isValid()) { 1691 // Make sure we have all the metadata we need. 1692 return ERROR_MALFORMED; 1693 } 1694 1695 return OK; 1696 } 1697 1698 status_t MPEG4Extractor::updateAudioTrackInfoFromESDS_MPEG4Audio( 1699 const void *esds_data, size_t esds_size) { 1700 ESDS esds(esds_data, esds_size); 1701 1702 uint8_t objectTypeIndication; 1703 if (esds.getObjectTypeIndication(&objectTypeIndication) != OK) { 1704 return ERROR_MALFORMED; 1705 } 1706 1707 if (objectTypeIndication == 0xe1) { 1708 // This isn't MPEG4 audio at all, it's QCELP 14k... 1709 mLastTrack->meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_QCELP); 1710 return OK; 1711 } 1712 1713 if (objectTypeIndication == 0x6b) { 1714 // The media subtype is MP3 audio 1715 // Our software MP3 audio decoder may not be able to handle 1716 // packetized MP3 audio; for now, lets just return ERROR_UNSUPPORTED 1717 LOGE("MP3 track in MP4/3GPP file is not supported"); 1718 return ERROR_UNSUPPORTED; 1719 } 1720 1721 const uint8_t *csd; 1722 size_t csd_size; 1723 if (esds.getCodecSpecificInfo( 1724 (const void **)&csd, &csd_size) != OK) { 1725 return ERROR_MALFORMED; 1726 } 1727 1728 #if 0 1729 printf("ESD of size %d\n", csd_size); 1730 hexdump(csd, csd_size); 1731 #endif 1732 1733 if (csd_size == 0) { 1734 // There's no further information, i.e. no codec specific data 1735 // Let's assume that the information provided in the mpeg4 headers 1736 // is accurate and hope for the best. 1737 1738 return OK; 1739 } 1740 1741 if (csd_size < 2) { 1742 return ERROR_MALFORMED; 1743 } 1744 1745 uint32_t objectType = csd[0] >> 3; 1746 1747 if (objectType == 31) { 1748 return ERROR_UNSUPPORTED; 1749 } 1750 1751 uint32_t freqIndex = (csd[0] & 7) << 1 | (csd[1] >> 7); 1752 int32_t sampleRate = 0; 1753 int32_t numChannels = 0; 1754 if (freqIndex == 15) { 1755 if (csd_size < 5) { 1756 return ERROR_MALFORMED; 1757 } 1758 1759 sampleRate = (csd[1] & 0x7f) << 17 1760 | csd[2] << 9 1761 | csd[3] << 1 1762 | (csd[4] >> 7); 1763 1764 numChannels = (csd[4] >> 3) & 15; 1765 } else { 1766 static uint32_t kSamplingRate[] = { 1767 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 1768 16000, 12000, 11025, 8000, 7350 1769 }; 1770 1771 if (freqIndex == 13 || freqIndex == 14) { 1772 return ERROR_MALFORMED; 1773 } 1774 1775 sampleRate = kSamplingRate[freqIndex]; 1776 numChannels = (csd[1] >> 3) & 15; 1777 } 1778 1779 if (numChannels == 0) { 1780 return ERROR_UNSUPPORTED; 1781 } 1782 1783 int32_t prevSampleRate; 1784 CHECK(mLastTrack->meta->findInt32(kKeySampleRate, &prevSampleRate)); 1785 1786 if (prevSampleRate != sampleRate) { 1787 LOGV("mpeg4 audio sample rate different from previous setting. " 1788 "was: %d, now: %d", prevSampleRate, sampleRate); 1789 } 1790 1791 mLastTrack->meta->setInt32(kKeySampleRate, sampleRate); 1792 1793 int32_t prevChannelCount; 1794 CHECK(mLastTrack->meta->findInt32(kKeyChannelCount, &prevChannelCount)); 1795 1796 if (prevChannelCount != numChannels) { 1797 LOGV("mpeg4 audio channel count different from previous setting. " 1798 "was: %d, now: %d", prevChannelCount, numChannels); 1799 } 1800 1801 mLastTrack->meta->setInt32(kKeyChannelCount, numChannels); 1802 1803 return OK; 1804 } 1805 1806 //////////////////////////////////////////////////////////////////////////////// 1807 1808 MPEG4Source::MPEG4Source( 1809 const sp<MetaData> &format, 1810 const sp<DataSource> &dataSource, 1811 int32_t timeScale, 1812 const sp<SampleTable> &sampleTable) 1813 : mFormat(format), 1814 mDataSource(dataSource), 1815 mTimescale(timeScale), 1816 mSampleTable(sampleTable), 1817 mCurrentSampleIndex(0), 1818 mIsAVC(false), 1819 mNALLengthSize(0), 1820 mStarted(false), 1821 mGroup(NULL), 1822 mBuffer(NULL), 1823 mWantsNALFragments(false), 1824 mSrcBuffer(NULL) { 1825 const char *mime; 1826 bool success = mFormat->findCString(kKeyMIMEType, &mime); 1827 CHECK(success); 1828 1829 mIsAVC = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC); 1830 1831 if (mIsAVC) { 1832 uint32_t type; 1833 const void *data; 1834 size_t size; 1835 CHECK(format->findData(kKeyAVCC, &type, &data, &size)); 1836 1837 const uint8_t *ptr = (const uint8_t *)data; 1838 1839 CHECK(size >= 7); 1840 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 1841 1842 // The number of bytes used to encode the length of a NAL unit. 1843 mNALLengthSize = 1 + (ptr[4] & 3); 1844 } 1845 } 1846 1847 MPEG4Source::~MPEG4Source() { 1848 if (mStarted) { 1849 stop(); 1850 } 1851 } 1852 1853 status_t MPEG4Source::start(MetaData *params) { 1854 Mutex::Autolock autoLock(mLock); 1855 1856 CHECK(!mStarted); 1857 1858 int32_t val; 1859 if (params && params->findInt32(kKeyWantsNALFragments, &val) 1860 && val != 0) { 1861 mWantsNALFragments = true; 1862 } else { 1863 mWantsNALFragments = false; 1864 } 1865 1866 mGroup = new MediaBufferGroup; 1867 1868 int32_t max_size; 1869 CHECK(mFormat->findInt32(kKeyMaxInputSize, &max_size)); 1870 1871 mGroup->add_buffer(new MediaBuffer(max_size)); 1872 1873 mSrcBuffer = new uint8_t[max_size]; 1874 1875 mStarted = true; 1876 1877 return OK; 1878 } 1879 1880 status_t MPEG4Source::stop() { 1881 Mutex::Autolock autoLock(mLock); 1882 1883 CHECK(mStarted); 1884 1885 if (mBuffer != NULL) { 1886 mBuffer->release(); 1887 mBuffer = NULL; 1888 } 1889 1890 delete[] mSrcBuffer; 1891 mSrcBuffer = NULL; 1892 1893 delete mGroup; 1894 mGroup = NULL; 1895 1896 mStarted = false; 1897 mCurrentSampleIndex = 0; 1898 1899 return OK; 1900 } 1901 1902 sp<MetaData> MPEG4Source::getFormat() { 1903 Mutex::Autolock autoLock(mLock); 1904 1905 return mFormat; 1906 } 1907 1908 size_t MPEG4Source::parseNALSize(const uint8_t *data) const { 1909 switch (mNALLengthSize) { 1910 case 1: 1911 return *data; 1912 case 2: 1913 return U16_AT(data); 1914 case 3: 1915 return ((size_t)data[0] << 16) | U16_AT(&data[1]); 1916 case 4: 1917 return U32_AT(data); 1918 } 1919 1920 // This cannot happen, mNALLengthSize springs to life by adding 1 to 1921 // a 2-bit integer. 1922 CHECK(!"Should not be here."); 1923 1924 return 0; 1925 } 1926 1927 status_t MPEG4Source::read( 1928 MediaBuffer **out, const ReadOptions *options) { 1929 Mutex::Autolock autoLock(mLock); 1930 1931 CHECK(mStarted); 1932 1933 *out = NULL; 1934 1935 int64_t targetSampleTimeUs = -1; 1936 1937 int64_t seekTimeUs; 1938 ReadOptions::SeekMode mode; 1939 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 1940 uint32_t findFlags = 0; 1941 switch (mode) { 1942 case ReadOptions::SEEK_PREVIOUS_SYNC: 1943 findFlags = SampleTable::kFlagBefore; 1944 break; 1945 case ReadOptions::SEEK_NEXT_SYNC: 1946 findFlags = SampleTable::kFlagAfter; 1947 break; 1948 case ReadOptions::SEEK_CLOSEST_SYNC: 1949 case ReadOptions::SEEK_CLOSEST: 1950 findFlags = SampleTable::kFlagClosest; 1951 break; 1952 default: 1953 CHECK(!"Should not be here."); 1954 break; 1955 } 1956 1957 uint32_t sampleIndex; 1958 status_t err = mSampleTable->findSampleAtTime( 1959 seekTimeUs * mTimescale / 1000000, 1960 &sampleIndex, findFlags); 1961 1962 if (mode == ReadOptions::SEEK_CLOSEST) { 1963 // We found the closest sample already, now we want the sync 1964 // sample preceding it (or the sample itself of course), even 1965 // if the subsequent sync sample is closer. 1966 findFlags = SampleTable::kFlagBefore; 1967 } 1968 1969 uint32_t syncSampleIndex; 1970 if (err == OK) { 1971 err = mSampleTable->findSyncSampleNear( 1972 sampleIndex, &syncSampleIndex, findFlags); 1973 } 1974 1975 uint32_t sampleTime; 1976 if (err == OK) { 1977 err = mSampleTable->getMetaDataForSample( 1978 sampleIndex, NULL, NULL, &sampleTime); 1979 } 1980 1981 if (err != OK) { 1982 if (err == ERROR_OUT_OF_RANGE) { 1983 // An attempt to seek past the end of the stream would 1984 // normally cause this ERROR_OUT_OF_RANGE error. Propagating 1985 // this all the way to the MediaPlayer would cause abnormal 1986 // termination. Legacy behaviour appears to be to behave as if 1987 // we had seeked to the end of stream, ending normally. 1988 err = ERROR_END_OF_STREAM; 1989 } 1990 return err; 1991 } 1992 1993 if (mode == ReadOptions::SEEK_CLOSEST) { 1994 targetSampleTimeUs = (sampleTime * 1000000ll) / mTimescale; 1995 } 1996 1997 #if 0 1998 uint32_t syncSampleTime; 1999 CHECK_EQ(OK, mSampleTable->getMetaDataForSample( 2000 syncSampleIndex, NULL, NULL, &syncSampleTime)); 2001 2002 LOGI("seek to time %lld us => sample at time %lld us, " 2003 "sync sample at time %lld us", 2004 seekTimeUs, 2005 sampleTime * 1000000ll / mTimescale, 2006 syncSampleTime * 1000000ll / mTimescale); 2007 #endif 2008 2009 mCurrentSampleIndex = syncSampleIndex; 2010 if (mBuffer != NULL) { 2011 mBuffer->release(); 2012 mBuffer = NULL; 2013 } 2014 2015 // fall through 2016 } 2017 2018 off64_t offset; 2019 size_t size; 2020 uint32_t cts; 2021 bool isSyncSample; 2022 bool newBuffer = false; 2023 if (mBuffer == NULL) { 2024 newBuffer = true; 2025 2026 status_t err = 2027 mSampleTable->getMetaDataForSample( 2028 mCurrentSampleIndex, &offset, &size, &cts, &isSyncSample); 2029 2030 if (err != OK) { 2031 return err; 2032 } 2033 2034 err = mGroup->acquire_buffer(&mBuffer); 2035 2036 if (err != OK) { 2037 CHECK(mBuffer == NULL); 2038 return err; 2039 } 2040 } 2041 2042 if (!mIsAVC || mWantsNALFragments) { 2043 if (newBuffer) { 2044 ssize_t num_bytes_read = 2045 mDataSource->readAt(offset, (uint8_t *)mBuffer->data(), size); 2046 2047 if (num_bytes_read < (ssize_t)size) { 2048 mBuffer->release(); 2049 mBuffer = NULL; 2050 2051 return ERROR_IO; 2052 } 2053 2054 CHECK(mBuffer != NULL); 2055 mBuffer->set_range(0, size); 2056 mBuffer->meta_data()->clear(); 2057 mBuffer->meta_data()->setInt64( 2058 kKeyTime, ((int64_t)cts * 1000000) / mTimescale); 2059 2060 if (targetSampleTimeUs >= 0) { 2061 mBuffer->meta_data()->setInt64( 2062 kKeyTargetTime, targetSampleTimeUs); 2063 } 2064 2065 if (isSyncSample) { 2066 mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 2067 } 2068 2069 ++mCurrentSampleIndex; 2070 } 2071 2072 if (!mIsAVC) { 2073 *out = mBuffer; 2074 mBuffer = NULL; 2075 2076 return OK; 2077 } 2078 2079 // Each NAL unit is split up into its constituent fragments and 2080 // each one of them returned in its own buffer. 2081 2082 CHECK(mBuffer->range_length() >= mNALLengthSize); 2083 2084 const uint8_t *src = 2085 (const uint8_t *)mBuffer->data() + mBuffer->range_offset(); 2086 2087 size_t nal_size = parseNALSize(src); 2088 if (mBuffer->range_length() < mNALLengthSize + nal_size) { 2089 LOGE("incomplete NAL unit."); 2090 2091 mBuffer->release(); 2092 mBuffer = NULL; 2093 2094 return ERROR_MALFORMED; 2095 } 2096 2097 MediaBuffer *clone = mBuffer->clone(); 2098 CHECK(clone != NULL); 2099 clone->set_range(mBuffer->range_offset() + mNALLengthSize, nal_size); 2100 2101 CHECK(mBuffer != NULL); 2102 mBuffer->set_range( 2103 mBuffer->range_offset() + mNALLengthSize + nal_size, 2104 mBuffer->range_length() - mNALLengthSize - nal_size); 2105 2106 if (mBuffer->range_length() == 0) { 2107 mBuffer->release(); 2108 mBuffer = NULL; 2109 } 2110 2111 *out = clone; 2112 2113 return OK; 2114 } else { 2115 // Whole NAL units are returned but each fragment is prefixed by 2116 // the start code (0x00 00 00 01). 2117 ssize_t num_bytes_read = 0; 2118 int32_t drm = 0; 2119 bool usesDRM = (mFormat->findInt32(kKeyIsDRM, &drm) && drm != 0); 2120 if (usesDRM) { 2121 num_bytes_read = 2122 mDataSource->readAt(offset, (uint8_t*)mBuffer->data(), size); 2123 } else { 2124 num_bytes_read = mDataSource->readAt(offset, mSrcBuffer, size); 2125 } 2126 2127 if (num_bytes_read < (ssize_t)size) { 2128 mBuffer->release(); 2129 mBuffer = NULL; 2130 2131 return ERROR_IO; 2132 } 2133 2134 if (usesDRM) { 2135 CHECK(mBuffer != NULL); 2136 mBuffer->set_range(0, size); 2137 2138 } else { 2139 uint8_t *dstData = (uint8_t *)mBuffer->data(); 2140 size_t srcOffset = 0; 2141 size_t dstOffset = 0; 2142 2143 while (srcOffset < size) { 2144 bool isMalFormed = (srcOffset + mNALLengthSize > size); 2145 size_t nalLength = 0; 2146 if (!isMalFormed) { 2147 nalLength = parseNALSize(&mSrcBuffer[srcOffset]); 2148 srcOffset += mNALLengthSize; 2149 isMalFormed = srcOffset + nalLength > size; 2150 } 2151 2152 if (isMalFormed) { 2153 LOGE("Video is malformed"); 2154 mBuffer->release(); 2155 mBuffer = NULL; 2156 return ERROR_MALFORMED; 2157 } 2158 2159 if (nalLength == 0) { 2160 continue; 2161 } 2162 2163 CHECK(dstOffset + 4 <= mBuffer->size()); 2164 2165 dstData[dstOffset++] = 0; 2166 dstData[dstOffset++] = 0; 2167 dstData[dstOffset++] = 0; 2168 dstData[dstOffset++] = 1; 2169 memcpy(&dstData[dstOffset], &mSrcBuffer[srcOffset], nalLength); 2170 srcOffset += nalLength; 2171 dstOffset += nalLength; 2172 } 2173 CHECK_EQ(srcOffset, size); 2174 CHECK(mBuffer != NULL); 2175 mBuffer->set_range(0, dstOffset); 2176 } 2177 2178 mBuffer->meta_data()->clear(); 2179 mBuffer->meta_data()->setInt64( 2180 kKeyTime, ((int64_t)cts * 1000000) / mTimescale); 2181 2182 if (targetSampleTimeUs >= 0) { 2183 mBuffer->meta_data()->setInt64( 2184 kKeyTargetTime, targetSampleTimeUs); 2185 } 2186 2187 if (isSyncSample) { 2188 mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 2189 } 2190 2191 ++mCurrentSampleIndex; 2192 2193 *out = mBuffer; 2194 mBuffer = NULL; 2195 2196 return OK; 2197 } 2198 } 2199 2200 MPEG4Extractor::Track *MPEG4Extractor::findTrackByMimePrefix( 2201 const char *mimePrefix) { 2202 for (Track *track = mFirstTrack; track != NULL; track = track->next) { 2203 const char *mime; 2204 if (track->meta != NULL 2205 && track->meta->findCString(kKeyMIMEType, &mime) 2206 && !strncasecmp(mime, mimePrefix, strlen(mimePrefix))) { 2207 return track; 2208 } 2209 } 2210 2211 return NULL; 2212 } 2213 2214 static bool LegacySniffMPEG4( 2215 const sp<DataSource> &source, String8 *mimeType, float *confidence) { 2216 uint8_t header[8]; 2217 2218 ssize_t n = source->readAt(4, header, sizeof(header)); 2219 if (n < (ssize_t)sizeof(header)) { 2220 return false; 2221 } 2222 2223 if (!memcmp(header, "ftyp3gp", 7) || !memcmp(header, "ftypmp42", 8) 2224 || !memcmp(header, "ftyp3gr6", 8) || !memcmp(header, "ftyp3gs6", 8) 2225 || !memcmp(header, "ftyp3ge6", 8) || !memcmp(header, "ftyp3gg6", 8) 2226 || !memcmp(header, "ftypisom", 8) || !memcmp(header, "ftypM4V ", 8) 2227 || !memcmp(header, "ftypM4A ", 8) || !memcmp(header, "ftypf4v ", 8) 2228 || !memcmp(header, "ftypkddi", 8) || !memcmp(header, "ftypM4VP", 8)) { 2229 *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; 2230 *confidence = 0.4; 2231 2232 return true; 2233 } 2234 2235 return false; 2236 } 2237 2238 static bool isCompatibleBrand(uint32_t fourcc) { 2239 static const uint32_t kCompatibleBrands[] = { 2240 FOURCC('i', 's', 'o', 'm'), 2241 FOURCC('i', 's', 'o', '2'), 2242 FOURCC('a', 'v', 'c', '1'), 2243 FOURCC('3', 'g', 'p', '4'), 2244 FOURCC('m', 'p', '4', '1'), 2245 FOURCC('m', 'p', '4', '2'), 2246 2247 // Won't promise that the following file types can be played. 2248 // Just give these file types a chance. 2249 FOURCC('q', 't', ' ', ' '), // Apple's QuickTime 2250 FOURCC('M', 'S', 'N', 'V'), // Sony's PSP 2251 2252 FOURCC('3', 'g', '2', 'a'), // 3GPP2 2253 FOURCC('3', 'g', '2', 'b'), 2254 }; 2255 2256 for (size_t i = 0; 2257 i < sizeof(kCompatibleBrands) / sizeof(kCompatibleBrands[0]); 2258 ++i) { 2259 if (kCompatibleBrands[i] == fourcc) { 2260 return true; 2261 } 2262 } 2263 2264 return false; 2265 } 2266 2267 // Attempt to actually parse the 'ftyp' atom and determine if a suitable 2268 // compatible brand is present. 2269 static bool BetterSniffMPEG4( 2270 const sp<DataSource> &source, String8 *mimeType, float *confidence) { 2271 uint8_t header[12]; 2272 if (source->readAt(0, header, 12) != 12 2273 || memcmp("ftyp", &header[4], 4)) { 2274 return false; 2275 } 2276 2277 size_t atomSize = U32_AT(&header[0]); 2278 if (atomSize < 16 || (atomSize % 4) != 0) { 2279 return false; 2280 } 2281 2282 bool success = false; 2283 if (isCompatibleBrand(U32_AT(&header[8]))) { 2284 success = true; 2285 } else { 2286 size_t numCompatibleBrands = (atomSize - 16) / 4; 2287 for (size_t i = 0; i < numCompatibleBrands; ++i) { 2288 uint8_t tmp[4]; 2289 if (source->readAt(16 + i * 4, tmp, 4) != 4) { 2290 return false; 2291 } 2292 2293 if (isCompatibleBrand(U32_AT(&tmp[0]))) { 2294 success = true; 2295 break; 2296 } 2297 } 2298 } 2299 2300 if (!success) { 2301 return false; 2302 } 2303 2304 *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; 2305 *confidence = 0.4f; 2306 2307 return true; 2308 } 2309 2310 bool SniffMPEG4( 2311 const sp<DataSource> &source, String8 *mimeType, float *confidence, 2312 sp<AMessage> *) { 2313 if (BetterSniffMPEG4(source, mimeType, confidence)) { 2314 return true; 2315 } 2316 2317 if (LegacySniffMPEG4(source, mimeType, confidence)) { 2318 LOGW("Identified supported mpeg4 through LegacySniffMPEG4."); 2319 return true; 2320 } 2321 2322 return false; 2323 } 2324 2325 } // namespace android 2326 2327