blob: dd688601b456d3a85a8bddb3145ef7187f331d6a [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
Marco Nelissen2a243f02018-01-30 08:29:57 -080030class DataSourceBase;
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:
Marco Nelissen2a243f02018-01-30 08:29:57 -080035 explicit SampleTable(DataSourceBase *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
zhongli.wangae7f7402014-06-12 14:30:31 +020072 // call only after getMetaDataForSample has been called successfully.
73 uint32_t getLastSampleIndexInChunk();
74
Andreas Huber20111aa2009-07-14 16:56:47 -070075 enum {
Andreas Huberabd1f4f2010-07-20 15:04:28 -070076 kFlagBefore,
77 kFlagAfter,
Chong Zhangd3e0d862017-10-03 13:17:13 -070078 kFlagClosest,
79 kFlagFrameIndex,
Andreas Huber20111aa2009-07-14 16:56:47 -070080 };
Andreas Huberabd1f4f2010-07-20 15:04:28 -070081 status_t findSampleAtTime(
Lajos Molnar599950e2014-07-17 10:52:36 -070082 uint64_t req_time, uint64_t scale_num, uint64_t scale_den,
83 uint32_t *sample_index, uint32_t flags);
Andreas Huber20111aa2009-07-14 16:56:47 -070084
Andreas Huberabd1f4f2010-07-20 15:04:28 -070085 status_t findSyncSampleNear(
86 uint32_t start_sample_index, uint32_t *sample_index,
87 uint32_t flags);
88
Andreas Huber7e04dcf2009-10-22 13:49:30 -070089 status_t findThumbnailSample(uint32_t *sample_index);
90
Andreas Huber693d2712009-08-14 14:37:10 -070091protected:
92 ~SampleTable();
93
Andreas Huber20111aa2009-07-14 16:56:47 -070094private:
Andreas Huber89aa8fe2011-09-19 12:18:47 -070095 struct CompositionDeltaLookup;
96
Andreas Huberc57b6792010-01-19 10:39:21 -080097 static const uint32_t kChunkOffsetType32;
98 static const uint32_t kChunkOffsetType64;
99 static const uint32_t kSampleSizeType32;
100 static const uint32_t kSampleSizeTypeCompact;
101
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700102 // Limit the total size of all internal tables to 200MiB.
103 static const size_t kMaxTotalSize = 200 * (1 << 20);
104
Marco Nelissen2a243f02018-01-30 08:29:57 -0800105 DataSourceBase *mDataSource;
Andreas Huber20111aa2009-07-14 16:56:47 -0700106 Mutex mLock;
107
James Dongc7fc37a2010-11-16 14:04:54 -0800108 off64_t mChunkOffsetOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700109 uint32_t mChunkOffsetType;
110 uint32_t mNumChunkOffsets;
111
James Dongc7fc37a2010-11-16 14:04:54 -0800112 off64_t mSampleToChunkOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700113 uint32_t mNumSampleToChunkOffsets;
114
James Dongc7fc37a2010-11-16 14:04:54 -0800115 off64_t mSampleSizeOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700116 uint32_t mSampleSizeFieldSize;
117 uint32_t mDefaultSampleSize;
118 uint32_t mNumSampleSizes;
119
Pawin Vongmasa70dec4d2016-04-20 15:51:48 -0700120 bool mHasTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700121 uint32_t mTimeToSampleCount;
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700122 uint32_t* mTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700123
Andreas Huber4678a6d2011-04-15 11:52:29 -0700124 struct SampleTimeEntry {
125 uint32_t mSampleIndex;
126 uint32_t mCompositionTime;
127 };
128 SampleTimeEntry *mSampleTimeEntries;
129
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800130 int32_t *mCompositionTimeDeltaEntries;
Andreas Huber4931bb52011-02-03 13:18:16 -0800131 size_t mNumCompositionTimeDeltaEntries;
Andreas Huber89aa8fe2011-09-19 12:18:47 -0700132 CompositionDeltaLookup *mCompositionDeltaLookup;
Andreas Huber4931bb52011-02-03 13:18:16 -0800133
James Dongc7fc37a2010-11-16 14:04:54 -0800134 off64_t mSyncSampleOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700135 uint32_t mNumSyncSamples;
Andreas Huber8bf59e72010-08-06 14:13:10 -0700136 uint32_t *mSyncSamples;
137 size_t mLastSyncSampleIndex;
Andreas Huber20111aa2009-07-14 16:56:47 -0700138
Andreas Huberc57b6792010-01-19 10:39:21 -0800139 SampleIterator *mSampleIterator;
140
141 struct SampleToChunkEntry {
142 uint32_t startChunk;
143 uint32_t samplesPerChunk;
144 uint32_t chunkDesc;
145 };
146 SampleToChunkEntry *mSampleToChunkEntries;
147
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700148 // Approximate size of all tables combined.
149 uint64_t mTotalSize;
150
Andreas Huberc57b6792010-01-19 10:39:21 -0800151 friend struct SampleIterator;
152
Lajos Molnar599950e2014-07-17 10:52:36 -0700153 // normally we don't round
154 inline uint64_t getSampleTime(
155 size_t sample_index, uint64_t scale_num, uint64_t scale_den) const {
Wei Jia3564c452015-08-18 11:17:24 -0700156 return (sample_index < (size_t)mNumSampleSizes && mSampleTimeEntries != NULL
157 && scale_den != 0)
158 ? (mSampleTimeEntries[sample_index].mCompositionTime * scale_num) / scale_den : 0;
Lajos Molnar599950e2014-07-17 10:52:36 -0700159 }
160
Andreas Huberc57b6792010-01-19 10:39:21 -0800161 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800162 int32_t getCompositionTimeOffset(uint32_t sampleIndex);
Andreas Huber4931bb52011-02-03 13:18:16 -0800163
Andreas Huber4678a6d2011-04-15 11:52:29 -0700164 static int CompareIncreasingTime(const void *, const void *);
165
166 void buildSampleEntriesTable();
167
Andreas Huber20111aa2009-07-14 16:56:47 -0700168 SampleTable(const SampleTable &);
169 SampleTable &operator=(const SampleTable &);
170};
171
172} // namespace android
173
174#endif // SAMPLE_TABLE_H_