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