-
Notifications
You must be signed in to change notification settings - Fork 84
/
lou.go
286 lines (259 loc) · 8.23 KB
/
lou.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package mp4
import (
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// TlouBox - Track loudness info Box
//
// Contained in : Ludt Box (ludt)
type TlouBox struct {
loudnessBaseBox
}
// TlouBox - Album loudness info Box
//
// Contained in : Ludt Box (ludt)
type AlouBox struct {
loudnessBaseBox
}
type LoudnessBase struct {
EQSetID uint8
DownmixID uint8
DRCSetID uint8
BsSamplePeakLevel int16
BsTruePeakLevel int16
MeasurementSystemForTP uint8
ReliabilityForTP uint8
Measurements []Measurement
}
type Measurement struct {
MethodDefinition uint8
MethodValue uint8
MeasurementSystem uint8
Reliability uint8
}
// loudnessBaseBox according to ISO/IEC 14496-12 Section 12.2.7.2
type loudnessBaseBox struct {
Version byte
Flags uint32
LoudnessBases []*LoudnessBase
}
func decodeLoudnessBaseBoxSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (*loudnessBaseBox, error) {
versionAndFlags := sr.ReadUint32()
version := byte(versionAndFlags >> 24)
b := &loudnessBaseBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
var loudnessBaseCount uint8
if b.Version >= 1 {
loudnessInfoTypeAndCount := sr.ReadUint8()
loudnessInfoType := (loudnessInfoTypeAndCount >> 6) & 0x3
if loudnessInfoType != 0 {
return nil, fmt.Errorf("loudnessInfoType %d not supported", loudnessInfoType)
}
loudnessBaseCount = 0x3f & loudnessInfoTypeAndCount
} else {
loudnessBaseCount = 1
}
b.LoudnessBases = make([]*LoudnessBase, 0, loudnessBaseCount)
for a := uint8(0); a < loudnessBaseCount; a++ {
l := &LoudnessBase{}
if b.Version >= 1 {
l.EQSetID = 0x3f & sr.ReadUint8()
}
downmixIDAndDRCSetID := sr.ReadUint16()
l.DownmixID = uint8(downmixIDAndDRCSetID >> 6)
l.DRCSetID = uint8(downmixIDAndDRCSetID & 0x3f)
peakLevels := sr.ReadUint24()
l.BsSamplePeakLevel = int16(peakLevels >> 12)
l.BsTruePeakLevel = int16(peakLevels & 0x0fff)
measurementSystemAndReliablityForTP := sr.ReadUint8()
l.MeasurementSystemForTP = measurementSystemAndReliablityForTP >> 4
l.ReliabilityForTP = measurementSystemAndReliablityForTP & 0x0f
measurementCount := sr.ReadUint8()
l.Measurements = make([]Measurement, 0, measurementCount)
for i := uint8(0); i < measurementCount; i++ {
m := Measurement{}
m.MethodDefinition = sr.ReadUint8()
m.MethodValue = sr.ReadUint8()
measurementSystemAndReliablity := sr.ReadUint8()
m.MeasurementSystem = measurementSystemAndReliablity >> 4
m.Reliability = measurementSystemAndReliablity & 0x0f
l.Measurements = append(l.Measurements, m)
}
b.LoudnessBases = append(b.LoudnessBases, l)
}
return b, nil
}
func (b *loudnessBaseBox) size() uint64 {
size := 4
if b.Version >= 1 {
size += 1
}
for _, l := range b.LoudnessBases {
if b.Version >= 1 {
size += 8 + len(l.Measurements)*3
} else {
size += 7 + len(l.Measurements)*3
}
}
return uint64(size)
}
func (b *loudnessBaseBox) 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
}
func (b *loudnessBaseBox) encodeSW(sw bits.SliceWriter) error {
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
if b.Version >= 1 {
sw.WriteUint8(0x3f & uint8(len(b.LoudnessBases)))
}
for a := 0; a < len(b.LoudnessBases); a++ {
l := b.LoudnessBases[a]
if b.Version >= 1 {
sw.WriteUint8(0x3f & l.EQSetID)
}
downmixIDAndDRCSetID := (uint16(l.DownmixID) << 6) | uint16(0x3f&l.DRCSetID)
sw.WriteUint16(downmixIDAndDRCSetID)
peakLevels := (uint32(l.BsSamplePeakLevel) << 12) | uint32(0x0fff&l.BsTruePeakLevel)
sw.WriteUint24(peakLevels)
measurementSystemAndReliablityForTP := (l.MeasurementSystemForTP << 4) | (0x0f & l.ReliabilityForTP)
sw.WriteUint8(measurementSystemAndReliablityForTP)
sw.WriteUint8(uint8(len(l.Measurements)))
for i := 0; i < len(l.Measurements); i++ {
m := l.Measurements[i]
sw.WriteUint8(m.MethodDefinition)
sw.WriteUint8(m.MethodValue)
measurementSystemAndReliablity := (m.MeasurementSystem << 4) | (0x0f & m.Reliability)
sw.WriteUint8(measurementSystemAndReliablity)
}
}
return sw.AccError()
}
func (b *loudnessBaseBox) info(realBox Box, w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, realBox, int(b.Version), 0)
bd.write(" - LoudnessBaseCount: %d", len(b.LoudnessBases))
level := getInfoLevel(realBox, specificBoxLevels)
if level >= 1 {
loudnessIndent := " "
for a, l := range b.LoudnessBases {
bd.write(" - loudnessBase[%d]:", a+1)
if b.Version == 1 {
bd.write(loudnessIndent+"EQSetID=%d", l.EQSetID)
}
bd.write(loudnessIndent+"DownmixID=%d", l.DownmixID)
bd.write(loudnessIndent+"DRCSetID=%d", l.DRCSetID)
bd.write(loudnessIndent+"BsSamplePeakLevel=%d", l.BsSamplePeakLevel)
bd.write(loudnessIndent+"BsTruePeakLevel=%d", l.BsTruePeakLevel)
bd.write(loudnessIndent+"MeasurementSystemForTP=%d", l.MeasurementSystemForTP)
bd.write(loudnessIndent+"ReliabilityForTP=%d", l.ReliabilityForTP)
bd.write(loudnessIndent+"MeasurementCount=%d", len(l.Measurements))
for i := 0; i < len(l.Measurements); i++ {
msg := fmt.Sprintf(loudnessIndent+" - measurement[%d]: ", i+1)
msg += fmt.Sprintf("MethodDefinition=%d ", l.Measurements[i].MethodDefinition)
msg += fmt.Sprintf("MethodValue=%d ", l.Measurements[i].MethodValue)
msg += fmt.Sprintf("MeasurementSystem=%d ", l.Measurements[i].MeasurementSystem)
msg += fmt.Sprintf("Reliability=%d ", l.Measurements[i].Reliability)
bd.write(msg)
}
}
}
return bd.err
}
// DecodeTlou - box-specific decode
func DecodeTlou(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
if err != nil {
return nil, err
}
sr := bits.NewFixedSliceReader(data)
return DecodeTlouSR(hdr, startPos, sr)
}
// DecodeTlouSR - box-specific decode
func DecodeTlouSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
loudnessBaseBox, err := decodeLoudnessBaseBoxSR(hdr, startPos+hdr.Size, sr)
if err != nil {
return nil, err
}
return &TlouBox{loudnessBaseBox: *loudnessBaseBox}, nil
}
// Encode - write tlou container to w
func (b *TlouBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
return b.loudnessBaseBox.encode(w)
}
// Encode - write tlou container to sw
func (b *TlouBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
return b.loudnessBaseBox.encodeSW(sw)
}
// Type - return box type
func (b *TlouBox) Type() string {
return "tlou"
}
// Size - calculated size of box
func (b *TlouBox) Size() uint64 {
return b.loudnessBaseBox.size() + boxHeaderSize
}
// Info - write box-specific information
func (b *TlouBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
return b.loudnessBaseBox.info(b, w, specificBoxLevels, indent, indentStep)
}
// DecodeAlou - box-specific decode
func DecodeAlou(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
if err != nil {
return nil, err
}
sr := bits.NewFixedSliceReader(data)
return DecodeAlouBoxSR(hdr, startPos, sr)
}
// DecodeAlouSR - box-specific decode
func DecodeAlouBoxSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
loudnessBaseBox, err := decodeLoudnessBaseBoxSR(hdr, startPos+hdr.Size, sr)
if err != nil {
return nil, err
}
return &AlouBox{loudnessBaseBox: *loudnessBaseBox}, nil
}
// Encode - write alou container to w
func (b *AlouBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
return b.loudnessBaseBox.encode(w)
}
// Encode - write alou container to sw
func (b *AlouBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
return b.loudnessBaseBox.encodeSW(sw)
}
// Type - return box type
func (b *AlouBox) Type() string {
return "alou"
}
// Size - calculated size of box
func (b *AlouBox) Size() uint64 {
return b.loudnessBaseBox.size() + boxHeaderSize
}
// Info - write box-specific information
func (b *AlouBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
return b.loudnessBaseBox.info(b, w, specificBoxLevels, indent, indentStep)
}