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