Repository navigation
Expand file tree
/
Copy pathsrs_kernel_mp4.hpp
More file actions
2229 lines (2143 loc) · 80 KB
/
Copy pathsrs_kernel_mp4.hpp
File metadata and controls
2229 lines (2143 loc) · 80 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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2017 OSSRS(winlin)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_KERNEL_MP4_HPP
#define SRS_KERNEL_MP4_HPP
#include <srs_core.hpp>
#include <srs_kernel_buffer.hpp>
#include <srs_kernel_codec.hpp>
#include <string>
#include <sstream>
#include <vector>
#include <map>
class ISrsWriter;
class ISrsWriteSeeker;
class ISrsReadSeeker;
class SrsMp4TrackBox;
class SrsMp4MediaBox;
class SrsSimpleStream;
class SrsMp4MovieHeaderBox;
class SrsMp4TrackHeaderBox;
class SrsMp4SampleTableBox;
class SrsMp4MediaInformationBox;
class SrsMp4SampleDescriptionBox;
class SrsMp4AvccBox;
class SrsMp4DecoderSpecificInfo;
class SrsMp4VisualSampleEntry;
class SrsMp4AvccBox;
class SrsMp4AudioSampleEntry;
class SrsMp4EsdsBox;
class SrsMp4ChunkOffsetBox;
class SrsMp4SampleSizeBox;
class SrsMp4Sample2ChunkBox;
class SrsMp4DecodingTime2SampleBox;
class SrsMp4CompositionTime2SampleBox;
class SrsMp4SyncSampleBox;
class SrsMp4MediaHeaderBox;
class SrsMp4HandlerReferenceBox;
class SrsMp4VideoMeidaHeaderBox;
class SrsMp4DataInformationBox;
class SrsMp4DataReferenceBox;
class SrsMp4SoundMeidaHeaderBox;
class SrsMp4MovieExtendsBox;
class SrsMp4TrackExtendsBox;
class SrsMp4MovieFragmentHeaderBox;
class SrsMp4TrackFragmentBox;
class SrsMp4TrackFragmentHeaderBox;
class SrsMp4TrackFragmentDecodeTimeBox;
class SrsMp4TrackFragmentRunBox;
/**
* 4.2 Object Structure
* ISO_IEC_14496-12-base-format-2012.pdf, page 16
*/
enum SrsMp4BoxType
{
SrsMp4BoxTypeForbidden = 0x00,
SrsMp4BoxTypeUUID = 0x75756964, // 'uuid'
SrsMp4BoxTypeFTYP = 0x66747970, // 'ftyp'
SrsMp4BoxTypeMDAT = 0x6d646174, // 'mdat'
SrsMp4BoxTypeFREE = 0x66726565, // 'free'
SrsMp4BoxTypeSKIP = 0x736b6970, // 'skip'
SrsMp4BoxTypeMOOV = 0x6d6f6f76, // 'moov'
SrsMp4BoxTypeMVHD = 0x6d766864, // 'mvhd'
SrsMp4BoxTypeTRAK = 0x7472616b, // 'trak'
SrsMp4BoxTypeTKHD = 0x746b6864, // 'tkhd'
SrsMp4BoxTypeEDTS = 0x65647473, // 'edts'
SrsMp4BoxTypeELST = 0x656c7374, // 'elst'
SrsMp4BoxTypeMDIA = 0x6d646961, // 'mdia'
SrsMp4BoxTypeMDHD = 0x6d646864, // 'mdhd'
SrsMp4BoxTypeHDLR = 0x68646c72, // 'hdlr'
SrsMp4BoxTypeMINF = 0x6d696e66, // 'minf'
SrsMp4BoxTypeVMHD = 0x766d6864, // 'vmhd'
SrsMp4BoxTypeSMHD = 0x736d6864, // 'smhd'
SrsMp4BoxTypeDINF = 0x64696e66, // 'dinf'
SrsMp4BoxTypeURL = 0x75726c20, // 'url '
SrsMp4BoxTypeURN = 0x75726e20, // 'urn '
SrsMp4BoxTypeDREF = 0x64726566, // 'dref'
SrsMp4BoxTypeSTBL = 0x7374626c, // 'stbl'
SrsMp4BoxTypeSTSD = 0x73747364, // 'stsd'
SrsMp4BoxTypeSTTS = 0x73747473, // 'stts'
SrsMp4BoxTypeCTTS = 0x63747473, // 'ctts'
SrsMp4BoxTypeSTSS = 0x73747373, // 'stss'
SrsMp4BoxTypeSTSC = 0x73747363, // 'stsc'
SrsMp4BoxTypeSTCO = 0x7374636f, // 'stco'
SrsMp4BoxTypeCO64 = 0x636f3634, // 'co64'
SrsMp4BoxTypeSTSZ = 0x7374737a, // 'stsz'
SrsMp4BoxTypeSTZ2 = 0x73747a32, // 'stz2'
SrsMp4BoxTypeAVC1 = 0x61766331, // 'avc1'
SrsMp4BoxTypeAVCC = 0x61766343, // 'avcC'
SrsMp4BoxTypeMP4A = 0x6d703461, // 'mp4a'
SrsMp4BoxTypeESDS = 0x65736473, // 'esds'
SrsMp4BoxTypeUDTA = 0x75647461, // 'udta'
SrsMp4BoxTypeMVEX = 0x6d766578, // 'mvex'
SrsMp4BoxTypeTREX = 0x74726578, // 'trex'
SrsMp4BoxTypePASP = 0x70617370, // 'pasp'
SrsMp4BoxTypeSTYP = 0x73747970, // 'styp'
SrsMp4BoxTypeMOOF = 0x6d6f6f66, // 'moof'
SrsMp4BoxTypeMFHD = 0x6d666864, // 'mfhd'
SrsMp4BoxTypeTRAF = 0x74726166, // 'traf'
SrsMp4BoxTypeTFHD = 0x74666864, // 'tfhd'
SrsMp4BoxTypeTFDT = 0x74666474, // 'tfdt'
SrsMp4BoxTypeTRUN = 0x7472756e, // 'trun'
};
/**
* 8.4.3.3 Semantics
* ISO_IEC_14496-12-base-format-2012.pdf, page 37
*/
enum SrsMp4HandlerType
{
SrsMp4HandlerTypeForbidden = 0x00,
SrsMp4HandlerTypeVIDE = 0x76696465, // 'vide'
SrsMp4HandlerTypeSOUN = 0x736f756e, // 'soun'
};
/**
* File format brands
* ISO_IEC_14496-12-base-format-2012.pdf, page 166
*/
enum SrsMp4BoxBrand
{
SrsMp4BoxBrandForbidden = 0x00,
SrsMp4BoxBrandISOM = 0x69736f6d, // 'isom'
SrsMp4BoxBrandISO2 = 0x69736f32, // 'iso2'
SrsMp4BoxBrandAVC1 = 0x61766331, // 'avc1'
SrsMp4BoxBrandMP41 = 0x6d703431, // 'mp41'
SrsMp4BoxBrandISO5 = 0x69736f35, // 'iso5'
SrsMp4BoxBrandMP42 = 0x6d703432, // 'mp42'
SrsMp4BoxBrandDASH = 0x64617368, // 'dash'
SrsMp4BoxBrandMSDH = 0x6d736468, // 'msdh'
};
/**
* The context to dump.
*/
struct SrsMp4DumpContext
{
int level;
bool summary;
SrsMp4DumpContext indent();
};
/**
* 4.2 Object Structure
* ISO_IEC_14496-12-base-format-2012.pdf, page 16
*/
class SrsMp4Box : public ISrsCodec
{
private:
// The size is the entire size of the box, including the size and type header, fields,
// and all contained boxes. This facilitates general parsing of the file.
//
// if size is 1 then the actual size is in the field largesize;
// if size is 0, then this box is the last one in the file, and its contents
// extend to the end of the file (normally only used for a Media Data Box)
uint32_t smallsize;
uint64_t largesize;
public:
// identifies the box type; standard boxes use a compact type, which is normally four printable
// characters, to permit ease of identification, and is shown so in the boxes below. User extensions use
// an extended type; in this case, the type field is set to ‘uuid’.
SrsMp4BoxType type;
// For box 'uuid'.
std::vector<char> usertype;
protected:
std::vector<SrsMp4Box*> boxes;
private:
// The position at buffer to start demux the box.
int start_pos;
public:
SrsMp4Box();
virtual ~SrsMp4Box();
public:
// Get the size of box, whatever small or large size.
// @remark For general box(except mdat), we use this sz() to create the buffer to codec it.
virtual uint64_t sz();
// Get the size of header, without contained boxes.
// @remark For mdat box, we must codec its header, use this instead of sz().
virtual int sz_header();
// Get the left space of box, for decoder.
virtual int left_space(SrsBuffer* buf);
// Box type helper.
virtual bool is_ftyp();
virtual bool is_moov();
virtual bool is_mdat();
// Get the contained box of specific type.
// @return The first matched box.
virtual SrsMp4Box* get(SrsMp4BoxType bt);
// Remove the contained box of specified type.
// @return The removed count.
virtual int remove(SrsMp4BoxType bt);
// Dumps the box and all contained boxes.
virtual std::stringstream& dumps(std::stringstream& ss, SrsMp4DumpContext dc);
/**
* Discovery the box from buffer.
* @param ppbox Output the discoveried box, which user must free it.
*/
static int discovery(SrsBuffer* buf, SrsMp4Box** ppbox);
// Interface ISrsCodec
public:
virtual int nb_bytes();
virtual int encode(SrsBuffer* buf);
virtual int decode(SrsBuffer* buf);
protected:
virtual int encode_boxes(SrsBuffer* buf);
virtual int decode_boxes(SrsBuffer* buf);
// Sub classes can override these functions for special codec.
// @remark For mdat box, we use completely different codec.
protected:
// The size of header, not including the contained boxes.
virtual int nb_header();
// It's not necessary to check the buffer, because we already know the size in parent function,
// so we have checked the buffer is ok to write.
virtual int encode_header(SrsBuffer* buf);
// It's not necessary to check the buffer, unless the box is not only determined by the verson.
// Generally, it's not necessary, that is, all boxes is determinated by version.
virtual int decode_header(SrsBuffer* buf);
// Whether there contained boxes in header.
virtual bool boxes_in_header();
// @remark internal for template methods.
public:
// Dumps the detail of box.
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 4.2 Object Structure
* ISO_IEC_14496-12-base-format-2012.pdf, page 17
*/
class SrsMp4FullBox : public SrsMp4Box
{
public:
// an integer that specifies the version of this format of the box.
uint8_t version;
// a map of flags
uint32_t flags;
public:
SrsMp4FullBox();
virtual ~SrsMp4FullBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 4.3 File Type Box (ftyp)
* ISO_IEC_14496-12-base-format-2012.pdf, page 17
* Files written to this version of this specification must contain a file-type box. For compatibility with an earlier
* version of this specification, files may be conformant to this specification and not contain a file-type box. Files
* with no file-type box should be read as if they contained an FTYP box with Major_brand='mp41', minor_version=0, and
* the single compatible brand 'mp41'.
*/
class SrsMp4FileTypeBox : public SrsMp4Box
{
public:
// a brand identifier
SrsMp4BoxBrand major_brand;
// an informative integer for the minor version of the major brand
uint32_t minor_version;
private:
// a list, to the end of the box, of brands
std::vector<SrsMp4BoxBrand> compatible_brands;
public:
SrsMp4FileTypeBox();
virtual ~SrsMp4FileTypeBox();
public:
virtual void set_compatible_brands(SrsMp4BoxBrand b0, SrsMp4BoxBrand b1);
virtual void set_compatible_brands(SrsMp4BoxBrand b0, SrsMp4BoxBrand b1, SrsMp4BoxBrand b2, SrsMp4BoxBrand b3);
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.16.2 Segment Type Box (styp)
* ISO_IEC_14496-12-base-format-2012.pdf, page 105
* If segments are stored in separate files (e.g. on a standard HTTP server) it is recommended that these
* 'segment files' contain a segment-type box, which must be first if present, to enable identification of those files,
* and declaration of the specifications with which they are compliant.
*/
class SrsMp4SegmentTypeBox : public SrsMp4FileTypeBox
{
public:
SrsMp4SegmentTypeBox();
virtual ~SrsMp4SegmentTypeBox();
};
/**
* 8.8.4 Movie Fragment Box (moof)
* ISO_IEC_14496-12-base-format-2012.pdf, page 66
* The movie fragments extend the presentation in time. They provide the information that would previously have
* been in the Movie Box. The actual samples are in Media Data Boxes, as usual, if they are in the same file.
* The data reference index is in the sample description, so it is possible to build incremental presentations
* where the media data is in files other than the file containing the Movie Box.
*/
class SrsMp4MovieFragmentBox : public SrsMp4Box
{
public:
SrsMp4MovieFragmentBox();
virtual ~SrsMp4MovieFragmentBox();
public:
// Get the header of moof.
virtual SrsMp4MovieFragmentHeaderBox* mfhd();
virtual void set_mfhd(SrsMp4MovieFragmentHeaderBox* v);
// Get the traf.
virtual SrsMp4TrackFragmentBox* traf();
virtual void set_traf(SrsMp4TrackFragmentBox* v);
};
/**
* 8.8.5 Movie Fragment Header Box (mfhd)
* ISO_IEC_14496-12-base-format-2012.pdf, page 67
* The movie fragment header contains a sequence number, as a safety check. The sequence number usually
* starts at 1 and must increase for each movie fragment in the file, in the order in which they occur. This allows
* readers to verify integrity of the sequence; it is an error to construct a file where the fragments are out of
* sequence.
*/
class SrsMp4MovieFragmentHeaderBox : public SrsMp4FullBox
{
public:
// the ordinal number of this fragment, in increasing order
uint32_t sequence_number;
public:
SrsMp4MovieFragmentHeaderBox();
virtual ~SrsMp4MovieFragmentHeaderBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.8.6 Track Fragment Box (traf)
* ISO_IEC_14496-12-base-format-2012.pdf, page 67
* Within the movie fragment there is a set of track fragments, zero or more per track. The track fragments in
* turn contain zero or more track runs, each of which document a contiguous run of samples for that track.
* Within these structures, many fields are optional and can be defaulted.
*/
class SrsMp4TrackFragmentBox : public SrsMp4Box
{
public:
SrsMp4TrackFragmentBox();
virtual ~SrsMp4TrackFragmentBox();
public:
// Get the tfhd.
virtual SrsMp4TrackFragmentHeaderBox* tfhd();
virtual void set_tfhd(SrsMp4TrackFragmentHeaderBox* v);
// Get the tfdt.
virtual SrsMp4TrackFragmentDecodeTimeBox* tfdt();
virtual void set_tfdt(SrsMp4TrackFragmentDecodeTimeBox* tfdt);
// Get the trun.
virtual SrsMp4TrackFragmentRunBox* trun();
virtual void set_trun(SrsMp4TrackFragmentRunBox* v);
};
/**
* The tf_flags of tfhd.
* ISO_IEC_14496-12-base-format-2012.pdf, page 68
*/
enum SrsMp4TfhdFlags
{
/**
* indicates the presence of the base-data-offset field. This provides
* an explicit anchor for the data offsets in each track run (see below). If not provided, the base-data-
* offset for the first track in the movie fragment is the position of the first byte of the enclosing Movie
* Fragment Box, and for second and subsequent track fragments, the default is the end of the data
* defined by the preceding fragment. Fragments 'inheriting' their offset in this way must all use
* the same data-reference (i.e., the data for these tracks must be in the same file).
*/
SrsMp4TfhdFlagsBaseDataOffset = 0x000001,
/**
* indicates the presence of this field, which over-rides, in this
* fragment, the default set up in the Track Extends Box.
*/
SrsMp4TfhdFlagsSampleDescriptionIndex = 0x000002,
SrsMp4TfhdFlagsDefaultSampleDuration = 0x000008,
SrsMp4TfhdFlagsDefautlSampleSize = 0x000010,
SrsMp4TfhdFlagsDefaultSampleFlags = 0x000020,
/**
* this indicates that the duration provided in either default-sample-duration,
* or by the default-duration in the Track Extends Box, is empty, i.e. that there are no samples for this
* time interval. It is an error to make a presentation that has both edit lists in the Movie Box, and empty-
* duration fragments.
*/
SrsMp4TfhdFlagsDurationIsEmpty = 0x010000,
/**
* if base-data-offset-present is zero, this indicates that the base-data-
* offset for this track fragment is the position of the first byte of the enclosing Movie Fragment Box.
* Support for the default-base-is-moof flag is required under the ‘iso5’ brand, and it shall not be used in
* brands or compatible brands earlier than iso5.
*/
SrsMp4TfhdFlagsDefaultBaseIsMoof = 0x020000,
};
/**
* 8.8.7 Track Fragment Header Box (tfhd)
* ISO_IEC_14496-12-base-format-2012.pdf, page 68
* Each movie fragment can add zero or more fragments to each track; and a track fragment can add zero or
* more contiguous runs of samples. The track fragment header sets up information and defaults used for those
* runs of samples.
*/
class SrsMp4TrackFragmentHeaderBox : public SrsMp4FullBox
{
public:
uint32_t track_id;
// all the following are optional fields
public:
// the base offset to use when calculating data offsets
uint64_t base_data_offset;
uint32_t sample_description_index;
uint32_t default_sample_duration;
uint32_t default_sample_size;
uint32_t default_sample_flags;
public:
SrsMp4TrackFragmentHeaderBox();
virtual ~SrsMp4TrackFragmentHeaderBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.8.12 Track fragment decode time (tfdt)
* ISO_IEC_14496-12-base-format-2012.pdf, page 72
* The Track Fragment Base Media Decode Time Box provides the absolute decode time, measured on
* the media timeline, of the first sample in decode order in the track fragment. This can be useful, for example,
* when performing random access in a file; it is not necessary to sum the sample durations of all preceding
* samples in previous fragments to find this value (where the sample durations are the deltas in the Decoding
* Time to Sample Box and the sample_durations in the preceding track runs).
*/
class SrsMp4TrackFragmentDecodeTimeBox : public SrsMp4FullBox
{
public:
uint64_t base_media_decode_time;
public:
SrsMp4TrackFragmentDecodeTimeBox();
virtual ~SrsMp4TrackFragmentDecodeTimeBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* The tr_flags for trun
* ISO_IEC_14496-12-base-format-2012.pdf, page 69
*/
enum SrsMp4TrunFlags
{
// data-offset-present.
SrsMp4TrunFlagsDataOffset = 0x000001,
// this over-rides the default flags for the first sample only. This
// makes it possible to record a group of frames where the first is a key and the rest are difference
// frames, without supplying explicit flags for every sample. If this flag and field are used, sample-flags
// shall not be present.
SrsMp4TrunFlagsFirstSample = 0x000004,
// indicates that each sample has its own duration, otherwise the default is used.
SrsMp4TrunFlagsSampleDuration = 0x000100,
// each sample has its own size, otherwise the default is used.
SrsMp4TrunFlagsSampleSize = 0x000200,
// each sample has its own flags, otherwise the default is used.
SrsMp4TrunFlagsSampleFlag = 0x000400,
// each sample has a composition time offset (e.g. as used for I/P/B video in MPEG).
SrsMp4TrunFlagsSampleCtsOffset = 0x000800,
};
/**
* Entry for trun.
* ISO_IEC_14496-12-base-format-2012.pdf, page 69
*/
struct SrsMp4TrunEntry
{
SrsMp4FullBox* owner;
uint32_t sample_duration;
uint32_t sample_size;
uint32_t sample_flags;
// if version == 0, unsigned int(32); otherwise, signed int(32).
int64_t sample_composition_time_offset;
SrsMp4TrunEntry(SrsMp4FullBox* o);
virtual ~SrsMp4TrunEntry();
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.8.8 Track Fragment Run Box (trun)
* ISO_IEC_14496-12-base-format-2012.pdf, page 69
* Within the Track Fragment Box, there are zero or more Track Run Boxes. If the duration-is-empty flag is set in
* the tf_flags, there are no track runs. A track run documents a contiguous set of samples for a track.
*/
class SrsMp4TrackFragmentRunBox : public SrsMp4FullBox
{
public:
// the number of samples being added in this run; also the number of rows in the following
// table (the rows can be empty)
uint32_t sample_count;
// the following are optional fields
public:
// added to the implicit or explicit data_offset established in the track fragment header.
int32_t data_offset;
// provides a set of flags for the first sample only of this run.
uint32_t first_sample_flags;
// all fields in the following array are optional
public:
std::vector<SrsMp4TrunEntry*> entries;
public:
SrsMp4TrackFragmentRunBox();
virtual ~SrsMp4TrackFragmentRunBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.1.1 Media Data Box (mdat)
* ISO_IEC_14496-12-base-format-2012.pdf, page 29
* This box contains the media data. In video tracks, this box would contain video frames.
* A presentation may contain zero or more Media Data Boxes. The actual media data follows the type field;
* its structure is described by the metadata (see particularly the sample table, subclause 8.5, and the
* item location box, subclause 8.11.3).
*
* @remark The mdat box only decode and encode the header,
* so user must read and write the data by yourself.
* To encode mdat:
* SrsMp4MediaDataBox* mdat = new SrsMp4MediaDataBox();
* mdat->nb_data = 1024000;
*
* char* buffer = new char[mdat->sz_header()];
* SrsBuffer* buf = new SrsBuffer(buffer);
* mdat->encode(buf);
*
* file->write(buffer, mdat->sz_header()); // Write the mdat box header.
* file->write(data, size); // Write the mdat box data.
*
* To decode mdat:
* SrsMp4MediaDataBox* mdat = new SrsMp4MediaDataBox();
* char* buffer = new char[mdat->sz_header()];
* SrsBuffer* buf = ...; // Read mdat->sz_header() data from io.
*
* mdat->decode(buf); // The buf should be empty now.
* file->lseek(mdat->nb_data, SEEK_CUR); // Skip the mdat data in file.
*
* To discovery any box from file:
* SrsSimpleStream* stream = new SrsSimpleStream();
* SrsBuffer* buf = new SrsBuffer(stream...); // Create read buffer from stream.
*
* // We don't know what's the next box, so try to read 4bytes and discovery it.
* append(file, stream, 4); // Append 4bytes from file to stream.
*
* SrsMp4Box* box = NULL;
* SrsMp4Box::discovery(buf, &box);
*
* required = (box->is_mdat()? box->sz_header():box->sz()); // Now we know how many bytes we needed.
* append(file, stream, required);
* box->decode(buf);
*
* if (box->is_mdat()) {
* file->lseek(mdat->nb_data, SEEK_CUR); // Skip the mdat data in file.
* }
*/
class SrsMp4MediaDataBox : public SrsMp4Box
{
public:
// The contained media data, which we never directly read/write it.
// TODO: FIXME: Support 64bits size.
int nb_data;
public:
SrsMp4MediaDataBox();
virtual ~SrsMp4MediaDataBox();
// Interface ISrsCodec
public:
// The total size of bytes, including the sz_header() and nb_data,
// which used to write the smallsize or largesize of mp4.
virtual int nb_bytes();
// To encode the mdat box, the buf should only contains the sz_header(),
// because the mdata only encode the header.
virtual int encode(SrsBuffer* buf);
// To decode the mdat box, the buf should only contains the sz_header(),
// because the mdat only decode the header.
virtual int decode(SrsBuffer* buf);
protected:
virtual int encode_boxes(SrsBuffer* buf);
virtual int decode_boxes(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.1.2 Free Space Box (free or skip)
* ISO_IEC_14496-12-base-format-2012.pdf, page 29
*/
class SrsMp4FreeSpaceBox : public SrsMp4Box
{
private:
std::vector<char> data;
public:
SrsMp4FreeSpaceBox(SrsMp4BoxType v);
virtual ~SrsMp4FreeSpaceBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.2.1 Movie Box (moov)
* ISO_IEC_14496-12-base-format-2012.pdf, page 30
* The metadata for a presentation is stored in the single Movie Box which occurs at the top-level of a file.
* Normally this box is close to the beginning or end of the file, though this is not required.
*/
class SrsMp4MovieBox : public SrsMp4Box
{
public:
SrsMp4MovieBox();
virtual ~SrsMp4MovieBox();
public:
// Get the header of moov.
virtual SrsMp4MovieHeaderBox* mvhd();
virtual void set_mvhd(SrsMp4MovieHeaderBox* v);
// Get the movie extends header.
virtual SrsMp4MovieExtendsBox* mvex();
virtual void set_mvex(SrsMp4MovieExtendsBox* v);
// Get the first video track.
virtual SrsMp4TrackBox* video();
// Get the first audio track.
virtual SrsMp4TrackBox* audio();
// Add a new track.
virtual void add_trak(SrsMp4TrackBox* v);
// Get the number of video tracks.
virtual int nb_vide_tracks();
// Get the number of audio tracks.
virtual int nb_soun_tracks();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
};
/**
* 8.2.2 Movie Header Box (mvhd)
* ISO_IEC_14496-12-base-format-2012.pdf, page 31
*/
class SrsMp4MovieHeaderBox : public SrsMp4FullBox
{
public:
// an integer that declares the creation time of the presentation (in seconds since
// midnight, Jan. 1, 1904, in UTC time)
uint64_t creation_time;
// an integer that declares the most recent time the presentation was modified (in
// seconds since midnight, Jan. 1, 1904, in UTC time)
uint64_t modification_time;
public:
// an integer that specifies the time-scale for the entire presentation; this is the number of
// time units that pass in one second. For example, a time coordinate system that measures time in
// sixtieths of a second has a time scale of 60.
uint32_t timescale;
// an integer that declares length of the presentation (in the indicated timescale). This property
// is derived from the presentation’s tracks: the value of this field corresponds to the duration of the
// longest track in the presentation. If the duration cannot be determined then duration is set to all 1s.
uint64_t duration_in_tbn;
public:
// a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0
// (0x00010000) is normal forward playback
uint32_t rate;
// a fixed point 8.8 number that indicates the preferred playback volume. 1.0 (0x0100) is full volume.
uint16_t volume;
uint16_t reserved0;
uint64_t reserved1;
// a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex values (0,0,0x40000000).
int32_t matrix[9];
uint32_t pre_defined[6];
// a non-zero integer that indicates a value to use for the track ID of the next track to be
// added to this presentation. Zero is not a valid track ID value. The value of next_track_ID shall be
// larger than the largest track-ID in use. If this value is equal to all 1s (32-bit maxint), and a new media
// track is to be added, then a search must be made in the file for an unused track identifier.
uint32_t next_track_ID;
public:
SrsMp4MovieHeaderBox();
virtual ~SrsMp4MovieHeaderBox();
public:
// Get the duration in ms.
virtual uint64_t duration();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
// The type of track, maybe combine of types.
enum SrsMp4TrackType
{
SrsMp4TrackTypeForbidden = 0x00,
SrsMp4TrackTypeAudio = 0x01,
SrsMp4TrackTypeVideo = 0x02,
};
/**
* 8.8.1 Movie Extends Box (mvex)
* ISO_IEC_14496-12-base-format-2012.pdf, page 64
* This box warns readers that there might be Movie Fragment Boxes in this file. To know of all samples in the
* tracks, these Movie Fragment Boxes must be found and scanned in order, and their information logically
* added to that found in the Movie Box.
*/
class SrsMp4MovieExtendsBox : public SrsMp4Box
{
public:
SrsMp4MovieExtendsBox();
virtual ~SrsMp4MovieExtendsBox();
public:
// Get the track extends box.
virtual SrsMp4TrackExtendsBox* trex();
virtual void set_trex(SrsMp4TrackExtendsBox* v);
};
/**
* 8.8.3 Track Extends Box(trex)
* ISO_IEC_14496-12-base-format-2012.pdf, page 65
*/
class SrsMp4TrackExtendsBox : public SrsMp4FullBox
{
public:
// identifies the track; this shall be the track ID of a track in the Movie Box
uint32_t track_ID;
// these fields set up defaults used in the track fragments.
uint32_t default_sample_description_index;
uint32_t default_sample_duration;
uint32_t default_sample_size;
uint32_t default_sample_flags;
public:
SrsMp4TrackExtendsBox();
virtual ~SrsMp4TrackExtendsBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.3.1 Track Box (trak)
* ISO_IEC_14496-12-base-format-2012.pdf, page 32
* This is a container box for a single track of a presentation. A presentation consists of one or more tracks.
* Each track is independent of the other tracks in the presentation and carries its own temporal and spatial
* information. Each track will contain its associated Media Box.
*/
class SrsMp4TrackBox : public SrsMp4Box
{
public:
SrsMp4TrackBox();
virtual ~SrsMp4TrackBox();
public:
// Get the type of track, maybe combine of track type,
// for example, it maybe Audio|Video when contains both.
// Generally, only single type, no combination.
virtual SrsMp4TrackType track_type();
// Get the track header box.
virtual SrsMp4TrackHeaderBox* tkhd();
virtual void set_tkhd(SrsMp4TrackHeaderBox* v);
public:
// Get the chunk offset box.
virtual SrsMp4ChunkOffsetBox* stco();
// Get the sample size box.
virtual SrsMp4SampleSizeBox* stsz();
// Get the sample to chunk box.
virtual SrsMp4Sample2ChunkBox* stsc();
// Get the dts box.
virtual SrsMp4DecodingTime2SampleBox* stts();
// Get the cts/pts box.
virtual SrsMp4CompositionTime2SampleBox* ctts();
// Get the sync dts box.
virtual SrsMp4SyncSampleBox* stss();
// Get the media header box.
virtual SrsMp4MediaHeaderBox* mdhd();
public:
// For vide track, get the video codec.
virtual SrsVideoCodecId vide_codec();
// For soun track, get the audio codec.
virtual SrsAudioCodecId soun_codec();
// For H.264/AVC codec, get the sps/pps.
virtual SrsMp4AvccBox* avcc();
// For AAC codec, get the asc.
virtual SrsMp4DecoderSpecificInfo* asc();
public:
// Get the media box.
virtual SrsMp4MediaBox* mdia();
virtual void set_mdia(SrsMp4MediaBox* v);
// Get the media info box.
virtual SrsMp4MediaInformationBox* minf();
// Get the sample table box.
virtual SrsMp4SampleTableBox* stbl();
// Get the sample description box
virtual SrsMp4SampleDescriptionBox* stsd();
public:
// For H.264/AVC, get the avc1 box.
virtual SrsMp4VisualSampleEntry* avc1();
// For AAC, get the mp4a box.
virtual SrsMp4AudioSampleEntry* mp4a();
};
/**
* 8.3.2 Track Header Box (tkhd)
* ISO_IEC_14496-12-base-format-2012.pdf, page 32
*/
class SrsMp4TrackHeaderBox : public SrsMp4FullBox
{
public:
// an integer that declares the creation time of the presentation (in seconds since
// midnight, Jan. 1, 1904, in UTC time)
uint64_t creation_time;
// an integer that declares the most recent time the presentation was modified (in
// seconds since midnight, Jan. 1, 1904, in UTC time)
uint64_t modification_time;
// an integer that uniquely identifies this track over the entire life-time of this presentation.
// Track IDs are never re-used and cannot be zero.
uint32_t track_ID;
uint32_t reserved0;
// an integer that indicates the duration of this track (in the timescale indicated in the Movie
// Header Box). The value of this field is equal to the sum of the durations of all of the track’s edits. If
// there is no edit list, then the duration is the sum of the sample durations, converted into the timescale
// in the Movie Header Box. If the duration of this track cannot be determined then duration is set to all
// 1s.
uint64_t duration;
public:
uint64_t reserved1;
// specifies the front-to-back ordering of video tracks; tracks with lower numbers are closer to the
// viewer. 0 is the normal value, and -1 would be in front of track 0, and so on.
int16_t layer;
// an integer that specifies a group or collection of tracks. If this field is 0 there is no
// information on possible relations to other tracks. If this field is not 0, it should be the same for tracks
// that contain alternate data for one another and different for tracks belonging to different such groups.
// Only one track within an alternate group should be played or streamed at any one time, and must be
// distinguishable from other tracks in the group via attributes such as bitrate, codec, language, packet
// size etc. A group may have only one member.
int16_t alternate_group;
// a fixed 8.8 value specifying the track's relative audio volume. Full volume is 1.0 (0x0100) and
// is the normal value. Its value is irrelevant for a purely visual track. Tracks may be composed by
// combining them according to their volume, and then using the overall Movie Header Box volume
// setting; or more complex audio composition (e.g. MPEG-4 BIFS) may be used.
int16_t volume;
uint16_t reserved2;
// a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex (0,0,0x40000000).
int32_t matrix[9];
// the track's visual presentation size as fixed-point 16.16 values. These need
// not be the same as the pixel dimensions of the images, which is documented in the sample
// description(s); all images in the sequence are scaled to this size, before any overall transformation of
// the track represented by the matrix. The pixel dimensions of the images are the default values.
int32_t width;
int32_t height;
public:
SrsMp4TrackHeaderBox();
virtual ~SrsMp4TrackHeaderBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.6.5 Edit Box (edts)
* ISO_IEC_14496-12-base-format-2012.pdf, page 54
* An Edit Box maps the presentation time-line to the media time-line as it is stored in the file.
* The Edit Box is a container for the edit lists.
*/
class SrsMp4EditBox : public SrsMp4Box
{
public:
SrsMp4EditBox();
virtual ~SrsMp4EditBox();
};
/**
* 8.6.6 Edit List Box
* ISO_IEC_14496-12-base-format-2012.pdf, page 55
*/
struct SrsMp4ElstEntry
{
public:
// an integer that specifies the duration of this edit segment in units of the timescale
// in the Movie Header Box
uint64_t segment_duration;
// an integer containing the starting time within the media of this edit segment (in media time
// scale units, in composition time). If this field is set to –1, it is an empty edit. The last edit in a track
// shall never be an empty edit. Any difference between the duration in the Movie Header Box, and the
// track’s duration is expressed as an implicit empty edit at the end.
int64_t media_time;
public:
// specifies the relative rate at which to play the media corresponding to this edit segment. If this value is 0,
// then the edit is specifying a ‘dwell’: the media at media-time is presented for the segment-duration. Otherwise
// this field shall contain the value 1.
int16_t media_rate_integer;
int16_t media_rate_fraction;
public:
SrsMp4ElstEntry();
public:
virtual std::stringstream& dumps(std::stringstream& ss, SrsMp4DumpContext dc);
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.6.6 Edit List Box (elst)
* ISO_IEC_14496-12-base-format-2012.pdf, page 54
* This box contains an explicit timeline map. Each entry defines part of the track time-line: by mapping part of
* the media time-line, or by indicating ‘empty’ time, or by defining a ‘dwell’, where a single time-point in the
* media is held for a period.
*/
class SrsMp4EditListBox : public SrsMp4FullBox
{
public:
// an integer that gives the number of entries in the following table
std::vector<SrsMp4ElstEntry> entries;
public:
SrsMp4EditListBox();
virtual ~SrsMp4EditListBox();
protected:
virtual int nb_header();
virtual int encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf);
public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
};
/**
* 8.4.1 Media Box (mdia)
* ISO_IEC_14496-12-base-format-2012.pdf, page 36
* The media declaration container contains all the objects that declare information about the media data within a
* track.
*/
class SrsMp4MediaBox : public SrsMp4Box
{
public:
SrsMp4MediaBox();
virtual ~SrsMp4MediaBox();
public:
// Get the type of track, maybe combine of track type,
// for example, it maybe Audio|Video when contains both.
// Generally, only single type, no combination.
virtual SrsMp4TrackType track_type();
// Get the media header box.
virtual SrsMp4MediaHeaderBox* mdhd();
virtual void set_mdhd(SrsMp4MediaHeaderBox* v);
// Get the hdlr box.
virtual SrsMp4HandlerReferenceBox* hdlr();
virtual void set_hdlr(SrsMp4HandlerReferenceBox* v);
// Get the media info box.
virtual SrsMp4MediaInformationBox* minf();