support HE-AAC v1 and HE-AAC v2 tracks (#1068)

This commit is contained in:
aler9
2022-08-05 23:33:44 +02:00
parent b1513c6802
commit 592d62e859
4 changed files with 97 additions and 37 deletions

View File

@@ -16,6 +16,9 @@ type Config struct {
FrameLengthFlag bool
DependsOnCoreCoder bool
CoreCoderDelay uint16
// SBR specific
ExtensionSampleRate int
}
// Unmarshal decodes a Config.
@@ -32,8 +35,9 @@ func (c *Config) Unmarshal(buf []byte) error {
switch c.Type {
case ObjectTypeAACLC:
case ObjectTypeSBR:
default:
return fmt.Errorf("unsupported type: %d", c.Type)
return fmt.Errorf("unsupported object type: %d", c.Type)
}
sampleRateIndex, err := bits.ReadBits(buf, &pos, 4)
@@ -45,7 +49,7 @@ func (c *Config) Unmarshal(buf []byte) error {
case sampleRateIndex <= 12:
c.SampleRate = sampleRates[sampleRateIndex]
case sampleRateIndex == 15:
case sampleRateIndex == 0x0F:
tmp, err := bits.ReadBits(buf, &pos, 24)
if err != nil {
return err
@@ -75,6 +79,27 @@ func (c *Config) Unmarshal(buf []byte) error {
return fmt.Errorf("invalid channel configuration (%d)", channelConfig)
}
if c.Type == ObjectTypeSBR {
extensionSamplingFrequencyIndex, err := bits.ReadBits(buf, &pos, 4)
if err != nil {
return err
}
switch {
case extensionSamplingFrequencyIndex <= 12:
c.ExtensionSampleRate = sampleRates[extensionSamplingFrequencyIndex]
case extensionSamplingFrequencyIndex == 0x0F:
tmp, err := bits.ReadBits(buf, &pos, 24)
if err != nil {
return err
}
c.ExtensionSampleRate = int(tmp)
default:
return fmt.Errorf("invalid extension sample rate index (%d)", extensionSamplingFrequencyIndex)
}
} else {
c.FrameLengthFlag, err = bits.ReadFlag(buf, &pos)
if err != nil {
return err
@@ -101,6 +126,7 @@ func (c *Config) Unmarshal(buf []byte) error {
if extensionFlag {
return fmt.Errorf("unsupported")
}
}
return nil
}
@@ -115,9 +141,18 @@ func (c Config) marshalSize() int {
n += 4
}
if c.Type == ObjectTypeSBR {
_, ok := reverseSampleRates[c.ExtensionSampleRate]
if !ok {
n += 28
} else {
n += 4
}
} else {
if c.DependsOnCoreCoder {
n += 14
}
}
ret := n / 8
if (n % 8) != 0 {
@@ -153,9 +188,17 @@ func (c Config) Marshal() ([]byte, error) {
default:
return nil, fmt.Errorf("invalid channel count (%d)", c.ChannelCount)
}
bits.WriteBits(buf, &pos, uint64(channelConfig), 4)
if c.Type == ObjectTypeSBR {
sampleRateIndex, ok := reverseSampleRates[c.ExtensionSampleRate]
if !ok {
bits.WriteBits(buf, &pos, uint64(0x0F), 4)
bits.WriteBits(buf, &pos, uint64(c.ExtensionSampleRate), 24)
} else {
bits.WriteBits(buf, &pos, uint64(sampleRateIndex), 4)
}
} else {
if c.FrameLengthFlag {
bits.WriteBits(buf, &pos, 1, 1)
} else {
@@ -171,6 +214,7 @@ func (c Config) Marshal() ([]byte, error) {
if c.DependsOnCoreCoder {
bits.WriteBits(buf, &pos, uint64(c.CoreCoderDelay), 14)
}
}
return buf, nil
}

View File

@@ -67,6 +67,16 @@ var configCases = []struct {
CoreCoderDelay: 385,
},
},
{
"sbr (he-aac v1) 44.1khz stereo",
[]byte{0x2b, 0x8a, 0x00},
Config{
Type: ObjectTypeSBR,
SampleRate: 22050,
ChannelCount: 1,
ExtensionSampleRate: 44100,
},
},
}
func TestConfigUnmarshal(t *testing.T) {

View File

@@ -6,4 +6,5 @@ type ObjectType int
// supported types.
const (
ObjectTypeAACLC ObjectType = 2
ObjectTypeSBR ObjectType = 5
)

View File

@@ -128,6 +128,11 @@ func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
typ := strconv.FormatInt(int64(t.PayloadType), 10)
sampleRate := t.Config.SampleRate
if t.Config.ExtensionSampleRate != 0 {
sampleRate = t.Config.ExtensionSampleRate
}
return &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
@@ -137,7 +142,7 @@ func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: typ + " mpeg4-generic/" + strconv.FormatInt(int64(t.Config.SampleRate), 10) +
Value: typ + " mpeg4-generic/" + strconv.FormatInt(int64(sampleRate), 10) +
"/" + strconv.FormatInt(int64(t.Config.ChannelCount), 10),
},
{