MPEG4Extractor.cpp revision c1f4b2b73fe9432cb75ce1dd368475a2867e70ed
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_NDEBUG 0 18#define LOG_TAG "MPEG4Extractor" 19#include <utils/Log.h> 20 21#include "include/MPEG4Extractor.h" 22#include "include/SampleTable.h" 23#include "include/ESDS.h" 24 25#include <ctype.h> 26#include <stdint.h> 27#include <stdlib.h> 28#include <string.h> 29 30#include <media/stagefright/foundation/ABitReader.h> 31#include <media/stagefright/foundation/ABuffer.h> 32#include <media/stagefright/foundation/ADebug.h> 33#include <media/stagefright/foundation/AMessage.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 <utils/String8.h> 40 41#include <byteswap.h> 42#include "include/ID3.h" 43 44namespace android { 45 46class MPEG4Source : public MediaSource { 47public: 48 // Caller retains ownership of both "dataSource" and "sampleTable". 49 MPEG4Source(const sp<MetaData> &format, 50 const sp<DataSource> &dataSource, 51 int32_t timeScale, 52 const sp<SampleTable> &sampleTable, 53 Vector<SidxEntry> &sidx, 54 const Trex *trex, 55 off64_t firstMoofOffset); 56 57 virtual status_t start(MetaData *params = NULL); 58 virtual status_t stop(); 59 60 virtual sp<MetaData> getFormat(); 61 62 virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 63 virtual status_t fragmentedRead(MediaBuffer **buffer, const ReadOptions *options = NULL); 64 65protected: 66 virtual ~MPEG4Source(); 67 68private: 69 Mutex mLock; 70 71 sp<MetaData> mFormat; 72 sp<DataSource> mDataSource; 73 int32_t mTimescale; 74 sp<SampleTable> mSampleTable; 75 uint32_t mCurrentSampleIndex; 76 uint32_t mCurrentFragmentIndex; 77 Vector<SidxEntry> &mSegments; 78 const Trex *mTrex; 79 off64_t mFirstMoofOffset; 80 off64_t mCurrentMoofOffset; 81 off64_t mNextMoofOffset; 82 uint32_t mCurrentTime; 83 int32_t mLastParsedTrackId; 84 int32_t mTrackId; 85 86 int32_t mCryptoMode; // passed in from extractor 87 int32_t mDefaultIVSize; // passed in from extractor 88 uint8_t mCryptoKey[16]; // passed in from extractor 89 uint32_t mCurrentAuxInfoType; 90 uint32_t mCurrentAuxInfoTypeParameter; 91 int32_t mCurrentDefaultSampleInfoSize; 92 uint32_t mCurrentSampleInfoCount; 93 uint32_t mCurrentSampleInfoAllocSize; 94 uint8_t* mCurrentSampleInfoSizes; 95 uint32_t mCurrentSampleInfoOffsetCount; 96 uint32_t mCurrentSampleInfoOffsetsAllocSize; 97 uint64_t* mCurrentSampleInfoOffsets; 98 99 bool mIsAVC; 100 bool mIsHEVC; 101 size_t mNALLengthSize; 102 103 bool mStarted; 104 105 MediaBufferGroup *mGroup; 106 107 MediaBuffer *mBuffer; 108 109 bool mWantsNALFragments; 110 111 uint8_t *mSrcBuffer; 112 113 size_t parseNALSize(const uint8_t *data) const; 114 status_t parseChunk(off64_t *offset); 115 status_t parseTrackFragmentHeader(off64_t offset, off64_t size); 116 status_t parseTrackFragmentRun(off64_t offset, off64_t size); 117 status_t parseSampleAuxiliaryInformationSizes(off64_t offset, off64_t size); 118 status_t parseSampleAuxiliaryInformationOffsets(off64_t offset, off64_t size); 119 120 struct TrackFragmentHeaderInfo { 121 enum Flags { 122 kBaseDataOffsetPresent = 0x01, 123 kSampleDescriptionIndexPresent = 0x02, 124 kDefaultSampleDurationPresent = 0x08, 125 kDefaultSampleSizePresent = 0x10, 126 kDefaultSampleFlagsPresent = 0x20, 127 kDurationIsEmpty = 0x10000, 128 }; 129 130 uint32_t mTrackID; 131 uint32_t mFlags; 132 uint64_t mBaseDataOffset; 133 uint32_t mSampleDescriptionIndex; 134 uint32_t mDefaultSampleDuration; 135 uint32_t mDefaultSampleSize; 136 uint32_t mDefaultSampleFlags; 137 138 uint64_t mDataOffset; 139 }; 140 TrackFragmentHeaderInfo mTrackFragmentHeaderInfo; 141 142 struct Sample { 143 off64_t offset; 144 size_t size; 145 uint32_t duration; 146 int32_t compositionOffset; 147 uint8_t iv[16]; 148 Vector<size_t> clearsizes; 149 Vector<size_t> encryptedsizes; 150 }; 151 Vector<Sample> mCurrentSamples; 152 153 MPEG4Source(const MPEG4Source &); 154 MPEG4Source &operator=(const MPEG4Source &); 155}; 156 157// This custom data source wraps an existing one and satisfies requests 158// falling entirely within a cached range from the cache while forwarding 159// all remaining requests to the wrapped datasource. 160// This is used to cache the full sampletable metadata for a single track, 161// possibly wrapping multiple times to cover all tracks, i.e. 162// Each MPEG4DataSource caches the sampletable metadata for a single track. 163 164struct MPEG4DataSource : public DataSource { 165 MPEG4DataSource(const sp<DataSource> &source); 166 167 virtual status_t initCheck() const; 168 virtual ssize_t readAt(off64_t offset, void *data, size_t size); 169 virtual status_t getSize(off64_t *size); 170 virtual uint32_t flags(); 171 172 status_t setCachedRange(off64_t offset, size_t size); 173 174protected: 175 virtual ~MPEG4DataSource(); 176 177private: 178 Mutex mLock; 179 180 sp<DataSource> mSource; 181 off64_t mCachedOffset; 182 size_t mCachedSize; 183 uint8_t *mCache; 184 185 void clearCache(); 186 187 MPEG4DataSource(const MPEG4DataSource &); 188 MPEG4DataSource &operator=(const MPEG4DataSource &); 189}; 190 191MPEG4DataSource::MPEG4DataSource(const sp<DataSource> &source) 192 : mSource(source), 193 mCachedOffset(0), 194 mCachedSize(0), 195 mCache(NULL) { 196} 197 198MPEG4DataSource::~MPEG4DataSource() { 199 clearCache(); 200} 201 202void MPEG4DataSource::clearCache() { 203 if (mCache) { 204 free(mCache); 205 mCache = NULL; 206 } 207 208 mCachedOffset = 0; 209 mCachedSize = 0; 210} 211 212status_t MPEG4DataSource::initCheck() const { 213 return mSource->initCheck(); 214} 215 216ssize_t MPEG4DataSource::readAt(off64_t offset, void *data, size_t size) { 217 Mutex::Autolock autoLock(mLock); 218 219 if (offset >= mCachedOffset 220 && offset + size <= mCachedOffset + mCachedSize) { 221 memcpy(data, &mCache[offset - mCachedOffset], size); 222 return size; 223 } 224 225 return mSource->readAt(offset, data, size); 226} 227 228status_t MPEG4DataSource::getSize(off64_t *size) { 229 return mSource->getSize(size); 230} 231 232uint32_t MPEG4DataSource::flags() { 233 return mSource->flags(); 234} 235 236status_t MPEG4DataSource::setCachedRange(off64_t offset, size_t size) { 237 Mutex::Autolock autoLock(mLock); 238 239 clearCache(); 240 241 mCache = (uint8_t *)malloc(size); 242 243 if (mCache == NULL) { 244 return -ENOMEM; 245 } 246 247 mCachedOffset = offset; 248 mCachedSize = size; 249 250 ssize_t err = mSource->readAt(mCachedOffset, mCache, mCachedSize); 251 252 if (err < (ssize_t)size) { 253 clearCache(); 254 255 return ERROR_IO; 256 } 257 258 return OK; 259} 260 261//////////////////////////////////////////////////////////////////////////////// 262 263static void hexdump(const void *_data, size_t size) { 264 const uint8_t *data = (const uint8_t *)_data; 265 size_t offset = 0; 266 while (offset < size) { 267 printf("0x%04zx ", offset); 268 269 size_t n = size - offset; 270 if (n > 16) { 271 n = 16; 272 } 273 274 for (size_t i = 0; i < 16; ++i) { 275 if (i == 8) { 276 printf(" "); 277 } 278 279 if (offset + i < size) { 280 printf("%02x ", data[offset + i]); 281 } else { 282 printf(" "); 283 } 284 } 285 286 printf(" "); 287 288 for (size_t i = 0; i < n; ++i) { 289 if (isprint(data[offset + i])) { 290 printf("%c", data[offset + i]); 291 } else { 292 printf("."); 293 } 294 } 295 296 printf("\n"); 297 298 offset += 16; 299 } 300} 301 302static const char *FourCC2MIME(uint32_t fourcc) { 303 switch (fourcc) { 304 case FOURCC('m', 'p', '4', 'a'): 305 return MEDIA_MIMETYPE_AUDIO_AAC; 306 307 case FOURCC('s', 'a', 'm', 'r'): 308 return MEDIA_MIMETYPE_AUDIO_AMR_NB; 309 310 case FOURCC('s', 'a', 'w', 'b'): 311 return MEDIA_MIMETYPE_AUDIO_AMR_WB; 312 313 case FOURCC('m', 'p', '4', 'v'): 314 return MEDIA_MIMETYPE_VIDEO_MPEG4; 315 316 case FOURCC('s', '2', '6', '3'): 317 case FOURCC('h', '2', '6', '3'): 318 case FOURCC('H', '2', '6', '3'): 319 return MEDIA_MIMETYPE_VIDEO_H263; 320 321 case FOURCC('a', 'v', 'c', '1'): 322 return MEDIA_MIMETYPE_VIDEO_AVC; 323 324 case FOURCC('h', 'v', 'c', '1'): 325 case FOURCC('h', 'e', 'v', '1'): 326 return MEDIA_MIMETYPE_VIDEO_HEVC; 327 default: 328 CHECK(!"should not be here."); 329 return NULL; 330 } 331} 332 333static bool AdjustChannelsAndRate(uint32_t fourcc, uint32_t *channels, uint32_t *rate) { 334 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, FourCC2MIME(fourcc))) { 335 // AMR NB audio is always mono, 8kHz 336 *channels = 1; 337 *rate = 8000; 338 return true; 339 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, FourCC2MIME(fourcc))) { 340 // AMR WB audio is always mono, 16kHz 341 *channels = 1; 342 *rate = 16000; 343 return true; 344 } 345 return false; 346} 347 348MPEG4Extractor::MPEG4Extractor(const sp<DataSource> &source) 349 : mMoofOffset(0), 350 mDataSource(source), 351 mInitCheck(NO_INIT), 352 mHasVideo(false), 353 mHeaderTimescale(0), 354 mFirstTrack(NULL), 355 mLastTrack(NULL), 356 mFileMetaData(new MetaData), 357 mFirstSINF(NULL), 358 mIsDrm(false) { 359} 360 361MPEG4Extractor::~MPEG4Extractor() { 362 Track *track = mFirstTrack; 363 while (track) { 364 Track *next = track->next; 365 366 delete track; 367 track = next; 368 } 369 mFirstTrack = mLastTrack = NULL; 370 371 SINF *sinf = mFirstSINF; 372 while (sinf) { 373 SINF *next = sinf->next; 374 delete[] sinf->IPMPData; 375 delete sinf; 376 sinf = next; 377 } 378 mFirstSINF = NULL; 379 380 for (size_t i = 0; i < mPssh.size(); i++) { 381 delete [] mPssh[i].data; 382 } 383} 384 385uint32_t MPEG4Extractor::flags() const { 386 return CAN_PAUSE | 387 ((mMoofOffset == 0 || mSidxEntries.size() != 0) ? 388 (CAN_SEEK_BACKWARD | CAN_SEEK_FORWARD | CAN_SEEK) : 0); 389} 390 391sp<MetaData> MPEG4Extractor::getMetaData() { 392 status_t err; 393 if ((err = readMetaData()) != OK) { 394 return new MetaData; 395 } 396 397 return mFileMetaData; 398} 399 400size_t MPEG4Extractor::countTracks() { 401 status_t err; 402 if ((err = readMetaData()) != OK) { 403 ALOGV("MPEG4Extractor::countTracks: no tracks"); 404 return 0; 405 } 406 407 size_t n = 0; 408 Track *track = mFirstTrack; 409 while (track) { 410 ++n; 411 track = track->next; 412 } 413 414 ALOGV("MPEG4Extractor::countTracks: %d tracks", n); 415 return n; 416} 417 418sp<MetaData> MPEG4Extractor::getTrackMetaData( 419 size_t index, uint32_t flags) { 420 status_t err; 421 if ((err = readMetaData()) != OK) { 422 return NULL; 423 } 424 425 Track *track = mFirstTrack; 426 while (index > 0) { 427 if (track == NULL) { 428 return NULL; 429 } 430 431 track = track->next; 432 --index; 433 } 434 435 if (track == NULL) { 436 return NULL; 437 } 438 439 if ((flags & kIncludeExtensiveMetaData) 440 && !track->includes_expensive_metadata) { 441 track->includes_expensive_metadata = true; 442 443 const char *mime; 444 CHECK(track->meta->findCString(kKeyMIMEType, &mime)); 445 if (!strncasecmp("video/", mime, 6)) { 446 if (mMoofOffset > 0) { 447 int64_t duration; 448 if (track->meta->findInt64(kKeyDuration, &duration)) { 449 // nothing fancy, just pick a frame near 1/4th of the duration 450 track->meta->setInt64( 451 kKeyThumbnailTime, duration / 4); 452 } 453 } else { 454 uint32_t sampleIndex; 455 uint32_t sampleTime; 456 if (track->sampleTable->findThumbnailSample(&sampleIndex) == OK 457 && track->sampleTable->getMetaDataForSample( 458 sampleIndex, NULL /* offset */, NULL /* size */, 459 &sampleTime) == OK) { 460 track->meta->setInt64( 461 kKeyThumbnailTime, 462 ((int64_t)sampleTime * 1000000) / track->timescale); 463 } 464 } 465 } 466 } 467 468 return track->meta; 469} 470 471static void MakeFourCCString(uint32_t x, char *s) { 472 s[0] = x >> 24; 473 s[1] = (x >> 16) & 0xff; 474 s[2] = (x >> 8) & 0xff; 475 s[3] = x & 0xff; 476 s[4] = '\0'; 477} 478 479status_t MPEG4Extractor::readMetaData() { 480 if (mInitCheck != NO_INIT) { 481 return mInitCheck; 482 } 483 484 off64_t offset = 0; 485 status_t err; 486 while (true) { 487 off64_t orig_offset = offset; 488 err = parseChunk(&offset, 0); 489 490 if (offset <= orig_offset) { 491 // only continue parsing if the offset was advanced, 492 // otherwise we might end up in an infinite loop 493 ALOGE("did not advance: 0x%lld->0x%lld", orig_offset, offset); 494 err = ERROR_MALFORMED; 495 break; 496 } else if (err == OK) { 497 continue; 498 } else if (err != UNKNOWN_ERROR) { 499 break; 500 } 501 uint32_t hdr[2]; 502 if (mDataSource->readAt(offset, hdr, 8) < 8) { 503 break; 504 } 505 uint32_t chunk_type = ntohl(hdr[1]); 506 if (chunk_type == FOURCC('m', 'o', 'o', 'f')) { 507 // store the offset of the first segment 508 mMoofOffset = offset; 509 } else if (chunk_type != FOURCC('m', 'd', 'a', 't')) { 510 // keep parsing until we get to the data 511 continue; 512 } 513 break; 514 } 515 516 if (mInitCheck == OK) { 517 if (mHasVideo) { 518 mFileMetaData->setCString( 519 kKeyMIMEType, MEDIA_MIMETYPE_CONTAINER_MPEG4); 520 } else { 521 mFileMetaData->setCString(kKeyMIMEType, "audio/mp4"); 522 } 523 } else { 524 mInitCheck = err; 525 } 526 527 CHECK_NE(err, (status_t)NO_INIT); 528 529 // copy pssh data into file metadata 530 int psshsize = 0; 531 for (size_t i = 0; i < mPssh.size(); i++) { 532 psshsize += 20 + mPssh[i].datalen; 533 } 534 if (psshsize) { 535 char *buf = (char*)malloc(psshsize); 536 char *ptr = buf; 537 for (size_t i = 0; i < mPssh.size(); i++) { 538 memcpy(ptr, mPssh[i].uuid, 20); // uuid + length 539 memcpy(ptr + 20, mPssh[i].data, mPssh[i].datalen); 540 ptr += (20 + mPssh[i].datalen); 541 } 542 mFileMetaData->setData(kKeyPssh, 'pssh', buf, psshsize); 543 free(buf); 544 } 545 return mInitCheck; 546} 547 548char* MPEG4Extractor::getDrmTrackInfo(size_t trackID, int *len) { 549 if (mFirstSINF == NULL) { 550 return NULL; 551 } 552 553 SINF *sinf = mFirstSINF; 554 while (sinf && (trackID != sinf->trackID)) { 555 sinf = sinf->next; 556 } 557 558 if (sinf == NULL) { 559 return NULL; 560 } 561 562 *len = sinf->len; 563 return sinf->IPMPData; 564} 565 566// Reads an encoded integer 7 bits at a time until it encounters the high bit clear. 567static int32_t readSize(off64_t offset, 568 const sp<DataSource> DataSource, uint8_t *numOfBytes) { 569 uint32_t size = 0; 570 uint8_t data; 571 bool moreData = true; 572 *numOfBytes = 0; 573 574 while (moreData) { 575 if (DataSource->readAt(offset, &data, 1) < 1) { 576 return -1; 577 } 578 offset ++; 579 moreData = (data >= 128) ? true : false; 580 size = (size << 7) | (data & 0x7f); // Take last 7 bits 581 (*numOfBytes) ++; 582 } 583 584 return size; 585} 586 587status_t MPEG4Extractor::parseDrmSINF( 588 off64_t * /* offset */, off64_t data_offset) { 589 uint8_t updateIdTag; 590 if (mDataSource->readAt(data_offset, &updateIdTag, 1) < 1) { 591 return ERROR_IO; 592 } 593 data_offset ++; 594 595 if (0x01/*OBJECT_DESCRIPTOR_UPDATE_ID_TAG*/ != updateIdTag) { 596 return ERROR_MALFORMED; 597 } 598 599 uint8_t numOfBytes; 600 int32_t size = readSize(data_offset, mDataSource, &numOfBytes); 601 if (size < 0) { 602 return ERROR_IO; 603 } 604 int32_t classSize = size; 605 data_offset += numOfBytes; 606 607 while(size >= 11 ) { 608 uint8_t descriptorTag; 609 if (mDataSource->readAt(data_offset, &descriptorTag, 1) < 1) { 610 return ERROR_IO; 611 } 612 data_offset ++; 613 614 if (0x11/*OBJECT_DESCRIPTOR_ID_TAG*/ != descriptorTag) { 615 return ERROR_MALFORMED; 616 } 617 618 uint8_t buffer[8]; 619 //ObjectDescriptorID and ObjectDescriptor url flag 620 if (mDataSource->readAt(data_offset, buffer, 2) < 2) { 621 return ERROR_IO; 622 } 623 data_offset += 2; 624 625 if ((buffer[1] >> 5) & 0x0001) { //url flag is set 626 return ERROR_MALFORMED; 627 } 628 629 if (mDataSource->readAt(data_offset, buffer, 8) < 8) { 630 return ERROR_IO; 631 } 632 data_offset += 8; 633 634 if ((0x0F/*ES_ID_REF_TAG*/ != buffer[1]) 635 || ( 0x0A/*IPMP_DESCRIPTOR_POINTER_ID_TAG*/ != buffer[5])) { 636 return ERROR_MALFORMED; 637 } 638 639 SINF *sinf = new SINF; 640 sinf->trackID = U16_AT(&buffer[3]); 641 sinf->IPMPDescriptorID = buffer[7]; 642 sinf->next = mFirstSINF; 643 mFirstSINF = sinf; 644 645 size -= (8 + 2 + 1); 646 } 647 648 if (size != 0) { 649 return ERROR_MALFORMED; 650 } 651 652 if (mDataSource->readAt(data_offset, &updateIdTag, 1) < 1) { 653 return ERROR_IO; 654 } 655 data_offset ++; 656 657 if(0x05/*IPMP_DESCRIPTOR_UPDATE_ID_TAG*/ != updateIdTag) { 658 return ERROR_MALFORMED; 659 } 660 661 size = readSize(data_offset, mDataSource, &numOfBytes); 662 if (size < 0) { 663 return ERROR_IO; 664 } 665 classSize = size; 666 data_offset += numOfBytes; 667 668 while (size > 0) { 669 uint8_t tag; 670 int32_t dataLen; 671 if (mDataSource->readAt(data_offset, &tag, 1) < 1) { 672 return ERROR_IO; 673 } 674 data_offset ++; 675 676 if (0x0B/*IPMP_DESCRIPTOR_ID_TAG*/ == tag) { 677 uint8_t id; 678 dataLen = readSize(data_offset, mDataSource, &numOfBytes); 679 if (dataLen < 0) { 680 return ERROR_IO; 681 } else if (dataLen < 4) { 682 return ERROR_MALFORMED; 683 } 684 data_offset += numOfBytes; 685 686 if (mDataSource->readAt(data_offset, &id, 1) < 1) { 687 return ERROR_IO; 688 } 689 data_offset ++; 690 691 SINF *sinf = mFirstSINF; 692 while (sinf && (sinf->IPMPDescriptorID != id)) { 693 sinf = sinf->next; 694 } 695 if (sinf == NULL) { 696 return ERROR_MALFORMED; 697 } 698 sinf->len = dataLen - 3; 699 sinf->IPMPData = new (std::nothrow) char[sinf->len]; 700 if (sinf->IPMPData == NULL) { 701 return ERROR_MALFORMED; 702 } 703 data_offset += 2; 704 705 if (mDataSource->readAt(data_offset, sinf->IPMPData, sinf->len) < sinf->len) { 706 return ERROR_IO; 707 } 708 data_offset += sinf->len; 709 710 size -= (dataLen + numOfBytes + 1); 711 } 712 } 713 714 if (size != 0) { 715 return ERROR_MALFORMED; 716 } 717 718 return UNKNOWN_ERROR; // Return a dummy error. 719} 720 721struct PathAdder { 722 PathAdder(Vector<uint32_t> *path, uint32_t chunkType) 723 : mPath(path) { 724 mPath->push(chunkType); 725 } 726 727 ~PathAdder() { 728 mPath->pop(); 729 } 730 731private: 732 Vector<uint32_t> *mPath; 733 734 PathAdder(const PathAdder &); 735 PathAdder &operator=(const PathAdder &); 736}; 737 738static bool underMetaDataPath(const Vector<uint32_t> &path) { 739 return path.size() >= 5 740 && path[0] == FOURCC('m', 'o', 'o', 'v') 741 && path[1] == FOURCC('u', 'd', 't', 'a') 742 && path[2] == FOURCC('m', 'e', 't', 'a') 743 && path[3] == FOURCC('i', 'l', 's', 't'); 744} 745 746// Given a time in seconds since Jan 1 1904, produce a human-readable string. 747static void convertTimeToDate(int64_t time_1904, String8 *s) { 748 time_t time_1970 = time_1904 - (((66 * 365 + 17) * 24) * 3600); 749 750 char tmp[32]; 751 strftime(tmp, sizeof(tmp), "%Y%m%dT%H%M%S.000Z", gmtime(&time_1970)); 752 753 s->setTo(tmp); 754} 755 756status_t MPEG4Extractor::parseChunk(off64_t *offset, int depth) { 757 ALOGV("entering parseChunk %lld/%d", *offset, depth); 758 uint32_t hdr[2]; 759 if (mDataSource->readAt(*offset, hdr, 8) < 8) { 760 return ERROR_IO; 761 } 762 uint64_t chunk_size = ntohl(hdr[0]); 763 uint32_t chunk_type = ntohl(hdr[1]); 764 off64_t data_offset = *offset + 8; 765 766 if (chunk_size == 1) { 767 if (mDataSource->readAt(*offset + 8, &chunk_size, 8) < 8) { 768 return ERROR_IO; 769 } 770 chunk_size = ntoh64(chunk_size); 771 data_offset += 8; 772 773 if (chunk_size < 16) { 774 // The smallest valid chunk is 16 bytes long in this case. 775 return ERROR_MALFORMED; 776 } 777 } else if (chunk_size == 0) { 778 if (depth == 0) { 779 // atom extends to end of file 780 off64_t sourceSize; 781 if (mDataSource->getSize(&sourceSize) == OK) { 782 chunk_size = (sourceSize - *offset); 783 } else { 784 // XXX could we just pick a "sufficiently large" value here? 785 ALOGE("atom size is 0, and data source has no size"); 786 return ERROR_MALFORMED; 787 } 788 } else { 789 // not allowed for non-toplevel atoms, skip it 790 *offset += 4; 791 return OK; 792 } 793 } else if (chunk_size < 8) { 794 // The smallest valid chunk is 8 bytes long. 795 ALOGE("invalid chunk size: %d", int(chunk_size)); 796 return ERROR_MALFORMED; 797 } 798 799 char chunk[5]; 800 MakeFourCCString(chunk_type, chunk); 801 ALOGV("chunk: %s @ %lld, %d", chunk, *offset, depth); 802 803#if 0 804 static const char kWhitespace[] = " "; 805 const char *indent = &kWhitespace[sizeof(kWhitespace) - 1 - 2 * depth]; 806 printf("%sfound chunk '%s' of size %lld\n", indent, chunk, chunk_size); 807 808 char buffer[256]; 809 size_t n = chunk_size; 810 if (n > sizeof(buffer)) { 811 n = sizeof(buffer); 812 } 813 if (mDataSource->readAt(*offset, buffer, n) 814 < (ssize_t)n) { 815 return ERROR_IO; 816 } 817 818 hexdump(buffer, n); 819#endif 820 821 PathAdder autoAdder(&mPath, chunk_type); 822 823 off64_t chunk_data_size = *offset + chunk_size - data_offset; 824 825 if (chunk_type != FOURCC('c', 'p', 'r', 't') 826 && chunk_type != FOURCC('c', 'o', 'v', 'r') 827 && mPath.size() == 5 && underMetaDataPath(mPath)) { 828 off64_t stop_offset = *offset + chunk_size; 829 *offset = data_offset; 830 while (*offset < stop_offset) { 831 status_t err = parseChunk(offset, depth + 1); 832 if (err != OK) { 833 return err; 834 } 835 } 836 837 if (*offset != stop_offset) { 838 return ERROR_MALFORMED; 839 } 840 841 return OK; 842 } 843 844 switch(chunk_type) { 845 case FOURCC('m', 'o', 'o', 'v'): 846 case FOURCC('t', 'r', 'a', 'k'): 847 case FOURCC('m', 'd', 'i', 'a'): 848 case FOURCC('m', 'i', 'n', 'f'): 849 case FOURCC('d', 'i', 'n', 'f'): 850 case FOURCC('s', 't', 'b', 'l'): 851 case FOURCC('m', 'v', 'e', 'x'): 852 case FOURCC('m', 'o', 'o', 'f'): 853 case FOURCC('t', 'r', 'a', 'f'): 854 case FOURCC('m', 'f', 'r', 'a'): 855 case FOURCC('u', 'd', 't', 'a'): 856 case FOURCC('i', 'l', 's', 't'): 857 case FOURCC('s', 'i', 'n', 'f'): 858 case FOURCC('s', 'c', 'h', 'i'): 859 case FOURCC('e', 'd', 't', 's'): 860 { 861 if (chunk_type == FOURCC('s', 't', 'b', 'l')) { 862 ALOGV("sampleTable chunk is %d bytes long.", (size_t)chunk_size); 863 864 if (mDataSource->flags() 865 & (DataSource::kWantsPrefetching 866 | DataSource::kIsCachingDataSource)) { 867 sp<MPEG4DataSource> cachedSource = 868 new MPEG4DataSource(mDataSource); 869 870 if (cachedSource->setCachedRange(*offset, chunk_size) == OK) { 871 mDataSource = cachedSource; 872 } 873 } 874 875 mLastTrack->sampleTable = new SampleTable(mDataSource); 876 } 877 878 bool isTrack = false; 879 if (chunk_type == FOURCC('t', 'r', 'a', 'k')) { 880 isTrack = true; 881 882 Track *track = new Track; 883 track->next = NULL; 884 if (mLastTrack) { 885 mLastTrack->next = track; 886 } else { 887 mFirstTrack = track; 888 } 889 mLastTrack = track; 890 891 track->meta = new MetaData; 892 track->includes_expensive_metadata = false; 893 track->skipTrack = false; 894 track->timescale = 0; 895 track->meta->setCString(kKeyMIMEType, "application/octet-stream"); 896 } 897 898 off64_t stop_offset = *offset + chunk_size; 899 *offset = data_offset; 900 while (*offset < stop_offset) { 901 status_t err = parseChunk(offset, depth + 1); 902 if (err != OK) { 903 return err; 904 } 905 } 906 907 if (*offset != stop_offset) { 908 return ERROR_MALFORMED; 909 } 910 911 if (isTrack) { 912 if (mLastTrack->skipTrack) { 913 Track *cur = mFirstTrack; 914 915 if (cur == mLastTrack) { 916 delete cur; 917 mFirstTrack = mLastTrack = NULL; 918 } else { 919 while (cur && cur->next != mLastTrack) { 920 cur = cur->next; 921 } 922 cur->next = NULL; 923 delete mLastTrack; 924 mLastTrack = cur; 925 } 926 927 return OK; 928 } 929 930 status_t err = verifyTrack(mLastTrack); 931 932 if (err != OK) { 933 return err; 934 } 935 } else if (chunk_type == FOURCC('m', 'o', 'o', 'v')) { 936 mInitCheck = OK; 937 938 if (!mIsDrm) { 939 return UNKNOWN_ERROR; // Return a dummy error. 940 } else { 941 return OK; 942 } 943 } 944 break; 945 } 946 947 case FOURCC('e', 'l', 's', 't'): 948 { 949 *offset += chunk_size; 950 951 // See 14496-12 8.6.6 952 uint8_t version; 953 if (mDataSource->readAt(data_offset, &version, 1) < 1) { 954 return ERROR_IO; 955 } 956 957 uint32_t entry_count; 958 if (!mDataSource->getUInt32(data_offset + 4, &entry_count)) { 959 return ERROR_IO; 960 } 961 962 if (entry_count != 1) { 963 // we only support a single entry at the moment, for gapless playback 964 ALOGW("ignoring edit list with %d entries", entry_count); 965 } else if (mHeaderTimescale == 0) { 966 ALOGW("ignoring edit list because timescale is 0"); 967 } else { 968 off64_t entriesoffset = data_offset + 8; 969 uint64_t segment_duration; 970 int64_t media_time; 971 972 if (version == 1) { 973 if (!mDataSource->getUInt64(entriesoffset, &segment_duration) || 974 !mDataSource->getUInt64(entriesoffset + 8, (uint64_t*)&media_time)) { 975 return ERROR_IO; 976 } 977 } else if (version == 0) { 978 uint32_t sd; 979 int32_t mt; 980 if (!mDataSource->getUInt32(entriesoffset, &sd) || 981 !mDataSource->getUInt32(entriesoffset + 4, (uint32_t*)&mt)) { 982 return ERROR_IO; 983 } 984 segment_duration = sd; 985 media_time = mt; 986 } else { 987 return ERROR_IO; 988 } 989 990 uint64_t halfscale = mHeaderTimescale / 2; 991 segment_duration = (segment_duration * 1000000 + halfscale)/ mHeaderTimescale; 992 media_time = (media_time * 1000000 + halfscale) / mHeaderTimescale; 993 994 int64_t duration; 995 int32_t samplerate; 996 if (mLastTrack->meta->findInt64(kKeyDuration, &duration) && 997 mLastTrack->meta->findInt32(kKeySampleRate, &samplerate)) { 998 999 int64_t delay = (media_time * samplerate + 500000) / 1000000; 1000 mLastTrack->meta->setInt32(kKeyEncoderDelay, delay); 1001 1002 int64_t paddingus = duration - (segment_duration + media_time); 1003 if (paddingus < 0) { 1004 // track duration from media header (which is what kKeyDuration is) might 1005 // be slightly shorter than the segment duration, which would make the 1006 // padding negative. Clamp to zero. 1007 paddingus = 0; 1008 } 1009 int64_t paddingsamples = (paddingus * samplerate + 500000) / 1000000; 1010 mLastTrack->meta->setInt32(kKeyEncoderPadding, paddingsamples); 1011 } 1012 } 1013 break; 1014 } 1015 1016 case FOURCC('f', 'r', 'm', 'a'): 1017 { 1018 *offset += chunk_size; 1019 1020 uint32_t original_fourcc; 1021 if (mDataSource->readAt(data_offset, &original_fourcc, 4) < 4) { 1022 return ERROR_IO; 1023 } 1024 original_fourcc = ntohl(original_fourcc); 1025 ALOGV("read original format: %d", original_fourcc); 1026 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(original_fourcc)); 1027 uint32_t num_channels = 0; 1028 uint32_t sample_rate = 0; 1029 if (AdjustChannelsAndRate(original_fourcc, &num_channels, &sample_rate)) { 1030 mLastTrack->meta->setInt32(kKeyChannelCount, num_channels); 1031 mLastTrack->meta->setInt32(kKeySampleRate, sample_rate); 1032 } 1033 break; 1034 } 1035 1036 case FOURCC('t', 'e', 'n', 'c'): 1037 { 1038 *offset += chunk_size; 1039 1040 if (chunk_size < 32) { 1041 return ERROR_MALFORMED; 1042 } 1043 1044 // tenc box contains 1 byte version, 3 byte flags, 3 byte default algorithm id, one byte 1045 // default IV size, 16 bytes default KeyID 1046 // (ISO 23001-7) 1047 char buf[4]; 1048 memset(buf, 0, 4); 1049 if (mDataSource->readAt(data_offset + 4, buf + 1, 3) < 3) { 1050 return ERROR_IO; 1051 } 1052 uint32_t defaultAlgorithmId = ntohl(*((int32_t*)buf)); 1053 if (defaultAlgorithmId > 1) { 1054 // only 0 (clear) and 1 (AES-128) are valid 1055 return ERROR_MALFORMED; 1056 } 1057 1058 memset(buf, 0, 4); 1059 if (mDataSource->readAt(data_offset + 7, buf + 3, 1) < 1) { 1060 return ERROR_IO; 1061 } 1062 uint32_t defaultIVSize = ntohl(*((int32_t*)buf)); 1063 1064 if ((defaultAlgorithmId == 0 && defaultIVSize != 0) || 1065 (defaultAlgorithmId != 0 && defaultIVSize == 0)) { 1066 // only unencrypted data must have 0 IV size 1067 return ERROR_MALFORMED; 1068 } else if (defaultIVSize != 0 && 1069 defaultIVSize != 8 && 1070 defaultIVSize != 16) { 1071 // only supported sizes are 0, 8 and 16 1072 return ERROR_MALFORMED; 1073 } 1074 1075 uint8_t defaultKeyId[16]; 1076 1077 if (mDataSource->readAt(data_offset + 8, &defaultKeyId, 16) < 16) { 1078 return ERROR_IO; 1079 } 1080 1081 mLastTrack->meta->setInt32(kKeyCryptoMode, defaultAlgorithmId); 1082 mLastTrack->meta->setInt32(kKeyCryptoDefaultIVSize, defaultIVSize); 1083 mLastTrack->meta->setData(kKeyCryptoKey, 'tenc', defaultKeyId, 16); 1084 break; 1085 } 1086 1087 case FOURCC('t', 'k', 'h', 'd'): 1088 { 1089 *offset += chunk_size; 1090 1091 status_t err; 1092 if ((err = parseTrackHeader(data_offset, chunk_data_size)) != OK) { 1093 return err; 1094 } 1095 1096 break; 1097 } 1098 1099 case FOURCC('p', 's', 's', 'h'): 1100 { 1101 *offset += chunk_size; 1102 1103 PsshInfo pssh; 1104 1105 if (mDataSource->readAt(data_offset + 4, &pssh.uuid, 16) < 16) { 1106 return ERROR_IO; 1107 } 1108 1109 uint32_t psshdatalen = 0; 1110 if (mDataSource->readAt(data_offset + 20, &psshdatalen, 4) < 4) { 1111 return ERROR_IO; 1112 } 1113 pssh.datalen = ntohl(psshdatalen); 1114 ALOGV("pssh data size: %d", pssh.datalen); 1115 if (pssh.datalen + 20 > chunk_size) { 1116 // pssh data length exceeds size of containing box 1117 return ERROR_MALFORMED; 1118 } 1119 1120 pssh.data = new (std::nothrow) uint8_t[pssh.datalen]; 1121 if (pssh.data == NULL) { 1122 return ERROR_MALFORMED; 1123 } 1124 ALOGV("allocated pssh @ %p", pssh.data); 1125 ssize_t requested = (ssize_t) pssh.datalen; 1126 if (mDataSource->readAt(data_offset + 24, pssh.data, requested) < requested) { 1127 return ERROR_IO; 1128 } 1129 mPssh.push_back(pssh); 1130 1131 break; 1132 } 1133 1134 case FOURCC('m', 'd', 'h', 'd'): 1135 { 1136 *offset += chunk_size; 1137 1138 if (chunk_data_size < 4) { 1139 return ERROR_MALFORMED; 1140 } 1141 1142 uint8_t version; 1143 if (mDataSource->readAt( 1144 data_offset, &version, sizeof(version)) 1145 < (ssize_t)sizeof(version)) { 1146 return ERROR_IO; 1147 } 1148 1149 off64_t timescale_offset; 1150 1151 if (version == 1) { 1152 timescale_offset = data_offset + 4 + 16; 1153 } else if (version == 0) { 1154 timescale_offset = data_offset + 4 + 8; 1155 } else { 1156 return ERROR_IO; 1157 } 1158 1159 uint32_t timescale; 1160 if (mDataSource->readAt( 1161 timescale_offset, ×cale, sizeof(timescale)) 1162 < (ssize_t)sizeof(timescale)) { 1163 return ERROR_IO; 1164 } 1165 1166 mLastTrack->timescale = ntohl(timescale); 1167 1168 // 14496-12 says all ones means indeterminate, but some files seem to use 1169 // 0 instead. We treat both the same. 1170 int64_t duration = 0; 1171 if (version == 1) { 1172 if (mDataSource->readAt( 1173 timescale_offset + 4, &duration, sizeof(duration)) 1174 < (ssize_t)sizeof(duration)) { 1175 return ERROR_IO; 1176 } 1177 if (duration != -1) { 1178 duration = ntoh64(duration); 1179 } 1180 } else { 1181 uint32_t duration32; 1182 if (mDataSource->readAt( 1183 timescale_offset + 4, &duration32, sizeof(duration32)) 1184 < (ssize_t)sizeof(duration32)) { 1185 return ERROR_IO; 1186 } 1187 if (duration32 != 0xffffffff) { 1188 duration = ntohl(duration32); 1189 } 1190 } 1191 if (duration != 0) { 1192 mLastTrack->meta->setInt64( 1193 kKeyDuration, (duration * 1000000) / mLastTrack->timescale); 1194 } 1195 1196 uint8_t lang[2]; 1197 off64_t lang_offset; 1198 if (version == 1) { 1199 lang_offset = timescale_offset + 4 + 8; 1200 } else if (version == 0) { 1201 lang_offset = timescale_offset + 4 + 4; 1202 } else { 1203 return ERROR_IO; 1204 } 1205 1206 if (mDataSource->readAt(lang_offset, &lang, sizeof(lang)) 1207 < (ssize_t)sizeof(lang)) { 1208 return ERROR_IO; 1209 } 1210 1211 // To get the ISO-639-2/T three character language code 1212 // 1 bit pad followed by 3 5-bits characters. Each character 1213 // is packed as the difference between its ASCII value and 0x60. 1214 char lang_code[4]; 1215 lang_code[0] = ((lang[0] >> 2) & 0x1f) + 0x60; 1216 lang_code[1] = ((lang[0] & 0x3) << 3 | (lang[1] >> 5)) + 0x60; 1217 lang_code[2] = (lang[1] & 0x1f) + 0x60; 1218 lang_code[3] = '\0'; 1219 1220 mLastTrack->meta->setCString( 1221 kKeyMediaLanguage, lang_code); 1222 1223 break; 1224 } 1225 1226 case FOURCC('s', 't', 's', 'd'): 1227 { 1228 if (chunk_data_size < 8) { 1229 return ERROR_MALFORMED; 1230 } 1231 1232 uint8_t buffer[8]; 1233 if (chunk_data_size < (off64_t)sizeof(buffer)) { 1234 return ERROR_MALFORMED; 1235 } 1236 1237 if (mDataSource->readAt( 1238 data_offset, buffer, 8) < 8) { 1239 return ERROR_IO; 1240 } 1241 1242 if (U32_AT(buffer) != 0) { 1243 // Should be version 0, flags 0. 1244 return ERROR_MALFORMED; 1245 } 1246 1247 uint32_t entry_count = U32_AT(&buffer[4]); 1248 1249 if (entry_count > 1) { 1250 // For 3GPP timed text, there could be multiple tx3g boxes contain 1251 // multiple text display formats. These formats will be used to 1252 // display the timed text. 1253 // For encrypted files, there may also be more than one entry. 1254 const char *mime; 1255 CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); 1256 if (strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP) && 1257 strcasecmp(mime, "application/octet-stream")) { 1258 // For now we only support a single type of media per track. 1259 mLastTrack->skipTrack = true; 1260 *offset += chunk_size; 1261 break; 1262 } 1263 } 1264 off64_t stop_offset = *offset + chunk_size; 1265 *offset = data_offset + 8; 1266 for (uint32_t i = 0; i < entry_count; ++i) { 1267 status_t err = parseChunk(offset, depth + 1); 1268 if (err != OK) { 1269 return err; 1270 } 1271 } 1272 1273 if (*offset != stop_offset) { 1274 return ERROR_MALFORMED; 1275 } 1276 break; 1277 } 1278 1279 case FOURCC('m', 'p', '4', 'a'): 1280 case FOURCC('e', 'n', 'c', 'a'): 1281 case FOURCC('s', 'a', 'm', 'r'): 1282 case FOURCC('s', 'a', 'w', 'b'): 1283 { 1284 uint8_t buffer[8 + 20]; 1285 if (chunk_data_size < (ssize_t)sizeof(buffer)) { 1286 // Basic AudioSampleEntry size. 1287 return ERROR_MALFORMED; 1288 } 1289 1290 if (mDataSource->readAt( 1291 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 1292 return ERROR_IO; 1293 } 1294 1295 uint16_t data_ref_index = U16_AT(&buffer[6]); 1296 uint32_t num_channels = U16_AT(&buffer[16]); 1297 1298 uint16_t sample_size = U16_AT(&buffer[18]); 1299 uint32_t sample_rate = U32_AT(&buffer[24]) >> 16; 1300 1301 if (chunk_type != FOURCC('e', 'n', 'c', 'a')) { 1302 // if the chunk type is enca, we'll get the type from the sinf/frma box later 1303 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); 1304 AdjustChannelsAndRate(chunk_type, &num_channels, &sample_rate); 1305 } 1306 ALOGV("*** coding='%s' %d channels, size %d, rate %d\n", 1307 chunk, num_channels, sample_size, sample_rate); 1308 mLastTrack->meta->setInt32(kKeyChannelCount, num_channels); 1309 mLastTrack->meta->setInt32(kKeySampleRate, sample_rate); 1310 1311 off64_t stop_offset = *offset + chunk_size; 1312 *offset = data_offset + sizeof(buffer); 1313 while (*offset < stop_offset) { 1314 status_t err = parseChunk(offset, depth + 1); 1315 if (err != OK) { 1316 return err; 1317 } 1318 } 1319 1320 if (*offset != stop_offset) { 1321 return ERROR_MALFORMED; 1322 } 1323 break; 1324 } 1325 1326 case FOURCC('m', 'p', '4', 'v'): 1327 case FOURCC('e', 'n', 'c', 'v'): 1328 case FOURCC('s', '2', '6', '3'): 1329 case FOURCC('H', '2', '6', '3'): 1330 case FOURCC('h', '2', '6', '3'): 1331 case FOURCC('a', 'v', 'c', '1'): 1332 case FOURCC('h', 'v', 'c', '1'): 1333 case FOURCC('h', 'e', 'v', '1'): 1334 { 1335 mHasVideo = true; 1336 1337 uint8_t buffer[78]; 1338 if (chunk_data_size < (ssize_t)sizeof(buffer)) { 1339 // Basic VideoSampleEntry size. 1340 return ERROR_MALFORMED; 1341 } 1342 1343 if (mDataSource->readAt( 1344 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 1345 return ERROR_IO; 1346 } 1347 1348 uint16_t data_ref_index = U16_AT(&buffer[6]); 1349 uint16_t width = U16_AT(&buffer[6 + 18]); 1350 uint16_t height = U16_AT(&buffer[6 + 20]); 1351 1352 // The video sample is not standard-compliant if it has invalid dimension. 1353 // Use some default width and height value, and 1354 // let the decoder figure out the actual width and height (and thus 1355 // be prepared for INFO_FOMRAT_CHANGED event). 1356 if (width == 0) width = 352; 1357 if (height == 0) height = 288; 1358 1359 // printf("*** coding='%s' width=%d height=%d\n", 1360 // chunk, width, height); 1361 1362 if (chunk_type != FOURCC('e', 'n', 'c', 'v')) { 1363 // if the chunk type is encv, we'll get the type from the sinf/frma box later 1364 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); 1365 } 1366 mLastTrack->meta->setInt32(kKeyWidth, width); 1367 mLastTrack->meta->setInt32(kKeyHeight, height); 1368 1369 off64_t stop_offset = *offset + chunk_size; 1370 *offset = data_offset + sizeof(buffer); 1371 while (*offset < stop_offset) { 1372 status_t err = parseChunk(offset, depth + 1); 1373 if (err != OK) { 1374 return err; 1375 } 1376 } 1377 1378 if (*offset != stop_offset) { 1379 return ERROR_MALFORMED; 1380 } 1381 break; 1382 } 1383 1384 case FOURCC('s', 't', 'c', 'o'): 1385 case FOURCC('c', 'o', '6', '4'): 1386 { 1387 status_t err = 1388 mLastTrack->sampleTable->setChunkOffsetParams( 1389 chunk_type, data_offset, chunk_data_size); 1390 1391 *offset += chunk_size; 1392 1393 if (err != OK) { 1394 return err; 1395 } 1396 1397 break; 1398 } 1399 1400 case FOURCC('s', 't', 's', 'c'): 1401 { 1402 status_t err = 1403 mLastTrack->sampleTable->setSampleToChunkParams( 1404 data_offset, chunk_data_size); 1405 1406 *offset += chunk_size; 1407 1408 if (err != OK) { 1409 return err; 1410 } 1411 1412 break; 1413 } 1414 1415 case FOURCC('s', 't', 's', 'z'): 1416 case FOURCC('s', 't', 'z', '2'): 1417 { 1418 status_t err = 1419 mLastTrack->sampleTable->setSampleSizeParams( 1420 chunk_type, data_offset, chunk_data_size); 1421 1422 *offset += chunk_size; 1423 1424 if (err != OK) { 1425 return err; 1426 } 1427 1428 size_t max_size; 1429 err = mLastTrack->sampleTable->getMaxSampleSize(&max_size); 1430 1431 if (err != OK) { 1432 return err; 1433 } 1434 1435 if (max_size != 0) { 1436 // Assume that a given buffer only contains at most 10 chunks, 1437 // each chunk originally prefixed with a 2 byte length will 1438 // have a 4 byte header (0x00 0x00 0x00 0x01) after conversion, 1439 // and thus will grow by 2 bytes per chunk. 1440 mLastTrack->meta->setInt32(kKeyMaxInputSize, max_size + 10 * 2); 1441 } else { 1442 // No size was specified. Pick a conservatively large size. 1443 int32_t width, height; 1444 if (!mLastTrack->meta->findInt32(kKeyWidth, &width) || 1445 !mLastTrack->meta->findInt32(kKeyHeight, &height)) { 1446 ALOGE("No width or height, assuming worst case 1080p"); 1447 width = 1920; 1448 height = 1080; 1449 } 1450 1451 const char *mime; 1452 CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); 1453 if (!strcmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { 1454 // AVC requires compression ratio of at least 2, and uses 1455 // macroblocks 1456 max_size = ((width + 15) / 16) * ((height + 15) / 16) * 192; 1457 } else { 1458 // For all other formats there is no minimum compression 1459 // ratio. Use compression ratio of 1. 1460 max_size = width * height * 3 / 2; 1461 } 1462 mLastTrack->meta->setInt32(kKeyMaxInputSize, max_size); 1463 } 1464 1465 // NOTE: setting another piece of metadata invalidates any pointers (such as the 1466 // mimetype) previously obtained, so don't cache them. 1467 const char *mime; 1468 CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); 1469 // Calculate average frame rate. 1470 if (!strncasecmp("video/", mime, 6)) { 1471 size_t nSamples = mLastTrack->sampleTable->countSamples(); 1472 int64_t durationUs; 1473 if (mLastTrack->meta->findInt64(kKeyDuration, &durationUs)) { 1474 if (durationUs > 0) { 1475 int32_t frameRate = (nSamples * 1000000LL + 1476 (durationUs >> 1)) / durationUs; 1477 mLastTrack->meta->setInt32(kKeyFrameRate, frameRate); 1478 } 1479 } 1480 } 1481 1482 break; 1483 } 1484 1485 case FOURCC('s', 't', 't', 's'): 1486 { 1487 *offset += chunk_size; 1488 1489 status_t err = 1490 mLastTrack->sampleTable->setTimeToSampleParams( 1491 data_offset, chunk_data_size); 1492 1493 if (err != OK) { 1494 return err; 1495 } 1496 1497 break; 1498 } 1499 1500 case FOURCC('c', 't', 't', 's'): 1501 { 1502 *offset += chunk_size; 1503 1504 status_t err = 1505 mLastTrack->sampleTable->setCompositionTimeToSampleParams( 1506 data_offset, chunk_data_size); 1507 1508 if (err != OK) { 1509 return err; 1510 } 1511 1512 break; 1513 } 1514 1515 case FOURCC('s', 't', 's', 's'): 1516 { 1517 *offset += chunk_size; 1518 1519 status_t err = 1520 mLastTrack->sampleTable->setSyncSampleParams( 1521 data_offset, chunk_data_size); 1522 1523 if (err != OK) { 1524 return err; 1525 } 1526 1527 break; 1528 } 1529 1530 // @xyz 1531 case FOURCC('\xA9', 'x', 'y', 'z'): 1532 { 1533 *offset += chunk_size; 1534 1535 // Best case the total data length inside "@xyz" box 1536 // would be 8, for instance "@xyz" + "\x00\x04\x15\xc7" + "0+0/", 1537 // where "\x00\x04" is the text string length with value = 4, 1538 // "\0x15\xc7" is the language code = en, and "0+0" is a 1539 // location (string) value with longitude = 0 and latitude = 0. 1540 if (chunk_data_size < 8) { 1541 return ERROR_MALFORMED; 1542 } 1543 1544 // Worst case the location string length would be 18, 1545 // for instance +90.0000-180.0000, without the trailing "/" and 1546 // the string length + language code. 1547 char buffer[18]; 1548 1549 // Substracting 5 from the data size is because the text string length + 1550 // language code takes 4 bytes, and the trailing slash "/" takes 1 byte. 1551 off64_t location_length = chunk_data_size - 5; 1552 if (location_length >= (off64_t) sizeof(buffer)) { 1553 return ERROR_MALFORMED; 1554 } 1555 1556 if (mDataSource->readAt( 1557 data_offset + 4, buffer, location_length) < location_length) { 1558 return ERROR_IO; 1559 } 1560 1561 buffer[location_length] = '\0'; 1562 mFileMetaData->setCString(kKeyLocation, buffer); 1563 break; 1564 } 1565 1566 case FOURCC('e', 's', 'd', 's'): 1567 { 1568 *offset += chunk_size; 1569 1570 if (chunk_data_size < 4) { 1571 return ERROR_MALFORMED; 1572 } 1573 1574 uint8_t buffer[256]; 1575 if (chunk_data_size > (off64_t)sizeof(buffer)) { 1576 return ERROR_BUFFER_TOO_SMALL; 1577 } 1578 1579 if (mDataSource->readAt( 1580 data_offset, buffer, chunk_data_size) < chunk_data_size) { 1581 return ERROR_IO; 1582 } 1583 1584 if (U32_AT(buffer) != 0) { 1585 // Should be version 0, flags 0. 1586 return ERROR_MALFORMED; 1587 } 1588 1589 mLastTrack->meta->setData( 1590 kKeyESDS, kTypeESDS, &buffer[4], chunk_data_size - 4); 1591 1592 if (mPath.size() >= 2 1593 && mPath[mPath.size() - 2] == FOURCC('m', 'p', '4', 'a')) { 1594 // Information from the ESDS must be relied on for proper 1595 // setup of sample rate and channel count for MPEG4 Audio. 1596 // The generic header appears to only contain generic 1597 // information... 1598 1599 status_t err = updateAudioTrackInfoFromESDS_MPEG4Audio( 1600 &buffer[4], chunk_data_size - 4); 1601 1602 if (err != OK) { 1603 return err; 1604 } 1605 } 1606 1607 break; 1608 } 1609 1610 case FOURCC('a', 'v', 'c', 'C'): 1611 { 1612 *offset += chunk_size; 1613 1614 sp<ABuffer> buffer = new ABuffer(chunk_data_size); 1615 1616 if (mDataSource->readAt( 1617 data_offset, buffer->data(), chunk_data_size) < chunk_data_size) { 1618 return ERROR_IO; 1619 } 1620 1621 mLastTrack->meta->setData( 1622 kKeyAVCC, kTypeAVCC, buffer->data(), chunk_data_size); 1623 1624 break; 1625 } 1626 case FOURCC('h', 'v', 'c', 'C'): 1627 { 1628 sp<ABuffer> buffer = new ABuffer(chunk_data_size); 1629 1630 if (mDataSource->readAt( 1631 data_offset, buffer->data(), chunk_data_size) < chunk_data_size) { 1632 return ERROR_IO; 1633 } 1634 1635 mLastTrack->meta->setData( 1636 kKeyHVCC, kTypeHVCC, buffer->data(), chunk_data_size); 1637 1638 *offset += chunk_size; 1639 break; 1640 } 1641 1642 case FOURCC('d', '2', '6', '3'): 1643 { 1644 *offset += chunk_size; 1645 /* 1646 * d263 contains a fixed 7 bytes part: 1647 * vendor - 4 bytes 1648 * version - 1 byte 1649 * level - 1 byte 1650 * profile - 1 byte 1651 * optionally, "d263" box itself may contain a 16-byte 1652 * bit rate box (bitr) 1653 * average bit rate - 4 bytes 1654 * max bit rate - 4 bytes 1655 */ 1656 char buffer[23]; 1657 if (chunk_data_size != 7 && 1658 chunk_data_size != 23) { 1659 ALOGE("Incorrect D263 box size %lld", chunk_data_size); 1660 return ERROR_MALFORMED; 1661 } 1662 1663 if (mDataSource->readAt( 1664 data_offset, buffer, chunk_data_size) < chunk_data_size) { 1665 return ERROR_IO; 1666 } 1667 1668 mLastTrack->meta->setData(kKeyD263, kTypeD263, buffer, chunk_data_size); 1669 1670 break; 1671 } 1672 1673 case FOURCC('m', 'e', 't', 'a'): 1674 { 1675 uint8_t buffer[4]; 1676 if (chunk_data_size < (off64_t)sizeof(buffer)) { 1677 *offset += chunk_size; 1678 return ERROR_MALFORMED; 1679 } 1680 1681 if (mDataSource->readAt( 1682 data_offset, buffer, 4) < 4) { 1683 *offset += chunk_size; 1684 return ERROR_IO; 1685 } 1686 1687 if (U32_AT(buffer) != 0) { 1688 // Should be version 0, flags 0. 1689 1690 // If it's not, let's assume this is one of those 1691 // apparently malformed chunks that don't have flags 1692 // and completely different semantics than what's 1693 // in the MPEG4 specs and skip it. 1694 *offset += chunk_size; 1695 return OK; 1696 } 1697 1698 off64_t stop_offset = *offset + chunk_size; 1699 *offset = data_offset + sizeof(buffer); 1700 while (*offset < stop_offset) { 1701 status_t err = parseChunk(offset, depth + 1); 1702 if (err != OK) { 1703 return err; 1704 } 1705 } 1706 1707 if (*offset != stop_offset) { 1708 return ERROR_MALFORMED; 1709 } 1710 break; 1711 } 1712 1713 case FOURCC('m', 'e', 'a', 'n'): 1714 case FOURCC('n', 'a', 'm', 'e'): 1715 case FOURCC('d', 'a', 't', 'a'): 1716 { 1717 *offset += chunk_size; 1718 1719 if (mPath.size() == 6 && underMetaDataPath(mPath)) { 1720 status_t err = parseITunesMetaData(data_offset, chunk_data_size); 1721 1722 if (err != OK) { 1723 return err; 1724 } 1725 } 1726 1727 break; 1728 } 1729 1730 case FOURCC('m', 'v', 'h', 'd'): 1731 { 1732 *offset += chunk_size; 1733 1734 if (chunk_data_size < 32) { 1735 return ERROR_MALFORMED; 1736 } 1737 1738 uint8_t header[32]; 1739 if (mDataSource->readAt( 1740 data_offset, header, sizeof(header)) 1741 < (ssize_t)sizeof(header)) { 1742 return ERROR_IO; 1743 } 1744 1745 uint64_t creationTime; 1746 uint64_t duration = 0; 1747 if (header[0] == 1) { 1748 creationTime = U64_AT(&header[4]); 1749 mHeaderTimescale = U32_AT(&header[20]); 1750 duration = U64_AT(&header[24]); 1751 if (duration == 0xffffffffffffffff) { 1752 duration = 0; 1753 } 1754 } else if (header[0] != 0) { 1755 return ERROR_MALFORMED; 1756 } else { 1757 creationTime = U32_AT(&header[4]); 1758 mHeaderTimescale = U32_AT(&header[12]); 1759 uint32_t d32 = U32_AT(&header[16]); 1760 if (d32 == 0xffffffff) { 1761 d32 = 0; 1762 } 1763 duration = d32; 1764 } 1765 if (duration != 0) { 1766 mFileMetaData->setInt64(kKeyDuration, duration * 1000000 / mHeaderTimescale); 1767 } 1768 1769 String8 s; 1770 convertTimeToDate(creationTime, &s); 1771 1772 mFileMetaData->setCString(kKeyDate, s.string()); 1773 1774 break; 1775 } 1776 1777 case FOURCC('m', 'e', 'h', 'd'): 1778 { 1779 *offset += chunk_size; 1780 1781 if (chunk_data_size < 8) { 1782 return ERROR_MALFORMED; 1783 } 1784 1785 uint8_t flags[4]; 1786 if (mDataSource->readAt( 1787 data_offset, flags, sizeof(flags)) 1788 < (ssize_t)sizeof(flags)) { 1789 return ERROR_IO; 1790 } 1791 1792 uint64_t duration = 0; 1793 if (flags[0] == 1) { 1794 // 64 bit 1795 if (chunk_data_size < 12) { 1796 return ERROR_MALFORMED; 1797 } 1798 mDataSource->getUInt64(data_offset + 4, &duration); 1799 if (duration == 0xffffffffffffffff) { 1800 duration = 0; 1801 } 1802 } else if (flags[0] == 0) { 1803 // 32 bit 1804 uint32_t d32; 1805 mDataSource->getUInt32(data_offset + 4, &d32); 1806 if (d32 == 0xffffffff) { 1807 d32 = 0; 1808 } 1809 duration = d32; 1810 } else { 1811 return ERROR_MALFORMED; 1812 } 1813 1814 if (duration != 0) { 1815 mFileMetaData->setInt64(kKeyDuration, duration * 1000000 / mHeaderTimescale); 1816 } 1817 1818 break; 1819 } 1820 1821 case FOURCC('m', 'd', 'a', 't'): 1822 { 1823 ALOGV("mdat chunk, drm: %d", mIsDrm); 1824 if (!mIsDrm) { 1825 *offset += chunk_size; 1826 break; 1827 } 1828 1829 if (chunk_size < 8) { 1830 return ERROR_MALFORMED; 1831 } 1832 1833 return parseDrmSINF(offset, data_offset); 1834 } 1835 1836 case FOURCC('h', 'd', 'l', 'r'): 1837 { 1838 *offset += chunk_size; 1839 1840 uint32_t buffer; 1841 if (mDataSource->readAt( 1842 data_offset + 8, &buffer, 4) < 4) { 1843 return ERROR_IO; 1844 } 1845 1846 uint32_t type = ntohl(buffer); 1847 // For the 3GPP file format, the handler-type within the 'hdlr' box 1848 // shall be 'text'. We also want to support 'sbtl' handler type 1849 // for a practical reason as various MPEG4 containers use it. 1850 if (type == FOURCC('t', 'e', 'x', 't') || type == FOURCC('s', 'b', 't', 'l')) { 1851 mLastTrack->meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_TEXT_3GPP); 1852 } 1853 1854 break; 1855 } 1856 1857 case FOURCC('t', 'r', 'e', 'x'): 1858 { 1859 *offset += chunk_size; 1860 1861 if (chunk_data_size < 24) { 1862 return ERROR_IO; 1863 } 1864 uint32_t duration; 1865 Trex trex; 1866 if (!mDataSource->getUInt32(data_offset + 4, &trex.track_ID) || 1867 !mDataSource->getUInt32(data_offset + 8, &trex.default_sample_description_index) || 1868 !mDataSource->getUInt32(data_offset + 12, &trex.default_sample_duration) || 1869 !mDataSource->getUInt32(data_offset + 16, &trex.default_sample_size) || 1870 !mDataSource->getUInt32(data_offset + 20, &trex.default_sample_flags)) { 1871 return ERROR_IO; 1872 } 1873 mTrex.add(trex); 1874 break; 1875 } 1876 1877 case FOURCC('t', 'x', '3', 'g'): 1878 { 1879 uint32_t type; 1880 const void *data; 1881 size_t size = 0; 1882 if (!mLastTrack->meta->findData( 1883 kKeyTextFormatData, &type, &data, &size)) { 1884 size = 0; 1885 } 1886 1887 uint8_t *buffer = new (std::nothrow) uint8_t[size + chunk_size]; 1888 if (buffer == NULL) { 1889 return ERROR_MALFORMED; 1890 } 1891 1892 if (size > 0) { 1893 memcpy(buffer, data, size); 1894 } 1895 1896 if ((size_t)(mDataSource->readAt(*offset, buffer + size, chunk_size)) 1897 < chunk_size) { 1898 delete[] buffer; 1899 buffer = NULL; 1900 1901 // advance read pointer so we don't end up reading this again 1902 *offset += chunk_size; 1903 return ERROR_IO; 1904 } 1905 1906 mLastTrack->meta->setData( 1907 kKeyTextFormatData, 0, buffer, size + chunk_size); 1908 1909 delete[] buffer; 1910 1911 *offset += chunk_size; 1912 break; 1913 } 1914 1915 case FOURCC('c', 'o', 'v', 'r'): 1916 { 1917 *offset += chunk_size; 1918 1919 if (mFileMetaData != NULL) { 1920 ALOGV("chunk_data_size = %lld and data_offset = %lld", 1921 chunk_data_size, data_offset); 1922 sp<ABuffer> buffer = new ABuffer(chunk_data_size + 1); 1923 if (mDataSource->readAt( 1924 data_offset, buffer->data(), chunk_data_size) != (ssize_t)chunk_data_size) { 1925 return ERROR_IO; 1926 } 1927 const int kSkipBytesOfDataBox = 16; 1928 mFileMetaData->setData( 1929 kKeyAlbumArt, MetaData::TYPE_NONE, 1930 buffer->data() + kSkipBytesOfDataBox, chunk_data_size - kSkipBytesOfDataBox); 1931 } 1932 1933 break; 1934 } 1935 1936 case FOURCC('t', 'i', 't', 'l'): 1937 case FOURCC('p', 'e', 'r', 'f'): 1938 case FOURCC('a', 'u', 't', 'h'): 1939 case FOURCC('g', 'n', 'r', 'e'): 1940 case FOURCC('a', 'l', 'b', 'm'): 1941 case FOURCC('y', 'r', 'r', 'c'): 1942 { 1943 *offset += chunk_size; 1944 1945 status_t err = parse3GPPMetaData(data_offset, chunk_data_size, depth); 1946 1947 if (err != OK) { 1948 return err; 1949 } 1950 1951 break; 1952 } 1953 1954 case FOURCC('I', 'D', '3', '2'): 1955 { 1956 *offset += chunk_size; 1957 1958 if (chunk_data_size < 6) { 1959 return ERROR_MALFORMED; 1960 } 1961 1962 parseID3v2MetaData(data_offset + 6); 1963 1964 break; 1965 } 1966 1967 case FOURCC('-', '-', '-', '-'): 1968 { 1969 mLastCommentMean.clear(); 1970 mLastCommentName.clear(); 1971 mLastCommentData.clear(); 1972 *offset += chunk_size; 1973 break; 1974 } 1975 1976 case FOURCC('s', 'i', 'd', 'x'): 1977 { 1978 parseSegmentIndex(data_offset, chunk_data_size); 1979 *offset += chunk_size; 1980 return UNKNOWN_ERROR; // stop parsing after sidx 1981 } 1982 1983 default: 1984 { 1985 *offset += chunk_size; 1986 break; 1987 } 1988 } 1989 1990 return OK; 1991} 1992 1993status_t MPEG4Extractor::parseSegmentIndex(off64_t offset, size_t size) { 1994 ALOGV("MPEG4Extractor::parseSegmentIndex"); 1995 1996 if (size < 12) { 1997 return -EINVAL; 1998 } 1999 2000 uint32_t flags; 2001 if (!mDataSource->getUInt32(offset, &flags)) { 2002 return ERROR_MALFORMED; 2003 } 2004 2005 uint32_t version = flags >> 24; 2006 flags &= 0xffffff; 2007 2008 ALOGV("sidx version %d", version); 2009 2010 uint32_t referenceId; 2011 if (!mDataSource->getUInt32(offset + 4, &referenceId)) { 2012 return ERROR_MALFORMED; 2013 } 2014 2015 uint32_t timeScale; 2016 if (!mDataSource->getUInt32(offset + 8, &timeScale)) { 2017 return ERROR_MALFORMED; 2018 } 2019 ALOGV("sidx refid/timescale: %d/%d", referenceId, timeScale); 2020 2021 uint64_t earliestPresentationTime; 2022 uint64_t firstOffset; 2023 2024 offset += 12; 2025 size -= 12; 2026 2027 if (version == 0) { 2028 if (size < 8) { 2029 return -EINVAL; 2030 } 2031 uint32_t tmp; 2032 if (!mDataSource->getUInt32(offset, &tmp)) { 2033 return ERROR_MALFORMED; 2034 } 2035 earliestPresentationTime = tmp; 2036 if (!mDataSource->getUInt32(offset + 4, &tmp)) { 2037 return ERROR_MALFORMED; 2038 } 2039 firstOffset = tmp; 2040 offset += 8; 2041 size -= 8; 2042 } else { 2043 if (size < 16) { 2044 return -EINVAL; 2045 } 2046 if (!mDataSource->getUInt64(offset, &earliestPresentationTime)) { 2047 return ERROR_MALFORMED; 2048 } 2049 if (!mDataSource->getUInt64(offset + 8, &firstOffset)) { 2050 return ERROR_MALFORMED; 2051 } 2052 offset += 16; 2053 size -= 16; 2054 } 2055 ALOGV("sidx pres/off: %Ld/%Ld", earliestPresentationTime, firstOffset); 2056 2057 if (size < 4) { 2058 return -EINVAL; 2059 } 2060 2061 uint16_t referenceCount; 2062 if (!mDataSource->getUInt16(offset + 2, &referenceCount)) { 2063 return ERROR_MALFORMED; 2064 } 2065 offset += 4; 2066 size -= 4; 2067 ALOGV("refcount: %d", referenceCount); 2068 2069 if (size < referenceCount * 12) { 2070 return -EINVAL; 2071 } 2072 2073 uint64_t total_duration = 0; 2074 for (unsigned int i = 0; i < referenceCount; i++) { 2075 uint32_t d1, d2, d3; 2076 2077 if (!mDataSource->getUInt32(offset, &d1) || // size 2078 !mDataSource->getUInt32(offset + 4, &d2) || // duration 2079 !mDataSource->getUInt32(offset + 8, &d3)) { // flags 2080 return ERROR_MALFORMED; 2081 } 2082 2083 if (d1 & 0x80000000) { 2084 ALOGW("sub-sidx boxes not supported yet"); 2085 } 2086 bool sap = d3 & 0x80000000; 2087 uint32_t saptype = (d3 >> 28) & 7; 2088 if (!sap || (saptype != 1 && saptype != 2)) { 2089 // type 1 and 2 are sync samples 2090 ALOGW("not a stream access point, or unsupported type: %08x", d3); 2091 } 2092 total_duration += d2; 2093 offset += 12; 2094 ALOGV(" item %d, %08x %08x %08x", i, d1, d2, d3); 2095 SidxEntry se; 2096 se.mSize = d1 & 0x7fffffff; 2097 se.mDurationUs = 1000000LL * d2 / timeScale; 2098 mSidxEntries.add(se); 2099 } 2100 2101 uint64_t sidxDuration = total_duration * 1000000 / timeScale; 2102 2103 int64_t metaDuration; 2104 if (!mLastTrack->meta->findInt64(kKeyDuration, &metaDuration) || metaDuration == 0) { 2105 mLastTrack->meta->setInt64(kKeyDuration, sidxDuration); 2106 } 2107 return OK; 2108} 2109 2110 2111 2112status_t MPEG4Extractor::parseTrackHeader( 2113 off64_t data_offset, off64_t data_size) { 2114 if (data_size < 4) { 2115 return ERROR_MALFORMED; 2116 } 2117 2118 uint8_t version; 2119 if (mDataSource->readAt(data_offset, &version, 1) < 1) { 2120 return ERROR_IO; 2121 } 2122 2123 size_t dynSize = (version == 1) ? 36 : 24; 2124 2125 uint8_t buffer[36 + 60]; 2126 2127 if (data_size != (off64_t)dynSize + 60) { 2128 return ERROR_MALFORMED; 2129 } 2130 2131 if (mDataSource->readAt( 2132 data_offset, buffer, data_size) < (ssize_t)data_size) { 2133 return ERROR_IO; 2134 } 2135 2136 uint64_t ctime, mtime, duration; 2137 int32_t id; 2138 2139 if (version == 1) { 2140 ctime = U64_AT(&buffer[4]); 2141 mtime = U64_AT(&buffer[12]); 2142 id = U32_AT(&buffer[20]); 2143 duration = U64_AT(&buffer[28]); 2144 } else if (version == 0) { 2145 ctime = U32_AT(&buffer[4]); 2146 mtime = U32_AT(&buffer[8]); 2147 id = U32_AT(&buffer[12]); 2148 duration = U32_AT(&buffer[20]); 2149 } else { 2150 return ERROR_UNSUPPORTED; 2151 } 2152 2153 mLastTrack->meta->setInt32(kKeyTrackID, id); 2154 2155 size_t matrixOffset = dynSize + 16; 2156 int32_t a00 = U32_AT(&buffer[matrixOffset]); 2157 int32_t a01 = U32_AT(&buffer[matrixOffset + 4]); 2158 int32_t dx = U32_AT(&buffer[matrixOffset + 8]); 2159 int32_t a10 = U32_AT(&buffer[matrixOffset + 12]); 2160 int32_t a11 = U32_AT(&buffer[matrixOffset + 16]); 2161 int32_t dy = U32_AT(&buffer[matrixOffset + 20]); 2162 2163#if 0 2164 ALOGI("x' = %.2f * x + %.2f * y + %.2f", 2165 a00 / 65536.0f, a01 / 65536.0f, dx / 65536.0f); 2166 ALOGI("y' = %.2f * x + %.2f * y + %.2f", 2167 a10 / 65536.0f, a11 / 65536.0f, dy / 65536.0f); 2168#endif 2169 2170 uint32_t rotationDegrees; 2171 2172 static const int32_t kFixedOne = 0x10000; 2173 if (a00 == kFixedOne && a01 == 0 && a10 == 0 && a11 == kFixedOne) { 2174 // Identity, no rotation 2175 rotationDegrees = 0; 2176 } else if (a00 == 0 && a01 == kFixedOne && a10 == -kFixedOne && a11 == 0) { 2177 rotationDegrees = 90; 2178 } else if (a00 == 0 && a01 == -kFixedOne && a10 == kFixedOne && a11 == 0) { 2179 rotationDegrees = 270; 2180 } else if (a00 == -kFixedOne && a01 == 0 && a10 == 0 && a11 == -kFixedOne) { 2181 rotationDegrees = 180; 2182 } else { 2183 ALOGW("We only support 0,90,180,270 degree rotation matrices"); 2184 rotationDegrees = 0; 2185 } 2186 2187 if (rotationDegrees != 0) { 2188 mLastTrack->meta->setInt32(kKeyRotation, rotationDegrees); 2189 } 2190 2191 // Handle presentation display size, which could be different 2192 // from the image size indicated by kKeyWidth and kKeyHeight. 2193 uint32_t width = U32_AT(&buffer[dynSize + 52]); 2194 uint32_t height = U32_AT(&buffer[dynSize + 56]); 2195 mLastTrack->meta->setInt32(kKeyDisplayWidth, width >> 16); 2196 mLastTrack->meta->setInt32(kKeyDisplayHeight, height >> 16); 2197 2198 return OK; 2199} 2200 2201status_t MPEG4Extractor::parseITunesMetaData(off64_t offset, size_t size) { 2202 if (size < 4) { 2203 return ERROR_MALFORMED; 2204 } 2205 2206 uint8_t *buffer = new (std::nothrow) uint8_t[size + 1]; 2207 if (buffer == NULL) { 2208 return ERROR_MALFORMED; 2209 } 2210 if (mDataSource->readAt( 2211 offset, buffer, size) != (ssize_t)size) { 2212 delete[] buffer; 2213 buffer = NULL; 2214 2215 return ERROR_IO; 2216 } 2217 2218 uint32_t flags = U32_AT(buffer); 2219 2220 uint32_t metadataKey = 0; 2221 char chunk[5]; 2222 MakeFourCCString(mPath[4], chunk); 2223 ALOGV("meta: %s @ %lld", chunk, offset); 2224 switch (mPath[4]) { 2225 case FOURCC(0xa9, 'a', 'l', 'b'): 2226 { 2227 metadataKey = kKeyAlbum; 2228 break; 2229 } 2230 case FOURCC(0xa9, 'A', 'R', 'T'): 2231 { 2232 metadataKey = kKeyArtist; 2233 break; 2234 } 2235 case FOURCC('a', 'A', 'R', 'T'): 2236 { 2237 metadataKey = kKeyAlbumArtist; 2238 break; 2239 } 2240 case FOURCC(0xa9, 'd', 'a', 'y'): 2241 { 2242 metadataKey = kKeyYear; 2243 break; 2244 } 2245 case FOURCC(0xa9, 'n', 'a', 'm'): 2246 { 2247 metadataKey = kKeyTitle; 2248 break; 2249 } 2250 case FOURCC(0xa9, 'w', 'r', 't'): 2251 { 2252 metadataKey = kKeyWriter; 2253 break; 2254 } 2255 case FOURCC('c', 'o', 'v', 'r'): 2256 { 2257 metadataKey = kKeyAlbumArt; 2258 break; 2259 } 2260 case FOURCC('g', 'n', 'r', 'e'): 2261 { 2262 metadataKey = kKeyGenre; 2263 break; 2264 } 2265 case FOURCC(0xa9, 'g', 'e', 'n'): 2266 { 2267 metadataKey = kKeyGenre; 2268 break; 2269 } 2270 case FOURCC('c', 'p', 'i', 'l'): 2271 { 2272 if (size == 9 && flags == 21) { 2273 char tmp[16]; 2274 sprintf(tmp, "%d", 2275 (int)buffer[size - 1]); 2276 2277 mFileMetaData->setCString(kKeyCompilation, tmp); 2278 } 2279 break; 2280 } 2281 case FOURCC('t', 'r', 'k', 'n'): 2282 { 2283 if (size == 16 && flags == 0) { 2284 char tmp[16]; 2285 uint16_t* pTrack = (uint16_t*)&buffer[10]; 2286 uint16_t* pTotalTracks = (uint16_t*)&buffer[12]; 2287 sprintf(tmp, "%d/%d", ntohs(*pTrack), ntohs(*pTotalTracks)); 2288 2289 mFileMetaData->setCString(kKeyCDTrackNumber, tmp); 2290 } 2291 break; 2292 } 2293 case FOURCC('d', 'i', 's', 'k'): 2294 { 2295 if ((size == 14 || size == 16) && flags == 0) { 2296 char tmp[16]; 2297 uint16_t* pDisc = (uint16_t*)&buffer[10]; 2298 uint16_t* pTotalDiscs = (uint16_t*)&buffer[12]; 2299 sprintf(tmp, "%d/%d", ntohs(*pDisc), ntohs(*pTotalDiscs)); 2300 2301 mFileMetaData->setCString(kKeyDiscNumber, tmp); 2302 } 2303 break; 2304 } 2305 case FOURCC('-', '-', '-', '-'): 2306 { 2307 buffer[size] = '\0'; 2308 switch (mPath[5]) { 2309 case FOURCC('m', 'e', 'a', 'n'): 2310 mLastCommentMean.setTo((const char *)buffer + 4); 2311 break; 2312 case FOURCC('n', 'a', 'm', 'e'): 2313 mLastCommentName.setTo((const char *)buffer + 4); 2314 break; 2315 case FOURCC('d', 'a', 't', 'a'): 2316 mLastCommentData.setTo((const char *)buffer + 8); 2317 break; 2318 } 2319 2320 // Once we have a set of mean/name/data info, go ahead and process 2321 // it to see if its something we are interested in. Whether or not 2322 // were are interested in the specific tag, make sure to clear out 2323 // the set so we can be ready to process another tuple should one 2324 // show up later in the file. 2325 if ((mLastCommentMean.length() != 0) && 2326 (mLastCommentName.length() != 0) && 2327 (mLastCommentData.length() != 0)) { 2328 2329 if (mLastCommentMean == "com.apple.iTunes" 2330 && mLastCommentName == "iTunSMPB") { 2331 int32_t delay, padding; 2332 if (sscanf(mLastCommentData, 2333 " %*x %x %x %*x", &delay, &padding) == 2) { 2334 mLastTrack->meta->setInt32(kKeyEncoderDelay, delay); 2335 mLastTrack->meta->setInt32(kKeyEncoderPadding, padding); 2336 } 2337 } 2338 2339 mLastCommentMean.clear(); 2340 mLastCommentName.clear(); 2341 mLastCommentData.clear(); 2342 } 2343 break; 2344 } 2345 2346 default: 2347 break; 2348 } 2349 2350 if (size >= 8 && metadataKey && !mFileMetaData->hasData(metadataKey)) { 2351 if (metadataKey == kKeyAlbumArt) { 2352 mFileMetaData->setData( 2353 kKeyAlbumArt, MetaData::TYPE_NONE, 2354 buffer + 8, size - 8); 2355 } else if (metadataKey == kKeyGenre) { 2356 if (flags == 0) { 2357 // uint8_t genre code, iTunes genre codes are 2358 // the standard id3 codes, except they start 2359 // at 1 instead of 0 (e.g. Pop is 14, not 13) 2360 // We use standard id3 numbering, so subtract 1. 2361 int genrecode = (int)buffer[size - 1]; 2362 genrecode--; 2363 if (genrecode < 0) { 2364 genrecode = 255; // reserved for 'unknown genre' 2365 } 2366 char genre[10]; 2367 sprintf(genre, "%d", genrecode); 2368 2369 mFileMetaData->setCString(metadataKey, genre); 2370 } else if (flags == 1) { 2371 // custom genre string 2372 buffer[size] = '\0'; 2373 2374 mFileMetaData->setCString( 2375 metadataKey, (const char *)buffer + 8); 2376 } 2377 } else { 2378 buffer[size] = '\0'; 2379 2380 mFileMetaData->setCString( 2381 metadataKey, (const char *)buffer + 8); 2382 } 2383 } 2384 2385 delete[] buffer; 2386 buffer = NULL; 2387 2388 return OK; 2389} 2390 2391status_t MPEG4Extractor::parse3GPPMetaData(off64_t offset, size_t size, int depth) { 2392 if (size < 4) { 2393 return ERROR_MALFORMED; 2394 } 2395 2396 uint8_t *buffer = new (std::nothrow) uint8_t[size]; 2397 if (buffer == NULL) { 2398 return ERROR_MALFORMED; 2399 } 2400 if (mDataSource->readAt( 2401 offset, buffer, size) != (ssize_t)size) { 2402 delete[] buffer; 2403 buffer = NULL; 2404 2405 return ERROR_IO; 2406 } 2407 2408 uint32_t metadataKey = 0; 2409 switch (mPath[depth]) { 2410 case FOURCC('t', 'i', 't', 'l'): 2411 { 2412 metadataKey = kKeyTitle; 2413 break; 2414 } 2415 case FOURCC('p', 'e', 'r', 'f'): 2416 { 2417 metadataKey = kKeyArtist; 2418 break; 2419 } 2420 case FOURCC('a', 'u', 't', 'h'): 2421 { 2422 metadataKey = kKeyWriter; 2423 break; 2424 } 2425 case FOURCC('g', 'n', 'r', 'e'): 2426 { 2427 metadataKey = kKeyGenre; 2428 break; 2429 } 2430 case FOURCC('a', 'l', 'b', 'm'): 2431 { 2432 if (buffer[size - 1] != '\0') { 2433 char tmp[4]; 2434 sprintf(tmp, "%u", buffer[size - 1]); 2435 2436 mFileMetaData->setCString(kKeyCDTrackNumber, tmp); 2437 } 2438 2439 metadataKey = kKeyAlbum; 2440 break; 2441 } 2442 case FOURCC('y', 'r', 'r', 'c'): 2443 { 2444 char tmp[5]; 2445 uint16_t year = U16_AT(&buffer[4]); 2446 2447 if (year < 10000) { 2448 sprintf(tmp, "%u", year); 2449 2450 mFileMetaData->setCString(kKeyYear, tmp); 2451 } 2452 break; 2453 } 2454 2455 default: 2456 break; 2457 } 2458 2459 if (metadataKey > 0) { 2460 bool isUTF8 = true; // Common case 2461 char16_t *framedata = NULL; 2462 int len16 = 0; // Number of UTF-16 characters 2463 2464 // smallest possible valid UTF-16 string w BOM: 0xfe 0xff 0x00 0x00 2465 if (size - 6 >= 4) { 2466 len16 = ((size - 6) / 2) - 1; // don't include 0x0000 terminator 2467 framedata = (char16_t *)(buffer + 6); 2468 if (0xfffe == *framedata) { 2469 // endianness marker (BOM) doesn't match host endianness 2470 for (int i = 0; i < len16; i++) { 2471 framedata[i] = bswap_16(framedata[i]); 2472 } 2473 // BOM is now swapped to 0xfeff, we will execute next block too 2474 } 2475 2476 if (0xfeff == *framedata) { 2477 // Remove the BOM 2478 framedata++; 2479 len16--; 2480 isUTF8 = false; 2481 } 2482 // else normal non-zero-length UTF-8 string 2483 // we can't handle UTF-16 without BOM as there is no other 2484 // indication of encoding. 2485 } 2486 2487 if (isUTF8) { 2488 mFileMetaData->setCString(metadataKey, (const char *)buffer + 6); 2489 } else { 2490 // Convert from UTF-16 string to UTF-8 string. 2491 String8 tmpUTF8str(framedata, len16); 2492 mFileMetaData->setCString(metadataKey, tmpUTF8str.string()); 2493 } 2494 } 2495 2496 delete[] buffer; 2497 buffer = NULL; 2498 2499 return OK; 2500} 2501 2502void MPEG4Extractor::parseID3v2MetaData(off64_t offset) { 2503 ID3 id3(mDataSource, true /* ignorev1 */, offset); 2504 2505 if (id3.isValid()) { 2506 struct Map { 2507 int key; 2508 const char *tag1; 2509 const char *tag2; 2510 }; 2511 static const Map kMap[] = { 2512 { kKeyAlbum, "TALB", "TAL" }, 2513 { kKeyArtist, "TPE1", "TP1" }, 2514 { kKeyAlbumArtist, "TPE2", "TP2" }, 2515 { kKeyComposer, "TCOM", "TCM" }, 2516 { kKeyGenre, "TCON", "TCO" }, 2517 { kKeyTitle, "TIT2", "TT2" }, 2518 { kKeyYear, "TYE", "TYER" }, 2519 { kKeyAuthor, "TXT", "TEXT" }, 2520 { kKeyCDTrackNumber, "TRK", "TRCK" }, 2521 { kKeyDiscNumber, "TPA", "TPOS" }, 2522 { kKeyCompilation, "TCP", "TCMP" }, 2523 }; 2524 static const size_t kNumMapEntries = sizeof(kMap) / sizeof(kMap[0]); 2525 2526 for (size_t i = 0; i < kNumMapEntries; ++i) { 2527 if (!mFileMetaData->hasData(kMap[i].key)) { 2528 ID3::Iterator *it = new ID3::Iterator(id3, kMap[i].tag1); 2529 if (it->done()) { 2530 delete it; 2531 it = new ID3::Iterator(id3, kMap[i].tag2); 2532 } 2533 2534 if (it->done()) { 2535 delete it; 2536 continue; 2537 } 2538 2539 String8 s; 2540 it->getString(&s); 2541 delete it; 2542 2543 mFileMetaData->setCString(kMap[i].key, s); 2544 } 2545 } 2546 2547 size_t dataSize; 2548 String8 mime; 2549 const void *data = id3.getAlbumArt(&dataSize, &mime); 2550 2551 if (data) { 2552 mFileMetaData->setData(kKeyAlbumArt, MetaData::TYPE_NONE, data, dataSize); 2553 mFileMetaData->setCString(kKeyAlbumArtMIME, mime.string()); 2554 } 2555 } 2556} 2557 2558sp<MediaSource> MPEG4Extractor::getTrack(size_t index) { 2559 status_t err; 2560 if ((err = readMetaData()) != OK) { 2561 return NULL; 2562 } 2563 2564 Track *track = mFirstTrack; 2565 while (index > 0) { 2566 if (track == NULL) { 2567 return NULL; 2568 } 2569 2570 track = track->next; 2571 --index; 2572 } 2573 2574 if (track == NULL) { 2575 return NULL; 2576 } 2577 2578 2579 Trex *trex = NULL; 2580 int32_t trackId; 2581 if (track->meta->findInt32(kKeyTrackID, &trackId)) { 2582 for (size_t i = 0; i < mTrex.size(); i++) { 2583 Trex *t = &mTrex.editItemAt(index); 2584 if (t->track_ID == (uint32_t) trackId) { 2585 trex = t; 2586 break; 2587 } 2588 } 2589 } 2590 2591 ALOGV("getTrack called, pssh: %d", mPssh.size()); 2592 2593 return new MPEG4Source( 2594 track->meta, mDataSource, track->timescale, track->sampleTable, 2595 mSidxEntries, trex, mMoofOffset); 2596} 2597 2598// static 2599status_t MPEG4Extractor::verifyTrack(Track *track) { 2600 const char *mime; 2601 CHECK(track->meta->findCString(kKeyMIMEType, &mime)); 2602 2603 uint32_t type; 2604 const void *data; 2605 size_t size; 2606 if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { 2607 if (!track->meta->findData(kKeyAVCC, &type, &data, &size) 2608 || type != kTypeAVCC) { 2609 return ERROR_MALFORMED; 2610 } 2611 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_HEVC)) { 2612 if (!track->meta->findData(kKeyHVCC, &type, &data, &size) 2613 || type != kTypeHVCC) { 2614 return ERROR_MALFORMED; 2615 } 2616 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) 2617 || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { 2618 if (!track->meta->findData(kKeyESDS, &type, &data, &size) 2619 || type != kTypeESDS) { 2620 return ERROR_MALFORMED; 2621 } 2622 } 2623 2624 if (track->sampleTable == NULL || !track->sampleTable->isValid()) { 2625 // Make sure we have all the metadata we need. 2626 ALOGE("stbl atom missing/invalid."); 2627 return ERROR_MALFORMED; 2628 } 2629 2630 return OK; 2631} 2632 2633typedef enum { 2634 //AOT_NONE = -1, 2635 //AOT_NULL_OBJECT = 0, 2636 //AOT_AAC_MAIN = 1, /**< Main profile */ 2637 AOT_AAC_LC = 2, /**< Low Complexity object */ 2638 //AOT_AAC_SSR = 3, 2639 //AOT_AAC_LTP = 4, 2640 AOT_SBR = 5, 2641 //AOT_AAC_SCAL = 6, 2642 //AOT_TWIN_VQ = 7, 2643 //AOT_CELP = 8, 2644 //AOT_HVXC = 9, 2645 //AOT_RSVD_10 = 10, /**< (reserved) */ 2646 //AOT_RSVD_11 = 11, /**< (reserved) */ 2647 //AOT_TTSI = 12, /**< TTSI Object */ 2648 //AOT_MAIN_SYNTH = 13, /**< Main Synthetic object */ 2649 //AOT_WAV_TAB_SYNTH = 14, /**< Wavetable Synthesis object */ 2650 //AOT_GEN_MIDI = 15, /**< General MIDI object */ 2651 //AOT_ALG_SYNTH_AUD_FX = 16, /**< Algorithmic Synthesis and Audio FX object */ 2652 AOT_ER_AAC_LC = 17, /**< Error Resilient(ER) AAC Low Complexity */ 2653 //AOT_RSVD_18 = 18, /**< (reserved) */ 2654 //AOT_ER_AAC_LTP = 19, /**< Error Resilient(ER) AAC LTP object */ 2655 AOT_ER_AAC_SCAL = 20, /**< Error Resilient(ER) AAC Scalable object */ 2656 //AOT_ER_TWIN_VQ = 21, /**< Error Resilient(ER) TwinVQ object */ 2657 AOT_ER_BSAC = 22, /**< Error Resilient(ER) BSAC object */ 2658 AOT_ER_AAC_LD = 23, /**< Error Resilient(ER) AAC LowDelay object */ 2659 //AOT_ER_CELP = 24, /**< Error Resilient(ER) CELP object */ 2660 //AOT_ER_HVXC = 25, /**< Error Resilient(ER) HVXC object */ 2661 //AOT_ER_HILN = 26, /**< Error Resilient(ER) HILN object */ 2662 //AOT_ER_PARA = 27, /**< Error Resilient(ER) Parametric object */ 2663 //AOT_RSVD_28 = 28, /**< might become SSC */ 2664 AOT_PS = 29, /**< PS, Parametric Stereo (includes SBR) */ 2665 //AOT_MPEGS = 30, /**< MPEG Surround */ 2666 2667 AOT_ESCAPE = 31, /**< Signal AOT uses more than 5 bits */ 2668 2669 //AOT_MP3ONMP4_L1 = 32, /**< MPEG-Layer1 in mp4 */ 2670 //AOT_MP3ONMP4_L2 = 33, /**< MPEG-Layer2 in mp4 */ 2671 //AOT_MP3ONMP4_L3 = 34, /**< MPEG-Layer3 in mp4 */ 2672 //AOT_RSVD_35 = 35, /**< might become DST */ 2673 //AOT_RSVD_36 = 36, /**< might become ALS */ 2674 //AOT_AAC_SLS = 37, /**< AAC + SLS */ 2675 //AOT_SLS = 38, /**< SLS */ 2676 //AOT_ER_AAC_ELD = 39, /**< AAC Enhanced Low Delay */ 2677 2678 //AOT_USAC = 42, /**< USAC */ 2679 //AOT_SAOC = 43, /**< SAOC */ 2680 //AOT_LD_MPEGS = 44, /**< Low Delay MPEG Surround */ 2681 2682 //AOT_RSVD50 = 50, /**< Interim AOT for Rsvd50 */ 2683} AUDIO_OBJECT_TYPE; 2684 2685status_t MPEG4Extractor::updateAudioTrackInfoFromESDS_MPEG4Audio( 2686 const void *esds_data, size_t esds_size) { 2687 ESDS esds(esds_data, esds_size); 2688 2689 uint8_t objectTypeIndication; 2690 if (esds.getObjectTypeIndication(&objectTypeIndication) != OK) { 2691 return ERROR_MALFORMED; 2692 } 2693 2694 if (objectTypeIndication == 0xe1) { 2695 // This isn't MPEG4 audio at all, it's QCELP 14k... 2696 mLastTrack->meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_QCELP); 2697 return OK; 2698 } 2699 2700 if (objectTypeIndication == 0x6b) { 2701 // The media subtype is MP3 audio 2702 // Our software MP3 audio decoder may not be able to handle 2703 // packetized MP3 audio; for now, lets just return ERROR_UNSUPPORTED 2704 ALOGE("MP3 track in MP4/3GPP file is not supported"); 2705 return ERROR_UNSUPPORTED; 2706 } 2707 2708 const uint8_t *csd; 2709 size_t csd_size; 2710 if (esds.getCodecSpecificInfo( 2711 (const void **)&csd, &csd_size) != OK) { 2712 return ERROR_MALFORMED; 2713 } 2714 2715#if 0 2716 printf("ESD of size %d\n", csd_size); 2717 hexdump(csd, csd_size); 2718#endif 2719 2720 if (csd_size == 0) { 2721 // There's no further information, i.e. no codec specific data 2722 // Let's assume that the information provided in the mpeg4 headers 2723 // is accurate and hope for the best. 2724 2725 return OK; 2726 } 2727 2728 if (csd_size < 2) { 2729 return ERROR_MALFORMED; 2730 } 2731 2732 static uint32_t kSamplingRate[] = { 2733 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 2734 16000, 12000, 11025, 8000, 7350 2735 }; 2736 2737 ABitReader br(csd, csd_size); 2738 uint32_t objectType = br.getBits(5); 2739 2740 if (objectType == 31) { // AAC-ELD => additional 6 bits 2741 objectType = 32 + br.getBits(6); 2742 } 2743 2744 //keep AOT type 2745 mLastTrack->meta->setInt32(kKeyAACAOT, objectType); 2746 2747 uint32_t freqIndex = br.getBits(4); 2748 2749 int32_t sampleRate = 0; 2750 int32_t numChannels = 0; 2751 if (freqIndex == 15) { 2752 if (csd_size < 5) { 2753 return ERROR_MALFORMED; 2754 } 2755 sampleRate = br.getBits(24); 2756 numChannels = br.getBits(4); 2757 } else { 2758 numChannels = br.getBits(4); 2759 2760 if (freqIndex == 13 || freqIndex == 14) { 2761 return ERROR_MALFORMED; 2762 } 2763 2764 sampleRate = kSamplingRate[freqIndex]; 2765 } 2766 2767 if (objectType == AOT_SBR || objectType == AOT_PS) {//SBR specific config per 14496-3 table 1.13 2768 uint32_t extFreqIndex = br.getBits(4); 2769 int32_t extSampleRate; 2770 if (extFreqIndex == 15) { 2771 if (csd_size < 8) { 2772 return ERROR_MALFORMED; 2773 } 2774 extSampleRate = br.getBits(24); 2775 } else { 2776 if (extFreqIndex == 13 || extFreqIndex == 14) { 2777 return ERROR_MALFORMED; 2778 } 2779 extSampleRate = kSamplingRate[extFreqIndex]; 2780 } 2781 //TODO: save the extension sampling rate value in meta data => 2782 // mLastTrack->meta->setInt32(kKeyExtSampleRate, extSampleRate); 2783 } 2784 2785 switch (numChannels) { 2786 // values defined in 14496-3_2009 amendment-4 Table 1.19 - Channel Configuration 2787 case 0: 2788 case 1:// FC 2789 case 2:// FL FR 2790 case 3:// FC, FL FR 2791 case 4:// FC, FL FR, RC 2792 case 5:// FC, FL FR, SL SR 2793 case 6:// FC, FL FR, SL SR, LFE 2794 //numChannels already contains the right value 2795 break; 2796 case 11:// FC, FL FR, SL SR, RC, LFE 2797 numChannels = 7; 2798 break; 2799 case 7: // FC, FCL FCR, FL FR, SL SR, LFE 2800 case 12:// FC, FL FR, SL SR, RL RR, LFE 2801 case 14:// FC, FL FR, SL SR, LFE, FHL FHR 2802 numChannels = 8; 2803 break; 2804 default: 2805 return ERROR_UNSUPPORTED; 2806 } 2807 2808 { 2809 if (objectType == AOT_SBR || objectType == AOT_PS) { 2810 const int32_t extensionSamplingFrequency = br.getBits(4); 2811 objectType = br.getBits(5); 2812 2813 if (objectType == AOT_ESCAPE) { 2814 objectType = 32 + br.getBits(6); 2815 } 2816 } 2817 if (objectType == AOT_AAC_LC || objectType == AOT_ER_AAC_LC || 2818 objectType == AOT_ER_AAC_LD || objectType == AOT_ER_AAC_SCAL || 2819 objectType == AOT_ER_BSAC) { 2820 const int32_t frameLengthFlag = br.getBits(1); 2821 2822 const int32_t dependsOnCoreCoder = br.getBits(1); 2823 2824 if (dependsOnCoreCoder ) { 2825 const int32_t coreCoderDelay = br.getBits(14); 2826 } 2827 2828 const int32_t extensionFlag = br.getBits(1); 2829 2830 if (numChannels == 0 ) { 2831 int32_t channelsEffectiveNum = 0; 2832 int32_t channelsNum = 0; 2833 const int32_t ElementInstanceTag = br.getBits(4); 2834 const int32_t Profile = br.getBits(2); 2835 const int32_t SamplingFrequencyIndex = br.getBits(4); 2836 const int32_t NumFrontChannelElements = br.getBits(4); 2837 const int32_t NumSideChannelElements = br.getBits(4); 2838 const int32_t NumBackChannelElements = br.getBits(4); 2839 const int32_t NumLfeChannelElements = br.getBits(2); 2840 const int32_t NumAssocDataElements = br.getBits(3); 2841 const int32_t NumValidCcElements = br.getBits(4); 2842 2843 const int32_t MonoMixdownPresent = br.getBits(1); 2844 if (MonoMixdownPresent != 0) { 2845 const int32_t MonoMixdownElementNumber = br.getBits(4); 2846 } 2847 2848 const int32_t StereoMixdownPresent = br.getBits(1); 2849 if (StereoMixdownPresent != 0) { 2850 const int32_t StereoMixdownElementNumber = br.getBits(4); 2851 } 2852 2853 const int32_t MatrixMixdownIndexPresent = br.getBits(1); 2854 if (MatrixMixdownIndexPresent != 0) { 2855 const int32_t MatrixMixdownIndex = br.getBits(2); 2856 const int32_t PseudoSurroundEnable = br.getBits(1); 2857 } 2858 2859 int i; 2860 for (i=0; i < NumFrontChannelElements; i++) { 2861 const int32_t FrontElementIsCpe = br.getBits(1); 2862 const int32_t FrontElementTagSelect = br.getBits(4); 2863 channelsNum += FrontElementIsCpe ? 2 : 1; 2864 } 2865 2866 for (i=0; i < NumSideChannelElements; i++) { 2867 const int32_t SideElementIsCpe = br.getBits(1); 2868 const int32_t SideElementTagSelect = br.getBits(4); 2869 channelsNum += SideElementIsCpe ? 2 : 1; 2870 } 2871 2872 for (i=0; i < NumBackChannelElements; i++) { 2873 const int32_t BackElementIsCpe = br.getBits(1); 2874 const int32_t BackElementTagSelect = br.getBits(4); 2875 channelsNum += BackElementIsCpe ? 2 : 1; 2876 } 2877 channelsEffectiveNum = channelsNum; 2878 2879 for (i=0; i < NumLfeChannelElements; i++) { 2880 const int32_t LfeElementTagSelect = br.getBits(4); 2881 channelsNum += 1; 2882 } 2883 ALOGV("mpeg4 audio channelsNum = %d", channelsNum); 2884 ALOGV("mpeg4 audio channelsEffectiveNum = %d", channelsEffectiveNum); 2885 numChannels = channelsNum; 2886 } 2887 } 2888 } 2889 2890 if (numChannels == 0) { 2891 return ERROR_UNSUPPORTED; 2892 } 2893 2894 int32_t prevSampleRate; 2895 CHECK(mLastTrack->meta->findInt32(kKeySampleRate, &prevSampleRate)); 2896 2897 if (prevSampleRate != sampleRate) { 2898 ALOGV("mpeg4 audio sample rate different from previous setting. " 2899 "was: %d, now: %d", prevSampleRate, sampleRate); 2900 } 2901 2902 mLastTrack->meta->setInt32(kKeySampleRate, sampleRate); 2903 2904 int32_t prevChannelCount; 2905 CHECK(mLastTrack->meta->findInt32(kKeyChannelCount, &prevChannelCount)); 2906 2907 if (prevChannelCount != numChannels) { 2908 ALOGV("mpeg4 audio channel count different from previous setting. " 2909 "was: %d, now: %d", prevChannelCount, numChannels); 2910 } 2911 2912 mLastTrack->meta->setInt32(kKeyChannelCount, numChannels); 2913 2914 return OK; 2915} 2916 2917//////////////////////////////////////////////////////////////////////////////// 2918 2919MPEG4Source::MPEG4Source( 2920 const sp<MetaData> &format, 2921 const sp<DataSource> &dataSource, 2922 int32_t timeScale, 2923 const sp<SampleTable> &sampleTable, 2924 Vector<SidxEntry> &sidx, 2925 const Trex *trex, 2926 off64_t firstMoofOffset) 2927 : mFormat(format), 2928 mDataSource(dataSource), 2929 mTimescale(timeScale), 2930 mSampleTable(sampleTable), 2931 mCurrentSampleIndex(0), 2932 mCurrentFragmentIndex(0), 2933 mSegments(sidx), 2934 mTrex(trex), 2935 mFirstMoofOffset(firstMoofOffset), 2936 mCurrentMoofOffset(firstMoofOffset), 2937 mCurrentTime(0), 2938 mCurrentSampleInfoAllocSize(0), 2939 mCurrentSampleInfoSizes(NULL), 2940 mCurrentSampleInfoOffsetsAllocSize(0), 2941 mCurrentSampleInfoOffsets(NULL), 2942 mIsAVC(false), 2943 mIsHEVC(false), 2944 mNALLengthSize(0), 2945 mStarted(false), 2946 mGroup(NULL), 2947 mBuffer(NULL), 2948 mWantsNALFragments(false), 2949 mSrcBuffer(NULL) { 2950 2951 memset(&mTrackFragmentHeaderInfo, 0, sizeof(mTrackFragmentHeaderInfo)); 2952 2953 mFormat->findInt32(kKeyCryptoMode, &mCryptoMode); 2954 mDefaultIVSize = 0; 2955 mFormat->findInt32(kKeyCryptoDefaultIVSize, &mDefaultIVSize); 2956 uint32_t keytype; 2957 const void *key; 2958 size_t keysize; 2959 if (mFormat->findData(kKeyCryptoKey, &keytype, &key, &keysize)) { 2960 CHECK(keysize <= 16); 2961 memset(mCryptoKey, 0, 16); 2962 memcpy(mCryptoKey, key, keysize); 2963 } 2964 2965 const char *mime; 2966 bool success = mFormat->findCString(kKeyMIMEType, &mime); 2967 CHECK(success); 2968 2969 mIsAVC = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC); 2970 mIsHEVC = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_HEVC); 2971 2972 if (mIsAVC) { 2973 uint32_t type; 2974 const void *data; 2975 size_t size; 2976 CHECK(format->findData(kKeyAVCC, &type, &data, &size)); 2977 2978 const uint8_t *ptr = (const uint8_t *)data; 2979 2980 CHECK(size >= 7); 2981 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 2982 2983 // The number of bytes used to encode the length of a NAL unit. 2984 mNALLengthSize = 1 + (ptr[4] & 3); 2985 } else if (mIsHEVC) { 2986 uint32_t type; 2987 const void *data; 2988 size_t size; 2989 CHECK(format->findData(kKeyHVCC, &type, &data, &size)); 2990 2991 const uint8_t *ptr = (const uint8_t *)data; 2992 2993 CHECK(size >= 7); 2994 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 2995 2996 mNALLengthSize = 1 + (ptr[14 + 7] & 3); 2997 } 2998 2999 CHECK(format->findInt32(kKeyTrackID, &mTrackId)); 3000 3001 if (mFirstMoofOffset != 0) { 3002 off64_t offset = mFirstMoofOffset; 3003 parseChunk(&offset); 3004 } 3005} 3006 3007MPEG4Source::~MPEG4Source() { 3008 if (mStarted) { 3009 stop(); 3010 } 3011 free(mCurrentSampleInfoSizes); 3012 free(mCurrentSampleInfoOffsets); 3013} 3014 3015status_t MPEG4Source::start(MetaData *params) { 3016 Mutex::Autolock autoLock(mLock); 3017 3018 CHECK(!mStarted); 3019 3020 int32_t val; 3021 if (params && params->findInt32(kKeyWantsNALFragments, &val) 3022 && val != 0) { 3023 mWantsNALFragments = true; 3024 } else { 3025 mWantsNALFragments = false; 3026 } 3027 3028 mGroup = new MediaBufferGroup; 3029 3030 int32_t max_size; 3031 CHECK(mFormat->findInt32(kKeyMaxInputSize, &max_size)); 3032 3033 mGroup->add_buffer(new MediaBuffer(max_size)); 3034 3035 mSrcBuffer = new (std::nothrow) uint8_t[max_size]; 3036 if (mSrcBuffer == NULL) { 3037 // file probably specified a bad max size 3038 return ERROR_MALFORMED; 3039 } 3040 3041 mStarted = true; 3042 3043 return OK; 3044} 3045 3046status_t MPEG4Source::stop() { 3047 Mutex::Autolock autoLock(mLock); 3048 3049 CHECK(mStarted); 3050 3051 if (mBuffer != NULL) { 3052 mBuffer->release(); 3053 mBuffer = NULL; 3054 } 3055 3056 delete[] mSrcBuffer; 3057 mSrcBuffer = NULL; 3058 3059 delete mGroup; 3060 mGroup = NULL; 3061 3062 mStarted = false; 3063 mCurrentSampleIndex = 0; 3064 3065 return OK; 3066} 3067 3068status_t MPEG4Source::parseChunk(off64_t *offset) { 3069 uint32_t hdr[2]; 3070 if (mDataSource->readAt(*offset, hdr, 8) < 8) { 3071 return ERROR_IO; 3072 } 3073 uint64_t chunk_size = ntohl(hdr[0]); 3074 uint32_t chunk_type = ntohl(hdr[1]); 3075 off64_t data_offset = *offset + 8; 3076 3077 if (chunk_size == 1) { 3078 if (mDataSource->readAt(*offset + 8, &chunk_size, 8) < 8) { 3079 return ERROR_IO; 3080 } 3081 chunk_size = ntoh64(chunk_size); 3082 data_offset += 8; 3083 3084 if (chunk_size < 16) { 3085 // The smallest valid chunk is 16 bytes long in this case. 3086 return ERROR_MALFORMED; 3087 } 3088 } else if (chunk_size < 8) { 3089 // The smallest valid chunk is 8 bytes long. 3090 return ERROR_MALFORMED; 3091 } 3092 3093 char chunk[5]; 3094 MakeFourCCString(chunk_type, chunk); 3095 ALOGV("MPEG4Source chunk %s @ %llx", chunk, *offset); 3096 3097 off64_t chunk_data_size = *offset + chunk_size - data_offset; 3098 3099 switch(chunk_type) { 3100 3101 case FOURCC('t', 'r', 'a', 'f'): 3102 case FOURCC('m', 'o', 'o', 'f'): { 3103 off64_t stop_offset = *offset + chunk_size; 3104 *offset = data_offset; 3105 while (*offset < stop_offset) { 3106 status_t err = parseChunk(offset); 3107 if (err != OK) { 3108 return err; 3109 } 3110 } 3111 if (chunk_type == FOURCC('m', 'o', 'o', 'f')) { 3112 // *offset points to the box following this moof. Find the next moof from there. 3113 3114 while (true) { 3115 if (mDataSource->readAt(*offset, hdr, 8) < 8) { 3116 return ERROR_END_OF_STREAM; 3117 } 3118 chunk_size = ntohl(hdr[0]); 3119 chunk_type = ntohl(hdr[1]); 3120 if (chunk_type == FOURCC('m', 'o', 'o', 'f')) { 3121 mNextMoofOffset = *offset; 3122 break; 3123 } 3124 *offset += chunk_size; 3125 } 3126 } 3127 break; 3128 } 3129 3130 case FOURCC('t', 'f', 'h', 'd'): { 3131 status_t err; 3132 if ((err = parseTrackFragmentHeader(data_offset, chunk_data_size)) != OK) { 3133 return err; 3134 } 3135 *offset += chunk_size; 3136 break; 3137 } 3138 3139 case FOURCC('t', 'r', 'u', 'n'): { 3140 status_t err; 3141 if (mLastParsedTrackId == mTrackId) { 3142 if ((err = parseTrackFragmentRun(data_offset, chunk_data_size)) != OK) { 3143 return err; 3144 } 3145 } 3146 3147 *offset += chunk_size; 3148 break; 3149 } 3150 3151 case FOURCC('s', 'a', 'i', 'z'): { 3152 status_t err; 3153 if ((err = parseSampleAuxiliaryInformationSizes(data_offset, chunk_data_size)) != OK) { 3154 return err; 3155 } 3156 *offset += chunk_size; 3157 break; 3158 } 3159 case FOURCC('s', 'a', 'i', 'o'): { 3160 status_t err; 3161 if ((err = parseSampleAuxiliaryInformationOffsets(data_offset, chunk_data_size)) != OK) { 3162 return err; 3163 } 3164 *offset += chunk_size; 3165 break; 3166 } 3167 3168 case FOURCC('m', 'd', 'a', 't'): { 3169 // parse DRM info if present 3170 ALOGV("MPEG4Source::parseChunk mdat"); 3171 // if saiz/saoi was previously observed, do something with the sampleinfos 3172 *offset += chunk_size; 3173 break; 3174 } 3175 3176 default: { 3177 *offset += chunk_size; 3178 break; 3179 } 3180 } 3181 return OK; 3182} 3183 3184status_t MPEG4Source::parseSampleAuxiliaryInformationSizes( 3185 off64_t offset, off64_t /* size */) { 3186 ALOGV("parseSampleAuxiliaryInformationSizes"); 3187 // 14496-12 8.7.12 3188 uint8_t version; 3189 if (mDataSource->readAt( 3190 offset, &version, sizeof(version)) 3191 < (ssize_t)sizeof(version)) { 3192 return ERROR_IO; 3193 } 3194 3195 if (version != 0) { 3196 return ERROR_UNSUPPORTED; 3197 } 3198 offset++; 3199 3200 uint32_t flags; 3201 if (!mDataSource->getUInt24(offset, &flags)) { 3202 return ERROR_IO; 3203 } 3204 offset += 3; 3205 3206 if (flags & 1) { 3207 uint32_t tmp; 3208 if (!mDataSource->getUInt32(offset, &tmp)) { 3209 return ERROR_MALFORMED; 3210 } 3211 mCurrentAuxInfoType = tmp; 3212 offset += 4; 3213 if (!mDataSource->getUInt32(offset, &tmp)) { 3214 return ERROR_MALFORMED; 3215 } 3216 mCurrentAuxInfoTypeParameter = tmp; 3217 offset += 4; 3218 } 3219 3220 uint8_t defsize; 3221 if (mDataSource->readAt(offset, &defsize, 1) != 1) { 3222 return ERROR_MALFORMED; 3223 } 3224 mCurrentDefaultSampleInfoSize = defsize; 3225 offset++; 3226 3227 uint32_t smplcnt; 3228 if (!mDataSource->getUInt32(offset, &smplcnt)) { 3229 return ERROR_MALFORMED; 3230 } 3231 mCurrentSampleInfoCount = smplcnt; 3232 offset += 4; 3233 3234 if (mCurrentDefaultSampleInfoSize != 0) { 3235 ALOGV("@@@@ using default sample info size of %d", mCurrentDefaultSampleInfoSize); 3236 return OK; 3237 } 3238 if (smplcnt > mCurrentSampleInfoAllocSize) { 3239 mCurrentSampleInfoSizes = (uint8_t*) realloc(mCurrentSampleInfoSizes, smplcnt); 3240 mCurrentSampleInfoAllocSize = smplcnt; 3241 } 3242 3243 mDataSource->readAt(offset, mCurrentSampleInfoSizes, smplcnt); 3244 return OK; 3245} 3246 3247status_t MPEG4Source::parseSampleAuxiliaryInformationOffsets( 3248 off64_t offset, off64_t /* size */) { 3249 ALOGV("parseSampleAuxiliaryInformationOffsets"); 3250 // 14496-12 8.7.13 3251 uint8_t version; 3252 if (mDataSource->readAt(offset, &version, sizeof(version)) != 1) { 3253 return ERROR_IO; 3254 } 3255 offset++; 3256 3257 uint32_t flags; 3258 if (!mDataSource->getUInt24(offset, &flags)) { 3259 return ERROR_IO; 3260 } 3261 offset += 3; 3262 3263 uint32_t entrycount; 3264 if (!mDataSource->getUInt32(offset, &entrycount)) { 3265 return ERROR_IO; 3266 } 3267 offset += 4; 3268 3269 if (entrycount > mCurrentSampleInfoOffsetsAllocSize) { 3270 mCurrentSampleInfoOffsets = (uint64_t*) realloc(mCurrentSampleInfoOffsets, entrycount * 8); 3271 mCurrentSampleInfoOffsetsAllocSize = entrycount; 3272 } 3273 mCurrentSampleInfoOffsetCount = entrycount; 3274 3275 for (size_t i = 0; i < entrycount; i++) { 3276 if (version == 0) { 3277 uint32_t tmp; 3278 if (!mDataSource->getUInt32(offset, &tmp)) { 3279 return ERROR_IO; 3280 } 3281 mCurrentSampleInfoOffsets[i] = tmp; 3282 offset += 4; 3283 } else { 3284 uint64_t tmp; 3285 if (!mDataSource->getUInt64(offset, &tmp)) { 3286 return ERROR_IO; 3287 } 3288 mCurrentSampleInfoOffsets[i] = tmp; 3289 offset += 8; 3290 } 3291 } 3292 3293 // parse clear/encrypted data 3294 3295 off64_t drmoffset = mCurrentSampleInfoOffsets[0]; // from moof 3296 3297 drmoffset += mCurrentMoofOffset; 3298 int ivlength; 3299 CHECK(mFormat->findInt32(kKeyCryptoDefaultIVSize, &ivlength)); 3300 3301 // read CencSampleAuxiliaryDataFormats 3302 for (size_t i = 0; i < mCurrentSampleInfoCount; i++) { 3303 Sample *smpl = &mCurrentSamples.editItemAt(i); 3304 3305 memset(smpl->iv, 0, 16); 3306 if (mDataSource->readAt(drmoffset, smpl->iv, ivlength) != ivlength) { 3307 return ERROR_IO; 3308 } 3309 3310 drmoffset += ivlength; 3311 3312 int32_t smplinfosize = mCurrentDefaultSampleInfoSize; 3313 if (smplinfosize == 0) { 3314 smplinfosize = mCurrentSampleInfoSizes[i]; 3315 } 3316 if (smplinfosize > ivlength) { 3317 uint16_t numsubsamples; 3318 if (!mDataSource->getUInt16(drmoffset, &numsubsamples)) { 3319 return ERROR_IO; 3320 } 3321 drmoffset += 2; 3322 for (size_t j = 0; j < numsubsamples; j++) { 3323 uint16_t numclear; 3324 uint32_t numencrypted; 3325 if (!mDataSource->getUInt16(drmoffset, &numclear)) { 3326 return ERROR_IO; 3327 } 3328 drmoffset += 2; 3329 if (!mDataSource->getUInt32(drmoffset, &numencrypted)) { 3330 return ERROR_IO; 3331 } 3332 drmoffset += 4; 3333 smpl->clearsizes.add(numclear); 3334 smpl->encryptedsizes.add(numencrypted); 3335 } 3336 } else { 3337 smpl->clearsizes.add(0); 3338 smpl->encryptedsizes.add(smpl->size); 3339 } 3340 } 3341 3342 3343 return OK; 3344} 3345 3346status_t MPEG4Source::parseTrackFragmentHeader(off64_t offset, off64_t size) { 3347 3348 if (size < 8) { 3349 return -EINVAL; 3350 } 3351 3352 uint32_t flags; 3353 if (!mDataSource->getUInt32(offset, &flags)) { // actually version + flags 3354 return ERROR_MALFORMED; 3355 } 3356 3357 if (flags & 0xff000000) { 3358 return -EINVAL; 3359 } 3360 3361 if (!mDataSource->getUInt32(offset + 4, (uint32_t*)&mLastParsedTrackId)) { 3362 return ERROR_MALFORMED; 3363 } 3364 3365 if (mLastParsedTrackId != mTrackId) { 3366 // this is not the right track, skip it 3367 return OK; 3368 } 3369 3370 mTrackFragmentHeaderInfo.mFlags = flags; 3371 mTrackFragmentHeaderInfo.mTrackID = mLastParsedTrackId; 3372 offset += 8; 3373 size -= 8; 3374 3375 ALOGV("fragment header: %08x %08x", flags, mTrackFragmentHeaderInfo.mTrackID); 3376 3377 if (flags & TrackFragmentHeaderInfo::kBaseDataOffsetPresent) { 3378 if (size < 8) { 3379 return -EINVAL; 3380 } 3381 3382 if (!mDataSource->getUInt64(offset, &mTrackFragmentHeaderInfo.mBaseDataOffset)) { 3383 return ERROR_MALFORMED; 3384 } 3385 offset += 8; 3386 size -= 8; 3387 } 3388 3389 if (flags & TrackFragmentHeaderInfo::kSampleDescriptionIndexPresent) { 3390 if (size < 4) { 3391 return -EINVAL; 3392 } 3393 3394 if (!mDataSource->getUInt32(offset, &mTrackFragmentHeaderInfo.mSampleDescriptionIndex)) { 3395 return ERROR_MALFORMED; 3396 } 3397 offset += 4; 3398 size -= 4; 3399 } 3400 3401 if (flags & TrackFragmentHeaderInfo::kDefaultSampleDurationPresent) { 3402 if (size < 4) { 3403 return -EINVAL; 3404 } 3405 3406 if (!mDataSource->getUInt32(offset, &mTrackFragmentHeaderInfo.mDefaultSampleDuration)) { 3407 return ERROR_MALFORMED; 3408 } 3409 offset += 4; 3410 size -= 4; 3411 } 3412 3413 if (flags & TrackFragmentHeaderInfo::kDefaultSampleSizePresent) { 3414 if (size < 4) { 3415 return -EINVAL; 3416 } 3417 3418 if (!mDataSource->getUInt32(offset, &mTrackFragmentHeaderInfo.mDefaultSampleSize)) { 3419 return ERROR_MALFORMED; 3420 } 3421 offset += 4; 3422 size -= 4; 3423 } 3424 3425 if (flags & TrackFragmentHeaderInfo::kDefaultSampleFlagsPresent) { 3426 if (size < 4) { 3427 return -EINVAL; 3428 } 3429 3430 if (!mDataSource->getUInt32(offset, &mTrackFragmentHeaderInfo.mDefaultSampleFlags)) { 3431 return ERROR_MALFORMED; 3432 } 3433 offset += 4; 3434 size -= 4; 3435 } 3436 3437 if (!(flags & TrackFragmentHeaderInfo::kBaseDataOffsetPresent)) { 3438 mTrackFragmentHeaderInfo.mBaseDataOffset = mCurrentMoofOffset; 3439 } 3440 3441 mTrackFragmentHeaderInfo.mDataOffset = 0; 3442 return OK; 3443} 3444 3445status_t MPEG4Source::parseTrackFragmentRun(off64_t offset, off64_t size) { 3446 3447 ALOGV("MPEG4Extractor::parseTrackFragmentRun"); 3448 if (size < 8) { 3449 return -EINVAL; 3450 } 3451 3452 enum { 3453 kDataOffsetPresent = 0x01, 3454 kFirstSampleFlagsPresent = 0x04, 3455 kSampleDurationPresent = 0x100, 3456 kSampleSizePresent = 0x200, 3457 kSampleFlagsPresent = 0x400, 3458 kSampleCompositionTimeOffsetPresent = 0x800, 3459 }; 3460 3461 uint32_t flags; 3462 if (!mDataSource->getUInt32(offset, &flags)) { 3463 return ERROR_MALFORMED; 3464 } 3465 ALOGV("fragment run flags: %08x", flags); 3466 3467 if (flags & 0xff000000) { 3468 return -EINVAL; 3469 } 3470 3471 if ((flags & kFirstSampleFlagsPresent) && (flags & kSampleFlagsPresent)) { 3472 // These two shall not be used together. 3473 return -EINVAL; 3474 } 3475 3476 uint32_t sampleCount; 3477 if (!mDataSource->getUInt32(offset + 4, &sampleCount)) { 3478 return ERROR_MALFORMED; 3479 } 3480 offset += 8; 3481 size -= 8; 3482 3483 uint64_t dataOffset = mTrackFragmentHeaderInfo.mDataOffset; 3484 3485 uint32_t firstSampleFlags = 0; 3486 3487 if (flags & kDataOffsetPresent) { 3488 if (size < 4) { 3489 return -EINVAL; 3490 } 3491 3492 int32_t dataOffsetDelta; 3493 if (!mDataSource->getUInt32(offset, (uint32_t*)&dataOffsetDelta)) { 3494 return ERROR_MALFORMED; 3495 } 3496 3497 dataOffset = mTrackFragmentHeaderInfo.mBaseDataOffset + dataOffsetDelta; 3498 3499 offset += 4; 3500 size -= 4; 3501 } 3502 3503 if (flags & kFirstSampleFlagsPresent) { 3504 if (size < 4) { 3505 return -EINVAL; 3506 } 3507 3508 if (!mDataSource->getUInt32(offset, &firstSampleFlags)) { 3509 return ERROR_MALFORMED; 3510 } 3511 offset += 4; 3512 size -= 4; 3513 } 3514 3515 uint32_t sampleDuration = 0, sampleSize = 0, sampleFlags = 0, 3516 sampleCtsOffset = 0; 3517 3518 size_t bytesPerSample = 0; 3519 if (flags & kSampleDurationPresent) { 3520 bytesPerSample += 4; 3521 } else if (mTrackFragmentHeaderInfo.mFlags 3522 & TrackFragmentHeaderInfo::kDefaultSampleDurationPresent) { 3523 sampleDuration = mTrackFragmentHeaderInfo.mDefaultSampleDuration; 3524 } else if (mTrex) { 3525 sampleDuration = mTrex->default_sample_duration; 3526 } 3527 3528 if (flags & kSampleSizePresent) { 3529 bytesPerSample += 4; 3530 } else if (mTrackFragmentHeaderInfo.mFlags 3531 & TrackFragmentHeaderInfo::kDefaultSampleSizePresent) { 3532 sampleSize = mTrackFragmentHeaderInfo.mDefaultSampleSize; 3533 } else { 3534 sampleSize = mTrackFragmentHeaderInfo.mDefaultSampleSize; 3535 } 3536 3537 if (flags & kSampleFlagsPresent) { 3538 bytesPerSample += 4; 3539 } else if (mTrackFragmentHeaderInfo.mFlags 3540 & TrackFragmentHeaderInfo::kDefaultSampleFlagsPresent) { 3541 sampleFlags = mTrackFragmentHeaderInfo.mDefaultSampleFlags; 3542 } else { 3543 sampleFlags = mTrackFragmentHeaderInfo.mDefaultSampleFlags; 3544 } 3545 3546 if (flags & kSampleCompositionTimeOffsetPresent) { 3547 bytesPerSample += 4; 3548 } else { 3549 sampleCtsOffset = 0; 3550 } 3551 3552 if (size < sampleCount * bytesPerSample) { 3553 return -EINVAL; 3554 } 3555 3556 Sample tmp; 3557 for (uint32_t i = 0; i < sampleCount; ++i) { 3558 if (flags & kSampleDurationPresent) { 3559 if (!mDataSource->getUInt32(offset, &sampleDuration)) { 3560 return ERROR_MALFORMED; 3561 } 3562 offset += 4; 3563 } 3564 3565 if (flags & kSampleSizePresent) { 3566 if (!mDataSource->getUInt32(offset, &sampleSize)) { 3567 return ERROR_MALFORMED; 3568 } 3569 offset += 4; 3570 } 3571 3572 if (flags & kSampleFlagsPresent) { 3573 if (!mDataSource->getUInt32(offset, &sampleFlags)) { 3574 return ERROR_MALFORMED; 3575 } 3576 offset += 4; 3577 } 3578 3579 if (flags & kSampleCompositionTimeOffsetPresent) { 3580 if (!mDataSource->getUInt32(offset, &sampleCtsOffset)) { 3581 return ERROR_MALFORMED; 3582 } 3583 offset += 4; 3584 } 3585 3586 ALOGV("adding sample %d at offset 0x%08llx, size %u, duration %u, " 3587 " flags 0x%08x", i + 1, 3588 dataOffset, sampleSize, sampleDuration, 3589 (flags & kFirstSampleFlagsPresent) && i == 0 3590 ? firstSampleFlags : sampleFlags); 3591 tmp.offset = dataOffset; 3592 tmp.size = sampleSize; 3593 tmp.duration = sampleDuration; 3594 tmp.compositionOffset = sampleCtsOffset; 3595 mCurrentSamples.add(tmp); 3596 3597 dataOffset += sampleSize; 3598 } 3599 3600 mTrackFragmentHeaderInfo.mDataOffset = dataOffset; 3601 3602 return OK; 3603} 3604 3605sp<MetaData> MPEG4Source::getFormat() { 3606 Mutex::Autolock autoLock(mLock); 3607 3608 return mFormat; 3609} 3610 3611size_t MPEG4Source::parseNALSize(const uint8_t *data) const { 3612 switch (mNALLengthSize) { 3613 case 1: 3614 return *data; 3615 case 2: 3616 return U16_AT(data); 3617 case 3: 3618 return ((size_t)data[0] << 16) | U16_AT(&data[1]); 3619 case 4: 3620 return U32_AT(data); 3621 } 3622 3623 // This cannot happen, mNALLengthSize springs to life by adding 1 to 3624 // a 2-bit integer. 3625 CHECK(!"Should not be here."); 3626 3627 return 0; 3628} 3629 3630status_t MPEG4Source::read( 3631 MediaBuffer **out, const ReadOptions *options) { 3632 Mutex::Autolock autoLock(mLock); 3633 3634 CHECK(mStarted); 3635 3636 if (mFirstMoofOffset > 0) { 3637 return fragmentedRead(out, options); 3638 } 3639 3640 *out = NULL; 3641 3642 int64_t targetSampleTimeUs = -1; 3643 3644 int64_t seekTimeUs; 3645 ReadOptions::SeekMode mode; 3646 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 3647 uint32_t findFlags = 0; 3648 switch (mode) { 3649 case ReadOptions::SEEK_PREVIOUS_SYNC: 3650 findFlags = SampleTable::kFlagBefore; 3651 break; 3652 case ReadOptions::SEEK_NEXT_SYNC: 3653 findFlags = SampleTable::kFlagAfter; 3654 break; 3655 case ReadOptions::SEEK_CLOSEST_SYNC: 3656 case ReadOptions::SEEK_CLOSEST: 3657 findFlags = SampleTable::kFlagClosest; 3658 break; 3659 default: 3660 CHECK(!"Should not be here."); 3661 break; 3662 } 3663 3664 uint32_t sampleIndex; 3665 status_t err = mSampleTable->findSampleAtTime( 3666 seekTimeUs * mTimescale / 1000000, 3667 &sampleIndex, findFlags); 3668 3669 if (mode == ReadOptions::SEEK_CLOSEST) { 3670 // We found the closest sample already, now we want the sync 3671 // sample preceding it (or the sample itself of course), even 3672 // if the subsequent sync sample is closer. 3673 findFlags = SampleTable::kFlagBefore; 3674 } 3675 3676 uint32_t syncSampleIndex; 3677 if (err == OK) { 3678 err = mSampleTable->findSyncSampleNear( 3679 sampleIndex, &syncSampleIndex, findFlags); 3680 } 3681 3682 uint32_t sampleTime; 3683 if (err == OK) { 3684 err = mSampleTable->getMetaDataForSample( 3685 sampleIndex, NULL, NULL, &sampleTime); 3686 } 3687 3688 if (err != OK) { 3689 if (err == ERROR_OUT_OF_RANGE) { 3690 // An attempt to seek past the end of the stream would 3691 // normally cause this ERROR_OUT_OF_RANGE error. Propagating 3692 // this all the way to the MediaPlayer would cause abnormal 3693 // termination. Legacy behaviour appears to be to behave as if 3694 // we had seeked to the end of stream, ending normally. 3695 err = ERROR_END_OF_STREAM; 3696 } 3697 ALOGV("end of stream"); 3698 return err; 3699 } 3700 3701 if (mode == ReadOptions::SEEK_CLOSEST) { 3702 targetSampleTimeUs = (sampleTime * 1000000ll) / mTimescale; 3703 } 3704 3705#if 0 3706 uint32_t syncSampleTime; 3707 CHECK_EQ(OK, mSampleTable->getMetaDataForSample( 3708 syncSampleIndex, NULL, NULL, &syncSampleTime)); 3709 3710 ALOGI("seek to time %lld us => sample at time %lld us, " 3711 "sync sample at time %lld us", 3712 seekTimeUs, 3713 sampleTime * 1000000ll / mTimescale, 3714 syncSampleTime * 1000000ll / mTimescale); 3715#endif 3716 3717 mCurrentSampleIndex = syncSampleIndex; 3718 if (mBuffer != NULL) { 3719 mBuffer->release(); 3720 mBuffer = NULL; 3721 } 3722 3723 // fall through 3724 } 3725 3726 off64_t offset; 3727 size_t size; 3728 uint32_t cts, stts; 3729 bool isSyncSample; 3730 bool newBuffer = false; 3731 if (mBuffer == NULL) { 3732 newBuffer = true; 3733 3734 status_t err = 3735 mSampleTable->getMetaDataForSample( 3736 mCurrentSampleIndex, &offset, &size, &cts, &isSyncSample, &stts); 3737 3738 if (err != OK) { 3739 return err; 3740 } 3741 3742 err = mGroup->acquire_buffer(&mBuffer); 3743 3744 if (err != OK) { 3745 CHECK(mBuffer == NULL); 3746 return err; 3747 } 3748 } 3749 3750 if ((!mIsAVC && !mIsHEVC) || mWantsNALFragments) { 3751 if (newBuffer) { 3752 ssize_t num_bytes_read = 3753 mDataSource->readAt(offset, (uint8_t *)mBuffer->data(), size); 3754 3755 if (num_bytes_read < (ssize_t)size) { 3756 mBuffer->release(); 3757 mBuffer = NULL; 3758 3759 return ERROR_IO; 3760 } 3761 3762 CHECK(mBuffer != NULL); 3763 mBuffer->set_range(0, size); 3764 mBuffer->meta_data()->clear(); 3765 mBuffer->meta_data()->setInt64( 3766 kKeyTime, ((int64_t)cts * 1000000) / mTimescale); 3767 mBuffer->meta_data()->setInt64( 3768 kKeyDuration, ((int64_t)stts * 1000000) / mTimescale); 3769 3770 if (targetSampleTimeUs >= 0) { 3771 mBuffer->meta_data()->setInt64( 3772 kKeyTargetTime, targetSampleTimeUs); 3773 } 3774 3775 if (isSyncSample) { 3776 mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 3777 } 3778 3779 ++mCurrentSampleIndex; 3780 } 3781 3782 if (!mIsAVC && !mIsHEVC) { 3783 *out = mBuffer; 3784 mBuffer = NULL; 3785 3786 return OK; 3787 } 3788 3789 // Each NAL unit is split up into its constituent fragments and 3790 // each one of them returned in its own buffer. 3791 3792 CHECK(mBuffer->range_length() >= mNALLengthSize); 3793 3794 const uint8_t *src = 3795 (const uint8_t *)mBuffer->data() + mBuffer->range_offset(); 3796 3797 size_t nal_size = parseNALSize(src); 3798 if (mBuffer->range_length() < mNALLengthSize + nal_size) { 3799 ALOGE("incomplete NAL unit."); 3800 3801 mBuffer->release(); 3802 mBuffer = NULL; 3803 3804 return ERROR_MALFORMED; 3805 } 3806 3807 MediaBuffer *clone = mBuffer->clone(); 3808 CHECK(clone != NULL); 3809 clone->set_range(mBuffer->range_offset() + mNALLengthSize, nal_size); 3810 3811 CHECK(mBuffer != NULL); 3812 mBuffer->set_range( 3813 mBuffer->range_offset() + mNALLengthSize + nal_size, 3814 mBuffer->range_length() - mNALLengthSize - nal_size); 3815 3816 if (mBuffer->range_length() == 0) { 3817 mBuffer->release(); 3818 mBuffer = NULL; 3819 } 3820 3821 *out = clone; 3822 3823 return OK; 3824 } else { 3825 // Whole NAL units are returned but each fragment is prefixed by 3826 // the start code (0x00 00 00 01). 3827 ssize_t num_bytes_read = 0; 3828 int32_t drm = 0; 3829 bool usesDRM = (mFormat->findInt32(kKeyIsDRM, &drm) && drm != 0); 3830 if (usesDRM) { 3831 num_bytes_read = 3832 mDataSource->readAt(offset, (uint8_t*)mBuffer->data(), size); 3833 } else { 3834 num_bytes_read = mDataSource->readAt(offset, mSrcBuffer, size); 3835 } 3836 3837 if (num_bytes_read < (ssize_t)size) { 3838 mBuffer->release(); 3839 mBuffer = NULL; 3840 3841 return ERROR_IO; 3842 } 3843 3844 if (usesDRM) { 3845 CHECK(mBuffer != NULL); 3846 mBuffer->set_range(0, size); 3847 3848 } else { 3849 uint8_t *dstData = (uint8_t *)mBuffer->data(); 3850 size_t srcOffset = 0; 3851 size_t dstOffset = 0; 3852 3853 while (srcOffset < size) { 3854 bool isMalFormed = (srcOffset + mNALLengthSize > size); 3855 size_t nalLength = 0; 3856 if (!isMalFormed) { 3857 nalLength = parseNALSize(&mSrcBuffer[srcOffset]); 3858 srcOffset += mNALLengthSize; 3859 isMalFormed = srcOffset + nalLength > size; 3860 } 3861 3862 if (isMalFormed) { 3863 ALOGE("Video is malformed"); 3864 mBuffer->release(); 3865 mBuffer = NULL; 3866 return ERROR_MALFORMED; 3867 } 3868 3869 if (nalLength == 0) { 3870 continue; 3871 } 3872 3873 CHECK(dstOffset + 4 <= mBuffer->size()); 3874 3875 dstData[dstOffset++] = 0; 3876 dstData[dstOffset++] = 0; 3877 dstData[dstOffset++] = 0; 3878 dstData[dstOffset++] = 1; 3879 memcpy(&dstData[dstOffset], &mSrcBuffer[srcOffset], nalLength); 3880 srcOffset += nalLength; 3881 dstOffset += nalLength; 3882 } 3883 CHECK_EQ(srcOffset, size); 3884 CHECK(mBuffer != NULL); 3885 mBuffer->set_range(0, dstOffset); 3886 } 3887 3888 mBuffer->meta_data()->clear(); 3889 mBuffer->meta_data()->setInt64( 3890 kKeyTime, ((int64_t)cts * 1000000) / mTimescale); 3891 mBuffer->meta_data()->setInt64( 3892 kKeyDuration, ((int64_t)stts * 1000000) / mTimescale); 3893 3894 if (targetSampleTimeUs >= 0) { 3895 mBuffer->meta_data()->setInt64( 3896 kKeyTargetTime, targetSampleTimeUs); 3897 } 3898 3899 if (isSyncSample) { 3900 mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 3901 } 3902 3903 ++mCurrentSampleIndex; 3904 3905 *out = mBuffer; 3906 mBuffer = NULL; 3907 3908 return OK; 3909 } 3910} 3911 3912status_t MPEG4Source::fragmentedRead( 3913 MediaBuffer **out, const ReadOptions *options) { 3914 3915 ALOGV("MPEG4Source::fragmentedRead"); 3916 3917 CHECK(mStarted); 3918 3919 *out = NULL; 3920 3921 int64_t targetSampleTimeUs = -1; 3922 3923 int64_t seekTimeUs; 3924 ReadOptions::SeekMode mode; 3925 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 3926 3927 int numSidxEntries = mSegments.size(); 3928 if (numSidxEntries != 0) { 3929 int64_t totalTime = 0; 3930 off64_t totalOffset = mFirstMoofOffset; 3931 for (int i = 0; i < numSidxEntries; i++) { 3932 const SidxEntry *se = &mSegments[i]; 3933 if (totalTime + se->mDurationUs > seekTimeUs) { 3934 // The requested time is somewhere in this segment 3935 if ((mode == ReadOptions::SEEK_NEXT_SYNC && seekTimeUs > totalTime) || 3936 (mode == ReadOptions::SEEK_CLOSEST_SYNC && 3937 (seekTimeUs - totalTime) > (totalTime + se->mDurationUs - seekTimeUs))) { 3938 // requested next sync, or closest sync and it was closer to the end of 3939 // this segment 3940 totalTime += se->mDurationUs; 3941 totalOffset += se->mSize; 3942 } 3943 break; 3944 } 3945 totalTime += se->mDurationUs; 3946 totalOffset += se->mSize; 3947 } 3948 mCurrentMoofOffset = totalOffset; 3949 mCurrentSamples.clear(); 3950 mCurrentSampleIndex = 0; 3951 parseChunk(&totalOffset); 3952 mCurrentTime = totalTime * mTimescale / 1000000ll; 3953 } else { 3954 // without sidx boxes, we can only seek to 0 3955 mCurrentMoofOffset = mFirstMoofOffset; 3956 mCurrentSamples.clear(); 3957 mCurrentSampleIndex = 0; 3958 off64_t tmp = mCurrentMoofOffset; 3959 parseChunk(&tmp); 3960 mCurrentTime = 0; 3961 } 3962 3963 if (mBuffer != NULL) { 3964 mBuffer->release(); 3965 mBuffer = NULL; 3966 } 3967 3968 // fall through 3969 } 3970 3971 off64_t offset = 0; 3972 size_t size = 0; 3973 uint32_t cts = 0; 3974 bool isSyncSample = false; 3975 bool newBuffer = false; 3976 if (mBuffer == NULL) { 3977 newBuffer = true; 3978 3979 if (mCurrentSampleIndex >= mCurrentSamples.size()) { 3980 // move to next fragment if there is one 3981 if (mNextMoofOffset <= mCurrentMoofOffset) { 3982 return ERROR_END_OF_STREAM; 3983 } 3984 off64_t nextMoof = mNextMoofOffset; 3985 mCurrentMoofOffset = nextMoof; 3986 mCurrentSamples.clear(); 3987 mCurrentSampleIndex = 0; 3988 parseChunk(&nextMoof); 3989 if (mCurrentSampleIndex >= mCurrentSamples.size()) { 3990 return ERROR_END_OF_STREAM; 3991 } 3992 } 3993 3994 const Sample *smpl = &mCurrentSamples[mCurrentSampleIndex]; 3995 offset = smpl->offset; 3996 size = smpl->size; 3997 cts = mCurrentTime + smpl->compositionOffset; 3998 mCurrentTime += smpl->duration; 3999 isSyncSample = (mCurrentSampleIndex == 0); // XXX 4000 4001 status_t err = mGroup->acquire_buffer(&mBuffer); 4002 4003 if (err != OK) { 4004 CHECK(mBuffer == NULL); 4005 ALOGV("acquire_buffer returned %d", err); 4006 return err; 4007 } 4008 } 4009 4010 const Sample *smpl = &mCurrentSamples[mCurrentSampleIndex]; 4011 const sp<MetaData> bufmeta = mBuffer->meta_data(); 4012 bufmeta->clear(); 4013 if (smpl->encryptedsizes.size()) { 4014 // store clear/encrypted lengths in metadata 4015 bufmeta->setData(kKeyPlainSizes, 0, 4016 smpl->clearsizes.array(), smpl->clearsizes.size() * 4); 4017 bufmeta->setData(kKeyEncryptedSizes, 0, 4018 smpl->encryptedsizes.array(), smpl->encryptedsizes.size() * 4); 4019 bufmeta->setData(kKeyCryptoIV, 0, smpl->iv, 16); // use 16 or the actual size? 4020 bufmeta->setInt32(kKeyCryptoDefaultIVSize, mDefaultIVSize); 4021 bufmeta->setInt32(kKeyCryptoMode, mCryptoMode); 4022 bufmeta->setData(kKeyCryptoKey, 0, mCryptoKey, 16); 4023 } 4024 4025 if ((!mIsAVC && !mIsHEVC)|| mWantsNALFragments) { 4026 if (newBuffer) { 4027 ssize_t num_bytes_read = 4028 mDataSource->readAt(offset, (uint8_t *)mBuffer->data(), size); 4029 4030 if (num_bytes_read < (ssize_t)size) { 4031 mBuffer->release(); 4032 mBuffer = NULL; 4033 4034 ALOGV("i/o error"); 4035 return ERROR_IO; 4036 } 4037 4038 CHECK(mBuffer != NULL); 4039 mBuffer->set_range(0, size); 4040 mBuffer->meta_data()->setInt64( 4041 kKeyTime, ((int64_t)cts * 1000000) / mTimescale); 4042 mBuffer->meta_data()->setInt64( 4043 kKeyDuration, ((int64_t)smpl->duration * 1000000) / mTimescale); 4044 4045 if (targetSampleTimeUs >= 0) { 4046 mBuffer->meta_data()->setInt64( 4047 kKeyTargetTime, targetSampleTimeUs); 4048 } 4049 4050 if (isSyncSample) { 4051 mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 4052 } 4053 4054 ++mCurrentSampleIndex; 4055 } 4056 4057 if (!mIsAVC && !mIsHEVC) { 4058 *out = mBuffer; 4059 mBuffer = NULL; 4060 4061 return OK; 4062 } 4063 4064 // Each NAL unit is split up into its constituent fragments and 4065 // each one of them returned in its own buffer. 4066 4067 CHECK(mBuffer->range_length() >= mNALLengthSize); 4068 4069 const uint8_t *src = 4070 (const uint8_t *)mBuffer->data() + mBuffer->range_offset(); 4071 4072 size_t nal_size = parseNALSize(src); 4073 if (mBuffer->range_length() < mNALLengthSize + nal_size) { 4074 ALOGE("incomplete NAL unit."); 4075 4076 mBuffer->release(); 4077 mBuffer = NULL; 4078 4079 return ERROR_MALFORMED; 4080 } 4081 4082 MediaBuffer *clone = mBuffer->clone(); 4083 CHECK(clone != NULL); 4084 clone->set_range(mBuffer->range_offset() + mNALLengthSize, nal_size); 4085 4086 CHECK(mBuffer != NULL); 4087 mBuffer->set_range( 4088 mBuffer->range_offset() + mNALLengthSize + nal_size, 4089 mBuffer->range_length() - mNALLengthSize - nal_size); 4090 4091 if (mBuffer->range_length() == 0) { 4092 mBuffer->release(); 4093 mBuffer = NULL; 4094 } 4095 4096 *out = clone; 4097 4098 return OK; 4099 } else { 4100 ALOGV("whole NAL"); 4101 // Whole NAL units are returned but each fragment is prefixed by 4102 // the start code (0x00 00 00 01). 4103 ssize_t num_bytes_read = 0; 4104 int32_t drm = 0; 4105 bool usesDRM = (mFormat->findInt32(kKeyIsDRM, &drm) && drm != 0); 4106 if (usesDRM) { 4107 num_bytes_read = 4108 mDataSource->readAt(offset, (uint8_t*)mBuffer->data(), size); 4109 } else { 4110 num_bytes_read = mDataSource->readAt(offset, mSrcBuffer, size); 4111 } 4112 4113 if (num_bytes_read < (ssize_t)size) { 4114 mBuffer->release(); 4115 mBuffer = NULL; 4116 4117 ALOGV("i/o error"); 4118 return ERROR_IO; 4119 } 4120 4121 if (usesDRM) { 4122 CHECK(mBuffer != NULL); 4123 mBuffer->set_range(0, size); 4124 4125 } else { 4126 uint8_t *dstData = (uint8_t *)mBuffer->data(); 4127 size_t srcOffset = 0; 4128 size_t dstOffset = 0; 4129 4130 while (srcOffset < size) { 4131 bool isMalFormed = (srcOffset + mNALLengthSize > size); 4132 size_t nalLength = 0; 4133 if (!isMalFormed) { 4134 nalLength = parseNALSize(&mSrcBuffer[srcOffset]); 4135 srcOffset += mNALLengthSize; 4136 isMalFormed = srcOffset + nalLength > size; 4137 } 4138 4139 if (isMalFormed) { 4140 ALOGE("Video is malformed"); 4141 mBuffer->release(); 4142 mBuffer = NULL; 4143 return ERROR_MALFORMED; 4144 } 4145 4146 if (nalLength == 0) { 4147 continue; 4148 } 4149 4150 CHECK(dstOffset + 4 <= mBuffer->size()); 4151 4152 dstData[dstOffset++] = 0; 4153 dstData[dstOffset++] = 0; 4154 dstData[dstOffset++] = 0; 4155 dstData[dstOffset++] = 1; 4156 memcpy(&dstData[dstOffset], &mSrcBuffer[srcOffset], nalLength); 4157 srcOffset += nalLength; 4158 dstOffset += nalLength; 4159 } 4160 CHECK_EQ(srcOffset, size); 4161 CHECK(mBuffer != NULL); 4162 mBuffer->set_range(0, dstOffset); 4163 } 4164 4165 mBuffer->meta_data()->setInt64( 4166 kKeyTime, ((int64_t)cts * 1000000) / mTimescale); 4167 mBuffer->meta_data()->setInt64( 4168 kKeyDuration, ((int64_t)smpl->duration * 1000000) / mTimescale); 4169 4170 if (targetSampleTimeUs >= 0) { 4171 mBuffer->meta_data()->setInt64( 4172 kKeyTargetTime, targetSampleTimeUs); 4173 } 4174 4175 if (isSyncSample) { 4176 mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 4177 } 4178 4179 ++mCurrentSampleIndex; 4180 4181 *out = mBuffer; 4182 mBuffer = NULL; 4183 4184 return OK; 4185 } 4186} 4187 4188MPEG4Extractor::Track *MPEG4Extractor::findTrackByMimePrefix( 4189 const char *mimePrefix) { 4190 for (Track *track = mFirstTrack; track != NULL; track = track->next) { 4191 const char *mime; 4192 if (track->meta != NULL 4193 && track->meta->findCString(kKeyMIMEType, &mime) 4194 && !strncasecmp(mime, mimePrefix, strlen(mimePrefix))) { 4195 return track; 4196 } 4197 } 4198 4199 return NULL; 4200} 4201 4202static bool LegacySniffMPEG4( 4203 const sp<DataSource> &source, String8 *mimeType, float *confidence) { 4204 uint8_t header[8]; 4205 4206 ssize_t n = source->readAt(4, header, sizeof(header)); 4207 if (n < (ssize_t)sizeof(header)) { 4208 return false; 4209 } 4210 4211 if (!memcmp(header, "ftyp3gp", 7) || !memcmp(header, "ftypmp42", 8) 4212 || !memcmp(header, "ftyp3gr6", 8) || !memcmp(header, "ftyp3gs6", 8) 4213 || !memcmp(header, "ftyp3ge6", 8) || !memcmp(header, "ftyp3gg6", 8) 4214 || !memcmp(header, "ftypisom", 8) || !memcmp(header, "ftypM4V ", 8) 4215 || !memcmp(header, "ftypM4A ", 8) || !memcmp(header, "ftypf4v ", 8) 4216 || !memcmp(header, "ftypkddi", 8) || !memcmp(header, "ftypM4VP", 8)) { 4217 *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; 4218 *confidence = 0.4; 4219 4220 return true; 4221 } 4222 4223 return false; 4224} 4225 4226static bool isCompatibleBrand(uint32_t fourcc) { 4227 static const uint32_t kCompatibleBrands[] = { 4228 FOURCC('i', 's', 'o', 'm'), 4229 FOURCC('i', 's', 'o', '2'), 4230 FOURCC('a', 'v', 'c', '1'), 4231 FOURCC('h', 'v', 'c', '1'), 4232 FOURCC('h', 'e', 'v', '1'), 4233 FOURCC('3', 'g', 'p', '4'), 4234 FOURCC('m', 'p', '4', '1'), 4235 FOURCC('m', 'p', '4', '2'), 4236 4237 // Won't promise that the following file types can be played. 4238 // Just give these file types a chance. 4239 FOURCC('q', 't', ' ', ' '), // Apple's QuickTime 4240 FOURCC('M', 'S', 'N', 'V'), // Sony's PSP 4241 4242 FOURCC('3', 'g', '2', 'a'), // 3GPP2 4243 FOURCC('3', 'g', '2', 'b'), 4244 }; 4245 4246 for (size_t i = 0; 4247 i < sizeof(kCompatibleBrands) / sizeof(kCompatibleBrands[0]); 4248 ++i) { 4249 if (kCompatibleBrands[i] == fourcc) { 4250 return true; 4251 } 4252 } 4253 4254 return false; 4255} 4256 4257// Attempt to actually parse the 'ftyp' atom and determine if a suitable 4258// compatible brand is present. 4259// Also try to identify where this file's metadata ends 4260// (end of the 'moov' atom) and report it to the caller as part of 4261// the metadata. 4262static bool BetterSniffMPEG4( 4263 const sp<DataSource> &source, String8 *mimeType, float *confidence, 4264 sp<AMessage> *meta) { 4265 // We scan up to 128 bytes to identify this file as an MP4. 4266 static const off64_t kMaxScanOffset = 128ll; 4267 4268 off64_t offset = 0ll; 4269 bool foundGoodFileType = false; 4270 off64_t moovAtomEndOffset = -1ll; 4271 bool done = false; 4272 4273 while (!done && offset < kMaxScanOffset) { 4274 uint32_t hdr[2]; 4275 if (source->readAt(offset, hdr, 8) < 8) { 4276 return false; 4277 } 4278 4279 uint64_t chunkSize = ntohl(hdr[0]); 4280 uint32_t chunkType = ntohl(hdr[1]); 4281 off64_t chunkDataOffset = offset + 8; 4282 4283 if (chunkSize == 1) { 4284 if (source->readAt(offset + 8, &chunkSize, 8) < 8) { 4285 return false; 4286 } 4287 4288 chunkSize = ntoh64(chunkSize); 4289 chunkDataOffset += 8; 4290 4291 if (chunkSize < 16) { 4292 // The smallest valid chunk is 16 bytes long in this case. 4293 return false; 4294 } 4295 } else if (chunkSize < 8) { 4296 // The smallest valid chunk is 8 bytes long. 4297 return false; 4298 } 4299 4300 off64_t chunkDataSize = offset + chunkSize - chunkDataOffset; 4301 4302 char chunkstring[5]; 4303 MakeFourCCString(chunkType, chunkstring); 4304 ALOGV("saw chunk type %s, size %lld @ %lld", chunkstring, chunkSize, offset); 4305 switch (chunkType) { 4306 case FOURCC('f', 't', 'y', 'p'): 4307 { 4308 if (chunkDataSize < 8) { 4309 return false; 4310 } 4311 4312 uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4; 4313 for (size_t i = 0; i < numCompatibleBrands + 2; ++i) { 4314 if (i == 1) { 4315 // Skip this index, it refers to the minorVersion, 4316 // not a brand. 4317 continue; 4318 } 4319 4320 uint32_t brand; 4321 if (source->readAt( 4322 chunkDataOffset + 4 * i, &brand, 4) < 4) { 4323 return false; 4324 } 4325 4326 brand = ntohl(brand); 4327 4328 if (isCompatibleBrand(brand)) { 4329 foundGoodFileType = true; 4330 break; 4331 } 4332 } 4333 4334 if (!foundGoodFileType) { 4335 return false; 4336 } 4337 4338 break; 4339 } 4340 4341 case FOURCC('m', 'o', 'o', 'v'): 4342 { 4343 moovAtomEndOffset = offset + chunkSize; 4344 4345 done = true; 4346 break; 4347 } 4348 4349 default: 4350 break; 4351 } 4352 4353 offset += chunkSize; 4354 } 4355 4356 if (!foundGoodFileType) { 4357 return false; 4358 } 4359 4360 *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; 4361 *confidence = 0.4f; 4362 4363 if (moovAtomEndOffset >= 0) { 4364 *meta = new AMessage; 4365 (*meta)->setInt64("meta-data-size", moovAtomEndOffset); 4366 4367 ALOGV("found metadata size: %lld", moovAtomEndOffset); 4368 } 4369 4370 return true; 4371} 4372 4373bool SniffMPEG4( 4374 const sp<DataSource> &source, String8 *mimeType, float *confidence, 4375 sp<AMessage> *meta) { 4376 if (BetterSniffMPEG4(source, mimeType, confidence, meta)) { 4377 return true; 4378 } 4379 4380 if (LegacySniffMPEG4(source, mimeType, confidence)) { 4381 ALOGW("Identified supported mpeg4 through LegacySniffMPEG4."); 4382 return true; 4383 } 4384 4385 return false; 4386} 4387 4388} // namespace android 4389