blob: d4b5dc84c1dae0f1948b1dfd0083477b82315525 [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,
Andreas Huber4678a6d2011-04-15 11:52:29 -070069 uint32_t *compositionTime,
Robert Shih17005652014-04-10 17:30:21 -070070 bool *isSyncSample = NULL,
71 uint32_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
Andreas Huber693d2712009-08-14 14:37:10 -070092protected:
93 ~SampleTable();
94
Andreas Huber20111aa2009-07-14 16:56:47 -070095private:
Andreas Huber89aa8fe2011-09-19 12:18:47 -070096 struct CompositionDeltaLookup;
97
Andreas Huberc57b6792010-01-19 10:39:21 -080098 static const uint32_t kChunkOffsetType32;
99 static const uint32_t kChunkOffsetType64;
100 static const uint32_t kSampleSizeType32;
101 static const uint32_t kSampleSizeTypeCompact;
102
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700103 // Limit the total size of all internal tables to 200MiB.
104 static const size_t kMaxTotalSize = 200 * (1 << 20);
105
Marco Nelissencec44d02018-06-17 22:21:09 -0700106 DataSourceHelper *mDataSource;
Andreas Huber20111aa2009-07-14 16:56:47 -0700107 Mutex mLock;
108
James Dongc7fc37a2010-11-16 14:04:54 -0800109 off64_t mChunkOffsetOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700110 uint32_t mChunkOffsetType;
111 uint32_t mNumChunkOffsets;
112
James Dongc7fc37a2010-11-16 14:04:54 -0800113 off64_t mSampleToChunkOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700114 uint32_t mNumSampleToChunkOffsets;
115
James Dongc7fc37a2010-11-16 14:04:54 -0800116 off64_t mSampleSizeOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700117 uint32_t mSampleSizeFieldSize;
118 uint32_t mDefaultSampleSize;
119 uint32_t mNumSampleSizes;
120
Pawin Vongmasa70dec4d2016-04-20 15:51:48 -0700121 bool mHasTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700122 uint32_t mTimeToSampleCount;
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700123 uint32_t* mTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700124
Andreas Huber4678a6d2011-04-15 11:52:29 -0700125 struct SampleTimeEntry {
126 uint32_t mSampleIndex;
127 uint32_t mCompositionTime;
128 };
129 SampleTimeEntry *mSampleTimeEntries;
130
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800131 int32_t *mCompositionTimeDeltaEntries;
Andreas Huber4931bb52011-02-03 13:18:16 -0800132 size_t mNumCompositionTimeDeltaEntries;
Andreas Huber89aa8fe2011-09-19 12:18:47 -0700133 CompositionDeltaLookup *mCompositionDeltaLookup;
Andreas Huber4931bb52011-02-03 13:18:16 -0800134
James Dongc7fc37a2010-11-16 14:04:54 -0800135 off64_t mSyncSampleOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700136 uint32_t mNumSyncSamples;
Andreas Huber8bf59e72010-08-06 14:13:10 -0700137 uint32_t *mSyncSamples;
138 size_t mLastSyncSampleIndex;
Andreas Huber20111aa2009-07-14 16:56:47 -0700139
Andreas Huberc57b6792010-01-19 10:39:21 -0800140 SampleIterator *mSampleIterator;
141
142 struct SampleToChunkEntry {
143 uint32_t startChunk;
144 uint32_t samplesPerChunk;
145 uint32_t chunkDesc;
146 };
147 SampleToChunkEntry *mSampleToChunkEntries;
148
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700149 // Approximate size of all tables combined.
150 uint64_t mTotalSize;
151
Andreas Huberc57b6792010-01-19 10:39:21 -0800152 friend struct SampleIterator;
153
Lajos Molnar599950e2014-07-17 10:52:36 -0700154 // normally we don't round
155 inline uint64_t getSampleTime(
156 size_t sample_index, uint64_t scale_num, uint64_t scale_den) const {
Wei Jia3564c452015-08-18 11:17:24 -0700157 return (sample_index < (size_t)mNumSampleSizes && mSampleTimeEntries != NULL
158 && scale_den != 0)
159 ? (mSampleTimeEntries[sample_index].mCompositionTime * scale_num) / scale_den : 0;
Lajos Molnar599950e2014-07-17 10:52:36 -0700160 }
161
Andreas Huberc57b6792010-01-19 10:39:21 -0800162 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800163 int32_t getCompositionTimeOffset(uint32_t sampleIndex);
Andreas Huber4931bb52011-02-03 13:18:16 -0800164
Andreas Huber4678a6d2011-04-15 11:52:29 -0700165 static int CompareIncreasingTime(const void *, const void *);
166
167 void buildSampleEntriesTable();
168
Andreas Huber20111aa2009-07-14 16:56:47 -0700169 SampleTable(const SampleTable &);
170 SampleTable &operator=(const SampleTable &);
171};
172
173} // namespace android
174
175#endif // SAMPLE_TABLE_H_