-
Notifications
You must be signed in to change notification settings - Fork 78
/
ogg.go
174 lines (150 loc) · 3.9 KB
/
ogg.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
169
170
171
172
173
174
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tag
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)
var (
vorbisCommentPrefix = []byte("\x03vorbis")
opusTagsPrefix = []byte("OpusTags")
)
var oggCRC32Poly04c11db7 = oggCRCTable(0x04c11db7)
type crc32Table [256]uint32
func oggCRCTable(poly uint32) *crc32Table {
var t crc32Table
for i := 0; i < 256; i++ {
crc := uint32(i) << 24
for j := 0; j < 8; j++ {
if crc&0x80000000 != 0 {
crc = (crc << 1) ^ poly
} else {
crc <<= 1
}
}
t[i] = crc
}
return &t
}
func oggCRCUpdate(crc uint32, tab *crc32Table, p []byte) uint32 {
for _, v := range p {
crc = (crc << 8) ^ tab[byte(crc>>24)^v]
}
return crc
}
type oggPageHeader struct {
Magic [4]byte // "OggS"
Version uint8
Flags uint8
GranulePosition uint64
SerialNumber uint32
SequenceNumber uint32
CRC uint32
Segments uint8
}
type oggDemuxer struct {
packetBufs map[uint32]*bytes.Buffer
}
// Read ogg packets, can return empty slice of packets and nil err
// if more data is needed
func (o *oggDemuxer) Read(r io.Reader) ([][]byte, error) {
headerBuf := &bytes.Buffer{}
var oh oggPageHeader
if err := binary.Read(io.TeeReader(r, headerBuf), binary.LittleEndian, &oh); err != nil {
return nil, err
}
if bytes.Compare(oh.Magic[:], []byte("OggS")) != 0 {
// TODO: seek for syncword?
return nil, errors.New("expected 'OggS'")
}
segmentTable := make([]byte, oh.Segments)
if _, err := io.ReadFull(r, segmentTable); err != nil {
return nil, err
}
var segmentsSize int64
for _, s := range segmentTable {
segmentsSize += int64(s)
}
segmentsData := make([]byte, segmentsSize)
if _, err := io.ReadFull(r, segmentsData); err != nil {
return nil, err
}
headerBytes := headerBuf.Bytes()
// reset CRC to zero in header before checksum
headerBytes[22] = 0
headerBytes[23] = 0
headerBytes[24] = 0
headerBytes[25] = 0
crc := oggCRCUpdate(0, oggCRC32Poly04c11db7, headerBytes)
crc = oggCRCUpdate(crc, oggCRC32Poly04c11db7, segmentTable)
crc = oggCRCUpdate(crc, oggCRC32Poly04c11db7, segmentsData)
if crc != oh.CRC {
return nil, fmt.Errorf("expected crc %x != %x", oh.CRC, crc)
}
if o.packetBufs == nil {
o.packetBufs = map[uint32]*bytes.Buffer{}
}
var packetBuf *bytes.Buffer
continued := oh.Flags&0x1 != 0
if continued {
if b, ok := o.packetBufs[oh.SerialNumber]; ok {
packetBuf = b
} else {
return nil, fmt.Errorf("could not find continued packet %d", oh.SerialNumber)
}
} else {
packetBuf = &bytes.Buffer{}
}
var packets [][]byte
var p int
for _, s := range segmentTable {
packetBuf.Write(segmentsData[p : p+int(s)])
if s < 255 {
packets = append(packets, packetBuf.Bytes())
packetBuf = &bytes.Buffer{}
}
p += int(s)
}
o.packetBufs[oh.SerialNumber] = packetBuf
return packets, nil
}
// ReadOGGTags reads OGG metadata from the io.ReadSeeker, returning the resulting
// metadata in a Metadata implementation, or non-nil error if there was a problem.
// See http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html
// and http://www.xiph.org/ogg/doc/framing.html for details.
// For Opus see https://tools.ietf.org/html/rfc7845
func ReadOGGTags(r io.Reader) (Metadata, error) {
od := &oggDemuxer{}
for {
bs, err := od.Read(r)
if err != nil {
return nil, err
}
for _, b := range bs {
switch {
case bytes.HasPrefix(b, vorbisCommentPrefix):
m := &metadataOGG{
newMetadataVorbis(),
}
err = m.readVorbisComment(bytes.NewReader(b[len(vorbisCommentPrefix):]))
return m, err
case bytes.HasPrefix(b, opusTagsPrefix):
m := &metadataOGG{
newMetadataVorbis(),
}
err = m.readVorbisComment(bytes.NewReader(b[len(opusTagsPrefix):]))
return m, err
}
}
}
}
type metadataOGG struct {
*metadataVorbis
}
func (m *metadataOGG) FileType() FileType {
return OGG
}