blob: 0d8d0f574b6be4aae9c201a6fd1c6e0e81dd6c85 [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 {
Andreas Huberbd828232012-08-13 10:59:48 -070074 uint64_t mOffset;
Andreas Huber84066782011-08-16 09:34:26 -070075 uint64_t mBytesRemaining;
76 uint32_t mType;
77 bool mExtendsToEOF;
78 };
79
80 struct SampleDescription {
81 uint32_t mType;
82 uint16_t mDataRefIndex;
83
84 sp<AMessage> mFormat;
85 };
86
87 struct SampleInfo {
88 off64_t mOffset;
89 size_t mSize;
90 uint32_t mPresentationTime;
91 size_t mSampleDescIndex;
92 uint32_t mFlags;
93 };
94
95 struct MediaDataInfo {
96 sp<ABuffer> mBuffer;
97 off64_t mOffset;
98 };
99
100 struct TrackInfo {
101 enum Flags {
102 kTrackEnabled = 0x01,
103 kTrackInMovie = 0x02,
104 kTrackInPreview = 0x04,
105 };
106
107 uint32_t mTrackID;
108 uint32_t mFlags;
109 uint32_t mDuration; // This is the duration in terms of movie timescale!
110
111 uint32_t mMediaTimeScale;
112
113 uint32_t mMediaHandlerType;
114 Vector<SampleDescription> mSampleDescs;
115
116 // from track extends:
117 uint32_t mDefaultSampleDescriptionIndex;
118 uint32_t mDefaultSampleDuration;
119 uint32_t mDefaultSampleSize;
120 uint32_t mDefaultSampleFlags;
121
122 uint32_t mDecodingTime;
123
124 sp<StaticTrackFragment> mStaticFragment;
125 List<sp<TrackFragment> > mFragments;
126 };
127
128 struct TrackFragmentHeaderInfo {
129 enum Flags {
130 kBaseDataOffsetPresent = 0x01,
131 kSampleDescriptionIndexPresent = 0x02,
132 kDefaultSampleDurationPresent = 0x08,
133 kDefaultSampleSizePresent = 0x10,
134 kDefaultSampleFlagsPresent = 0x20,
135 kDurationIsEmpty = 0x10000,
136 };
137
138 uint32_t mTrackID;
139 uint32_t mFlags;
140 uint64_t mBaseDataOffset;
141 uint32_t mSampleDescriptionIndex;
142 uint32_t mDefaultSampleDuration;
143 uint32_t mDefaultSampleSize;
144 uint32_t mDefaultSampleFlags;
145
146 uint64_t mDataOffset;
147 };
148
149 static const DispatchEntry kDispatchTable[];
150
151 sp<Source> mSource;
152 off_t mBufferPos;
153 bool mSuspended;
154 sp<ABuffer> mBuffer;
155 Vector<Container> mStack;
156 KeyedVector<uint32_t, TrackInfo> mTracks; // TrackInfo by trackID
157 Vector<MediaDataInfo> mMediaData;
158
159 uint32_t mCurrentTrackID;
160
Andreas Huberc7708552012-08-09 16:24:54 -0700161 status_t mFinalResult;
162
Andreas Huber84066782011-08-16 09:34:26 -0700163 TrackFragmentHeaderInfo mTrackFragmentHeaderInfo;
164
165 status_t onProceed();
166 status_t onDequeueAccessUnit(size_t trackIndex, sp<ABuffer> *accessUnit);
167
Andreas Huberbd828232012-08-13 10:59:48 -0700168 void enter(off64_t offset, uint32_t type, uint64_t size);
Andreas Huber84066782011-08-16 09:34:26 -0700169
170 uint16_t readU16(size_t offset);
171 uint32_t readU32(size_t offset);
172 uint64_t readU64(size_t offset);
173 void skip(off_t distance);
174 status_t need(size_t size);
175 bool fitsContainer(uint64_t size) const;
176
177 status_t parseTrackHeader(
178 uint32_t type, size_t offset, uint64_t size);
179
180 status_t parseMediaHeader(
181 uint32_t type, size_t offset, uint64_t size);
182
183 status_t parseMediaHandler(
184 uint32_t type, size_t offset, uint64_t size);
185
186 status_t parseTrackExtends(
187 uint32_t type, size_t offset, uint64_t size);
188
189 status_t parseTrackFragmentHeader(
190 uint32_t type, size_t offset, uint64_t size);
191
192 status_t parseTrackFragmentRun(
193 uint32_t type, size_t offset, uint64_t size);
194
195 status_t parseVisualSampleEntry(
196 uint32_t type, size_t offset, uint64_t size);
197
198 status_t parseAudioSampleEntry(
199 uint32_t type, size_t offset, uint64_t size);
200
201 status_t parseSampleSizes(
202 uint32_t type, size_t offset, uint64_t size);
203
204 status_t parseCompactSampleSizes(
205 uint32_t type, size_t offset, uint64_t size);
206
207 status_t parseSampleToChunk(
208 uint32_t type, size_t offset, uint64_t size);
209
210 status_t parseChunkOffsets(
211 uint32_t type, size_t offset, uint64_t size);
212
213 status_t parseChunkOffsets64(
214 uint32_t type, size_t offset, uint64_t size);
215
216 status_t parseAVCCodecSpecificData(
217 uint32_t type, size_t offset, uint64_t size);
218
219 status_t parseESDSCodecSpecificData(
220 uint32_t type, size_t offset, uint64_t size);
221
222 status_t parseMediaData(
223 uint32_t type, size_t offset, uint64_t size);
224
225 TrackInfo *editTrack(uint32_t trackID, bool createIfNecessary = false);
226
227 ssize_t findTrack(bool wantAudio) const;
228
229 status_t makeAccessUnit(
230 TrackInfo *info,
231 const SampleInfo &sample,
232 const MediaDataInfo &mdatInfo,
233 sp<ABuffer> *accessUnit);
234
235 status_t getSample(
236 TrackInfo *info,
237 sp<TrackFragment> *fragment,
238 SampleInfo *sampleInfo);
239
240 static int CompareSampleLocation(
241 const SampleInfo &sample, const MediaDataInfo &mdatInfo);
242
243 void resumeIfNecessary();
244
245 void copyBuffer(
246 sp<ABuffer> *dst,
247 size_t offset, uint64_t size, size_t extra = 0) const;
248
249 DISALLOW_EVIL_CONSTRUCTORS(Parser);
250};
251
252} // namespace android
253
254#endif // PARSER_H_
255