MPEG4Extractor.h revision 2a243f08193fe9ff1afe018e9953f01c44ced9de
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#ifndef MPEG4_EXTRACTOR_H_
18
19#define MPEG4_EXTRACTOR_H_
20
21#include <arpa/inet.h>
22
23#include <media/DataSourceBase.h>
24#include <media/MediaExtractor.h>
25#include <media/stagefright/foundation/AString.h>
26#include <utils/List.h>
27#include <utils/Vector.h>
28#include <utils/String8.h>
29
30namespace android {
31struct AMessage;
32class DataSourceBase;
33struct CachedRangedDataSource;
34class SampleTable;
35class String8;
36namespace heif {
37class ItemTable;
38}
39using heif::ItemTable;
40
41struct SidxEntry {
42    size_t mSize;
43    uint32_t mDurationUs;
44};
45
46struct Trex {
47    uint32_t track_ID;
48    uint32_t default_sample_description_index;
49    uint32_t default_sample_duration;
50    uint32_t default_sample_size;
51    uint32_t default_sample_flags;
52};
53
54class MPEG4Extractor : public MediaExtractor {
55public:
56    explicit MPEG4Extractor(DataSourceBase *source, const char *mime = NULL);
57
58    virtual size_t countTracks();
59    virtual MediaSourceBase *getTrack(size_t index);
60    virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
61
62    virtual sp<MetaData> getMetaData();
63    virtual uint32_t flags() const;
64    virtual const char * name() { return "MPEG4Extractor"; }
65
66    // for DRM
67    virtual char* getDrmTrackInfo(size_t trackID, int *len);
68
69protected:
70    virtual ~MPEG4Extractor();
71
72private:
73
74    struct PsshInfo {
75        uint8_t uuid[16];
76        uint32_t datalen;
77        uint8_t *data;
78    };
79    struct Track {
80        Track *next;
81        sp<MetaData> meta;
82        uint32_t timescale;
83        sp<SampleTable> sampleTable;
84        bool includes_expensive_metadata;
85        bool skipTrack;
86        bool has_elst;
87        int64_t elst_media_time;
88        uint64_t elst_segment_duration;
89    };
90
91    Vector<SidxEntry> mSidxEntries;
92    off64_t mMoofOffset;
93    bool mMoofFound;
94    bool mMdatFound;
95
96    Vector<PsshInfo> mPssh;
97
98    Vector<Trex> mTrex;
99
100    DataSourceBase *mDataSource;
101    CachedRangedDataSource *mCachedSource;
102    status_t mInitCheck;
103    uint32_t mHeaderTimescale;
104    bool mIsQT;
105    bool mIsHeif;
106    bool mHasMoovBox;
107    bool mPreferHeif;
108
109    Track *mFirstTrack, *mLastTrack;
110
111    sp<MetaData> mFileMetaData;
112
113    Vector<uint32_t> mPath;
114    String8 mLastCommentMean;
115    String8 mLastCommentName;
116    String8 mLastCommentData;
117
118    KeyedVector<uint32_t, AString> mMetaKeyMap;
119
120    status_t readMetaData();
121    status_t parseChunk(off64_t *offset, int depth);
122    status_t parseITunesMetaData(off64_t offset, size_t size);
123    status_t parseColorInfo(off64_t offset, size_t size);
124    status_t parse3GPPMetaData(off64_t offset, size_t size, int depth);
125    void parseID3v2MetaData(off64_t offset);
126    status_t parseQTMetaKey(off64_t data_offset, size_t data_size);
127    status_t parseQTMetaVal(int32_t keyId, off64_t data_offset, size_t data_size);
128
129    status_t updateAudioTrackInfoFromESDS_MPEG4Audio(
130            const void *esds_data, size_t esds_size);
131
132    static status_t verifyTrack(Track *track);
133
134    struct SINF {
135        SINF *next;
136        uint16_t trackID;
137        uint8_t IPMPDescriptorID;
138        ssize_t len;
139        char *IPMPData;
140    };
141
142    SINF *mFirstSINF;
143
144    bool mIsDrm;
145    sp<ItemTable> mItemTable;
146
147    status_t parseDrmSINF(off64_t *offset, off64_t data_offset);
148
149    status_t parseTrackHeader(off64_t data_offset, off64_t data_size);
150
151    status_t parseSegmentIndex(off64_t data_offset, size_t data_size);
152
153    Track *findTrackByMimePrefix(const char *mimePrefix);
154
155    status_t parseAC3SampleEntry(off64_t offset);
156    status_t parseAC3SpecificBox(off64_t offset, uint16_t sampleRate);
157
158    MPEG4Extractor(const MPEG4Extractor &);
159    MPEG4Extractor &operator=(const MPEG4Extractor &);
160};
161
162bool SniffMPEG4(
163        DataSourceBase *source, String8 *mimeType, float *confidence,
164        sp<AMessage> *);
165
166}  // namespace android
167
168#endif  // MPEG4_EXTRACTOR_H_
169