blob: eb1a6747700c30c37c68ee697f73b6f920bb558d [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,
75 kFlagClosest
Andreas Huber20111aa2009-07-14 16:56:47 -070076 };
Andreas Huberabd1f4f2010-07-20 15:04:28 -070077 status_t findSampleAtTime(
Lajos Molnar599950e2014-07-17 10:52:36 -070078 uint64_t req_time, uint64_t scale_num, uint64_t scale_den,
79 uint32_t *sample_index, uint32_t flags);
Andreas Huber20111aa2009-07-14 16:56:47 -070080
Andreas Huberabd1f4f2010-07-20 15:04:28 -070081 status_t findSyncSampleNear(
82 uint32_t start_sample_index, uint32_t *sample_index,
83 uint32_t flags);
84
Andreas Huber7e04dcf2009-10-22 13:49:30 -070085 status_t findThumbnailSample(uint32_t *sample_index);
86
Andreas Huber693d2712009-08-14 14:37:10 -070087protected:
88 ~SampleTable();
89
Andreas Huber20111aa2009-07-14 16:56:47 -070090private:
Andreas Huber89aa8fe2011-09-19 12:18:47 -070091 struct CompositionDeltaLookup;
92
Andreas Huberc57b6792010-01-19 10:39:21 -080093 static const uint32_t kChunkOffsetType32;
94 static const uint32_t kChunkOffsetType64;
95 static const uint32_t kSampleSizeType32;
96 static const uint32_t kSampleSizeTypeCompact;
97
Pawin Vongmasa583a0122016-06-21 19:10:21 -070098 // Limit the total size of all internal tables to 200MiB.
99 static const size_t kMaxTotalSize = 200 * (1 << 20);
100
Andreas Huber693d2712009-08-14 14:37:10 -0700101 sp<DataSource> mDataSource;
Andreas Huber20111aa2009-07-14 16:56:47 -0700102 Mutex mLock;
103
James Dongc7fc37a2010-11-16 14:04:54 -0800104 off64_t mChunkOffsetOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700105 uint32_t mChunkOffsetType;
106 uint32_t mNumChunkOffsets;
107
James Dongc7fc37a2010-11-16 14:04:54 -0800108 off64_t mSampleToChunkOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700109 uint32_t mNumSampleToChunkOffsets;
110
James Dongc7fc37a2010-11-16 14:04:54 -0800111 off64_t mSampleSizeOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700112 uint32_t mSampleSizeFieldSize;
113 uint32_t mDefaultSampleSize;
114 uint32_t mNumSampleSizes;
115
Pawin Vongmasa70dec4d2016-04-20 15:51:48 -0700116 bool mHasTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700117 uint32_t mTimeToSampleCount;
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700118 uint32_t* mTimeToSample;
Andreas Huber20111aa2009-07-14 16:56:47 -0700119
Andreas Huber4678a6d2011-04-15 11:52:29 -0700120 struct SampleTimeEntry {
121 uint32_t mSampleIndex;
122 uint32_t mCompositionTime;
123 };
124 SampleTimeEntry *mSampleTimeEntries;
125
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800126 int32_t *mCompositionTimeDeltaEntries;
Andreas Huber4931bb52011-02-03 13:18:16 -0800127 size_t mNumCompositionTimeDeltaEntries;
Andreas Huber89aa8fe2011-09-19 12:18:47 -0700128 CompositionDeltaLookup *mCompositionDeltaLookup;
Andreas Huber4931bb52011-02-03 13:18:16 -0800129
James Dongc7fc37a2010-11-16 14:04:54 -0800130 off64_t mSyncSampleOffset;
Andreas Huber20111aa2009-07-14 16:56:47 -0700131 uint32_t mNumSyncSamples;
Andreas Huber8bf59e72010-08-06 14:13:10 -0700132 uint32_t *mSyncSamples;
133 size_t mLastSyncSampleIndex;
Andreas Huber20111aa2009-07-14 16:56:47 -0700134
Andreas Huberc57b6792010-01-19 10:39:21 -0800135 SampleIterator *mSampleIterator;
136
137 struct SampleToChunkEntry {
138 uint32_t startChunk;
139 uint32_t samplesPerChunk;
140 uint32_t chunkDesc;
141 };
142 SampleToChunkEntry *mSampleToChunkEntries;
143
Pawin Vongmasa583a0122016-06-21 19:10:21 -0700144 // Approximate size of all tables combined.
145 uint64_t mTotalSize;
146
Andreas Huberc57b6792010-01-19 10:39:21 -0800147 friend struct SampleIterator;
148
Lajos Molnar599950e2014-07-17 10:52:36 -0700149 // normally we don't round
150 inline uint64_t getSampleTime(
151 size_t sample_index, uint64_t scale_num, uint64_t scale_den) const {
Wei Jia3564c452015-08-18 11:17:24 -0700152 return (sample_index < (size_t)mNumSampleSizes && mSampleTimeEntries != NULL
153 && scale_den != 0)
154 ? (mSampleTimeEntries[sample_index].mCompositionTime * scale_num) / scale_den : 0;
Lajos Molnar599950e2014-07-17 10:52:36 -0700155 }
156
Andreas Huberc57b6792010-01-19 10:39:21 -0800157 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
Marco Nelissenb1dc9e02016-02-17 10:37:29 -0800158 int32_t getCompositionTimeOffset(uint32_t sampleIndex);
Andreas Huber4931bb52011-02-03 13:18:16 -0800159
Andreas Huber4678a6d2011-04-15 11:52:29 -0700160 static int CompareIncreasingTime(const void *, const void *);
161
162 void buildSampleEntriesTable();
163
Andreas Huber20111aa2009-07-14 16:56:47 -0700164 SampleTable(const SampleTable &);
165 SampleTable &operator=(const SampleTable &);
166};
167
168} // namespace android
169
170#endif // SAMPLE_TABLE_H_