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