forked from andrewzeneski/mp4
-
Notifications
You must be signed in to change notification settings - Fork 2
/
box.go
167 lines (146 loc) · 3.42 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
package mp4
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
var (
// ErrUnknownBoxType is for unknown box types
ErrUnknownBoxType = errors.New("unknown box type")
// ErrTruncatedHeader is when a head gets truncated
ErrTruncatedHeader = errors.New("truncated header")
errSmallRead = errors.New("read less than expected")
)
// BadFormatErr is type of error when an unexpected box appears in unexpected places
type BadFormatErr struct {
enclosingBox, unexpectedBox string
}
func (b *BadFormatErr) Error() string {
return fmt.Sprintf("Bad format: unexpected %s box inside box %s",
b.unexpectedBox, b.enclosingBox)
}
var decoders map[string]boxDecoder
func init() {
decoders = map[string]boxDecoder{
"ftyp": DecodeFtyp,
"moov": DecodeMoov,
"mvhd": DecodeMvhd,
"iods": DecodeIods,
"trak": DecodeTrak,
"udta": DecodeUdta,
"tkhd": DecodeTkhd,
"edts": DecodeEdts,
"elst": DecodeElst,
"mdia": DecodeMdia,
"minf": DecodeMinf,
"mdhd": DecodeMdhd,
"hdlr": DecodeHdlr,
"vmhd": DecodeVmhd,
"smhd": DecodeSmhd,
"dinf": DecodeDinf,
"dref": DecodeDref,
"sbgp": DecodeSbgp,
"sdtp": DecodeSdtp,
"sgpd": DecodeSgpd,
"stbl": DecodeStbl,
"stco": DecodeStco,
"stsc": DecodeStsc,
"stsz": DecodeStsz,
"ctts": DecodeCtts,
"stsd": DecodeStsd,
"stts": DecodeStts,
"stss": DecodeStss,
"meta": DecodeMeta,
"mdat": DecodeMdat,
"free": DecodeFree,
"name": DecodeName,
"tref": DecodeTref,
"gmhd": DecodeGmhd,
"chpl": DecodeChpl,
"co64": DecodeCo64,
}
}
// Box an atom
type Box interface {
Type() string
Size() uint64
Encode(w io.Writer) error
}
type boxDecoder func(r io.Reader, size uint64) (Box, error)
// DecodeBox decodes a box
func DecodeBox(h BoxHeader, r io.Reader) (Box, error) {
d := decoders[h.Type]
if d == nil {
d = DecodeUni(h).Decode
}
b, err := d(r, RemoveHeaderSize(h.Size))
if err != nil {
return nil, err
}
return b, nil
}
// DecodeContainer decodes a container box
func DecodeContainer(r io.Reader, size uint64) ([]Box, error) {
l := []Box{}
for {
h, err := DecodeHeader(r)
if err == io.EOF {
return l, nil
}
if err != nil {
return l, err
}
b, err := DecodeBox(h, r)
if err != nil {
return l, err
}
l = append(l, b)
size -= h.Size
if size == 0 {
return l, nil
}
}
}
// Fixed16 is 8.8 fixed point number
type Fixed16 uint16
func (f Fixed16) String() string {
return fmt.Sprintf("%d.%d", uint16(f)>>8, uint16(f)&7)
}
func fixed16(bytes []byte) Fixed16 {
return Fixed16(binary.BigEndian.Uint16(bytes))
}
func putFixed16(bytes []byte, i Fixed16) {
binary.BigEndian.PutUint16(bytes, uint16(i))
}
// Fixed32 is 16.16 fixed point number
type Fixed32 uint32
func (f Fixed32) String() string {
return fmt.Sprintf("%d.%d", uint32(f)>>16, uint32(f)&15)
}
func fixed32(bytes []byte) Fixed32 {
return Fixed32(binary.BigEndian.Uint32(bytes))
}
func putFixed32(bytes []byte, i Fixed32) {
binary.BigEndian.PutUint32(bytes, uint32(i))
}
func strtobuf(out []byte, str string, l int) {
in := []byte(str)
if l < len(in) {
copy(out, in)
} else {
copy(out, in[0:l])
}
}
func makebuf(b Box) []byte {
return make([]byte, b.Size())
}
func read(r io.Reader, size uint64) ([]byte, error) {
var buf = make([]byte, size)
if readSize, err := io.ReadFull(r, buf); err != nil && err != io.EOF {
return nil, err
} else if readSize != int(size) {
return nil, fmt.Errorf("read %d which is different from the expected %d", readSize, size)
}
return buf, nil
}