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