forked from ponchio/untrunc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.cpp
More file actions
481 lines (398 loc) · 13.4 KB
/
track.cpp
File metadata and controls
481 lines (398 loc) · 13.4 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "track.h"
#include "atom.h"
#include <iostream>
#include <vector>
#include <string.h>
#include <assert.h>
#define __STDC_LIMIT_MACROS 1
#define __STDC_CONSTANT_MACROS 1
extern "C" {
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include <stdint.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}
using namespace std;
static void reverse(int &input) {
int output;
char *a = ( char* )&input;
char *b = ( char* )&output;
b[0] = a[3];
b[1] = a[2];
b[2] = a[1];
b[3] = a[0];
input = output;
}
using namespace std;
void Codec::parse(Atom *trak, vector<int> &offsets, Atom *mdat) {
Atom *stsd = trak->atomByName("stsd");
int entries = stsd->readInt(4);
if(entries != 1)
throw string("Multiplexed stream! Not supported");
char _codec[5];
stsd->readChar(_codec, 12, 4);
name = _codec;
mask1 = 0xffffffff;
mask0 = 0xffffffff;
//build the mask:
for(int i = 0; i < offsets.size(); i++) {
int s = mdat->readInt(offsets[i] - mdat->start - 8);
reverse(s);
mask1 &= s;
mask0 &= ~s;
assert((s & mask1) == mask1);
assert((~s & mask0) == mask0);
}
}
bool Codec::matchSample(unsigned char *start, int maxlength) {
int s = *(int *)start;
if(name == "avc1") {
//TODO use the first byte of the nal: forbidden bit and type!
int nal_type = (start[4] & 0x1f);
//the other values are really uncommon on cameras...
if(nal_type != 1 && nal_type != 5 && nal_type != 6 && nal_type != 7 && nal_type != 8 && nal_type != 9) return false;
//if nal is equal 7, the other fragments (starting with nal type 7) should be part of the same packet
//(we cannot recover time information, remember)
if(start[0] == 0) return true;
return false;
} else if(name == "mp4a") {
reverse(s);
if(s > 1000000) return true;
//horrible hack... these values might need to be changed depending on the file
if((start[4] == 0xee && start[5] == 0x1b) ||
(start[4] == 0x3e && start[5] == 0x64)) return true;
if(start[0] == 0) return false;
return true;
} else if(name == "alac") {
reverse(s);
int t = *(int *)(start + 4);
reverse(t);
t &= 0xffff0000;
cout << hex << t << dec << endl;
if(s == 0 && t == 0x00130000) return true;
if(s == 0x1000 && t == 0x001a0000) return true;
return false;
} else if(name == "samr") {
return start[0] == 0x3c;
}
return false;
}
int Codec::getLength(unsigned char *start, int maxlength) {
if(name == "mp4a") {
AVFrame *frame = avcodec_alloc_frame();
if(!frame)
throw string("Could not create AVFrame");
AVPacket avp;
av_init_packet(&avp);
int got_frame;
avp.data=(uint8_t *)(start);
avp.size = maxlength;
int consumed = avcodec_decode_audio4(context, frame, &got_frame, &avp);
av_freep(&frame);
return consumed;
} else if(name == "avc1") {
int first_nal_type = (start[4] & 0x1f);
cout << "Nal type: " << first_nal_type << endl;
int length = *(int *)start;
reverse(length);
if(length <= 0) return -1;
length += 4;
if(length > maxlength) return -1;
//consume all nal units where type is != 1, 3, 5
unsigned char *pos = start;
bool found = false;
while(!found) {
pos = start + length;
assert(pos - start < maxlength - 4);
int l = *(int *)pos;
reverse(l);
if(l <= 0) break;
if(pos[0] != 0) break; //not avc1
int nal_type = (pos[4] & 0x1f);
if(nal_type <= 5) found = true;
//if(nal_type <= 5 || nal_type >= 18) break;//wrong nal or not video
if(nal_type > 12) break; //unknown nal type
if(l + length + 8 >= maxlength) break; //out of boundary
assert(length < maxlength);
//ok include it
length += l + 4;
assert(length + 4 < maxlength);
}
return length;
} else if(name == "samr") { //lenght is multiple of 32, we split packets.
return 32;
} else
return -1;
}
bool Codec::isKeyframe(unsigned char *start, int maxlength) {
if(name == "avc1") {
//first byte of the NAL, the last 5 bits determine type (usually 5 for keyframe, 1 for intra frame)
return (start[4] & 0x1F) == 5;
} else
return false;
}
void Track::parse(Atom *t, Atom *mdat) {
trak = t;
Atom *mdhd = t->atomByName("mdhd");
if(!mdhd)
throw string("No mdhd atom: unknown duration and timescale");
timescale = mdhd->readInt(12);
duration = mdhd->readInt(16);
times = getSampleTimes(t);
keyframes = getKeyframes(t);
sizes = getSampleSizes(t);
vector<int> chunk_offsets = getChunkOffsets(t);
vector<int> sample_to_chunk = getSampleToChunk(t, chunk_offsets.size());
assert(times.size() == sizes.size());
assert(times.size() == sample_to_chunk.size());
//compute actual offsets
int old_chunk = -1;
int offset = -1;
for(unsigned int i = 0; i < sizes.size(); i++) {
int chunk = sample_to_chunk[i];
int size = sizes[i];
if(chunk != old_chunk) {
offset = chunk_offsets[chunk];
old_chunk= chunk;
}
offsets.push_back(offset);
offset += size;
}
//move this stuff into track!
Atom *hdlr = trak->atomByName("hdlr");
char type[5];
hdlr->readChar(type, 8, 4);
bool audio = (type == string("soun"));
if(type != string("soun") && type != string("vide"))
return;
//move this to Codec
codec.parse(trak, offsets, mdat);
codec.codec = avcodec_find_decoder(codec.context->codec_id);
//if audio use next?
if(!codec.codec) throw string("No codec found!");
if(avcodec_open(codec.context, codec.codec)<0)
throw string("Could not open codec: ") + codec.context->codec_name;
/*
Atom *mdat = root->atomByName("mdat");
if(!mdat)
throw string("Missing data atom");
//print sizes and offsets
for(int i = 0; i < 10 && i < sizes.size(); i++) {
cout << "offset: " << offsets[i] << " size: " << sizes[i] << endl;
cout << mdat->readInt(offsets[i] - (mdat->start + 8)) << endl;
} */
}
void Track::writeToAtoms() {
if(!keyframes.size())
trak->prune("stss");
saveSampleTimes();
saveKeyframes();
saveSampleSizes();
saveSampleToChunk();
saveChunkOffsets();
Atom *mdhd = trak->atomByName("mdhd");
mdhd->writeInt(duration, 16);
//Avc1 codec writes something inside stsd.
//In particular the picture parameter set (PPS) in avcC (after the sequence parameter set)
//See http://jaadec.sourceforge.net/specs/ISO_14496-15_AVCFF.pdf for the avcC stuff
//See http://www.iitk.ac.in/mwn/vaibhav/Vaibhav%20Sharma_files/h.264_standard_document.pdf for the PPS
//I have found out in shane,banana,bruno and nawfel that pic_init_qp_minus26 is different even for consecutive videos of the same camera.
//the only thing to do then is to test possible values, to do so remove 28 (the nal header) follow the golomb stuff
//This test could be done automatically when decoding fails..
#define SHANE 6
#ifdef SHANE
int pps[15] = {
0x28ee3880,
0x28ee1620,
0x28ee1e20,
0x28ee0988,
0x28ee0b88,
0x28ee0d88,
0x28ee0f88,
0x28ee0462,
0x28ee04e2,
0x28ee0562,
0x28ee05e2,
0x28ee0662,
0x28ee06e2,
0x28ee0762,
0x28ee07e2
};
if(codec.name == "avc1") {
Atom *stsd = trak->atomByName("stsd");
stsd->writeInt(pps[SHANE], 122); //a bit complicated to find.... find avcC... follow first link.
}
#endif
}
void Track::clear() {
//times.clear();
offsets.clear();
sizes.clear();
keyframes.clear();
}
void Track::fixTimes() {
if(codec.name == "samr") { //just return smallest of packets
unsigned int min = 0xffffffff;
for(int i = 0; i < times.size(); i++)
if(times[i] < min) min = i;
times.clear();
times.resize(offsets.size(), 160);
return;
}
while(times.size() < offsets.size())
times.insert(times.end(), times.begin(), times.end());
times.resize(offsets.size());
duration = 0;
for(unsigned int i = 0; i < times.size(); i++)
duration += times[i];
}
vector<int> Track::getSampleTimes(Atom *t) {
vector<int> sample_times;
//chunk offsets
Atom *stts = t->atomByName("stts");
if(!stts)
throw string("Missing sample to time atom");
int entries = stts->readInt(4);
for(int i = 0; i < entries; i++) {
int nsamples = stts->readInt(8 + 8*i);
int time = stts->readInt(12 + 8*i);
for(int i = 0; i < nsamples; i++)
sample_times.push_back(time);
}
return sample_times;
}
vector<int> Track::getKeyframes(Atom *t) {
vector<int> sample_key;
//chunk offsets
Atom *stss = t->atomByName("stss");
if(!stss)
return sample_key;
int entries = stss->readInt(4);
for(int i = 0; i < entries; i++)
sample_key.push_back(stss->readInt(8 + 4*i) - 1);
return sample_key;
}
vector<int> Track::getSampleSizes(Atom *t) {
vector<int> sample_sizes;
//chunk offsets
Atom *stsz = t->atomByName("stsz");
if(!stsz)
throw string("Missing sample to sizeatom");
int entries = stsz->readInt(8);
int default_size = stsz->readInt(4);
if(default_size == 0) {
for(int i = 0; i < entries; i++)
sample_sizes.push_back(stsz->readInt(12 + 4*i));
} else {
sample_sizes.resize(entries, default_size);
}
return sample_sizes;
}
vector<int> Track::getChunkOffsets(Atom *t) {
vector<int> chunk_offsets;
//chunk offsets
Atom *stco = t->atomByName("stco");
if(stco) {
int nchunks = stco->readInt(4);
for(int i = 0; i < nchunks; i++)
chunk_offsets.push_back(stco->readInt(8 + i*4));
} else {
Atom *co64 = t->atomByName("co64");
if(!co64)
throw string("Missing chunk offset atom");
int nchunks = co64->readInt(4);
for(int i = 0; i < nchunks; i++)
chunk_offsets.push_back(co64->readInt(12 + i*8));
}
return chunk_offsets;
}
vector<int> Track::getSampleToChunk(Atom *t, int nchunks){
vector<int> sample_to_chunk;
Atom *stsc = t->atomByName("stsc");
if(!stsc)
throw string("Missing sample to chunk atom");
vector<int> first_chunks;
int entries = stsc->readInt(4);
for(int i = 0; i < entries; i++)
first_chunks.push_back(stsc->readInt(8 + 12*i));
first_chunks.push_back(nchunks+1);
for(int i = 0; i < entries; i++) {
int first_chunk = first_chunks[i];
int last_chunk = first_chunks[i+1];
int n_samples = stsc->readInt(12 + 12*i);
for(int k = first_chunk; k < last_chunk; k++) {
for(int j = 0; j < n_samples; j++)
sample_to_chunk.push_back(k-1);
}
}
return sample_to_chunk;
}
void Track::saveSampleTimes() {
Atom *stts = trak->atomByName("stts");
assert(stts);
stts->content.resize(4 + //version
4 + //entries
8*times.size()); //time table
stts->writeInt(times.size(), 4);
for(unsigned int i = 0; i < times.size(); i++) {
stts->writeInt(1, 8 + 8*i);
stts->writeInt(times[i], 12 + 8*i);
}
}
void Track::saveKeyframes() {
Atom *stss = trak->atomByName("stss");
if(!stss) return;
assert(keyframes.size());
if(!keyframes.size()) return;
stss->content.resize(4 + //version
4 + //entries
4*keyframes.size()); //time table
stss->writeInt(keyframes.size(), 4);
for(unsigned int i = 0; i < keyframes.size(); i++)
stss->writeInt(keyframes[i] + 1, 8 + 4*i);
}
void Track::saveSampleSizes() {
Atom *stsz = trak->atomByName("stsz");
assert(stsz);
stsz->content.resize(4 + //version
4 + //default size
4 + //entries
4*sizes.size()); //size table
stsz->writeInt(0, 4);
stsz->writeInt(sizes.size(), 8);
for(unsigned int i = 0; i < sizes.size(); i++) {
stsz->writeInt(sizes[i], 12 + 4*i);
}
}
void Track::saveSampleToChunk() {
Atom *stsc = trak->atomByName("stsc");
assert(stsc);
stsc->content.resize(4 + // version
4 + //number of entries
12); //one sample per chunk.
stsc->writeInt(1, 4);
stsc->writeInt(1, 8); //first chunk (1 based)
stsc->writeInt(1, 12); //one sample per chunk
stsc->writeInt(1, 16); //id 1 (WHAT IS THIS!)
}
void Track::saveChunkOffsets() {
Atom *co64 = trak->atomByName("co64");
if(co64) {
trak->prune("co64");
Atom *stbl = trak->atomByName("stbl");
Atom *new_stco = new Atom;
memcpy(new_stco->name, "stco", 5);
stbl->children.push_back(new_stco);
}
Atom *stco = trak->atomByName("stco");
assert(stco);
stco->content.resize(4 + //version
4 + //number of entries
4*offsets.size());
stco->writeInt(offsets.size(), 4);
for(unsigned int i = 0; i < offsets.size(); i++)
stco->writeInt(offsets[i], 8 + 4*i);
}