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