blob: 50c96bb7dce5651a1395c7dd18034d09944bc8b0 [file] [log] [blame]
Andreas Huber84066782011-08-16 09:34:26 -07001/*
2 * Copyright (C) 2012 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 PARSER_H_
18
19#define PARSER_H_
20
21#include <media/stagefright/foundation/AHandler.h>
22#include <utils/Vector.h>
23
24namespace android {
25
26struct ABuffer;
27
28struct Parser : public AHandler {
29 struct Source : public RefBase {
30 Source() {}
31
32 virtual ssize_t readAt(off64_t offset, void *data, size_t size) = 0;
33
34 protected:
35 virtual ~Source() {}
36
37 private:
38 DISALLOW_EVIL_CONSTRUCTORS(Source);
39 };
40
41 Parser();
42
43 void start(const char *filename);
44 void start(const sp<Source> &source);
45
46 sp<AMessage> getFormat(bool audio);
47 status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
48
49 virtual void onMessageReceived(const sp<AMessage> &msg);
50
51protected:
52 virtual ~Parser();
53
54private:
55 enum {
56 kWhatStart,
57 kWhatProceed,
58 kWhatReadMore,
59 kWhatGetFormat,
60 kWhatDequeueAccessUnit,
61 };
62
63 struct TrackFragment;
64 struct DynamicTrackFragment;
65 struct StaticTrackFragment;
66
67 struct DispatchEntry {
68 uint32_t mType;
69 uint32_t mParentType;
70 status_t (Parser::*mHandler)(uint32_t, size_t, uint64_t);
71 };
72
73 struct Container {
74 uint64_t mBytesRemaining;
75 uint32_t mType;
76 bool mExtendsToEOF;
77 };
78
79 struct SampleDescription {
80 uint32_t mType;
81 uint16_t mDataRefIndex;
82
83 sp<AMessage> mFormat;
84 };
85
86 struct SampleInfo {
87 off64_t mOffset;
88 size_t mSize;
89 uint32_t mPresentationTime;
90 size_t mSampleDescIndex;
91 uint32_t mFlags;
92 };
93
94 struct MediaDataInfo {
95 sp<ABuffer> mBuffer;
96 off64_t mOffset;
97 };
98
99 struct TrackInfo {
100 enum Flags {
101 kTrackEnabled = 0x01,
102 kTrackInMovie = 0x02,
103 kTrackInPreview = 0x04,
104 };
105
106 uint32_t mTrackID;
107 uint32_t mFlags;
108 uint32_t mDuration; // This is the duration in terms of movie timescale!
109
110 uint32_t mMediaTimeScale;
111
112 uint32_t mMediaHandlerType;
113 Vector<SampleDescription> mSampleDescs;
114
115 // from track extends:
116 uint32_t mDefaultSampleDescriptionIndex;
117 uint32_t mDefaultSampleDuration;
118 uint32_t mDefaultSampleSize;
119 uint32_t mDefaultSampleFlags;
120
121 uint32_t mDecodingTime;
122
123 sp<StaticTrackFragment> mStaticFragment;
124 List<sp<TrackFragment> > mFragments;
125 };
126
127 struct TrackFragmentHeaderInfo {
128 enum Flags {
129 kBaseDataOffsetPresent = 0x01,
130 kSampleDescriptionIndexPresent = 0x02,
131 kDefaultSampleDurationPresent = 0x08,
132 kDefaultSampleSizePresent = 0x10,
133 kDefaultSampleFlagsPresent = 0x20,
134 kDurationIsEmpty = 0x10000,
135 };
136
137 uint32_t mTrackID;
138 uint32_t mFlags;
139 uint64_t mBaseDataOffset;
140 uint32_t mSampleDescriptionIndex;
141 uint32_t mDefaultSampleDuration;
142 uint32_t mDefaultSampleSize;
143 uint32_t mDefaultSampleFlags;
144
145 uint64_t mDataOffset;
146 };
147
148 static const DispatchEntry kDispatchTable[];
149
150 sp<Source> mSource;
151 off_t mBufferPos;
152 bool mSuspended;
153 sp<ABuffer> mBuffer;
154 Vector<Container> mStack;
155 KeyedVector<uint32_t, TrackInfo> mTracks; // TrackInfo by trackID
156 Vector<MediaDataInfo> mMediaData;
157
158 uint32_t mCurrentTrackID;
159
160 TrackFragmentHeaderInfo mTrackFragmentHeaderInfo;
161
162 status_t onProceed();
163 status_t onDequeueAccessUnit(size_t trackIndex, sp<ABuffer> *accessUnit);
164
165 void enter(uint32_t type, uint64_t size);
166
167 uint16_t readU16(size_t offset);
168 uint32_t readU32(size_t offset);
169 uint64_t readU64(size_t offset);
170 void skip(off_t distance);
171 status_t need(size_t size);
172 bool fitsContainer(uint64_t size) const;
173
174 status_t parseTrackHeader(
175 uint32_t type, size_t offset, uint64_t size);
176
177 status_t parseMediaHeader(
178 uint32_t type, size_t offset, uint64_t size);
179
180 status_t parseMediaHandler(
181 uint32_t type, size_t offset, uint64_t size);
182
183 status_t parseTrackExtends(
184 uint32_t type, size_t offset, uint64_t size);
185
186 status_t parseTrackFragmentHeader(
187 uint32_t type, size_t offset, uint64_t size);
188
189 status_t parseTrackFragmentRun(
190 uint32_t type, size_t offset, uint64_t size);
191
192 status_t parseVisualSampleEntry(
193 uint32_t type, size_t offset, uint64_t size);
194
195 status_t parseAudioSampleEntry(
196 uint32_t type, size_t offset, uint64_t size);
197
198 status_t parseSampleSizes(
199 uint32_t type, size_t offset, uint64_t size);
200
201 status_t parseCompactSampleSizes(
202 uint32_t type, size_t offset, uint64_t size);
203
204 status_t parseSampleToChunk(
205 uint32_t type, size_t offset, uint64_t size);
206
207 status_t parseChunkOffsets(
208 uint32_t type, size_t offset, uint64_t size);
209
210 status_t parseChunkOffsets64(
211 uint32_t type, size_t offset, uint64_t size);
212
213 status_t parseAVCCodecSpecificData(
214 uint32_t type, size_t offset, uint64_t size);
215
216 status_t parseESDSCodecSpecificData(
217 uint32_t type, size_t offset, uint64_t size);
218
219 status_t parseMediaData(
220 uint32_t type, size_t offset, uint64_t size);
221
222 TrackInfo *editTrack(uint32_t trackID, bool createIfNecessary = false);
223
224 ssize_t findTrack(bool wantAudio) const;
225
226 status_t makeAccessUnit(
227 TrackInfo *info,
228 const SampleInfo &sample,
229 const MediaDataInfo &mdatInfo,
230 sp<ABuffer> *accessUnit);
231
232 status_t getSample(
233 TrackInfo *info,
234 sp<TrackFragment> *fragment,
235 SampleInfo *sampleInfo);
236
237 static int CompareSampleLocation(
238 const SampleInfo &sample, const MediaDataInfo &mdatInfo);
239
240 void resumeIfNecessary();
241
242 void copyBuffer(
243 sp<ABuffer> *dst,
244 size_t offset, uint64_t size, size_t extra = 0) const;
245
246 DISALLOW_EVIL_CONSTRUCTORS(Parser);
247};
248
249} // namespace android
250
251#endif // PARSER_H_
252