Home | History | Annotate | Download | only in mp4
      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/MetaDataBase.h>
     26 #include <media/stagefright/foundation/AString.h>
     27 #include <utils/KeyedVector.h>
     28 #include <utils/List.h>
     29 #include <utils/String8.h>
     30 #include <utils/Vector.h>
     31 
     32 namespace android {
     33 struct AMessage;
     34 class DataSourceBase;
     35 struct CachedRangedDataSource;
     36 class SampleTable;
     37 class String8;
     38 namespace heif {
     39 class ItemTable;
     40 }
     41 using heif::ItemTable;
     42 
     43 struct SidxEntry {
     44     size_t mSize;
     45     uint32_t mDurationUs;
     46 };
     47 
     48 struct Trex {
     49     uint32_t track_ID;
     50     uint32_t default_sample_description_index;
     51     uint32_t default_sample_duration;
     52     uint32_t default_sample_size;
     53     uint32_t default_sample_flags;
     54 };
     55 
     56 class MPEG4Extractor : public MediaExtractor {
     57 public:
     58     explicit MPEG4Extractor(DataSourceBase *source, const char *mime = NULL);
     59 
     60     virtual size_t countTracks();
     61     virtual MediaTrack *getTrack(size_t index);
     62     virtual status_t getTrackMetaData(MetaDataBase& meta, size_t index, uint32_t flags);
     63 
     64     virtual status_t getMetaData(MetaDataBase& meta);
     65     virtual uint32_t flags() const;
     66     virtual const char * name() { return "MPEG4Extractor"; }
     67 
     68 protected:
     69     virtual ~MPEG4Extractor();
     70 
     71 private:
     72 
     73     struct PsshInfo {
     74         uint8_t uuid[16];
     75         uint32_t datalen;
     76         uint8_t *data;
     77     };
     78     struct Track {
     79         Track *next;
     80         MetaDataBase meta;
     81         uint32_t timescale;
     82         sp<SampleTable> sampleTable;
     83         bool includes_expensive_metadata;
     84         bool skipTrack;
     85         bool has_elst;
     86         int64_t elst_media_time;
     87         uint64_t elst_segment_duration;
     88         bool subsample_encryption;
     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     MetaDataBase 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     sp<ItemTable> mItemTable;
    135 
    136     status_t parseTrackHeader(off64_t data_offset, off64_t data_size);
    137 
    138     status_t parseSegmentIndex(off64_t data_offset, size_t data_size);
    139 
    140     Track *findTrackByMimePrefix(const char *mimePrefix);
    141 
    142     status_t parseAC3SampleEntry(off64_t offset);
    143     status_t parseAC3SpecificBox(off64_t offset, uint16_t sampleRate);
    144 
    145     MPEG4Extractor(const MPEG4Extractor &);
    146     MPEG4Extractor &operator=(const MPEG4Extractor &);
    147 };
    148 
    149 bool SniffMPEG4(
    150         DataSourceBase *source, String8 *mimeType, float *confidence,
    151         sp<AMessage> *);
    152 
    153 }  // namespace android
    154 
    155 #endif  // MPEG4_EXTRACTOR_H_
    156