1/*
2 * Copyright (C) 2011 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 AVI_EXTRACTOR_H_
18
19#define AVI_EXTRACTOR_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/MediaExtractor.h>
23#include <media/stagefright/MediaSource.h>
24#include <utils/Vector.h>
25
26namespace android {
27
28struct AVIExtractor : public MediaExtractor {
29    AVIExtractor(const sp<DataSource> &dataSource);
30
31    virtual size_t countTracks();
32
33    virtual sp<MediaSource> getTrack(size_t index);
34
35    virtual sp<MetaData> getTrackMetaData(
36            size_t index, uint32_t flags);
37
38    virtual sp<MetaData> getMetaData();
39    virtual const char * name() { return "AVIExtractor"; }
40
41protected:
42    virtual ~AVIExtractor();
43
44private:
45    struct AVISource;
46    struct MP3Splitter;
47
48    struct SampleInfo {
49        uint32_t mOffset;
50        bool mIsKey;
51    };
52
53    struct Track {
54        sp<MetaData> mMeta;
55        Vector<SampleInfo> mSamples;
56        uint32_t mRate;
57        uint32_t mScale;
58
59        // If bytes per sample == 0, each chunk represents a single sample,
60        // otherwise each chunk should me a multiple of bytes-per-sample in
61        // size.
62        uint32_t mBytesPerSample;
63
64        enum Kind {
65            AUDIO,
66            VIDEO,
67            OTHER
68
69        } mKind;
70
71        size_t mNumSyncSamples;
72        size_t mThumbnailSampleSize;
73        ssize_t mThumbnailSampleIndex;
74        size_t mMaxSampleSize;
75
76        // If mBytesPerSample > 0:
77        double mAvgChunkSize;
78        size_t mFirstChunkSize;
79    };
80
81    sp<DataSource> mDataSource;
82    status_t mInitCheck;
83    Vector<Track> mTracks;
84
85    off64_t mMovieOffset;
86    bool mFoundIndex;
87    bool mOffsetsAreAbsolute;
88
89    ssize_t parseChunk(off64_t offset, off64_t size, int depth = 0);
90    status_t parseStreamHeader(off64_t offset, size_t size);
91    status_t parseStreamFormat(off64_t offset, size_t size);
92    status_t parseIndex(off64_t offset, size_t size);
93
94    status_t parseHeaders();
95
96    status_t getSampleInfo(
97            size_t trackIndex, size_t sampleIndex,
98            off64_t *offset, size_t *size, bool *isKey,
99            int64_t *sampleTimeUs);
100
101    status_t getSampleTime(
102            size_t trackIndex, size_t sampleIndex, int64_t *sampleTimeUs);
103
104    status_t getSampleIndexAtTime(
105            size_t trackIndex,
106            int64_t timeUs, MediaSource::ReadOptions::SeekMode mode,
107            size_t *sampleIndex) const;
108
109    status_t addMPEG4CodecSpecificData(size_t trackIndex);
110    status_t addH264CodecSpecificData(size_t trackIndex);
111
112    static bool IsCorrectChunkType(
113        ssize_t trackIndex, Track::Kind kind, uint32_t chunkType);
114
115    DISALLOW_EVIL_CONSTRUCTORS(AVIExtractor);
116};
117
118class String8;
119struct AMessage;
120
121bool SniffAVI(
122        const sp<DataSource> &source, String8 *mimeType, float *confidence,
123        sp<AMessage> *);
124
125}  // namespace android
126
127#endif  // AVI_EXTRACTOR_H_
128