forked from yapingcat/gomedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogg-codec.go
238 lines (204 loc) · 6.27 KB
/
ogg-codec.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package ogg
import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"github.com/yapingcat/gomedia/go-codec"
)
type oggCodec interface {
codecid() codec.CodecID
magic() []byte
magicSize() int
}
type OpusCodec struct {
}
func (opus OpusCodec) codecid() codec.CodecID {
return codec.CODECID_AUDIO_OPUS
}
func (opus OpusCodec) magic() []byte {
return []byte("OpusHead")
}
func (opus OpusCodec) magicSize() int {
return 8
}
type VP8Codec struct {
}
func (vp8 VP8Codec) codecid() codec.CodecID {
return codec.CODECID_VIDEO_VP8
}
func (vp8 VP8Codec) magic() []byte {
return []byte("OVP80")
}
func (vp8 VP8Codec) magicSize() int {
return 5
}
var codecs []oggCodec
func init() {
codecs = make([]oggCodec, 2)
codecs[0] = OpusCodec{}
codecs[1] = VP8Codec{}
}
type oggParser interface {
header(stream *oggStream, packet []byte)(err error)
packet(stream *oggStream, packet []byte) (frame []byte, pts uint64, dts uint64)
gptopts(granulePos uint64) uint64
extraData() []byte
}
func createParser(cid codec.CodecID) oggParser {
switch cid {
case codec.CODECID_AUDIO_OPUS:
return &opusDemuxer{
lastpts: ^uint64(0),
}
case codec.CODECID_VIDEO_VP8:
return &vp8Demuxer{
lastpts: ^uint64(0),
pktIdx: 0,
}
default:
panic("unsupport codecid")
}
}
type opusDemuxer struct {
extradata []byte
ctx codec.OpusContext
lastpts uint64
granule uint64
}
// opus ID head
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | 'O' | 'p' | 'u' | 's' |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | 'H' | 'e' | 'a' | 'd' |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Version = 1 | Channel Count | Pre-skip |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Input Sample Rate (Hz) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Output Gain (Q7.8 in dB) | Mapping Family| |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
// | |
// : Optional Channel Mapping Table... :
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
func (opus *opusDemuxer) header(stream *oggStream, packet []byte) (err error) {
if bytes.Equal([]byte("OpusHead"), packet[0:8]) {
opus.extradata = make([]byte, len(packet))
copy(opus.extradata, packet)
err = opus.ctx.ParseExtranData(packet)
if err != nil {
return err
}
} else if bytes.Equal([]byte("OpusTags"), packet[0:8]) {
return nil
}
return errors.New(`unsupported opus header` + strconv.Quote(string(packet)))
}
func (opus *opusDemuxer) packet(stream *oggStream, packet []byte) (frame []byte, pts uint64, dts uint64) {
if stream.lost == 1 {
return packet, opus.lastpts, opus.lastpts
}
if opus.lastpts == ^uint64(0) {
opus.lastpts = 0
}
frame = packet
pts = opus.lastpts
dts = pts
if opus.granule != stream.currentPage.granulePos && !stream.currentPage.eos {
opus.lastpts = 0
opus.granule = stream.currentPage.granulePos
}
if opus.lastpts == 0 {
var duration uint64
for _, seg := range stream.currentPage.packets {
duration += codec.OpusPacketDuration(seg)
}
opus.lastpts = opus.granule - duration - uint64(opus.ctx.Preskip)
}
duration := codec.OpusPacketDuration(packet)
opus.lastpts = opus.lastpts + duration
return
}
func (opus *opusDemuxer) gptopts(granulePos uint64) uint64 {
return 0
}
func (opus *opusDemuxer) extraData() []byte {
return opus.extradata
}
//ffmpeg oggparsevp8.c
type vp8Demuxer struct {
pktIdx uint64
lastpts uint64
granule uint64
width uint16
height uint16
sampleAspectratio uint32
frameRate uint32
extradata []byte
}
func (vp8 *vp8Demuxer) header(stream *oggStream, packet []byte) (err error) {
if !bytes.Equal([]byte("OVP80"), packet[0:5]) {
return
}
switch packet[5] {
case 0x01:
if packet[6] != 1 {
return
}
vp8.width = binary.BigEndian.Uint16(packet[8:])
vp8.height = binary.BigEndian.Uint16(packet[10:])
num := uint32(packet[12])
num = (num << 8) | uint32(packet[13])
num = (num << 8) | uint32(packet[14])
den := uint32(packet[15])
den = (den << 8) | uint32(packet[16])
den = (den << 8) | uint32(packet[17])
vp8.sampleAspectratio = num / den
num = binary.BigEndian.Uint32(packet[18:])
den = binary.BigEndian.Uint32(packet[22:])
vp8.frameRate = num / den
vp8.extradata = make([]byte, len(packet))
copy(vp8.extradata, packet)
case 0x02:
if packet[6] != 0x20 {
return
}
//TODO Parse Comment
default:
return nil
}
return nil
}
func (vp8 *vp8Demuxer) packet(stream *oggStream, packet []byte) (frame []byte, pts uint64, dts uint64) {
if stream.lost == 1 {
return packet, vp8.lastpts, vp8.lastpts
}
if vp8.granule != stream.currentPage.granulePos {
vp8.lastpts = 0
vp8.pktIdx = 0
vp8.granule = stream.currentPage.granulePos
}
var duration uint64 = 0
for i := int(vp8.pktIdx); i < len(stream.currentPage.packets); i++ {
duration += uint64((stream.currentPage.packets[i][0] >> 4) & 1)
}
vp8.lastpts = vp8.gptopts(stream.currentPage.granulePos) - duration
frame = packet
pts = vp8.lastpts
dts = pts
vp8.pktIdx++
return
}
func (vp8 *vp8Demuxer) gptopts(granulePos uint64) uint64 {
var invcnt uint64 = 0
if ((granulePos >> 30) & 3) == 0 {
invcnt = 1
}
pts := (granulePos >> 32) - invcnt
return pts
}
func (vp8 *vp8Demuxer) extraData() []byte {
return vp8.extradata
}