-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathelst.go
137 lines (122 loc) · 3.76 KB
/
elst.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
package mp4
import (
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// ElstBox - Edit List Box (elst - optional)
//
// Contained in : Edit Box (edts)
type ElstBox struct {
Version byte
Flags uint32
Entries []ElstEntry
}
type ElstEntry struct {
SegmentDuration uint64
MediaTime int64
MediaRateInteger int16
MediaRateFraction int16
}
// DecodeElst - box-specific decode
func DecodeElst(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
if err != nil {
return nil, err
}
sr := bits.NewFixedSliceReader(data)
return DecodeElstSR(hdr, startPos, sr)
}
// DecodeElstSR - box-specific decode
func DecodeElstSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
versionAndFlags := sr.ReadUint32()
version := byte(versionAndFlags >> 24)
entryCount := sr.ReadUint32()
b := &ElstBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
if hdr.Size != b.expectedSize(entryCount) {
return nil, fmt.Errorf("elst: expected size %d, got %d", b.expectedSize(entryCount), hdr.Size)
}
b.Entries = make([]ElstEntry, entryCount)
if version == 1 {
for i := 0; i < int(entryCount); i++ {
b.Entries[i].SegmentDuration = sr.ReadUint64()
b.Entries[i].MediaTime = sr.ReadInt64()
b.Entries[i].MediaRateInteger = sr.ReadInt16()
b.Entries[i].MediaRateFraction = sr.ReadInt16()
}
} else if version == 0 {
for i := 0; i < int(entryCount); i++ {
b.Entries[i].SegmentDuration = uint64(sr.ReadUint32())
b.Entries[i].MediaTime = int64(sr.ReadInt32())
b.Entries[i].MediaRateInteger = sr.ReadInt16()
b.Entries[i].MediaRateFraction = sr.ReadInt16()
}
} else {
return nil, fmt.Errorf("unknown version for elst")
}
return b, sr.AccError()
}
// Type - box type
func (b *ElstBox) Type() string {
return "elst"
}
// Size - calculated size of box
func (b *ElstBox) Size() uint64 {
return b.expectedSize(uint32(len(b.Entries)))
}
// expectedSize - calculate size for a given entry count
func (b *ElstBox) expectedSize(entryCount uint32) uint64 {
if b.Version == 1 {
return uint64(boxHeaderSize + 8 + uint64(entryCount)*20) // 8 = version + flags + entryCount, 20 = uint64 + int64 + 2*int16
}
return uint64(boxHeaderSize + 8 + uint64(entryCount)*12) // 8 = version + flags + entryCount, 12 = uint32 + int32 + 2*int16
}
// Encode - write box to w
func (b *ElstBox) Encode(w io.Writer) error {
sw := bits.NewFixedSliceWriter(int(b.Size()))
err := b.EncodeSW(sw)
if err != nil {
return err
}
_, err = w.Write(sw.Bytes())
return err
}
// EncodeSW - box-specific encode to slicewriter
func (b *ElstBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteUint32(uint32(len(b.Entries)))
if b.Version == 1 {
for i := range b.Entries {
sw.WriteUint64(b.Entries[i].SegmentDuration)
sw.WriteInt64(b.Entries[i].MediaTime)
sw.WriteInt16(b.Entries[i].MediaRateInteger)
sw.WriteInt16(b.Entries[i].MediaRateFraction)
}
} else {
for i := range b.Entries {
sw.WriteUint32(uint32(b.Entries[i].SegmentDuration))
sw.WriteInt32(int32(b.Entries[i].MediaTime))
sw.WriteInt16(b.Entries[i].MediaRateInteger)
sw.WriteInt16(b.Entries[i].MediaRateFraction)
}
}
return sw.AccError()
}
// Info - write box-specific information
func (b *ElstBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
for i := 0; i < len(b.Entries); i++ {
bd.write("- entry[%d]: segmentDuration=%d mediaTime=%d, mediaRateInteger=%d "+
"mediaRateFraction=%d", i+1, b.Entries[i].SegmentDuration, b.Entries[i].MediaTime,
b.Entries[i].MediaRateInteger, b.Entries[i].MediaRateFraction)
}
return bd.err
}