1 /* 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> 25 #include <utils/RefBase.h> 26 #include <utils/threads.h> 27 28 namespace android { 29 30 class DataSource; 31 struct SampleIterator; 32 33 class SampleTable : public RefBase { 34 public: 35 SampleTable(const sp<DataSource> &source); 36 37 bool isValid() const; 38 39 // type can be 'stco' or 'co64'. 40 status_t setChunkOffsetParams( 41 uint32_t type, off64_t data_offset, size_t data_size); 42 43 status_t setSampleToChunkParams(off64_t data_offset, size_t data_size); 44 45 // type can be 'stsz' or 'stz2'. 46 status_t setSampleSizeParams( 47 uint32_t type, off64_t data_offset, size_t data_size); 48 49 status_t setTimeToSampleParams(off64_t data_offset, size_t data_size); 50 51 status_t setCompositionTimeToSampleParams( 52 off64_t data_offset, size_t data_size); 53 54 status_t setSyncSampleParams(off64_t data_offset, size_t data_size); 55 56 //////////////////////////////////////////////////////////////////////////// 57 58 uint32_t countChunkOffsets() const; 59 60 uint32_t countSamples() const; 61 62 status_t getMaxSampleSize(size_t *size); 63 64 status_t getMetaDataForSample( 65 uint32_t sampleIndex, 66 off64_t *offset, 67 size_t *size, 68 uint32_t *compositionTime, 69 bool *isSyncSample = NULL, 70 uint32_t *sampleDuration = NULL); 71 72 enum { 73 kFlagBefore, 74 kFlagAfter, 75 kFlagClosest 76 }; 77 status_t findSampleAtTime( 78 uint64_t req_time, uint64_t scale_num, uint64_t scale_den, 79 uint32_t *sample_index, uint32_t flags); 80 81 status_t findSyncSampleNear( 82 uint32_t start_sample_index, uint32_t *sample_index, 83 uint32_t flags); 84 85 status_t findThumbnailSample(uint32_t *sample_index); 86 87 protected: 88 ~SampleTable(); 89 90 private: 91 struct CompositionDeltaLookup; 92 93 static const uint32_t kChunkOffsetType32; 94 static const uint32_t kChunkOffsetType64; 95 static const uint32_t kSampleSizeType32; 96 static const uint32_t kSampleSizeTypeCompact; 97 98 // Limit the total size of all internal tables to 200MiB. 99 static const size_t kMaxTotalSize = 200 * (1 << 20); 100 101 sp<DataSource> mDataSource; 102 Mutex mLock; 103 104 off64_t mChunkOffsetOffset; 105 uint32_t mChunkOffsetType; 106 uint32_t mNumChunkOffsets; 107 108 off64_t mSampleToChunkOffset; 109 uint32_t mNumSampleToChunkOffsets; 110 111 off64_t mSampleSizeOffset; 112 uint32_t mSampleSizeFieldSize; 113 uint32_t mDefaultSampleSize; 114 uint32_t mNumSampleSizes; 115 116 bool mHasTimeToSample; 117 uint32_t mTimeToSampleCount; 118 uint32_t* mTimeToSample; 119 120 struct SampleTimeEntry { 121 uint32_t mSampleIndex; 122 uint32_t mCompositionTime; 123 }; 124 SampleTimeEntry *mSampleTimeEntries; 125 126 int32_t *mCompositionTimeDeltaEntries; 127 size_t mNumCompositionTimeDeltaEntries; 128 CompositionDeltaLookup *mCompositionDeltaLookup; 129 130 off64_t mSyncSampleOffset; 131 uint32_t mNumSyncSamples; 132 uint32_t *mSyncSamples; 133 size_t mLastSyncSampleIndex; 134 135 SampleIterator *mSampleIterator; 136 137 struct SampleToChunkEntry { 138 uint32_t startChunk; 139 uint32_t samplesPerChunk; 140 uint32_t chunkDesc; 141 }; 142 SampleToChunkEntry *mSampleToChunkEntries; 143 144 // Approximate size of all tables combined. 145 uint64_t mTotalSize; 146 147 friend struct SampleIterator; 148 149 // normally we don't round 150 inline uint64_t getSampleTime( 151 size_t sample_index, uint64_t scale_num, uint64_t scale_den) const { 152 return (sample_index < (size_t)mNumSampleSizes && mSampleTimeEntries != NULL 153 && scale_den != 0) 154 ? (mSampleTimeEntries[sample_index].mCompositionTime * scale_num) / scale_den : 0; 155 } 156 157 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size); 158 int32_t getCompositionTimeOffset(uint32_t sampleIndex); 159 160 static int CompareIncreasingTime(const void *, const void *); 161 162 void buildSampleEntriesTable(); 163 164 SampleTable(const SampleTable &); 165 SampleTable &operator=(const SampleTable &); 166 }; 167 168 } // namespace android 169 170 #endif // SAMPLE_TABLE_H_ 171