-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.go
2279 lines (1951 loc) · 63.1 KB
/
box.go
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
package main
import (
"fmt"
"io"
ol "github.com/ossrs/go-oryx-lib/logger"
"encoding/binary"
"reflect"
)
type Box interface {
Basic() *Mp4Box
NbHeader() int
DecodeHeader(r io.Reader) (err error)
}
type Mp4Box struct {
// 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)
SmallSize uint32
LargeSize uint64
// For box 'uuid'.
UserType [16]uint8
// The position at buffer to start demux the box.
StartPos int
// 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’.
BoxType uint32
Boxes []Box
UsedSize uint64
}
func NewMp4Box() *Mp4Box {
v := &Mp4Box{}
v.BoxType = SrsMp4BoxTypeForbidden
v.Boxes = []Box{}
v.UserType = [16]uint8{}
return v
}
// Get the size of box, whatever small or large size.
func (v *Mp4Box) sz() uint64 {
if v.SmallSize == SRS_MP4_USE_LARGE_SIZE {
return v.LargeSize
}
return uint64(v.SmallSize)
}
func (v *Mp4Box) left() uint64 {
ol.I(nil, "left:", v.sz(), v.UsedSize)
return v.sz() - v.UsedSize
}
// Get the contained box of specific type.
// @return The first matched box.
func (v *Mp4Box) get(bt uint32) (Box, error) {
for _, box := range v.Boxes {
if box.Basic().BoxType == bt {
return box, nil
}
}
return nil, fmt.Errorf("can't find bt:%v in boxes", bt)
}
// Remove the contained box of specified type.
// @return The removed count.
func (v *Mp4Box) remove(bt uint32) (nbRemoved int) {
for k, box := range v.Boxes {
if box.Basic().BoxType == bt {
v.Boxes = append(v.Boxes[:k], v.Boxes[k+1:]...)
nbRemoved ++
}
}
return
}
func (v *Mp4Box) NbHeader() int {
size := 8
if v.SmallSize == SRS_MP4_USE_LARGE_SIZE {
size += 8
}
if v.BoxType == SrsMp4BoxTypeUUID {
size += 16
}
return size
}
func (v *Mp4Box) DecodeHeader(r io.Reader) (err error) {
return
}
func (v *Mp4Box) discovery(r io.Reader) (box Box, err error) {
v.UsedSize = 0
// Discovery the size and type.
var largeSize uint64
var smallSize uint32
if err = v.Read(r, &smallSize); err != nil {
ol.E(nil, fmt.Sprintf("read small size failed, err is %v", err))
return
}
var bt uint32
if err = v.Read(r, &bt); err != nil {
ol.E(nil, fmt.Sprintf("read type failed, err is %v", err))
return
}
if smallSize == SRS_MP4_USE_LARGE_SIZE {
if err = v.Read(r, &largeSize); err != nil {
ol.E(nil, fmt.Sprintf("read large size failed, err is %v", err))
return
}
}
// Only support 31bits size.
if (largeSize > 0x7fffffff) {
err = fmt.Errorf("box overflow")
ol.E(nil, err.Error())
return
}
switch bt {
case SrsMp4BoxTypeFTYP:
box = NewMp4FileTypeBox()
case SrsMp4BoxTypeMOOV:
box = NewMp4MovieBox()
case SrsMp4BoxTypeMVHD:
box = NewMp4MovieHeaderBox()
case SrsMp4BoxTypeTRAK:
box = &Mp4TrackBox{}
case SrsMp4BoxTypeTKHD:
box = NewMp4TrackHeaderBox()
case SrsMp4BoxTypeMDIA:
box = &Mp4MediaBox{}
case SrsMp4BoxTypeMDHD:
box = &Mp4MediaHeaderBox{}
case SrsMp4BoxTypeHDLR:
box = NewMp4HandlerReferenceBox()
case SrsMp4BoxTypeMINF:
box = &Mp4MediaInformationBox{}
case SrsMp4BoxTypeVMHD:
box = NewMp4VideoMediaHeaderBox()
case SrsMp4BoxTypeDINF:
box = &Mp4DataInformationBox{}
case SrsMp4BoxTypeSTBL:
box = &Mp4SampleTableBox{}
case SrsMp4BoxTypeAVC1:
box = NewMp4VisualSampleEntry()
case SrsMp4BoxTypeAVCC:
box = &Mp4AvccBox{}
case SrsMp4BoxTypeMP4A:
box = &Mp4AudioSampleEntry{}
case SrsMp4BoxTypeESDS:
box = NewMp4EsdsBox()
case SrsMp4BoxTypeSTSD:
box = NewMp4SampleDescritionBox()
case SrsMp4BoxTypeSTTS:
box = NewMp4DecodingTime2SampleBox()
case SrsMp4BoxTypeCTTS:
box = NewMp4CompositionTime2SampleBox()
case SrsMp4BoxTypeSTSS:
box = NewMp4SyncSampleBox()
case SrsMp4BoxTypeSTSC:
box = NewMp4Sample2ChunkBox()
case SrsMp4BoxTypeSTSZ:
box = NewMp4SampleSizeBox()
case SrsMp4BoxTypeSTCO:
box = NewMp4ChunkOffsetBox()
case SrsMp4BoxTypeUDTA:
box = NewMp4UserDataBox()
case SrsMp4BoxTypeMDAT:
box = NewMp4MediaDataBox()
default:
box = NewMp4FreeSpaceBox()
}
box.Basic().BoxType = bt
box.Basic().SmallSize = smallSize
box.Basic().LargeSize = largeSize
box.Basic().UsedSize = v.UsedSize
ol.I(nil, fmt.Sprintf("discovery a new box:%v small size=%v, large size=%v, bt=%x", reflect.TypeOf(box), smallSize, largeSize, bt))
return
}
func (v *Mp4Box) DecodeBoxes(r io.Reader) (err error) {
// read left space
left := v.left()
ol.T(nil, fmt.Sprintf("after decode header, left space:%v", left))
for {
if left <= 0 {
break
}
var box Box
if box, err = v.discovery(r); err != nil {
ol.E(nil, fmt.Sprintf("mp4 discovery contained box failed, err is %v", err))
return
}
if err = box.DecodeHeader(r); err != nil {
ol.E(nil, fmt.Sprintf("mp4 decode contained box header failed, err is %v", err))
return
}
if err = box.Basic().DecodeBoxes(r); err != nil {
ol.E(nil, fmt.Sprintf("mp4 decode contained box boxes failed, err is %v", err))
return
}
ol.T(nil, fmt.Sprintf("box:%v decode boxes success, sub boxes=%v, box.sz=%v, left=%v %v.", reflect.TypeOf(box), len(box.Basic().Boxes), box.Basic().sz(), left, left - box.Basic().sz()))
v.Boxes = append(v.Boxes, box)
left -= box.Basic().sz()
}
return
}
func (v *Mp4Box) Skip(r io.Reader, num uint64) {
if num <= 0 {
return
}
data := make([]uint8, num)
v.Read(r, data)
//v.UsedSize += num
ol.I(nil, fmt.Sprintf("skip %v bytes", num))
}
func (v *Mp4Box) Read(r io.Reader, data interface{}) (err error) {
if err = binary.Read(r, binary.BigEndian, data); err != nil {
return
}
v.UsedSize += uint64DataSize(data)
return
}
type Mp4FreeSpaceBox struct {
Mp4Box
needSkip int
}
func NewMp4FreeSpaceBox() *Mp4FreeSpaceBox {
v := &Mp4FreeSpaceBox{
}
return v
}
func (v *Mp4FreeSpaceBox) Basic() *Mp4Box {
return &v.Mp4Box
}
func (v *Mp4FreeSpaceBox) NbHeader() int {
return v.Mp4Box.NbHeader() + v.needSkip
}
func (v *Mp4FreeSpaceBox) DecodeHeader(r io.Reader) (err error) {
v.needSkip = int(v.left())
v.Skip(r, v.left())
return
}
// ftyp box
type Mp4FileTypeBox struct {
Mp4Box
majorBrand uint32
minorVersion uint32
compatibleBrands []uint32
}
func NewMp4FileTypeBox() *Mp4FileTypeBox {
v := &Mp4FileTypeBox{
majorBrand: SrsMp4BoxBrandForbidden,
minorVersion: 0,
compatibleBrands: []uint32{},
}
return v
}
func (v *Mp4FileTypeBox) setCompatibleBrands(b0, b1, b2, b3 uint32) {
v.compatibleBrands = append(v.compatibleBrands, []uint32{b0, b1, b2, b3}...)
}
func (v *Mp4FileTypeBox) DecodeHeader(r io.Reader) (err error) {
/*if err = v.Mp4Box.DecodeHeader(r); err != nil {
return err
}*/
ol.I(nil, fmt.Sprintf("decode ftyp box, usedSize=%v", v.UsedSize))
if err = v.Read(r, &v.majorBrand); err != nil {
ol.E(nil, fmt.Sprintf("read major brand failed, err is %v", err))
return
}
if err = v.Read(r, &v.minorVersion); err != nil {
ol.E(nil, fmt.Sprintf("read minor version failed, err is %v", err))
return
}
// Compatible brands to the end of the box.
left := v.Mp4Box.left()
if (left > 0) {
for i := 0; i < int(left) / 4; i ++ {
var brand uint32
if err = v.Read(r, &brand); err != nil {
ol.E(nil, fmt.Sprintf("read brand failed, err is %v", err))
return
}
v.compatibleBrands = append(v.compatibleBrands, brand)
}
}
return
}
func (v *Mp4FileTypeBox) Basic() *Mp4Box {
return &v.Mp4Box
}
func (v *Mp4FileTypeBox) NbHeader() int {
return v.Mp4Box.NbHeader() + 8 + len(v.compatibleBrands) * 4
}
/**
* 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.
*/
// moov box
type Mp4MovieBox struct {
Mp4Box
}
func NewMp4MovieBox() *Mp4MovieBox {
v := &Mp4MovieBox{}
return v
}
// Get the header of moov.
func (v *Mp4MovieBox) Mvhd() (*Mp4MovieHeaderBox, error) {
if box, err := v.get(SrsMp4BoxTypeMVHD); err != nil {
return nil, err
} else {
return box.(*Mp4MovieHeaderBox), nil
}
}
func (v *Mp4MovieBox) Video() (*Mp4TrackBox, error) {
for _, box := range v.Boxes {
if tbox, ok := box.(*Mp4TrackBox); ok {
if tbox.trackType() == SrsMp4TrackTypeVideo {
return tbox, nil
}
}
}
return nil, fmt.Errorf("can't find video trak box in moov")
}
func (v *Mp4MovieBox) Audio() (*Mp4TrackBox, error) {
for _, box := range v.Boxes {
if tbox, ok := box.(*Mp4TrackBox); ok {
if tbox.trackType() == SrsMp4TrackTypeAudio {
return tbox, nil
}
}
}
return nil, fmt.Errorf("can't find audio trak box in moov")
}
// Get the number of video tracks
func (v *Mp4MovieBox) NbVideoTracks() (nb_tracks int) {
for _, box := range v.Boxes {
if tbox, ok := box.(*Mp4TrackBox); ok {
if tbox.trackType() == SrsMp4TrackTypeVideo {
nb_tracks ++
}
}
}
return
}
// Get the number of audio tracks
func (v *Mp4MovieBox) NbSoundTracks() (nb_tracks int) {
for _, box := range v.Boxes {
if tbox, ok := box.(*Mp4TrackBox); ok {
if tbox.trackType() == SrsMp4TrackTypeAudio {
nb_tracks ++
}
}
}
return
}
func (v *Mp4MovieBox) Basic() *Mp4Box {
return &v.Mp4Box
}
func (v *Mp4MovieBox) NbHeader() int {
return v.Mp4Box.NbHeader()
}
/**
* 4.2 Object Structure
* ISO_IEC_14496-12-base-format-2012.pdf, page 17
*/
type Mp4FullBox struct {
Mp4Box
Version uint8
Flags uint32
}
func (v *Mp4FullBox) Basic() *Mp4Box {
return &v.Mp4Box
}
func (v *Mp4FullBox) NbHeader() int {
return v.Mp4Box.NbHeader() + 1 + 3
}
func (v *Mp4FullBox) DecodeHeader(r io.Reader) (err error) {
/*if err = v.Mp4Box.DecodeHeader(r); err != nil {
return
}*/
if err = v.Read(r, &v.Flags); err != nil {
ol.E(nil, fmt.Sprintf("read moov flags failed, err is %v", err))
return
}
v.Version = uint8((v.Flags >> 24) & 0xff)
v.Flags = v.Flags & 0x00ffffff
return
}
/**
* 8.2.2 Movie Header Box (mvhd)
* ISO_IEC_14496-12-base-format-2012.pdf, page 31
*/
type Mp4MovieHeaderBox struct {
Mp4FullBox
// an integer that declares the creation time of the presentation (in seconds since
// midnight, Jan. 1, 1904, in UTC time)
CreateTime uint64
// an integer that declares the most recent time the presentation was modified (in
// seconds since midnight, Jan. 1, 1904, in UTC time)
ModTime uint64
// 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.
TimeScale uint32
// 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.
DurationInTbn uint64
// a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0
// (0x00010000) is normal forward playback
Rate uint32
// a fixed point 8.8 number that indicates the preferred playback volume. 1.0 (0x0100) is full volume.
Volume uint16
Reserved0 uint16
Reserved1 uint64
// a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex values (0,0,0x40000000).
Matrix [9]int32
PreDefined [6]uint32
// 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.
NextTrackId uint32
}
func NewMp4MovieHeaderBox() *Mp4MovieHeaderBox {
v := &Mp4MovieHeaderBox{
Matrix: [9]int32{},
PreDefined: [6]uint32{},
}
return v
}
// Get the duration in ms
func (v *Mp4MovieHeaderBox) Duration() uint64 {
if v.TimeScale > 0 {
return v.DurationInTbn * 1000 / uint64(v.TimeScale)
}
return 0
}
func (v *Mp4MovieHeaderBox) Basic() *Mp4Box {
return &v.Mp4FullBox.Mp4Box
}
func (v *Mp4MovieHeaderBox) NbHeader() int {
return 0
}
func (v *Mp4MovieHeaderBox) DecodeHeader(r io.Reader) (err error) {
if err = v.Mp4FullBox.DecodeHeader(r); err != nil {
return
}
if v.Version == 1 {
if err = v.Read(r, &v.CreateTime); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd create time failed, err is %v", err))
return
}
if err = v.Read(r, &v.ModTime); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd mod time failed, err is %v", err))
return
}
if err = v.Read(r, &v.TimeScale); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd time scale failed, err is %v", err))
return
}
if err = v.Read(r, &v.DurationInTbn); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd duration failed, err is %v", err))
return
}
} else {
var tmp uint32
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd create time failed, err is %v", err))
return
}
v.CreateTime = uint64(tmp)
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd mod time failed, err is %v", err))
return
}
v.ModTime = uint64(tmp)
if err = v.Read(r, &v.TimeScale); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd time scale failed, err is %v", err))
return
}
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd duration failed, err is %v", err))
return
}
v.DurationInTbn = uint64(tmp)
}
if err = v.Read(r, &v.Rate); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd rate failed, err is %v", err))
return
}
if err = v.Read(r, &v.Volume); err != nil {
ol.E(nil, fmt.Sprintf("read mvhd volume failed, err is %v", err))
return
}
v.Skip(r, v.left())
return
}
/**
* 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.
*/
type Mp4TrackBox struct {
Mp4Box
}
func (v *Mp4TrackBox) Basic() *Mp4Box {
return &v.Mp4Box
}
func (v *Mp4TrackBox) NdHeader() int {
return v.Mp4Box.NbHeader()
}
func (v *Mp4TrackBox) vide_codec() (codec int) {
codec = SrsVideoCodecIdForbidden
if box, err := v.stsd(); err != nil {
return
} else if len(box.Entries) == 0 {
return
} else {
entry := box.Entries[0]
if _, ok := entry.(*Mp4VisualSampleEntry); ok {
codec = SrsVideoCodecIdAVC
}
}
return
}
func (v *Mp4TrackBox) soun_codec() (codec int) {
codec = SrsAudioCodecIdForbidden
if box, err := v.stsd(); err != nil {
return
} else if len(box.Entries) == 0 {
return
} else {
entry := box.Entries[0]
if _, ok := entry.(*Mp4AudioSampleEntry); ok {
codec = SrsAudioCodecIdAAC
}
}
return
}
func (v *Mp4TrackBox) trackType() int {
if box, err := v.get(SrsMp4BoxTypeMDIA); err != nil {
return SrsMp4TrackTypeForbidden
} else {
mdia := box.(*Mp4MediaBox)
return mdia.trackType()
}
}
func (v *Mp4TrackBox) stsc() (*Mp4Sample2ChunkBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.stsc()
}
}
func (v *Mp4TrackBox) stts() (*Mp4DecodingTime2SampleBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.stts()
}
}
func (v *Mp4TrackBox) ctts() (*Mp4CompositionTime2SampleBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.ctts()
}
}
func (v *Mp4TrackBox) stsz() (*Mp4SampleSizeBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.stsz()
}
}
func (v *Mp4TrackBox) stss() (*Mp4SyncSampleBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.stss()
}
}
func (v *Mp4TrackBox) stco() (*Mp4ChunkOffsetBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.stco()
}
}
func (v *Mp4TrackBox) mdhd() (*Mp4MovieHeaderBox, error) {
if box, err := v.mdia(); err != nil {
return nil, err
} else {
return box.mdhd()
}
}
func (v *Mp4TrackBox) mdia() (*Mp4MediaBox, error) {
if box, err := v.get(SrsMp4BoxTypeMDIA); err != nil {
return nil, err
} else {
return box.(*Mp4MediaBox), nil
}
}
func (v *Mp4TrackBox) minf() (*Mp4MediaInformationBox, error) {
if box, err := v.mdia(); err != nil {
return nil, err
} else {
return box.minf()
}
}
func (v *Mp4TrackBox) stbl() (*Mp4SampleTableBox, error) {
if box, err := v.minf(); err != nil {
return nil, err
} else {
return box.stbl()
}
}
func (v *Mp4TrackBox) stsd() (*Mp4SampleDescritionBox, error) {
if box, err := v.stbl(); err != nil {
return nil, err
} else {
return box.stsd()
}
}
func (v *Mp4TrackBox) mp4a() (*Mp4AudioSampleEntry, error) {
if box, err := v.stsd(); err != nil {
return nil, err
} else {
return box.mp4a()
}
}
func (v *Mp4TrackBox) avc1() (*Mp4VisualSampleEntry, error) {
if box, err := v.stsd(); err != nil {
return nil, err
} else {
return box.avc1()
}
}
func (v *Mp4TrackBox) avcc() (*Mp4AvccBox, error) {
if box, err := v.avc1(); err != nil {
return nil, err
} else {
return box.avcc()
}
}
func (v *Mp4TrackBox) asc() (*Mp4DecoderSpecificInfo, error) {
if box, err := v.mp4a(); err != nil {
return nil, err
} else {
return box.asc()
}
}
/**
* 8.3.2 Track Header Box (tkhd)
* ISO_IEC_14496-12-base-format-2012.pdf, page 32
*/
type Mp4TrackHeaderBox struct {
Mp4FullBox
// an integer that declares the creation time of the presentation (in seconds since
// midnight, Jan. 1, 1904, in UTC time)
CreateTime uint64
ModTime uint64
TrackId uint32
Reserved0 uint32
Duration uint64
Reserved1 uint64
Layer int16
AlternateGroup int16
Volume int16
Reserved2 uint16
Matrix [9]int32
Width int32
Height int32
}
func NewMp4TrackHeaderBox() *Mp4TrackHeaderBox {
v := &Mp4TrackHeaderBox{
Matrix: [9]int32{0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000},
}
v.Flags = 0x03
return v
}
func (v *Mp4TrackHeaderBox) Basic() *Mp4Box {
return &v.Mp4FullBox.Mp4Box
}
func (v *Mp4TrackHeaderBox) NbHeader() int {
return v.Mp4FullBox.NbHeader()
}
func (v *Mp4TrackHeaderBox) DecodeHeader(r io.Reader) (err error) {
if err = v.Mp4FullBox.DecodeHeader(r); err != nil {
return
}
if v.Version == 1 {
if err = v.Read(r, &v.CreateTime); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read create time failed, err is %v", err))
return
}
if err = v.Read(r, &v.ModTime); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read mod time failed, err is %v", err))
return
}
if err = v.Read(r, &v.TrackId); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read track id failed, err is %v", err))
return
}
v.Skip(r, uint64(4))
if err = v.Read(r, &v.Duration); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read duration failed, err is %v", err))
return
}
} else {
var tmp uint32
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read create time failed, err is %v", err))
return
}
v.CreateTime = uint64(tmp)
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("tkhd mod time failed, err is %v", err))
return
}
v.ModTime = uint64(tmp)
if err = v.Read(r, &v.TrackId); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read track id failed, err is %v", err))
return
}
v.Skip(r, uint64(4))
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read duration failed, err is %v", err))
return
}
v.Duration = uint64(tmp)
}
v.Skip(r, uint64(8))
if err = v.Read(r, &v.Layer); err != nil {
ol.E(nil, fmt.Sprintf("read tkhd layer failed, err is %v", err))
return
}
if err = v.Read(r, &v.AlternateGroup); err != nil {
ol.E(nil, fmt.Sprintf("read tkhd alternate froup failed, err is %v", err))
return
}
if err = v.Read(r, &v.Volume); err != nil {
ol.E(nil, fmt.Sprintf("read tkhd volume failed, err is %v", err))
return
}
v.Skip(r, uint64(2))
for i := 0; i < len(v.Matrix); i ++ {
if err = v.Read(r, &v.Matrix[i]); err != nil {
ol.E(nil, fmt.Sprintf("read tkhd matrix %d failed, err is %v", i, err))
return
}
}
//TODO: width and height is 16.16 format, need to be convert
if err = v.Read(r, &v.Width); err != nil {
ol.E(nil, fmt.Sprintf("read tkhd width failed, err is %v", err))
return
}
if err = v.Read(r, &v.Height); err != nil {
ol.E(nil, fmt.Sprintf("read tkhd height failed, err is %v", err))
return
}
ol.T(nil, fmt.Sprintf("decode tkhd:%+v", v))
return
}
/**
* 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.
*/
type Mp4MediaBox struct {
Mp4Box
}
func (v *Mp4MediaBox) Basic() *Mp4Box {
return &v.Mp4Box
}
func (v *Mp4MediaBox) mdhd() (*Mp4MovieHeaderBox, error) {
if box, err := v.get(SrsMp4BoxTypeMDHD); err != nil {
return nil, err
} else {
return box.(*Mp4MovieHeaderBox), nil
}
}
func (v *Mp4MediaBox) minf() (*Mp4MediaInformationBox, error) {
if box, err := v.get(SrsMp4BoxTypeMINF); err != nil {
return nil, err
} else {
return box.(*Mp4MediaInformationBox), nil
}
}
func (v *Mp4MediaBox) trackType() int {
if box, err := v.get(SrsMp4BoxTypeHDLR); err != nil {
return SrsMp4TrackTypeForbidden
} else {
hdlr := box.(*Mp4HandlerReferenceBox)
if hdlr.HandlerType == SrsMp4HandlerTypeSOUN {
return SrsMp4TrackTypeAudio
}
if hdlr.HandlerType == SrsMp4HandlerTypeVIDE {
return SrsMp4TrackTypeVideo
}
}
return SrsMp4TrackTypeForbidden
}
/**
* 8.4.2 Media Header Box (mdhd)
* 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.
*/
type Mp4MediaHeaderBox struct {
Mp4FullBox
// an integer that declares the creation time of the presentation (in seconds since
// midnight, Jan. 1, 1904, in UTC time)
CreateTime uint64
// an integer that declares the most recent time the presentation was modified (in
// seconds since midnight, Jan. 1, 1904, in UTC time)
ModTime uint64
// 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.
TimeScale uint32
// 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.
Duration uint64
// the language code for this media. See ISO 639-2/T for the set of three character
// codes. Each character is packed as the difference between its ASCII value and 0x60. Since the code
// is confined to being three lower-case letters, these values are strictly positive.
Language uint16
PreDefined uint16
}
func (v *Mp4MediaHeaderBox) Basic() *Mp4Box {
return &v.Mp4FullBox.Mp4Box
}
func (v *Mp4MediaHeaderBox) DecodeHeader(r io.Reader) (err error) {
if err = v.Mp4FullBox.DecodeHeader(r); err != nil {
return
}
if v.Version == 1 {
if err = v.Read(r, &v.CreateTime); err != nil {
ol.E(nil, fmt.Sprintf("mdhd read create time failed, err is %v", err))
return
}
if err = v.Read(r, &v.ModTime); err != nil {
ol.E(nil, fmt.Sprintf("mdhd read mod time failed, err is %v", err))
return
}
if err = v.Read(r, &v.TimeScale); err != nil {
ol.E(nil, fmt.Sprintf("mdhd read timescale failed, err is %v", err))
return
}
if err = v.Read(r, &v.Duration); err != nil {
ol.E(nil, fmt.Sprintf("tkhd read duration failed, err is %v", err))
return
}
} else {
var tmp uint32
if err = v.Read(r, &tmp); err != nil {
ol.E(nil, fmt.Sprintf("mdhd read create time failed, err is %v", err))
return
}
v.CreateTime = uint64(tmp)
if err = v.Read(r, &tmp); err != nil {