Home | History | Annotate | Download | only in include
      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     sp<DataSource> mDataSource;
     99     Mutex mLock;
    100 
    101     off64_t mChunkOffsetOffset;
    102     uint32_t mChunkOffsetType;
    103     uint32_t mNumChunkOffsets;
    104 
    105     off64_t mSampleToChunkOffset;
    106     uint32_t mNumSampleToChunkOffsets;
    107 
    108     off64_t mSampleSizeOffset;
    109     uint32_t mSampleSizeFieldSize;
    110     uint32_t mDefaultSampleSize;
    111     uint32_t mNumSampleSizes;
    112 
    113     uint32_t mTimeToSampleCount;
    114     uint32_t *mTimeToSample;
    115 
    116     struct SampleTimeEntry {
    117         uint32_t mSampleIndex;
    118         uint32_t mCompositionTime;
    119     };
    120     SampleTimeEntry *mSampleTimeEntries;
    121 
    122     uint32_t *mCompositionTimeDeltaEntries;
    123     size_t mNumCompositionTimeDeltaEntries;
    124     CompositionDeltaLookup *mCompositionDeltaLookup;
    125 
    126     off64_t mSyncSampleOffset;
    127     uint32_t mNumSyncSamples;
    128     uint32_t *mSyncSamples;
    129     size_t mLastSyncSampleIndex;
    130 
    131     SampleIterator *mSampleIterator;
    132 
    133     struct SampleToChunkEntry {
    134         uint32_t startChunk;
    135         uint32_t samplesPerChunk;
    136         uint32_t chunkDesc;
    137     };
    138     SampleToChunkEntry *mSampleToChunkEntries;
    139 
    140     friend struct SampleIterator;
    141 
    142     // normally we don't round
    143     inline uint64_t getSampleTime(
    144             size_t sample_index, uint64_t scale_num, uint64_t scale_den) const {
    145         return (mSampleTimeEntries[sample_index].mCompositionTime
    146             * scale_num) / scale_den;
    147     }
    148 
    149     status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
    150     uint32_t getCompositionTimeOffset(uint32_t sampleIndex);
    151 
    152     static int CompareIncreasingTime(const void *, const void *);
    153 
    154     void buildSampleEntriesTable();
    155 
    156     SampleTable(const SampleTable &);
    157     SampleTable &operator=(const SampleTable &);
    158 };
    159 
    160 }  // namespace android
    161 
    162 #endif  // SAMPLE_TABLE_H_
    163