mirror of
https://github.com/aler9/gortsplib
synced 2025-10-18 21:14:38 +08:00
support HE-AAC v1 and HE-AAC v2 tracks (#1068)
This commit is contained in:
@@ -16,6 +16,9 @@ type Config struct {
|
|||||||
FrameLengthFlag bool
|
FrameLengthFlag bool
|
||||||
DependsOnCoreCoder bool
|
DependsOnCoreCoder bool
|
||||||
CoreCoderDelay uint16
|
CoreCoderDelay uint16
|
||||||
|
|
||||||
|
// SBR specific
|
||||||
|
ExtensionSampleRate int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal decodes a Config.
|
// Unmarshal decodes a Config.
|
||||||
@@ -32,8 +35,9 @@ func (c *Config) Unmarshal(buf []byte) error {
|
|||||||
|
|
||||||
switch c.Type {
|
switch c.Type {
|
||||||
case ObjectTypeAACLC:
|
case ObjectTypeAACLC:
|
||||||
|
case ObjectTypeSBR:
|
||||||
default:
|
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)
|
sampleRateIndex, err := bits.ReadBits(buf, &pos, 4)
|
||||||
@@ -45,7 +49,7 @@ func (c *Config) Unmarshal(buf []byte) error {
|
|||||||
case sampleRateIndex <= 12:
|
case sampleRateIndex <= 12:
|
||||||
c.SampleRate = sampleRates[sampleRateIndex]
|
c.SampleRate = sampleRates[sampleRateIndex]
|
||||||
|
|
||||||
case sampleRateIndex == 15:
|
case sampleRateIndex == 0x0F:
|
||||||
tmp, err := bits.ReadBits(buf, &pos, 24)
|
tmp, err := bits.ReadBits(buf, &pos, 24)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -75,31 +79,53 @@ func (c *Config) Unmarshal(buf []byte) error {
|
|||||||
return fmt.Errorf("invalid channel configuration (%d)", channelConfig)
|
return fmt.Errorf("invalid channel configuration (%d)", channelConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.FrameLengthFlag, err = bits.ReadFlag(buf, &pos)
|
if c.Type == ObjectTypeSBR {
|
||||||
if err != nil {
|
extensionSamplingFrequencyIndex, err := bits.ReadBits(buf, &pos, 4)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.DependsOnCoreCoder, err = bits.ReadFlag(buf, &pos)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.DependsOnCoreCoder {
|
|
||||||
tmp, err := bits.ReadBits(buf, &pos, 14)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.CoreCoderDelay = uint16(tmp)
|
|
||||||
}
|
|
||||||
|
|
||||||
extensionFlag, err := bits.ReadFlag(buf, &pos)
|
switch {
|
||||||
if err != nil {
|
case extensionSamplingFrequencyIndex <= 12:
|
||||||
return err
|
c.ExtensionSampleRate = sampleRates[extensionSamplingFrequencyIndex]
|
||||||
}
|
|
||||||
|
|
||||||
if extensionFlag {
|
case extensionSamplingFrequencyIndex == 0x0F:
|
||||||
return fmt.Errorf("unsupported")
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
c.DependsOnCoreCoder, err = bits.ReadFlag(buf, &pos)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.DependsOnCoreCoder {
|
||||||
|
tmp, err := bits.ReadBits(buf, &pos, 14)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.CoreCoderDelay = uint16(tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
extensionFlag, err := bits.ReadFlag(buf, &pos)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if extensionFlag {
|
||||||
|
return fmt.Errorf("unsupported")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -115,8 +141,17 @@ func (c Config) marshalSize() int {
|
|||||||
n += 4
|
n += 4
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.DependsOnCoreCoder {
|
if c.Type == ObjectTypeSBR {
|
||||||
n += 14
|
_, ok := reverseSampleRates[c.ExtensionSampleRate]
|
||||||
|
if !ok {
|
||||||
|
n += 28
|
||||||
|
} else {
|
||||||
|
n += 4
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if c.DependsOnCoreCoder {
|
||||||
|
n += 14
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := n / 8
|
ret := n / 8
|
||||||
@@ -153,23 +188,32 @@ func (c Config) Marshal() ([]byte, error) {
|
|||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid channel count (%d)", c.ChannelCount)
|
return nil, fmt.Errorf("invalid channel count (%d)", c.ChannelCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
bits.WriteBits(buf, &pos, uint64(channelConfig), 4)
|
bits.WriteBits(buf, &pos, uint64(channelConfig), 4)
|
||||||
|
|
||||||
if c.FrameLengthFlag {
|
if c.Type == ObjectTypeSBR {
|
||||||
bits.WriteBits(buf, &pos, 1, 1)
|
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 {
|
} else {
|
||||||
bits.WriteBits(buf, &pos, 0, 1)
|
if c.FrameLengthFlag {
|
||||||
}
|
bits.WriteBits(buf, &pos, 1, 1)
|
||||||
|
} else {
|
||||||
|
bits.WriteBits(buf, &pos, 0, 1)
|
||||||
|
}
|
||||||
|
|
||||||
if c.DependsOnCoreCoder {
|
if c.DependsOnCoreCoder {
|
||||||
bits.WriteBits(buf, &pos, 1, 1)
|
bits.WriteBits(buf, &pos, 1, 1)
|
||||||
} else {
|
} else {
|
||||||
bits.WriteBits(buf, &pos, 0, 1)
|
bits.WriteBits(buf, &pos, 0, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.DependsOnCoreCoder {
|
if c.DependsOnCoreCoder {
|
||||||
bits.WriteBits(buf, &pos, uint64(c.CoreCoderDelay), 14)
|
bits.WriteBits(buf, &pos, uint64(c.CoreCoderDelay), 14)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf, nil
|
return buf, nil
|
||||||
|
@@ -67,6 +67,16 @@ var configCases = []struct {
|
|||||||
CoreCoderDelay: 385,
|
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) {
|
func TestConfigUnmarshal(t *testing.T) {
|
||||||
|
@@ -6,4 +6,5 @@ type ObjectType int
|
|||||||
// supported types.
|
// supported types.
|
||||||
const (
|
const (
|
||||||
ObjectTypeAACLC ObjectType = 2
|
ObjectTypeAACLC ObjectType = 2
|
||||||
|
ObjectTypeSBR ObjectType = 5
|
||||||
)
|
)
|
||||||
|
@@ -128,6 +128,11 @@ func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
|
|||||||
|
|
||||||
typ := strconv.FormatInt(int64(t.PayloadType), 10)
|
typ := strconv.FormatInt(int64(t.PayloadType), 10)
|
||||||
|
|
||||||
|
sampleRate := t.Config.SampleRate
|
||||||
|
if t.Config.ExtensionSampleRate != 0 {
|
||||||
|
sampleRate = t.Config.ExtensionSampleRate
|
||||||
|
}
|
||||||
|
|
||||||
return &psdp.MediaDescription{
|
return &psdp.MediaDescription{
|
||||||
MediaName: psdp.MediaName{
|
MediaName: psdp.MediaName{
|
||||||
Media: "audio",
|
Media: "audio",
|
||||||
@@ -137,7 +142,7 @@ func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
|
|||||||
Attributes: []psdp.Attribute{
|
Attributes: []psdp.Attribute{
|
||||||
{
|
{
|
||||||
Key: "rtpmap",
|
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),
|
"/" + strconv.FormatInt(int64(t.Config.ChannelCount), 10),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user