forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdac3.go
188 lines (170 loc) · 4.54 KB
/
dac3.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
package mp4
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/Eyevinn/mp4ff/bits"
)
// AC3SampleRates - Sample rates as defined in ETSI TS 102 366 V1.4.1 (2017) section 4.4.1.3
// Signaled in fscod - Sample rate code - 2 bits
var AC3SampleRates = []int{48000, 44100, 32000}
// AX3acmodChanneTable - channel configurations from ETSI TS 102 366 V1.4.1 (2017) section 4.4.2.3A
// Signaled in acmod - audio coding mode - 3 bits
var AC3acmodChannelTable = []string{
"L/R", //Ch1 Ch2 dual mono but name them L R
"C",
"L/R",
"L/C/R",
"L/R/Cs",
"L/C/R/Cs",
"L/R/Ls/Rs",
"L/C/R/Ls/Rs",
}
// AC3BitrateCodesKbps - Bitrates in kbps ETSI TS 102 366 V1.4.1 Table F.4.1 (2017)
var AC3BitrateCodesKbps = []uint16{
32,
40,
48,
56,
64,
80,
96,
112,
128,
160,
192,
224,
256,
320,
384,
448,
512,
576,
640,
}
// Dac3Box - AC3SpecificBox from ETSI TS 102 366 V1.4.1 F.4 (2017)
// Extra b
type Dac3Box struct {
FSCod byte
BSID byte
BSMod byte
ACMod byte
LFEOn byte
BitRateCode byte
Reserved byte
InitialZeroes byte // Should be zero
}
// DecodeDac3 - box-specific decode
func DecodeDac3(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return decodeDac3FromData(data)
}
// DecodeDac3SR - box-specific decode
func DecodeDac3SR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
data := sr.ReadBytes(hdr.payloadLen())
if sr.AccError() != nil {
return nil, sr.AccError()
}
return decodeDac3FromData(data)
}
func decodeDac3FromData(data []byte) (Box, error) {
b := Dac3Box{}
if len(data) > 3 {
b.InitialZeroes = byte(len(data) - 3)
}
buf := bytes.NewBuffer(data)
br := bits.NewReader(buf)
for i := 0; i < int(b.InitialZeroes); i++ {
if zero := br.Read(8); zero != 0 {
return nil, fmt.Errorf("dac3 box, extra initial bytes are not zero")
}
}
b.FSCod = byte(br.Read(2))
b.BSID = byte(br.Read(5))
b.BSMod = byte(br.Read(3))
b.ACMod = byte(br.Read(3))
b.LFEOn = byte(br.Read(1))
b.BitRateCode = byte(br.Read(5))
// 5 bits reserved follows
b.Reserved = byte(br.Read(5))
return &b, nil
}
// Type - box type
func (b *Dac3Box) Type() string {
return "dac3"
}
// Size - calculated size of box
func (b *Dac3Box) Size() uint64 {
return uint64(boxHeaderSize + 3 + uint(b.InitialZeroes))
}
// Encode - write box to w
func (b *Dac3Box) Encode(w io.Writer) error {
sw := bits.NewFixedSliceWriter(int(b.Size()))
err := b.EncodeSW(sw)
if err != nil {
return err
}
_, err = w.Write(sw.Bytes())
return err
}
// Encode - write box to sw
func (b *Dac3Box) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
for i := 0; i < int(b.InitialZeroes); i++ {
sw.WriteBits(0, 8)
}
sw.WriteBits(uint(b.FSCod), 2)
sw.WriteBits(uint(b.BSID), 5)
sw.WriteBits(uint(b.BSMod), 3)
sw.WriteBits(uint(b.ACMod), 3)
sw.WriteBits(uint(b.LFEOn), 1)
sw.WriteBits(uint(b.BitRateCode), 5)
sw.WriteBits(uint(b.Reserved), 5) // 5-bits reserved
return sw.AccError()
}
// ChannelInfo - number of channels and channelmap according to E.1.3.1.8
func (b *Dac3Box) ChannelInfo() (nrChannels int, chanmap uint16) {
speakers := GetChannelListFromACMod(b.ACMod)
if b.LFEOn == 1 {
speakers = append(speakers, "LFE")
}
nrChannels = len(speakers)
for _, speaker := range speakers {
chanmap |= CustomChannelMapLocations[speaker]
}
return nrChannels, chanmap
}
func (b *Dac3Box) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, -1, 0)
bd.write(" - sampleRateCode=%d => sampleRate=%d", b.FSCod, AC3SampleRates[b.FSCod])
bd.write(" - bitStreamInformation=%d", b.BSID)
bd.write(" - audioCodingMode=%d => channelConfiguration=%q", b.ACMod, AC3acmodChannelTable[b.ACMod])
bd.write(" - lowFrequencyEffectsChannelOn=%d", b.LFEOn)
bd.write(" - bitRateCode=%d => bitrate=%dkbps", b.BitRateCode, AC3BitrateCodesKbps[b.BitRateCode])
nrChannels, chanmap := b.ChannelInfo()
bd.write(" - nrChannels=%d, chanmap=%04x", nrChannels, chanmap)
if b.Reserved != 0 {
bd.write(" - reserved=%d", b.Reserved)
}
if b.InitialZeroes > 0 {
bd.write(" - weird initial zero bytes=%d", b.InitialZeroes)
}
return bd.err
}
func (b *Dac3Box) BitrateBps() int {
return int(AC3BitrateCodesKbps[b.BitRateCode]) * 1000
}
func (b *Dac3Box) SamplingFrequency() int {
return int(AC3SampleRates[b.FSCod])
}
// GetChannelListFromACMod - get list of channels from acmod byte
func GetChannelListFromACMod(acmod byte) []string {
return strings.Split(AC3acmodChannelTable[acmod], "/")
}