Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Decoder struct {
silkResamplerChannels int
hybridSilkResampler [2]silkresample.Resampler
hybridSilkChannels int
hybridSilkBuffer []float32
hybridSilkPCM []float32
silkRedundancyFades []silkRedundancyFade
silkCeltAdditions []silkCeltAddition
floatBuffer []float32
Expand Down Expand Up @@ -755,7 +757,7 @@ func (d *Decoder) decodeHybridFrame(
d.rangeDecoder.Init(encodedFrame)

silkOutputChannelCount := min(streamChannelCount, outputChannelCount)
silkInternal := make([]float32, silkSamplesPerChannel*silkOutputChannelCount)
silkInternal := resizeFloat32Buffer(&d.hybridSilkBuffer, silkSamplesPerChannel*silkOutputChannelCount)
if err := d.silkDecoder.DecodeWithRangeToChannels(
&d.rangeDecoder,
silkInternal,
Expand Down Expand Up @@ -792,7 +794,7 @@ func (d *Decoder) decodeHybridFrame(
return err
}

silkPCM := make([]float32, outputFrameSampleCount*silkOutputChannelCount)
silkPCM := resizeFloat32Buffer(&d.hybridSilkPCM, outputFrameSampleCount*silkOutputChannelCount)
if err = d.resampleHybridSilk(silkInternal, silkPCM, silkOutputChannelCount); err != nil {
return err
}
Expand Down Expand Up @@ -1363,6 +1365,15 @@ func float32ToInt16(in []float32, out []int16, sampleCount int) {
}
}

func resizeFloat32Buffer(buffer *[]float32, sampleCount int) []float32 {
if cap(*buffer) < sampleCount {
*buffer = make([]float32, sampleCount)
}
*buffer = (*buffer)[:sampleCount]

return *buffer
}

// Decode decodes the Opus bitstream into S16LE PCM.
func (d *Decoder) Decode(in, out []byte) (bandwidth Bandwidth, isStereo bool, err error) {
if cap(d.floatBuffer) < len(out)/2 {
Expand Down
1 change: 1 addition & 0 deletions internal/celt/celt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
sampleRate = 48000
shortBlockSampleCount = 120
maxLM = 3
maxFrameSampleCount = shortBlockSampleCount << maxLM
maxBands = 21
hybridStartBand = 17
)
22 changes: 18 additions & 4 deletions internal/celt/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Decoder struct {
preemphasisMem [2]float32
rng uint32
lossCount int
scratch *decoderScratch
}

// NewDecoder creates a CELT decoder with the static Opus 48 kHz mode.
Expand Down Expand Up @@ -149,6 +150,7 @@ func (d *Decoder) decode(
outputSampleRate int,
rangeDecoder *rangecoding.Decoder,
) error {
scratch := d.scratchBuffer()
channelCount := 1
if isStereo {
channelCount = 2
Expand Down Expand Up @@ -189,10 +191,12 @@ func (d *Decoder) decode(
return err
}
if info.silence {
x := make([]float32, frameSampleCount)
x := scratch.x[:frameSampleCount]
clear(x)
var y []float32
if isStereo {
y = make([]float32, frameSampleCount)
y = scratch.y[:frameSampleCount]
clear(y)
}
for channel := range info.channelCount {
for band := info.startBand; band < info.endBand; band++ {
Expand All @@ -213,10 +217,12 @@ func (d *Decoder) decode(

// RFC 6716 Sections 4.3.4 through 4.3.7 decode the normalized residual,
// optionally repair collapsed transient blocks, then synthesize PCM.
x := make([]float32, frameSampleCount)
x := scratch.x[:frameSampleCount]
clear(x)
var y []float32
if isStereo {
y = make([]float32, frameSampleCount)
y = scratch.y[:frameSampleCount]
clear(y)
}
state := bandDecodeState{
rangeDecoder: &d.rangeDecoder,
Expand Down Expand Up @@ -284,3 +290,11 @@ func (d *Decoder) Mode() *Mode {
func (d *Decoder) FinalRange() uint32 {
return d.rangeDecoder.FinalRange()
}

func (d *Decoder) scratchBuffer() *decoderScratch {
if d.scratch == nil {
d.scratch = &decoderScratch{}
}

return d.scratch
}
Loading
Loading