-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMp4SampleBoxes.h
More file actions
661 lines (550 loc) · 13.8 KB
/
Mp4SampleBoxes.h
File metadata and controls
661 lines (550 loc) · 13.8 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/**
*
* author Jose Mortensen
* brief Iso Mp4 Sample Boxes
*
*/
#ifndef __MP4SAMPLEBOXES_H__
#define __MP4SAMPLEBOXES_H__
#include <vector>
#include "Mp4IStreamSource.h"
/**
aligned(8) class SampleTableBox extends Box(‘stbl’) {}
*/
class Stbl : public Box {
public:
Stbl () : Box("stbl") {
name = "Sample Table Box";
}
};
/**
aligned(8) class SampleDescriptionBox (unsigned int(32) handler_type) extends FullBox('stsd', 0, 0){
int i ;
unsigned int(32) entry_count;
for (i = 1 ; i <= entry_count ; i++){
switch (handler_type){
case ‘soun’: // for audio tracks
AudioSampleEntry();
break;
case ‘vide’: // for video tracks
VisualSampleEntry();
break;
case ‘hint’: // Hint track
HintSampleEntry();
break;
case ‘meta’: // Metadata track
MetadataSampleEntry();
break;
}
}
}
}
*/
class Stsd : public FullBox {
uint32_t entry_count;
std::vector<Box*> boxes;
public:
Stsd () : FullBox("stsd") {
name = "SampleDescription";
entry_count = 0;
increaseSize(4);
}
void addBox(Box* box) {
entry_count ++;
increaseSize(box->largesize);
boxes.push_back(box);
}
virtual void read(large_istream& is) {
FullBox::read(is);
entry_count = Box::read32(is);
consumed+=4;
uint32_t count = entry_count;
while(count--) {
Box* box = Mp4FileImpl::readBox(is);
boxes.push_back(box);
consumed += box->largesize;
}
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, entry_count);
uint32_t count = entry_count;
while(count--) {
Box* box = boxes[count];
Mp4FileImpl::writeBox(os, *box);
}
}
~Stsd() {
for(std::size_t i = 0; i < boxes.size(); i++) {
Box* box = boxes[i];
delete box;
}
}
};
/**
class BitRateBox extends Box(‘btrt’){
unsigned int(32) bufferSizeDB;
unsigned int(32) maxBitrate;
unsigned int(32) avgBitrate;
}
*/
class Btrt : public Box {
public:
uint32_t bufferSizeDB;
uint32_t maxBitrate;
uint32_t avgBitrate;
Btrt () : Box("btrt") {
name = "Bitrate";
bufferSizeDB = 0;
maxBitrate = 0;
avgBitrate = 0;
}
virtual void read(large_istream& is) {
Box::read(is);
bufferSizeDB = Box::read32(is);
maxBitrate = Box::read32(is);
avgBitrate = Box::read32(is);
consumed += 12;
}
virtual void write(large_ostream& os) {
Box::write(os);
Box::write32(os, bufferSizeDB);
Box::write32(os, maxBitrate);
Box::write32(os, avgBitrate);
}
};
/**
aligned(8) abstract class SampleEntry (unsigned int(32) format) extends Box(format){
const unsigned int(8)[6] reserved = 0;
unsigned int(16) data_reference_index;
}
*/
class SampleEntry : public Box {
public:
uint8_t reserved[6];
uint16_t data_reference_index;
SampleEntry (std::string type) : Box(type) {
memset(reserved, 0, 6);
data_reference_index = 1;
increaseSize(8);
}
virtual void read(large_istream& is) {
is.read((char*)&reserved, 6);
data_reference_index = Box::read16(is);
consumed += 8;
}
virtual void write(large_ostream& os) {
Box::write(os);
os.write((char*)reserved, 6);
Box::write16(os, data_reference_index);
}
};
/**
class MetaDataSampleEntry(codingname) extends SampleEntry (codingname) {
}
*/
class MetaDataSampleEntry : public SampleEntry {
public:
MetaDataSampleEntry (std::string condingname) : SampleEntry(condingname) {
}
};
/**
class TextMetaDataSampleEntry() extends MetaDataSampleEntry (‘mett’) {
string content_encoding; // optional
string mime_format;
BitRateBox (); // optional
}
*/
class Mett : public MetaDataSampleEntry {
std::string content_encoding;
std::string mime_format;
Box* bitrate;
public:
Mett () : MetaDataSampleEntry("mett") {
name = "TextMetaDataSampleEntry";
bitrate = 0;
increaseSize(2);
}
void setContentEncoding(std::string content) {
content_encoding = content;
increaseSize(content_encoding.length()); // + 1 already accounted
}
void setMime(std::string mime) {
mime_format = mime;
increaseSize(mime_format.length()); // + 1 already accounted
}
virtual void read(large_istream& is) {
SampleEntry::read(is);
content_encoding = Box::readStr(is);
mime_format = Box::readStr(is);
consumed += content_encoding.length() + mime_format.length() + 2;
if (largesize-consumed) {
bitrate = Mp4FileImpl::readBox(is);
}
}
void write(large_ostream& os) {
MetaDataSampleEntry::write(os);
Box::writeStr(os, content_encoding);
Box::writeStr(os, mime_format);
if (bitrate)
Mp4FileImpl::writeBox(os, *bitrate);
}
};
/**
Common class to access chunck offsets
*/
class Mp4Co {
public:
virtual void addOffset(uint64_t offset) = 0;
virtual uint64_t getOffset(uint32_t item) = 0;
virtual void setOffset(uint64_t offset, uint32_t item) = 0;
virtual uint32_t getEntryCount() = 0;
};
/**
aligned(8) class ChunkOffsetBox extends FullBox(‘stco’, version = 0, 0) {
unsigned int(32) entry_count;
for (i=1; i <= entry_count; i++) {
unsigned int(32) chunk_offset;
}
}
*/
class Stco : public FullBox, public Mp4Co {
public:
uint32_t entry_count;
std::vector<uint32_t> chunk_offsets;
Stco () : FullBox("stco") {
name = "ChunkOffset";
entry_count = 0;
increaseSize(4);
}
void addOffset(uint64_t offset)
{
entry_count++;
chunk_offsets.push_back((uint32_t)offset);
increaseSize(4);
}
uint64_t getOffset(uint32_t item) {
return chunk_offsets[item];
}
uint32_t getEntryCount() { return entry_count;}
void setOffset(uint64_t offset, uint32_t item) {
chunk_offsets[item] = (uint32_t)offset;
}
void read(large_istream& is) {
FullBox::read(is);
entry_count = Box::read32(is);
consumed += 4;
for (uint32_t i = 0; i < entry_count; i++) {
chunk_offsets.push_back(Box::read32(is));
consumed += 4;
}
if (largesize-consumed)
throw std::runtime_error("Wrong Stco count");
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, entry_count);
for (uint32_t i = 0; i < entry_count; i++) {
Box::write32(os, chunk_offsets[i]);
}
}
};
/**
aligned(8) class ChunkLargeOffsetBox extends FullBox(‘co64’, version = 0, 0) {
unsigned int(32) entry_count;
for (i=1; i <= entry_count; i++) {
unsigned int(64) chunk_offset;
}
}
*/
class Co64 : public FullBox, public Mp4Co {
uint32_t entry_count;
std::vector<uint64_t> chunk_offsets;
public:
Co64 () : FullBox("co64") {
name = "ChunkLargeOffset";
entry_count = 0;
increaseSize(4);
}
void addOffset(uint64_t offset)
{
entry_count++;
chunk_offsets.push_back(offset);
increaseSize(8);
}
uint64_t getOffset(uint32_t item) {
return chunk_offsets[item];
}
uint32_t getEntryCount() { return entry_count;}
void setOffset(uint64_t offset, uint32_t item) {
chunk_offsets[item] = offset;
}
virtual void read(large_istream& is) {
FullBox::read(is);
entry_count = Box::read32(is);
consumed += 4;
for (uint32_t i = 0; i < entry_count; i++) {
chunk_offsets.push_back(Box::read64(is));
consumed += 8;
}
if (largesize-consumed)
throw std::runtime_error("Wrong Co64 count");
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, entry_count);
for (uint32_t i = 0; i < entry_count; i++) {
Box::write64(os, chunk_offsets[i]);
}
}
};
/**
aligned(8) class TimeToSampleBox extends FullBox(’stts’, version = 0, 0) {
unsigned int(32) entry_count;
for (int i=0; i < entry_count; i++) {
unsigned int(32) sample_count;
unsigned int(32) sample_delta;
}
}
*/
class Stts : public FullBox {
public:
struct SttsSample {
uint32_t sample_count;
uint32_t sample_delta;
};
uint32_t entry_count;
std::vector<SttsSample> samples;
Stts () : FullBox("stts") {
name = "TimeToSample";
entry_count = 0;
increaseSize(4);
}
void addSttsSample(uint32_t count, uint32_t delta) {
SttsSample sample;
sample.sample_count = count;
sample.sample_delta = delta;
samples.push_back(sample);
entry_count++;
increaseSize(8);
}
virtual void read(large_istream& is) {
FullBox::read(is);
entry_count = Box::read32(is);
consumed += 4;
SttsSample sample;
for (uint32_t i = 0; i < entry_count; i++) {
sample.sample_count = Box::read32(is);
sample.sample_delta = Box::read32(is);
consumed += 8;
samples.push_back(sample);
}
if (largesize-consumed)
throw std::runtime_error("Wrong STTS count");
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, entry_count);
SttsSample sample;
for (uint32_t i = 0; i < entry_count; i++) {
sample = samples[i];
Box::write32(os, sample.sample_count);
Box::write32(os, sample.sample_delta);
}
}
};
/**
aligned(8) class SampleToChunkBox extends FullBox(‘stsc’, version = 0, 0) {
unsigned int(32) entry_count;
for (i=1; i <= entry_count; i++) {
unsigned int(32) first_chunk;
unsigned int(32) samples_per_chunk;
unsigned int(32) sample_description_index;
}
}
*/
class Stsc : public FullBox {
public:
struct StscSample {
uint32_t first_chunk;
uint32_t samples_per_chunk;
uint32_t sample_description_index;
};
uint32_t entry_count;
std::vector<StscSample> samples;
Stsc () : FullBox("stsc") {
name = "SampleToChunk";
entry_count = 0;
increaseSize(4);
}
void addStscSample(uint32_t first_chunk, uint32_t samples_per_chunk, uint32_t sample_description_index) {
StscSample sample;
sample.first_chunk = first_chunk;
sample.samples_per_chunk = samples_per_chunk;
sample.sample_description_index = sample_description_index;
samples.push_back(sample);
entry_count++;
increaseSize(12);
}
virtual void read(large_istream& is) {
FullBox::read(is);
entry_count = Box::read32(is);
consumed += 4;
StscSample sample;
for (uint32_t i = 0; i < entry_count; i++) {
sample.first_chunk = Box::read32(is);
sample.samples_per_chunk = Box::read32(is);
sample.sample_description_index = Box::read32(is);
consumed += 12;
samples.push_back(sample);
}
if (largesize-consumed)
throw std::runtime_error("Wrong STCS count");
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, entry_count);
StscSample sample;
for (uint32_t i = 0; i < entry_count; i++) {
sample = samples[i];
Box::write32(os, sample.first_chunk);
Box::write32(os, sample.samples_per_chunk);
Box::write32(os, sample.sample_description_index);
}
}
};
/**
aligned(8) class SampleSizeBox extends FullBox(‘stsz’, version = 0, 0) {
unsigned int(32) sample_size;
unsigned int(32) sample_count;
if (sample_size==0) {
for (i=1; i <= sample_count; i++) {
unsigned int(32) entry_size;
}
}
}
*/
class Stsz : public FullBox {
public:
uint32_t sample_size;
uint32_t entry_count;
std::vector<uint32_t> sizes;
Stsz () : FullBox("stsz") {
name = "SampleSizeBox";
sample_size = 0;
entry_count = 0;
increaseSize(8);
}
uint32_t getSize(std::size_t entry) {
if (entry >= entry_count)
throw std::runtime_error ("Stsc Invalid Entry");
if (sample_size!=0) {
return sample_size;
} else {
return sizes[entry];
}
}
void addStszSample(uint32_t sample_size) {
entry_count++;
sizes.push_back(sample_size);
increaseSize( 4);
}
virtual void read(large_istream& is) {
FullBox::read(is);
sample_size = Box::read32(is);
entry_count = Box::read32(is);
consumed += 8;
if (sample_size==0) {
for (uint32_t i = 0; i < entry_count; i++) {
uint32_t size = Box::read32(is);
consumed += 4;
sizes.push_back(size);
}
}
if (largesize-consumed)
throw std::runtime_error("Wrong STSZ count");
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, sample_size);
Box::write32(os, entry_count);
if (sample_size==0) {
for (uint32_t i = 0; i < entry_count; i++) {
uint32_t size = sizes[i];
Box::write32(os, size);
}
}
}
};
/**
aligned(8) class CompositionOffsetBox extends FullBox(‘ctts’, version = 0, 0) {
unsigned int(32) entry_count;
int i;
for (i=0; i < entry_count; i++) {
unsigned int(32) sample_count;
unsigned int(32) sample_offset;
}
}
*/
class Ctts : public FullBox {
public:
struct CttsSample {
uint32_t sample_count;
uint32_t sample_offset;
};
uint32_t entry_count;
std::vector<CttsSample> samples;
Ctts () : FullBox("ctts") {
name = "CompositionOffset";
entry_count = 0;
increaseSize(4);
}
void addCttsSample(uint32_t sample_count, uint32_t sample_offset) {
entry_count++;
CttsSample sample;
sample.sample_count = sample_count;
sample.sample_offset = sample_offset;
samples.push_back(sample);
increaseSize(8);
}
virtual void read(large_istream& is) {
FullBox::read(is);
entry_count = Box::read32(is);
consumed += 4;
CttsSample sample;
for (uint32_t i = 0; i < entry_count; i++) {
sample.sample_count = Box::read32(is);
sample.sample_offset = Box::read32(is);
consumed += 8;
samples.push_back(sample);
}
if (largesize-consumed)
throw std::runtime_error("Wrong CTTS count");
}
void write(large_ostream& os) {
FullBox::write(os);
Box::write32(os, entry_count);
CttsSample sample;
for (uint32_t i = 0; i < entry_count; i++) {
sample = samples[i];
Box::write32(os, sample.sample_count);
Box::write32(os, sample.sample_offset);
}
}
};
/**
class AMF0() extends SampleEntry (‘amf’) {
}
*/
class Amf0 : public SampleEntry {
public:
Amf0 () : SampleEntry("amf0") {
}
virtual void read(large_istream& is) {
SampleEntry::read(is);
}
void write(large_ostream& os) {
SampleEntry::write(os);
}
};
#endif //__MP4SAMPLEBOXES_H__