-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathatomfactory.cc
385 lines (335 loc) · 11.4 KB
/
atomfactory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// Created by wlanjie on 2018/2/7.
//
#include "atomfactory.h"
#include "sample_entry.h"
#include "moov.h"
#include "mvhd.h"
#include "hdlr.h"
#include "stz2.h"
#include "avcc.h"
#include "ftyp.h"
#include "vmhd.h"
#include "dref.h"
#include "url.h"
#include "smhd.h"
namespace mp4 {
AtomFactory::~AtomFactory() {
m_TypeHandlers.deleteReferences();
}
Result AtomFactory::addTypeHandler(TypeHandler *handler) {
return m_TypeHandlers.add(handler);
}
Result AtomFactory::removeTypeHandler(TypeHandler *handler) {
return m_TypeHandlers.remove(handler);
}
Result AtomFactory::createAtomFromStream(ByteStream &stream, LargeSize &bytesAvailable, Atom *&atom) {
Result result;
atom = nullptr;
if (bytesAvailable < 8) {
return ERROR_EOS;
}
Position start;
stream.tell(start);
UI32 size32;
result = stream.readUI32(size32);
if (FAILED(result)) {
stream.seek(start);
return result;
}
UI64 size = size32;
Atom::Type type;
result = stream.readUI32(type);
if (FAILED(result)) {
stream.seek(start);
return result;
}
bool atomIsLarge = false;
bool force64 = false;
if (size == 0) {
LargeSize streamSize = 0;
stream.getSize(streamSize);
if (streamSize >= start) {
size = streamSize - start;
}
} else if (size == 1) {
atomIsLarge = true;
if (bytesAvailable < 16) {
stream.seek(start);
return ERROR_INVALID_FORMAT;
}
stream.readUI64(size);
if (size <= 0xFFFFFFFF) {
force64 = true;
}
}
if ((size > 0 && size < 8) || size > bytesAvailable) {
stream.seek(start);
return ERROR_INVALID_FORMAT;
}
result = createAtomFromStream(stream, type, size32, size, atom);
if (FAILED(result)) {
return result;
}
if (!atom) {
unsigned int payloadOffset = 8;
if (atomIsLarge) {
payloadOffset += 8;
}
stream.seek(start + payloadOffset);
return ERROR_INVALID_FORMAT;
}
if (force64) {
atom->setSize32(1);
atom->setSize64(size);
}
bytesAvailable -= size;
result = stream.seek(start + size);
if (FAILED(result)) {
atom = nullptr;
}
return result;
}
Result AtomFactory::createAtomFromStream(ByteStream &stream, UI32 type, UI32 size32, UI64 size64, Atom *&atom) {
bool atomIsLarge = (size32 == 1);
bool force64 = (size32 == 1 && ((size64 >> 32) == 0));
if (getContext() == ATOM_TYPE_STSD) {
if (atomIsLarge) {
return ERROR_INVALID_FORMAT;
}
switch (type) {
case ATOM_TYPE_MP4A:
atom = new Mp4aSampleEntry(size32, stream, *this);
break;
case ATOM_TYPE_AVC1:
case ATOM_TYPE_AVC2:
case ATOM_TYPE_AVC3:
case ATOM_TYPE_AVC4:
case ATOM_TYPE_DVAV:
case ATOM_TYPE_DVA1:
atom = new AvcSampleEntry(type, size32, stream, *this);
break;
case ATOM_TYPE_HEV1:
case ATOM_TYPE_HVC1:
case ATOM_TYPE_DVHE:
case ATOM_TYPE_DVH1:
// h265
break;
case ATOM_TYPE_ALAC:
case ATOM_TYPE_AC_3:
case ATOM_TYPE_EC_3:
case ATOM_TYPE_DTSC:
case ATOM_TYPE_DTSH:
case ATOM_TYPE_DTSL:
case ATOM_TYPE_DTSE:
atom = new AudioSampleEntry(type, size32, stream, *this);
break;
}
} else {
switch(type) {
case ATOM_TYPE_FTYP:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Ftyp::create(size32, stream);
break;
case ATOM_TYPE_MOOV:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Moov::create(size32, stream, *this);
break;
case ATOM_TYPE_MVHD:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Mvhd::create(size32, stream);
break;
case ATOM_TYPE_TRAK:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Trak::create(size32, stream, *this);
break;
case ATOM_TYPE_HDLR:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Hdlr::create(size32, stream);
break;
case ATOM_TYPE_TKHD:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Tkhd::create(size32, stream);
break;
case ATOM_TYPE_MDHD:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Mdhd::create(size32, stream);
break;
case ATOM_TYPE_VMHD:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Vmhd::create(size32, stream);
break;
case ATOM_TYPE_STSD:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stsd::create(size32, stream, *this);
break;
case ATOM_TYPE_STSC:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stsc::create(size32, stream);
break;
case ATOM_TYPE_STCO:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stco::create(size32, stream);
break;
case ATOM_TYPE_CO64:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Co64::create(size32, stream);
break;
case ATOM_TYPE_STSZ:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stsz::create(size32, stream);
break;
case ATOM_TYPE_STZ2:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stz2::create(size32, stream);
break;
case ATOM_TYPE_STTS:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stts::create(size32, stream);
break;
case ATOM_TYPE_CTTS:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Ctts::create(size32, stream);
break;
case ATOM_TYPE_STSS:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Stss::create(size32, stream);
break;
case ATOM_TYPE_AVCC:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Avcc::create(size32, stream);
break;
case ATOM_TYPE_AVCE:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Avcc::create(size32, stream);
if (atom) {
atom->setType(ATOM_TYPE_AVCE);
}
break;
case ATOM_TYPE_HVCC:
// HvccAtom
break;
case ATOM_TYPE_HVCE:
// HvccAtom
break;
case ATOM_TYPE_DREF:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Dref::create(size32, stream, *this);
break;
case ATOM_TYPE_URL:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Url::create(size32, stream);
break;
case ATOM_TYPE_SMHD:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Smhd::create(size32, stream);
break;
// container atoms
case ATOM_TYPE_MOOF:
case ATOM_TYPE_MVEX:
case ATOM_TYPE_TRAF:
case ATOM_TYPE_TREF:
case ATOM_TYPE_MFRA:
case ATOM_TYPE_HNTI:
case ATOM_TYPE_STBL:
case ATOM_TYPE_MDIA:
case ATOM_TYPE_DINF:
case ATOM_TYPE_MINF:
case ATOM_TYPE_SCHI:
case ATOM_TYPE_SINF:
case ATOM_TYPE_UDTA:
case ATOM_TYPE_ILST:
case ATOM_TYPE_EDTS:
case ATOM_TYPE_MDRI:
case ATOM_TYPE_WAVE:
if (atomIsLarge) return ERROR_INVALID_FORMAT;
atom = Container::Create(type, size64, false, force64, stream, *this);
break;
// containers, only at the top
case ATOM_TYPE_MARL:
if (getContext() == 0) {
atom = Container::Create(type, size64, false, force64, stream, *this);
}
break;
// full container atoms
case ATOM_TYPE_META:
case ATOM_TYPE_ODRM:
case ATOM_TYPE_ODKM:
atom = Container::Create(type, size64, true, force64, stream, *this);
break;
case ATOM_TYPE_FREE:
case ATOM_TYPE_WIDE:
case ATOM_TYPE_MDAT:
// generic atoms
break;
default:
List<TypeHandler>::Item* item = m_TypeHandlers.firstItem();
while (item) {
TypeHandler* handler = item->getData();
if (SUCCEEDED(handler->createAtom(type, size32, stream, getContext(), atom))) {
break;
}
item = item->getNext();
}
break;
}
}
return SUCCESS;
}
Result AtomFactory::createAtomFromStream(ByteStream &stream, Atom *&atom) {
LargeSize stream_size = 0;
Position stream_position = 0;
LargeSize bytes_available = (LargeSize) (-1);
if (SUCCEEDED(stream.getSize(stream_size)) &&
stream_size != 0 &&
SUCCEEDED(stream.tell(stream_position)) &&
stream_position <= stream_size) {
bytes_available = stream_size - stream_position;
}
return createAtomFromStream(stream, bytes_available, atom);
}
Result AtomFactory::createAtomsFromStream(ByteStream &stream, AtomParent &atoms) {
LargeSize stream_size = 0;
Position stream_position = 0;
LargeSize bytes_available = (LargeSize) (-1);
if (SUCCEEDED(stream.getSize(stream_size)) &&
stream_size != 0 &&
SUCCEEDED(stream.tell(stream_position)) &&
stream_position <= stream_size) {
bytes_available = stream_size - stream_position;
}
return createAtomsFromStream(stream, bytes_available, atoms);
}
Result AtomFactory::createAtomsFromStream(ByteStream &stream,
LargeSize bytes_available,
AtomParent &atoms) {
Result result;
do {
Atom *atom = NULL;
result = createAtomFromStream(stream, bytes_available, atom);
if (SUCCEEDED(result) && atom != NULL) {
atoms.addChild(atom);
}
} while (SUCCEEDED(result));
return SUCCESS;
}
void AtomFactory::pushContext(Atom::Type context) {
m_ContextStack.Append(context);
}
void AtomFactory::popContext() {
m_ContextStack.RemoveLast();
}
Atom::Type AtomFactory::getContext(Ordinal depth) {
Ordinal available = m_ContextStack.ItemCount();
if (depth >= available) return 0;
return m_ContextStack[available - depth - 1];
}
DefaultAtomFactory DefaultAtomFactory::Instance_;
DefaultAtomFactory::DefaultAtomFactory() {
initialize();
}
Result DefaultAtomFactory::initialize() {
// register built-in type handlers
return SUCCESS;
}
}