forked from chundongwang/untrunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.cpp
343 lines (288 loc) · 10.1 KB
/
atom.cpp
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
#include "AP_AtomDefinitions.h"
#include "atom.h"
#include "file.h"
#include <string>
#include <map>
#include <iostream>
#include <assert.h>
#include "uendian.h"
using namespace std;
Atom::~Atom() {
for(unsigned int i = 0; i < children.size(); i++)
delete children[i];
}
void Atom::parseHeader(File &file) {
start = file.pos();
length = file.readInt();
file.readChar(name, 4);
if(length == 1) {
length = file.readInt64() - 8;
start += 8;
} else if(length == 0) {
length = file.length() - start;
}
}
void Atom::parse(File &file) {
parseHeader(file);
if(isParent(name) && name != string("udta")) { //user data atom is dangerous... i should actually skip all
while(file.pos() < start + length) {
Atom *atom = new Atom;
atom->parse(file);
children.push_back(atom);
}
assert(file.pos() == start + length);
} else {
content = file.read(length -8); //lenght includes header
if(content.size() < length -8)
throw string("Failed reading atom content: ") + name;
}
}
void Atom::write(File &file) {
//1 write length
int start = file.pos();
file.writeInt(length);
file.writeChar(name, 4);
if(content.size())
file.write(content);
for(unsigned int i = 0; i < children.size(); i++)
children[i]->write(file);
int end = file.pos();
assert(end - start == length);
}
void Atom::print(int offset) {
string indent(offset, ' ');
cout << string(offset, '-') << name << " [" << start << ", " << length << "]\n";
if(name == string("mvhd") || name == string("mdhd")) {
for(int i = 0; i < offset; i++)
cout << " ";
//timescale: time units per second
//duration: in time units
cout << indent << " Timescale: " << readInt(12) << " Duration: " << readInt(16) << endl;
} else if(name == string("tkhd")) {
for(int i = 0; i < offset; i++)
cout << " ";
//track id:
//duration
cout << indent << " Trak: " << readInt(12) << " Duration: " << readInt(20) << endl;
} else if(name == string("hdlr")) {
char type[5];
readChar(type, 8, 4);
cout << indent << " Type: " << type << endl;
} else if(name == string("dref")) {
cout << indent << " Entries: " << readInt(4) << endl;
} else if(name == string("stsd")) { //sample description: (which codec...)
//lets just read the first entry
char type[5];
readChar(type, 12, 4);
//4 bytes zero
//4 bytes reference index (see stsc)
//additional fields
//video:
//4 bytes zero
///avcC: //see ISO 14496 5.2.4.1.1.
//01 -> version
//4d -> profile
//00 -> compatibility
//28 -> level code
//ff -> 6 bit reserved as 1 + 2 bit as nal length -1 so this is 4.
//E1 -> 3 bit as 1 + 5 for SPS (so 1)
//00 09 -> length of sequence parameter set
//27 4D 00 28 F4 02 80 2D C8 -> sequence parameter set
//01 -> number of picture parameter set
//00 04 -> length of picture parameter set
//28 EE 16 20 -> picture parameter set. (28 ee 04 62), (28 ee 1e 20)
cout << indent << " Entries: " << readInt(4) << " codec: " << type << endl;
} else if(name == string("stts")) { //run length compressed duration of samples
//lets just read the first entry
int entries = readInt(4);
cout << indent << " Entries: " << entries << endl;
for(int i = 0; i < entries && i < 10; i++)
cout << indent << " samples: " << readInt(8 + 8*i) << " for: " << readInt(12 + 8*i) << endl;
} else if(name == string("stss")) { //sync sample: (keyframes)
//lets just read the first entry
int entries = readInt(4);
cout << indent << " Entries: " << entries << endl;
for(int i = 0; i < entries && i < 10; i++)
cout << indent << " Keyframe: " << readInt(8 + 4*i) << endl;
} else if(name == string("stsc")) { //samples to chucnk:
//lets just read the first entry
int entries = readInt(4);
cout << indent << " Entries: " << entries << endl;
for(int i = 0; i < entries && i < 10; i++)
cout << indent << " chunk: " << readInt(8 + 12*i) << " nsamples: " << readInt(12 + 12*i) << " id: " << readInt(16 + 12*i) << endl;
} else if(name == string("stsz")) { //sample size atoms
int entries = readInt(8);
int sample_size = readInt(4);
cout << indent << " Sample size: " << sample_size << " Entries: " << entries << endl;
if(sample_size == 0) {
for(int i = 0; i < entries && i < 10; i++)
cout << indent << " Size " << readInt(12 + i*4) << endl;
}
} else if(name == string("stco")) { //sample chunk offset atoms
int entries = readInt(4);
cout << indent << " Entries: " << entries << endl;
for(int i = 0; i < entries && i < 10; i++)
cout << indent << " chunk: " << readInt(8 + i*4) << endl;
} else if(name == string("co64")) {
int entries = readInt(4);
cout << indent << " Entries: " << entries << endl;
for(int i = 0; i < entries && i < 10; i++)
cout << indent << " chunk: " << readInt(12 + i*8) << endl;
}
for(unsigned int i = 0; i < children.size(); i++)
children[i]->print(offset+1);
}
AtomDefinition definition(char *id) {
map<string, AtomDefinition> def;
if(def.size() == 0) {
for(int i = 0; i < 174; i++)
def[knownAtoms[i].known_atom_name] = knownAtoms[i];
}
if(!def.count(id)) {
//return a fake definition
return def["<()>"];
}
return def[id];
}
bool Atom::isParent(char *id) {
AtomDefinition def = definition(id);
return def.container_state == PARENT_ATOM;// || def.container_state == DUAL_STATE_ATOM;
}
bool Atom::isDual(char *id) {
AtomDefinition def = definition(id);
return def.container_state == DUAL_STATE_ATOM;
}
bool Atom::isVersioned(char *id) {
AtomDefinition def = definition(id);
return def.box_type == VERSIONED_ATOM;
}
vector<Atom *> Atom::atomsByName(string name) {
vector<Atom *> atoms;
for(unsigned int i = 0; i < children.size(); i++) {
if(children[i]->name == name)
atoms.push_back(children[i]);
vector<Atom *> a = children[i]->atomsByName(name);
atoms.insert(atoms.end(), a.begin(), a.end());
}
return atoms;
}
Atom *Atom::atomByName(std::string name) {
for(unsigned int i = 0; i < children.size(); i++) {
if(children[i]->name == name)
return children[i];
Atom *a = children[i]->atomByName(name);
if(a) return a;
}
return NULL;
}
void Atom::replace(Atom *original, Atom *replacement) {
for(unsigned int i = 0; i < children.size(); i++) {
if(children[i] == original) {
children[i] = replacement;
return;
}
}
throw "Atom not found";
}
void Atom::prune(string name) {
if(!children.size()) return;
length = 8;
vector<Atom *>::iterator it = children.begin();
while(it != children.end()) {
Atom *child = *it;
if(name == child->name) {
delete child;
it = children.erase(it);
} else {
child->prune(name);
length += child->length;
++it;
}
}
}
void Atom::updateLength() {
length = 8;
length += content.size();
for(unsigned int i = 0; i < children.size(); i++) {
Atom *child = children[i];
child->updateLength();
length += child->length;
}
}
int Atom::readInt(int64_t offset) {
return be32toh(*(int *)&(content[offset]));
}
void Atom::writeInt(int value, int64_t offset) {
assert(content.size() >= offset + 4);
*(int *)&(content[offset]) = htobe32(value);
}
void Atom::readChar(char *str, int64_t offset, int64_t length) {
for(int i = 0; i < length; i++)
str[i] = content[offset + i];
str[length] = 0;
}
BufferedAtom::BufferedAtom(string filename): buffer(NULL) {
if(!file.open(filename))
throw "Could not open file.";
}
BufferedAtom::~BufferedAtom() {
if(buffer) delete []buffer;
}
unsigned char *BufferedAtom::getFragment(int64_t offset, int64_t size) {
if(offset < 0)
throw "Offset set before beginning of buffer";
if(offset + size > file_end - file_begin)
throw "Out of buffer";
if(buffer == NULL) {
buffer_begin = offset;
buffer_end = offset + 2*size;
if(buffer_end + file_begin > file_end)
buffer_end = file_end - file_begin;
buffer = new unsigned char[buffer_end - buffer_begin];
file.seek(file_begin + buffer_begin);
file.readChar((char *)buffer, buffer_end - buffer_begin);
return buffer;
}
if(buffer_begin >= offset && buffer_end >= offset + size)
return buffer + (offset - buffer_begin);
//reallocate and reread
delete []buffer;
buffer = NULL;
return getFragment(offset, size);
}
void BufferedAtom::updateLength() {
length = 8;
length += file_end - file_begin;
for(unsigned int i = 0; i < children.size(); i++) {
Atom *child = children[i];
child->updateLength();
length += child->length;
}
}
int BufferedAtom::readInt(int64_t offset) {
if(!buffer || offset < buffer_begin || offset > (buffer_end - 4)) {
buffer = getFragment(offset, 1<<16);
}
return *(int *)(buffer + offset - buffer_begin);
}
void BufferedAtom::write(File &output) {
//1 write length
int start = output.pos();
output.writeInt(length);
output.writeChar(name, 4);
char buff[1<<20];
int offset = file_begin;
file.seek(file_begin);
while(offset < file_end) {
int toread = 1<<20;
if(toread + offset > file_end)
toread = file_end - offset;
file.readChar(buff, toread);
offset += toread;
output.writeChar(buff,toread);
}
for(unsigned int i = 0; i < children.size(); i++)
children[i]->write(output);
int end = output.pos();
assert(end - start == length);
}