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 71 enum { 72 kFlagBefore, 73 kFlagAfter, 74 kFlagClosest 75 }; 76 status_t findSampleAtTime( 77 uint32_t req_time, uint32_t *sample_index, uint32_t flags); 78 79 status_t findSyncSampleNear( 80 uint32_t start_sample_index, uint32_t *sample_index, 81 uint32_t flags); 82 83 status_t findThumbnailSample(uint32_t *sample_index); 84 85 protected: 86 ~SampleTable(); 87 88 private: 89 struct CompositionDeltaLookup; 90 91 static const uint32_t kChunkOffsetType32; 92 static const uint32_t kChunkOffsetType64; 93 static const uint32_t kSampleSizeType32; 94 static const uint32_t kSampleSizeTypeCompact; 95 96 sp<DataSource> mDataSource; 97 Mutex mLock; 98 99 off64_t mChunkOffsetOffset; 100 uint32_t mChunkOffsetType; 101 uint32_t mNumChunkOffsets; 102 103 off64_t mSampleToChunkOffset; 104 uint32_t mNumSampleToChunkOffsets; 105 106 off64_t mSampleSizeOffset; 107 uint32_t mSampleSizeFieldSize; 108 uint32_t mDefaultSampleSize; 109 uint32_t mNumSampleSizes; 110 111 uint32_t mTimeToSampleCount; 112 uint32_t *mTimeToSample; 113 114 struct SampleTimeEntry { 115 uint32_t mSampleIndex; 116 uint32_t mCompositionTime; 117 }; 118 SampleTimeEntry *mSampleTimeEntries; 119 120 uint32_t *mCompositionTimeDeltaEntries; 121 size_t mNumCompositionTimeDeltaEntries; 122 CompositionDeltaLookup *mCompositionDeltaLookup; 123 124 off64_t mSyncSampleOffset; 125 uint32_t mNumSyncSamples; 126 uint32_t *mSyncSamples; 127 size_t mLastSyncSampleIndex; 128 129 SampleIterator *mSampleIterator; 130 131 struct SampleToChunkEntry { 132 uint32_t startChunk; 133 uint32_t samplesPerChunk; 134 uint32_t chunkDesc; 135 }; 136 SampleToChunkEntry *mSampleToChunkEntries; 137 138 friend struct SampleIterator; 139 140 status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size); 141 uint32_t getCompositionTimeOffset(uint32_t sampleIndex); 142 143 static int CompareIncreasingTime(const void *, const void *); 144 145 void buildSampleEntriesTable(); 146 147 SampleTable(const SampleTable &); 148 SampleTable &operator=(const SampleTable &); 149 }; 150 151 } // namespace android 152 153 #endif // SAMPLE_TABLE_H_ 154