-
Notifications
You must be signed in to change notification settings - Fork 84
/
mehd.go
92 lines (81 loc) · 2.05 KB
/
mehd.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
package mp4
import (
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// MehdBox - Movie Extends Header Box
// Optional, provides overall duration of a fragmented movie
type MehdBox struct {
Version byte
Flags uint32
FragmentDuration int64
}
// DecodeMehd - box-specific decode
func DecodeMehd(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 DecodeMehdSR(hdr, startPos, sr)
}
// DecodeMehdSR - box-specific decode
func DecodeMehdSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
versionAndFlags := sr.ReadUint32()
version := byte(versionAndFlags >> 24)
b := &MehdBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
if version == 0 {
b.FragmentDuration = int64(sr.ReadInt32())
} else {
b.FragmentDuration = sr.ReadInt64()
}
return b, sr.AccError()
}
// Type - return box type
func (b *MehdBox) Type() string {
return "mehd"
}
// Size - return calculated size
func (b *MehdBox) Size() uint64 {
size := uint64(boxHeaderSize) + 4
if b.Version == 0 {
size += 4
} else {
size += 8
}
return size
}
// Encode - write box to w
func (b *MehdBox) 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 *MehdBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
if b.Version == 0 {
sw.WriteUint32(uint32(b.FragmentDuration))
} else {
sw.WriteUint64(uint64(b.FragmentDuration))
}
return sw.AccError()
}
// Info - write box-specific information
func (b *MehdBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - fragmentDuration: %d", b.FragmentDuration)
return bd.err
}