SampleTable.h revision 25e029746796fe88e82417fb01af2e27b8bbadb2
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 SAMPLE_TABLE_H_
18
19#define SAMPLE_TABLE_H_
20
21#include <sys/types.h>
22#include <stdint.h>
23
24#include <media/stagefright/MediaErrors.h>
25#include <utils/RefBase.h>
26#include <utils/threads.h>
27#include <vector>
28
29namespace android {
30
31class DataSource;
32struct SampleIterator;
33
34class SampleTable : public RefBase {
35public:
36    SampleTable(const sp<DataSource> &source);
37
38    bool isValid() const;
39
40    // type can be 'stco' or 'co64'.
41    status_t setChunkOffsetParams(
42            uint32_t type, off64_t data_offset, size_t data_size);
43
44    status_t setSampleToChunkParams(off64_t data_offset, size_t data_size);
45
46    // type can be 'stsz' or 'stz2'.
47    status_t setSampleSizeParams(
48            uint32_t type, off64_t data_offset, size_t data_size);
49
50    status_t setTimeToSampleParams(off64_t data_offset, size_t data_size);
51
52    status_t setCompositionTimeToSampleParams(
53            off64_t data_offset, size_t data_size);
54
55    status_t setSyncSampleParams(off64_t data_offset, size_t data_size);
56
57    ////////////////////////////////////////////////////////////////////////////
58
59    uint32_t countChunkOffsets() const;
60
61    uint32_t countSamples() const;
62
63    status_t getMaxSampleSize(size_t *size);
64
65    status_t getMetaDataForSample(
66            uint32_t sampleIndex,
67            off64_t *offset,
68            size_t *size,
69            uint32_t *compositionTime,
70            bool *isSyncSample = NULL);
71
72    enum {
73        kFlagBefore,
74        kFlagAfter,
75        kFlagClosest
76    };
77    status_t findSampleAtTime(
78            uint32_t req_time, uint32_t *sample_index, uint32_t flags);
79
80    status_t findSyncSampleNear(
81            uint32_t start_sample_index, uint32_t *sample_index,
82            uint32_t flags);
83
84    status_t findThumbnailSample(uint32_t *sample_index);
85
86protected:
87    ~SampleTable();
88
89private:
90    struct CompositionDeltaLookup;
91
92    static const uint32_t kChunkOffsetType32;
93    static const uint32_t kChunkOffsetType64;
94    static const uint32_t kSampleSizeType32;
95    static const uint32_t kSampleSizeTypeCompact;
96
97    // Limit the total size of all internal tables to 200MiB.
98    static const size_t kMaxTotalSize = 200 * (1 << 20);
99
100    sp<DataSource> mDataSource;
101    Mutex mLock;
102
103    off64_t mChunkOffsetOffset;
104    uint32_t mChunkOffsetType;
105    uint32_t mNumChunkOffsets;
106
107    off64_t mSampleToChunkOffset;
108    uint32_t mNumSampleToChunkOffsets;
109
110    off64_t mSampleSizeOffset;
111    uint32_t mSampleSizeFieldSize;
112    uint32_t mDefaultSampleSize;
113    uint32_t mNumSampleSizes;
114
115    bool mHasTimeToSample;
116    uint32_t mTimeToSampleCount;
117    std::vector<uint32_t> mTimeToSample;
118
119    struct SampleTimeEntry {
120        uint32_t mSampleIndex;
121        uint32_t mCompositionTime;
122    };
123    SampleTimeEntry *mSampleTimeEntries;
124
125    uint32_t *mCompositionTimeDeltaEntries;
126    size_t mNumCompositionTimeDeltaEntries;
127    CompositionDeltaLookup *mCompositionDeltaLookup;
128
129    off64_t mSyncSampleOffset;
130    uint32_t mNumSyncSamples;
131    uint32_t *mSyncSamples;
132    size_t mLastSyncSampleIndex;
133
134    SampleIterator *mSampleIterator;
135
136    struct SampleToChunkEntry {
137        uint32_t startChunk;
138        uint32_t samplesPerChunk;
139        uint32_t chunkDesc;
140    };
141    SampleToChunkEntry *mSampleToChunkEntries;
142
143    // Approximate size of all tables combined.
144    uint64_t mTotalSize;
145
146    friend struct SampleIterator;
147
148    status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
149    uint32_t getCompositionTimeOffset(uint32_t sampleIndex);
150
151    static int CompareIncreasingTime(const void *, const void *);
152
153    void buildSampleEntriesTable();
154
155    SampleTable(const SampleTable &);
156    SampleTable &operator=(const SampleTable &);
157};
158
159}  // namespace android
160
161#endif  // SAMPLE_TABLE_H_
162