-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdecode.go
151 lines (129 loc) · 3.67 KB
/
decode.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
// © 2016 Steve McCoy under the MIT license. See LICENSE for details.
package ogg
import (
"bytes"
"encoding/binary"
"errors"
"io"
"strconv"
)
// A Decoder decodes an ogg stream page-by-page with its Decode method.
type Decoder struct {
// buffer for packet lengths, to avoid allocating (mss is also the max per page)
lenbuf [mss]int
r io.Reader
buf [maxPageSize]byte
}
// NewDecoder creates an ogg Decoder.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r}
}
// A Page represents a logical ogg page.
type Page struct {
// Type is a bitmask of COP, BOS, and/or EOS.
Type byte
// Serial is the bitstream serial number.
Serial uint32
// Granule is the granule position, whose meaning is dependent on the encapsulated codec.
Granule int64
// Packets are the raw packet data.
// If Type & COP != 0, the first element is
// a continuation of the previous page's last packet.
Packets [][]byte
}
// ErrBadSegs is the error used when trying to decode a page with a segment table size less than 1.
var ErrBadSegs = errors.New("invalid segment table size")
// ErrBadCrc is the error used when an ogg page's CRC field does not match the CRC calculated by the Decoder.
type ErrBadCrc struct {
Found uint32
Expected uint32
}
func (bc ErrBadCrc) Error() string {
return "invalid crc in packet: got " + strconv.FormatInt(int64(bc.Found), 16) +
", expected " + strconv.FormatInt(int64(bc.Expected), 16)
}
var oggs = []byte{'O', 'g', 'g', 'S'}
// Decode reads from d's Reader to the next ogg page, then returns the decoded Page or an error.
// The error may be io.EOF if that's what the Reader returned.
//
// The buffer underlying the returned Page's Packets' bytes is owned by the Decoder.
// It may be overwritten by subsequent calls to Decode.
//
// It is safe to call Decode concurrently on distinct Decoders if their Readers are distinct.
// Otherwise, the behavior is undefined.
func (d *Decoder) Decode() (Page, error) {
hbuf := d.buf[0:headsz]
b := 0
for {
_, err := io.ReadFull(d.r, hbuf[b:])
if err != nil {
return Page{}, err
}
i := bytes.Index(hbuf, oggs)
if i == 0 {
break
}
if i < 0 {
const n = headsz
if hbuf[n-1] == 'O' {
i = n - 1
} else if hbuf[n-2] == 'O' && hbuf[n-1] == 'g' {
i = n - 2
} else if hbuf[n-3] == 'O' && hbuf[n-2] == 'g' && hbuf[n-1] == 'g' {
i = n - 3
}
}
if i > 0 {
b = copy(hbuf, hbuf[i:])
}
}
var h pageHeader
_ = binary.Read(bytes.NewBuffer(hbuf), byteOrder, &h)
if h.Nsegs < 1 {
return Page{}, ErrBadSegs
}
nsegs := int(h.Nsegs)
segtbl := d.buf[headsz : headsz+nsegs]
_, err := io.ReadFull(d.r, segtbl)
if err != nil {
return Page{}, err
}
// A page can contain multiple packets; record their lengths from the table
// now and slice up the payload after reading it.
// I'm inclined to limit the Read calls this way,
// but it's possible it isn't worth the annoyance of iterating twice
packetlens := d.lenbuf[0:0]
payloadlen := 0
more := false
for _, l := range segtbl {
if more {
packetlens[len(packetlens)-1] += int(l)
} else {
packetlens = append(packetlens, int(l))
}
more = l == mss
payloadlen += int(l)
}
payload := d.buf[headsz+nsegs : headsz+nsegs+payloadlen]
_, err = io.ReadFull(d.r, payload)
if err != nil {
return Page{}, err
}
page := d.buf[0 : headsz+nsegs+payloadlen]
// Clear out existing crc before calculating it
page[22] = 0
page[23] = 0
page[24] = 0
page[25] = 0
crc := crc32(page)
if crc != h.Crc {
return Page{}, ErrBadCrc{h.Crc, crc}
}
packets := make([][]byte, len(packetlens))
s := 0
for i, l := range packetlens {
packets[i] = payload[s : s+l]
s += l
}
return Page{h.HeaderType, h.Serial, h.Granule, packets}, nil
}