blob: 466e26b778d893d109b348b1938c1a86560c4ed1 [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -07001/*
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>
Andreas Huber693d2712009-08-14 14:37:10 -070025#include <utils/RefBase.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070026#include <utils/threads.h>
27
28namespace android {
29
30class DataSource;
Andreas Huberc57b6792010-01-19 10:39:21 -080031struct SampleIterator;
Andreas Huber20111aa2009-07-14 16:56:47 -070032
Andreas Huber693d2712009-08-14 14:37:10 -070033class SampleTable : public RefBase {
Andreas Huber20111aa2009-07-14 16:56:47 -070034public:
Chih-Hung Hsieh40d88992016-08-09 14:28:14 -070035 explicit SampleTable(const sp<DataSource> &source);
Andreas Huber20111aa2009-07-14 16:56:47 -070036
Andreas Huber169c2862011-08-17 13:03:51 -070037 bool isValid() const;
38
Andreas Huber20111aa2009-07-14 16:56:47 -070039 // type can be 'stco' or 'co64'.
40 status_t setChunkOffsetParams(
James Dongc7fc37a2010-11-16 14:04:54 -080041 uint32_t type, off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070042
James Dongc7fc37a2010-11-16 14:04:54 -080043 status_t setSampleToChunkParams(off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070044
45 // type can be 'stsz' or 'stz2'.
46 status_t setSampleSizeParams(
James Dongc7fc37a2010-11-16 14:04:54 -080047 uint32_t type, off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070048
James Dongc7fc37a2010-11-16 14:04:54 -080049 status_t setTimeToSampleParams(off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070050
Andreas Huber4931bb52011-02-03 13:18:16 -080051 status_t setCompositionTimeToSampleParams(
52 off64_t data_offset, size_t data_size);
53
James Dongc7fc37a2010-11-16 14:04:54 -080054 status_t setSyncSampleParams(off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070055
56 ////////////////////////////////////////////////////////////////////////////
57
58 uint32_t countChunkOffsets() const;
Andreas Huber20111aa2009-07-14 16:56:47 -070059
60 uint32_t countSamples() const;
Andreas Huber20111aa2009-07-14 16:56:47 -070061
62 status_t getMaxSampleSize(size_t *size);
63
Andreas Huberc57b6792010-01-19 10:39:21 -080064 status_t getMetaDataForSample(
65 uint32_t sampleIndex,
James Dongc7fc37a2010-11-16 14:04:54 -080066 off64_t *offset,
Andreas Huberc57b6792010-01-19 10:39:21 -080067 size_t *size,
Andreas Huber4678a6d2011-04-15 11:52:29 -070068 uint32_t *compositionTime,
Robert Shih17005652014-04-10 17:30:21 -070069 bool *isSyncSample = NULL,
70 uint32_t *sampleDuration = NULL);
Andreas Huber20111aa2009-07-14 16:56:47 -070071
72 enum {
Andreas Huberabd1f4f2010-07-20 15:04:28 -070073 kFlagBefore,
74 kFlagAfter,
Chong Zhangd3e0d862017-10-03 13:17:13 -070075 kFlagClosest,
76 kFlagFrameIndex,
Andreas Huber20111aa2009-07-14 16:56:47 -070077 };
Andreas Huberabd1f4f2010-07-20 15:04:28 -070078 status_t findSampleAtTime(
Lajos Molnar599950e2014-07-17 10:52:36 -070079 uint64_t req_time, uint64_t scale_num, uint64_t scale_den,
80 uint32_t *sample_index, uint32_t flags);
Andreas Huber20111aa2009-07-14 16:56:47 -070081
Andreas Huberabd1f4f2010-07-20 15:04:28 -070082 status_t findSyncSampleNear(
83 uint32_t start_sample_index, uint32_t *sample_index,
84 uint32_t flags);
85
Andreas Huber7e04dcf2009-10-22 13:49:30 -070086 status_t findThumbnailSample(uint32_t *sample_index);
87
Andreas Huber693d2712009-08-14 14:37:10 -070088protected:
89 ~SampleTable();
90
Andreas Huber20111aa2009-07-14 16:56:47 -070091private:
Andreas Huber89aa8fe2011-09-19 12:18:47 -070092 struct CompositionDeltaLookup;
93
Andreas Huberc57b6792010-01-19 10:39:21 -080094 static const uint32_t kChunkOffsetType32;
95 static const uint32_t kChunkOffsetType64;
96 static const uint32_t kSampleSizeType32;
97 static const uint32_t kSampleSizeTypeCompact;
98
Pawin Vongmasa583a0122016-06-21 19:10:21 -070099 // Limit the total size of all internal tables to 200MiB.
100 static const size_t kMaxTotalSize = 200 * (1 << 20);
101
Andreas Huber693d2712009-08-14 14:37:10 -0700102 sp<DataSource> mDataSource;
Andreas Huber20111aa2009-07-14 16:56:47 -0700103 Mutex mLock;
104
James Dongc7fc37a2010-11-16 14:04:54 -0800105 off64_t mChunkOffsetOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700106 uint32_t mChunkOffsetType;
107 uint32_t mNumChunkOffsets;
108
James Dongc7fc37a2010-11-16 14:04:54 -0800109 off64_t mSampleToChunkOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700110 uint32_t mNumSampleToChunkOffsets;
111
James Dongc7fc37a2010-11-16 14:04:54 -0800112 off64_t mSampleSizeOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700113 uint32_t mSampleSizeFieldSize;
114 uint32_t mDefaultSampleSize;
115 uint32_t mNumSampleSizes;
116
Pawin Vongmasa70dec4d2016-04-20 15:51:48 -0700117 bool mHasTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700118 uint32_t mTimeToSampleCount;
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700119 uint32_t* mTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700120
Andreas Huber4678a6d2011-04-15 11:52:29 -0700121 struct SampleTimeEntry {
122 uint32_t mSampleIndex;
123 uint32_t mCompositionTime;
124 };
125 SampleTimeEntry *mSampleTimeEntries;
126
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800127 int32_t *mCompositionTimeDeltaEntries;
Andreas Huber4931bb52011-02-03 13:18:16 -0800128 size_t mNumCompositionTimeDeltaEntries;
Andreas Huber89aa8fe2011-09-19 12:18:47 -0700129 CompositionDeltaLookup *mCompositionDeltaLookup;
Andreas Huber4931bb52011-02-03 13:18:16 -0800130
James Dongc7fc37a2010-11-16 14:04:54 -0800131 off64_t mSyncSampleOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700132 uint32_t mNumSyncSamples;
Andreas Huber8bf59e72010-08-06 14:13:10 -0700133 uint32_t *mSyncSamples;
134 size_t mLastSyncSampleIndex;
Andreas Huber20111aa2009-07-14 16:56:47 -0700135
Andreas Huberc57b6792010-01-19 10:39:21 -0800136 SampleIterator *mSampleIterator;
137
138 struct SampleToChunkEntry {
139 uint32_t startChunk;
140 uint32_t samplesPerChunk;
141 uint32_t chunkDesc;
142 };
143 SampleToChunkEntry *mSampleToChunkEntries;
144
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700145 // Approximate size of all tables combined.
146 uint64_t mTotalSize;
147
Andreas Huberc57b6792010-01-19 10:39:21 -0800148 friend struct SampleIterator;
149
Lajos Molnar599950e2014-07-17 10:52:36 -0700150 // normally we don't round
151 inline uint64_t getSampleTime(
152 size_t sample_index, uint64_t scale_num, uint64_t scale_den) const {
Wei Jia3564c452015-08-18 11:17:24 -0700153 return (sample_index < (size_t)mNumSampleSizes && mSampleTimeEntries != NULL
154 && scale_den != 0)
155 ? (mSampleTimeEntries[sample_index].mCompositionTime * scale_num) / scale_den : 0;
Lajos Molnar599950e2014-07-17 10:52:36 -0700156 }
157
Andreas Huberc57b6792010-01-19 10:39:21 -0800158 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800159 int32_t getCompositionTimeOffset(uint32_t sampleIndex);
Andreas Huber4931bb52011-02-03 13:18:16 -0800160
Andreas Huber4678a6d2011-04-15 11:52:29 -0700161 static int CompareIncreasingTime(const void *, const void *);
162
163 void buildSampleEntriesTable();
164
Andreas Huber20111aa2009-07-14 16:56:47 -0700165 SampleTable(const SampleTable &);
166 SampleTable &operator=(const SampleTable &);
167};
168
169} // namespace android
170
171#endif // SAMPLE_TABLE_H_