1
1
mirror of https://github.com/wader/fq.git synced 2024-11-26 21:55:57 +03:00

mpeg,aac: Factor out escape value decoding

This commit is contained in:
Mattias Wadman 2021-12-14 17:31:31 +01:00
parent 28a3b71bd4
commit 9f08af31df
3 changed files with 49 additions and 31 deletions

View File

@ -71,16 +71,7 @@ func adtsFrameDecoder(d *decode.D, in interface{}) interface{} {
protectionAbsent := d.FieldBool("protection_absent", protectionAbsentNames)
objectType := d.FieldU2("profile", scalar.UAdd(1), format.MPEGAudioObjectTypeNames)
d.FieldUScalarFn("sampling_frequency", func(d *decode.D) scalar.S {
v := d.U4()
if v == 15 {
return scalar.S{Actual: d.U24()}
}
if f, ok := frequencyIndexHz[v]; ok {
return scalar.S{Actual: v, Sym: f}
}
return scalar.S{Description: "invalid"}
})
d.FieldUFn("sampling_frequency", decodeEscapeValueAbsFn(4, 24, 0), frequencyIndexHzMap)
d.FieldU1("private_bit")
d.FieldU3("channel_configuration", channelConfigurationNames)
d.FieldU1("originality")

View File

@ -17,7 +17,7 @@ func init() {
})
}
var frequencyIndexHz = map[uint64]int{
var frequencyIndexHzMap = scalar.UToSymU{
0x0: 96000,
0x1: 88200,
0x2: 64000,
@ -31,9 +31,6 @@ var frequencyIndexHz = map[uint64]int{
0xa: 11025,
0xb: 8000,
0xc: 7350,
0xd: -1,
0xe: -1,
0xf: -1,
}
var channelConfigurationNames = scalar.UToScalar{
@ -48,23 +45,8 @@ var channelConfigurationNames = scalar.UToScalar{
}
func ascDecoder(d *decode.D, in interface{}) interface{} {
objectType := d.FieldUFn("object_type", func(d *decode.D) uint64 {
n := d.U5()
if n == 31 {
n = 32 + d.U6()
}
return n
}, format.MPEGAudioObjectTypeNames)
d.FieldUScalarFn("sampling_frequency", func(d *decode.D) scalar.S {
v := d.U4()
if v == 15 {
return scalar.S{Actual: d.U24()}
}
if f, ok := frequencyIndexHz[v]; ok {
return scalar.S{Actual: v, Sym: f}
}
return scalar.S{Description: "invalid"}
})
objectType := d.FieldUFn("object_type", decodeEscapeValueCarryFn(5, 6, 0), format.MPEGAudioObjectTypeNames)
d.FieldUFn("sampling_frequency", decodeEscapeValueAbsFn(4, 24, 0), frequencyIndexHzMap)
d.FieldU4("channel_configuration", channelConfigurationNames)
// TODO: GASpecificConfig etc
d.FieldRawLen("var_aot_or_byte_align", d.BitsLeft())

45
format/mpeg/shared.go Normal file
View File

@ -0,0 +1,45 @@
package mpeg
import (
"github.com/wader/fq/pkg/decode"
)
func decodeEscapeValueFn(add int, b1 int, b2 int, b3 int) func(d *decode.D) uint64 {
return func(d *decode.D) uint64 {
n1 := d.U(b1)
n := n1
if n1 == (1<<b1)-1 {
n2 := d.U(b2)
if add != -1 {
n += n2 + uint64(add)
} else {
n = n2
}
if n2 == (1<<b2)-1 {
n3 := d.U(b3)
if add != -1 {
n += n3 + uint64(add)
} else {
n = n3
}
}
}
return n
}
}
// use last non-escaped value
func decodeEscapeValueAbsFn(b1 int, b2 int, b3 int) func(d *decode.D) uint64 {
return decodeEscapeValueFn(-1, b1, b2, b3)
}
// add values and escaped values
//nolint: deadcode,unused
func decodeEscapeValueAddFn(b1 int, b2 int, b3 int) func(d *decode.D) uint64 {
return decodeEscapeValueFn(0, b1, b2, b3)
}
// add values and escaped values+1
func decodeEscapeValueCarryFn(b1 int, b2 int, b3 int) func(d *decode.D) uint64 {
return decodeEscapeValueFn(1, b1, b2, b3)
}