blob: 076f4c30e22b177dd6cc2257345acc88e214fd25 [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
Marco Nelissencec44d02018-06-17 22:21:09 -070024#include <media/MediaExtractorPluginHelper.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070025#include <media/stagefright/MediaErrors.h>
Andreas Huber693d2712009-08-14 14:37:10 -070026#include <utils/RefBase.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070027#include <utils/threads.h>
28
29namespace android {
30
Marco Nelissencec44d02018-06-17 22:21:09 -070031class DataSourceHelper;
Andreas Huberc57b6792010-01-19 10:39:21 -080032struct SampleIterator;
Andreas Huber20111aa2009-07-14 16:56:47 -070033
Andreas Huber693d2712009-08-14 14:37:10 -070034class SampleTable : public RefBase {
Andreas Huber20111aa2009-07-14 16:56:47 -070035public:
Marco Nelissencec44d02018-06-17 22:21:09 -070036 explicit SampleTable(DataSourceHelper *source);
Andreas Huber20111aa2009-07-14 16:56:47 -070037
Andreas Huber169c2862011-08-17 13:03:51 -070038 bool isValid() const;
39
Andreas Huber20111aa2009-07-14 16:56:47 -070040 // type can be 'stco' or 'co64'.
41 status_t setChunkOffsetParams(
James Dongc7fc37a2010-11-16 14:04:54 -080042 uint32_t type, off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070043
James Dongc7fc37a2010-11-16 14:04:54 -080044 status_t setSampleToChunkParams(off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070045
46 // type can be 'stsz' or 'stz2'.
47 status_t setSampleSizeParams(
James Dongc7fc37a2010-11-16 14:04:54 -080048 uint32_t type, off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070049
James Dongc7fc37a2010-11-16 14:04:54 -080050 status_t setTimeToSampleParams(off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070051
Andreas Huber4931bb52011-02-03 13:18:16 -080052 status_t setCompositionTimeToSampleParams(
53 off64_t data_offset, size_t data_size);
54
James Dongc7fc37a2010-11-16 14:04:54 -080055 status_t setSyncSampleParams(off64_t data_offset, size_t data_size);
Andreas Huber20111aa2009-07-14 16:56:47 -070056
57 ////////////////////////////////////////////////////////////////////////////
58
59 uint32_t countChunkOffsets() const;
Andreas Huber20111aa2009-07-14 16:56:47 -070060
61 uint32_t countSamples() const;
Andreas Huber20111aa2009-07-14 16:56:47 -070062
63 status_t getMaxSampleSize(size_t *size);
64
Andreas Huberc57b6792010-01-19 10:39:21 -080065 status_t getMetaDataForSample(
66 uint32_t sampleIndex,
James Dongc7fc37a2010-11-16 14:04:54 -080067 off64_t *offset,
Andreas Huberc57b6792010-01-19 10:39:21 -080068 size_t *size,
wuhuang12de2b92019-01-18 17:14:03 +080069 uint64_t *compositionTime,
Robert Shih17005652014-04-10 17:30:21 -070070 bool *isSyncSample = NULL,
wuhuang12de2b92019-01-18 17:14:03 +080071 uint64_t *sampleDuration = NULL);
Andreas Huber20111aa2009-07-14 16:56:47 -070072
zhongli.wangae7f7402014-06-12 14:30:31 +020073 // call only after getMetaDataForSample has been called successfully.
74 uint32_t getLastSampleIndexInChunk();
75
Andreas Huber20111aa2009-07-14 16:56:47 -070076 enum {
Andreas Huberabd1f4f2010-07-20 15:04:28 -070077 kFlagBefore,
78 kFlagAfter,
Chong Zhangd3e0d862017-10-03 13:17:13 -070079 kFlagClosest,
80 kFlagFrameIndex,
Andreas Huber20111aa2009-07-14 16:56:47 -070081 };
Andreas Huberabd1f4f2010-07-20 15:04:28 -070082 status_t findSampleAtTime(
Lajos Molnar599950e2014-07-17 10:52:36 -070083 uint64_t req_time, uint64_t scale_num, uint64_t scale_den,
84 uint32_t *sample_index, uint32_t flags);
Andreas Huber20111aa2009-07-14 16:56:47 -070085
Andreas Huberabd1f4f2010-07-20 15:04:28 -070086 status_t findSyncSampleNear(
87 uint32_t start_sample_index, uint32_t *sample_index,
88 uint32_t flags);
89
Andreas Huber7e04dcf2009-10-22 13:49:30 -070090 status_t findThumbnailSample(uint32_t *sample_index);
91
Iris Chang93cf3df2019-05-06 13:53:43 +080092 void setPredictSampleSize(uint32_t sampleSize) {
93 mDefaultSampleSize = sampleSize;
94 }
95
Andreas Huber693d2712009-08-14 14:37:10 -070096protected:
97 ~SampleTable();
98
Andreas Huber20111aa2009-07-14 16:56:47 -070099private:
Andreas Huber89aa8fe2011-09-19 12:18:47 -0700100 struct CompositionDeltaLookup;
101
Andreas Huberc57b6792010-01-19 10:39:21 -0800102 static const uint32_t kChunkOffsetType32;
103 static const uint32_t kChunkOffsetType64;
104 static const uint32_t kSampleSizeType32;
105 static const uint32_t kSampleSizeTypeCompact;
106
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700107 // Limit the total size of all internal tables to 200MiB.
108 static const size_t kMaxTotalSize = 200 * (1 << 20);
109
Marco Nelissencec44d02018-06-17 22:21:09 -0700110 DataSourceHelper *mDataSource;
Andreas Huber20111aa2009-07-14 16:56:47 -0700111 Mutex mLock;
112
James Dongc7fc37a2010-11-16 14:04:54 -0800113 off64_t mChunkOffsetOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700114 uint32_t mChunkOffsetType;
115 uint32_t mNumChunkOffsets;
116
James Dongc7fc37a2010-11-16 14:04:54 -0800117 off64_t mSampleToChunkOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700118 uint32_t mNumSampleToChunkOffsets;
119
James Dongc7fc37a2010-11-16 14:04:54 -0800120 off64_t mSampleSizeOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700121 uint32_t mSampleSizeFieldSize;
122 uint32_t mDefaultSampleSize;
123 uint32_t mNumSampleSizes;
124
Pawin Vongmasa70dec4d2016-04-20 15:51:48 -0700125 bool mHasTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700126 uint32_t mTimeToSampleCount;
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700127 uint32_t* mTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700128
Andreas Huber4678a6d2011-04-15 11:52:29 -0700129 struct SampleTimeEntry {
130 uint32_t mSampleIndex;
wuhuang12de2b92019-01-18 17:14:03 +0800131 uint64_t mCompositionTime;
Andreas Huber4678a6d2011-04-15 11:52:29 -0700132 };
133 SampleTimeEntry *mSampleTimeEntries;
134
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800135 int32_t *mCompositionTimeDeltaEntries;
Andreas Huber4931bb52011-02-03 13:18:16 -0800136 size_t mNumCompositionTimeDeltaEntries;
Andreas Huber89aa8fe2011-09-19 12:18:47 -0700137 CompositionDeltaLookup *mCompositionDeltaLookup;
Andreas Huber4931bb52011-02-03 13:18:16 -0800138
James Dongc7fc37a2010-11-16 14:04:54 -0800139 off64_t mSyncSampleOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700140 uint32_t mNumSyncSamples;
Andreas Huber8bf59e72010-08-06 14:13:10 -0700141 uint32_t *mSyncSamples;
142 size_t mLastSyncSampleIndex;
Andreas Huber20111aa2009-07-14 16:56:47 -0700143
Andreas Huberc57b6792010-01-19 10:39:21 -0800144 SampleIterator *mSampleIterator;
145
146 struct SampleToChunkEntry {
147 uint32_t startChunk;
148 uint32_t samplesPerChunk;
149 uint32_t chunkDesc;
150 };
151 SampleToChunkEntry *mSampleToChunkEntries;
152
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700153 // Approximate size of all tables combined.
154 uint64_t mTotalSize;
155
Andreas Huberc57b6792010-01-19 10:39:21 -0800156 friend struct SampleIterator;
157
Lajos Molnar599950e2014-07-17 10:52:36 -0700158 // normally we don't round
159 inline uint64_t getSampleTime(
160 size_t sample_index, uint64_t scale_num, uint64_t scale_den) const {
Wei Jia3564c452015-08-18 11:17:24 -0700161 return (sample_index < (size_t)mNumSampleSizes && mSampleTimeEntries != NULL
162 && scale_den != 0)
163 ? (mSampleTimeEntries[sample_index].mCompositionTime * scale_num) / scale_den : 0;
Lajos Molnar599950e2014-07-17 10:52:36 -0700164 }
165
Andreas Huberc57b6792010-01-19 10:39:21 -0800166 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800167 int32_t getCompositionTimeOffset(uint32_t sampleIndex);
Andreas Huber4931bb52011-02-03 13:18:16 -0800168
Andreas Huber4678a6d2011-04-15 11:52:29 -0700169 static int CompareIncreasingTime(const void *, const void *);
170
171 void buildSampleEntriesTable();
172
Andreas Huber20111aa2009-07-14 16:56:47 -0700173 SampleTable(const SampleTable &);
174 SampleTable &operator=(const SampleTable &);
175};
176
177} // namespace android
178
179#endif // SAMPLE_TABLE_H_