forked from mstoykov/mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelst.go
83 lines (73 loc) · 2.24 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
package mp4
import (
"encoding/binary"
"fmt"
"io"
)
// ElstBox - Edit List Box (elst - optional)
//
// Contained in : Edit Box (edts)
//
// Status: version 0 decoded. version 1 not supported
type ElstBox struct {
Version byte
Flags [3]byte
SegmentDuration, MediaTime []uint32 // should be uint32/int32 for version 0 and uint64/int32 for version 1
MediaRateInteger, MediaRateFraction []uint16 // should be int16
}
func DecodeElst(r io.Reader, size uint64) (Box, error) {
data, err := read(r, size)
if err != nil {
return nil, err
}
c := binary.BigEndian.Uint32(data[4:8])
b := &ElstBox{
Flags: [3]byte{data[1], data[2], data[3]},
Version: data[0],
MediaTime: make([]uint32, c),
SegmentDuration: make([]uint32, c),
MediaRateInteger: make([]uint16, c),
MediaRateFraction: make([]uint16, c),
}
for i := 0; i < int(c); i++ {
b.MediaTime[i] = binary.BigEndian.Uint32(data[(12 + 12*i):(16 + 12*i)])
b.SegmentDuration[i] = binary.BigEndian.Uint32(data[(8 + 12*i):(12 + 12*i)])
b.MediaRateInteger[i] = binary.BigEndian.Uint16(data[(16 + 12*i):(18 + 12*i)])
b.MediaRateFraction[i] = binary.BigEndian.Uint16(data[(18 + 12*i):(20 + 12*i)])
}
return b, nil
}
// Type returns elst
func (b *ElstBox) Type() string {
return "elst"
}
// Size retruns the size of ElstBox
func (b *ElstBox) Size() uint64 {
return uint64(8 + len(b.SegmentDuration)*12)
}
// Dump dumps the elst box
func (b *ElstBox) Dump() {
fmt.Println("Segment Duration:")
for i, d := range b.SegmentDuration {
fmt.Printf(" #%d: %d units\n", i, d)
}
}
// Encode encodes the elst box
func (b *ElstBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := makebuf(b)
buf[0] = b.Version
buf[1], buf[2], buf[3] = b.Flags[0], b.Flags[1], b.Flags[2]
binary.BigEndian.PutUint32(buf[4:], uint32(len(b.SegmentDuration)))
for i := range b.SegmentDuration {
binary.BigEndian.PutUint32(buf[8+12*i:], b.SegmentDuration[i])
binary.BigEndian.PutUint32(buf[12+12*i:], b.MediaTime[i])
binary.BigEndian.PutUint16(buf[16+12*i:], b.MediaRateInteger[i])
binary.BigEndian.PutUint16(buf[18+12*i:], b.MediaRateFraction[i])
}
_, err = w.Write(buf)
return err
}