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