-
Notifications
You must be signed in to change notification settings - Fork 0
/
ac3parser.go
121 lines (96 loc) · 2.46 KB
/
ac3parser.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
package ac3parser
import (
"fmt"
"time"
"github.com/vitmod/vdk/av"
)
type CodecData struct {
CodecType_ av.CodecType
SampleRate_ int
SampleFormat_ av.SampleFormat
ChannelLayout_ av.ChannelLayout
}
var (
A52HalfRate = []int{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, }
A52BitRates = []int{
32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
192, 224, 256, 320, 384, 448, 512, 576, 640,
}
/* Only store frame sizes for 44.1KHz - others are simply multiples
of the bitrate */
A52FrameSizes441 = []int{
69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209,
243, 244, 278, 279, 348, 349, 417, 418, 487, 488, 557, 558, 696, 697,
835, 836, 975, 976, 1114, 1115, 1253, 1254, 1393, 1394,
}
)
var spf int
var bitrate int
var samplerate int
func IsValidAC3FrameHeader(header []byte) bool {
if header[0] != 0x0B || header[1] != 0x77 {
return false
}
return true
}
func ParseAC3(frame []byte) (spf_ int, framelen_ int, samplerate_ int) {
i := frame[4] & 0x3E
if i > 36 {
fmt.Printf("AC3: Invalid frmsizecod: %v\n", i)
return
}
r := frame[5] >> 3
rate_shift := A52HalfRate[r]
base_bitrate := A52BitRates[i>>1]
channels := 2
switch frame[4] & 0xC0 {
case 0x00:
samplerate_ = 48000
spf_ = base_bitrate * 2 * channels
case 0x40:
samplerate_ = 44100
spf_ = A52FrameSizes441[i] * channels
case 0x80:
samplerate_ = 32000
spf_ = base_bitrate * 3 * channels
default:
fmt.Printf("AC3: Invalid samplerate code: 0x%02x\n", frame[4]&0xc0)
}
spf = spf_
samplerate = samplerate_
bitrate = (base_bitrate * 1000) >> rate_shift
framelen_ = spf_
//fmt.Printf("AC3: spf: %v, framelen: %v, samplerate: %v, bitrate: %v\n", spf_, framelen_, samplerate_, bitrate)
return
}
func (self CodecData) Type() av.CodecType {
return av.AC3
}
func (self CodecData) SampleFormat() av.SampleFormat {
return av.FLTP
}
func (self CodecData) ChannelLayout() av.ChannelLayout {
return av.CH_STEREO
}
func (self CodecData) SampleRate() int {
var samplerate_ = 48000
if samplerate > 0 {
samplerate_ = samplerate
}
return samplerate_
}
func (self CodecData) PacketDuration(data []byte) (dur time.Duration, err error) {
var spf_ = 1536
if spf > 0 {
spf_ = spf
}
dur = time.Duration(spf_) * time.Second / time.Duration(self.SampleRate())
return
}
func NewCodecDataAC3() (self CodecData) {
self.CodecType_ = self.Type()
self.SampleRate_ = self.SampleRate()
self.SampleFormat_ = self.SampleFormat()
self.ChannelLayout_ = self.ChannelLayout()
return
}