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