forked from godeep/mp4
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbox.go
168 lines (146 loc) · 3.3 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
package mp4
import (
"encoding/binary"
"errors"
"fmt"
"io"
"log"
)
const (
BoxHeaderSize = 8
)
var (
ErrUnknownBoxType = errors.New("unknown box type")
ErrTruncatedHeader = errors.New("truncated header")
ErrBadFormat = errors.New("bad format")
)
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,
"stbl": DecodeStbl,
"stco": DecodeStco,
"stsc": DecodeStsc,
"stsz": DecodeStsz,
"ctts": DecodeCtts,
"stsd": DecodeStsd,
"stts": DecodeStts,
"stss": DecodeStss,
"meta": DecodeMeta,
"mdat": DecodeMdat,
"free": DecodeFree,
}
}
// The header of a box
type BoxHeader struct {
Type string
Size uint32
}
// DecodeHeader decodes a box header (size + box type)
func DecodeHeader(r io.Reader) (BoxHeader, error) {
buf := make([]byte, BoxHeaderSize)
n, err := r.Read(buf)
if err != nil {
return BoxHeader{}, err
}
if n != BoxHeaderSize {
return BoxHeader{}, ErrTruncatedHeader
}
return BoxHeader{string(buf[4:8]), binary.BigEndian.Uint32(buf[0:4])}, nil
}
// EncodeHeader encodes a box header to a writer
func EncodeHeader(b Box, w io.Writer) error {
buf := make([]byte, BoxHeaderSize)
binary.BigEndian.PutUint32(buf, uint32(b.Size()))
strtobuf(buf[4:], b.Type(), 4)
_, err := w.Write(buf)
return err
}
// A box
type Box interface {
Type() string
Size() int
Encode(w io.Writer) error
}
type BoxDecoder func(r io.Reader) (Box, error)
// DecodeBox decodes a box
func DecodeBox(h BoxHeader, r io.Reader) (Box, error) {
d := decoders[h.Type]
if d == nil {
log.Printf("Error while decoding %s : unknown box type", h.Type)
return nil, ErrUnknownBoxType
}
b, err := d(io.LimitReader(r, int64(h.Size-BoxHeaderSize)))
if err != nil {
log.Printf("Error while decoding %s : %s", h.Type, err)
return nil, err
}
return b, nil
}
// DecodeContainer decodes a container box
func DecodeContainer(r io.Reader) ([]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)
}
}
// An 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))
}
// A 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()-BoxHeaderSize)
}