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