SampleTable.h revision 8bf59e735760af0b6a85747fd90bf8cf1e5388d7
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
28namespace android {
29
30class DataSource;
31struct SampleIterator;
32
33class SampleTable : public RefBase {
34public:
35    SampleTable(const sp<DataSource> &source);
36
37    // type can be 'stco' or 'co64'.
38    status_t setChunkOffsetParams(
39            uint32_t type, off_t data_offset, size_t data_size);
40
41    status_t setSampleToChunkParams(off_t data_offset, size_t data_size);
42
43    // type can be 'stsz' or 'stz2'.
44    status_t setSampleSizeParams(
45            uint32_t type, off_t data_offset, size_t data_size);
46
47    status_t setTimeToSampleParams(off_t data_offset, size_t data_size);
48
49    status_t setSyncSampleParams(off_t data_offset, size_t data_size);
50
51    ////////////////////////////////////////////////////////////////////////////
52
53    uint32_t countChunkOffsets() const;
54
55    uint32_t countSamples() const;
56
57    status_t getMaxSampleSize(size_t *size);
58
59    status_t getMetaDataForSample(
60            uint32_t sampleIndex,
61            off_t *offset,
62            size_t *size,
63            uint32_t *decodingTime,
64            bool *isSyncSample = NULL);
65
66    enum {
67        kFlagBefore,
68        kFlagAfter,
69        kFlagClosest
70    };
71    status_t findSampleAtTime(
72            uint32_t req_time, uint32_t *sample_index, uint32_t flags);
73
74    status_t findSyncSampleNear(
75            uint32_t start_sample_index, uint32_t *sample_index,
76            uint32_t flags);
77
78    status_t findThumbnailSample(uint32_t *sample_index);
79
80protected:
81    ~SampleTable();
82
83private:
84    static const uint32_t kChunkOffsetType32;
85    static const uint32_t kChunkOffsetType64;
86    static const uint32_t kSampleSizeType32;
87    static const uint32_t kSampleSizeTypeCompact;
88
89    sp<DataSource> mDataSource;
90    Mutex mLock;
91
92    off_t mChunkOffsetOffset;
93    uint32_t mChunkOffsetType;
94    uint32_t mNumChunkOffsets;
95
96    off_t mSampleToChunkOffset;
97    uint32_t mNumSampleToChunkOffsets;
98
99    off_t mSampleSizeOffset;
100    uint32_t mSampleSizeFieldSize;
101    uint32_t mDefaultSampleSize;
102    uint32_t mNumSampleSizes;
103
104    uint32_t mTimeToSampleCount;
105    uint32_t *mTimeToSample;
106
107    off_t mSyncSampleOffset;
108    uint32_t mNumSyncSamples;
109    uint32_t *mSyncSamples;
110    size_t mLastSyncSampleIndex;
111
112    SampleIterator *mSampleIterator;
113
114    struct SampleToChunkEntry {
115        uint32_t startChunk;
116        uint32_t samplesPerChunk;
117        uint32_t chunkDesc;
118    };
119    SampleToChunkEntry *mSampleToChunkEntries;
120
121    friend struct SampleIterator;
122
123    status_t getSampleSize_l(uint32_t sample_index, size_t *sample_size);
124
125    SampleTable(const SampleTable &);
126    SampleTable &operator=(const SampleTable &);
127};
128
129}  // namespace android
130
131#endif  // SAMPLE_TABLE_H_
132