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