Documentation
¶
Overview ¶
Package dred implements libopus Deep Redundancy (DRED): the in-band extension that carries a heavily compressed RDOVAE representation of recent speech so a decoder can recover audio lost to packet loss.
It covers the full DRED lifecycle: encoder-side feature extraction and latent encoding (encode.go, encoder_buffer.go, latent_generator.go), the 16 kHz resampler the encoder feeds (convert16k.go), payload framing and parsing (header.go, parsed.go, entropy.go), and decoder-side latent decoding plus recovery scheduling (decode.go, recovery.go). The RDOVAE network itself lives in the rdovae subpackage. Everything mirrors libopus 1.6.1 dnn/dred_*.c bit-for-bit and is held in place by the package and decoder DRED parity tests.
DRED code paths are gated in the parent codec; this package is always compiled, while the heavy libopus differential parity tests are tag-gated behind gopus_dred / gopus_osce.
Index ¶
- Constants
- func ConvertTo16kMonoFloat32(dst []float32, mem *[ResamplingOrder + 1]float32, in []float32, ...) int
- func EncodeExperimentalPayload(dst []byte, maxChunks, q0, dQ, qmax int32, ...) int
- func EncodePayload(dst []byte, maxChunks, q0, dQ, qmax int32, ...) int
- func LatentSpanSamples(sampleRate int) int
- func MaxLatentsForRequest(maxDredSamples, sampleRate int) int
- func RequestedFeatureFrames(maxDredSamples, sampleRate int) int
- func UpdateActivityHistory(dst *[ActivityHistorySize]byte, frameSize, sampleRate int, active bool)
- func ValidExperimentalPayload(data []byte) bool
- type Availability
- type Cache
- type Decoded
- type EncoderBuffer
- type FeatureWindow
- type Header
- func (h Header) Availability(maxDredSamples, sampleRate int) Availability
- func (h Header) EndSamples(sampleRate int) int
- func (h Header) FillQuantizerLevels(dst []int32, maxDredSamples, sampleRate int) int
- func (h Header) MaxAvailableSamples(maxDredSamples, sampleRate int) int
- func (h Header) OffsetSamples(sampleRate int) int
- func (h Header) QuantizerLevel(i int) int32
- type LatentGenerator
- func (g *LatentGenerator) DREDOffset() int32
- func (g *LatentGenerator) LatentOffset() int32
- func (g *LatentGenerator) Loaded() bool
- func (g *LatentGenerator) Process16k(model *rdovae.EncoderModel, pcm []float32, extraDelay int32, ...) int
- func (g *LatentGenerator) Reset()
- func (g *LatentGenerator) SetDNNBlob(blob *dnnblob.Blob) error
- func (g *LatentGenerator) SetDNNBlobPreservingState(blob *dnnblob.Blob) error
- type Parsed
- type Request
- type Result
Constants ¶
const ( ExtensionID = 126 ExperimentalVersion = 12 ExperimentalHeaderBytes = 2 MinBytes = 8 SilkEncoderDelay = 79 + 12 - 80 FrameSize = 160 DFrameSize = 2 * FrameSize MaxDataSize = 1000 EncQ0 = 6 EncQ1 = 15 MaxLatents = 26 NumRedundancyFrames = 2 * MaxLatents MaxFrames = 4 * MaxLatents NumFeatures = 20 )
Constants mirrored from libopus 1.6.1 dnn/dred_config.h.
const ( // ResamplingOrder mirrors libopus RESAMPLING_ORDER in dred_encoder.h. ResamplingOrder = 8 // MaxConvert16kBuffer mirrors libopus MAX_DOWNMIX_BUFFER without QEXT. MaxConvert16kBuffer = 1920 )
const ( StateDim = 50 LatentDim = 25 LatentStride = LatentDim + 1 )
DRED latent geometry, mirroring libopus dnn/dred_rdovae_constants.h. StateDim is the RDOVAE initial-state vector width, LatentDim the per-frame latent width and LatentStride the stored stride (LatentDim plus the per-latent quantizer-level byte).
const ActivityHistorySize = 4 * MaxFrames
ActivityHistorySize is the length of the encoder voice-activity history at 2.5 ms (400 Hz) resolution, mirroring the activity_mem[DRED_MAX_FRAMES*4] buffer libopus keeps in src/opus_encoder.c.
Variables ¶
This section is empty.
Functions ¶
func ConvertTo16kMonoFloat32 ¶
func ConvertTo16kMonoFloat32(dst []float32, mem *[ResamplingOrder + 1]float32, in []float32, sampleRate, channels int) int
ConvertTo16kMonoFloat32 mirrors libopus dred_convert_to_16k() for callers that already hold float32 PCM. It keeps the downmix and quantization loop in float32, matching libopus' float input path without widening roundtrips.
func EncodeExperimentalPayload ¶
func EncodeExperimentalPayload(dst []byte, maxChunks, q0, dQ, qmax int32, stateBuffer, latentsBuffer []float32, latentsFill, dredOffset, latentOffset int32, lastExtraDREDOffset *int32, activity []byte) int
EncodeExperimentalPayload mirrors the current libopus temporary DRED payload framing by prepending the experimental header in front of EncodePayload().
func EncodePayload ¶
func EncodePayload(dst []byte, maxChunks, q0, dQ, qmax int32, stateBuffer, latentsBuffer []float32, latentsFill, dredOffset, latentOffset int32, lastExtraDREDOffset *int32, activity []byte) int
EncodePayload mirrors libopus dred_encode_silk_frame() for a caller-owned DRED history window. dst must not include the temporary experimental prefix. It returns the encoded payload length in bytes, or 0 when libopus would suppress DRED emission for the provided window.
func LatentSpanSamples ¶
LatentSpanSamples converts one DRED latent chunk (40 ms) to samples at the caller's sample rate, matching opus_dred_parse()'s nb_latents return math.
func MaxLatentsForRequest ¶
MaxLatentsForRequest reports the maximum number of latent chunks libopus can decode for a single opus_dred_parse() request before payload-size limits are considered.
func RequestedFeatureFrames ¶
RequestedFeatureFrames mirrors opus_dred_parse()'s min_feature_frames bound for a caller-provided max_dred_samples request. The result is capped to the libopus decoder's fixed DRED feature window.
func UpdateActivityHistory ¶
func UpdateActivityHistory(dst *[ActivityHistorySize]byte, frameSize, sampleRate int, active bool)
UpdateActivityHistory mirrors the libopus encoder-side DRED activity-memory rollover at 2.5 ms resolution.
func ValidExperimentalPayload ¶
ValidExperimentalPayload reports whether data matches the temporary libopus DRED extension framing and size bounds accepted by dred_find_payload().
Types ¶
type Availability ¶
type Availability struct {
FeatureFrames int
MaxLatents int
OffsetSamples int
EndSamples int
AvailableSamples int
}
Availability summarizes the low-cost request-bounded DRED coverage that libopus exposes through opus_dred_parse().
type Cache ¶
Cache retains the low-cost parsed DRED metadata plus the cached payload length. The payload bytes themselves stay in caller-owned storage.
func (*Cache) Invalidate ¶
func (c *Cache) Invalidate()
Invalidate drops cached payload visibility without zeroing the retained parsed metadata, which keeps packet-entry cache invalidation cheap.
type Decoded ¶
type Decoded struct {
State [StateDim]float32
Latents [MaxLatents * LatentStride]float32
Features [MaxFrames * NumFeatures]float32
NbLatents int
}
Decoded retains the request-bounded DRED decoder state and latent vectors libopus produces during dred_ec_decode().
func (*Decoded) Decode ¶
Decode parses and retains the request-bounded DRED decoder state and latent vectors libopus produces during dred_ec_decode().
func (*Decoded) FillFeatures ¶
FillFeatures copies the retained DRED feature frames into dst and returns the number of floats written.
func (*Decoded) FillLatents ¶
FillLatents copies the retained DRED latent vectors into dst and returns the number of floats written.
func (*Decoded) FillState ¶
FillState copies the retained DRED state into dst and returns the number of floats written.
func (*Decoded) Invalidate ¶
func (d *Decoded) Invalidate()
Invalidate drops retained decode visibility without zeroing the backing arrays, which keeps packet-entry invalidation cheap.
type EncoderBuffer ¶
type EncoderBuffer struct {
// contains filtered or unexported fields
}
EncoderBuffer mirrors the libopus DRED encoder-side 16 kHz d-frame staging state from dred_compute_latents(), without pulling in resampling or RDOVAE inference yet.
func (*EncoderBuffer) Append16k ¶
func (b *EncoderBuffer) Append16k(pcm []float32, extraDelay int32, emit func(frame []float32)) int
Append16k stages 16 kHz mono PCM into the libopus-shaped DRED d-frame buffer. The callback, when non-nil, is invoked once per emitted 320-sample d-frame with a slice that aliases internal storage and is only valid until the callback returns.
func (*EncoderBuffer) DREDOffset ¶
func (b *EncoderBuffer) DREDOffset() int32
DREDOffset reports the current libopus-shaped DRED offset in 2.5 ms units.
func (*EncoderBuffer) InputBufferFill ¶
func (b *EncoderBuffer) InputBufferFill() int32
InputBufferFill reports how many 16 kHz samples are currently staged.
func (*EncoderBuffer) LatentOffset ¶
func (b *EncoderBuffer) LatentOffset() int32
LatentOffset reports the current libopus-shaped latent offset.
func (*EncoderBuffer) Reset ¶
func (b *EncoderBuffer) Reset()
Reset restores the libopus DRED encoder buffer state after reset.
type FeatureWindow ¶
type FeatureWindow struct {
FeaturesPerFrame int
NeededFeatureFrames int
FeatureOffsetBase int
MaxFeatureIndex int
RecoverableFeatureFrames int
MissingPositiveFrames int
}
FeatureWindow is the payload-bounded DRED feature index window used by libopus's opus_decode_native() concealment path before any model processing. RecoverableFeatureFrames reflects the parsed payload latents retained in Result.Availability.
func ProcessedFeatureWindow ¶
func ProcessedFeatureWindow(result Result, decoded *Decoded, decodeOffsetSamples, frameSizeSamples, initFrames int) FeatureWindow
ProcessedFeatureWindow mirrors the feature scheduling window that opus_decode_native() uses after opus_dred_process(), where the processed DRED object contributes its retained latent count.
func QueueProcessedFeatures ¶
func QueueProcessedFeatures(plc *lpcnetplc.State, result Result, decoded *Decoded, decodeOffsetSamples, frameSizeSamples int) FeatureWindow
QueueProcessedFeatures mirrors the standalone DRED scheduling loop inside opus_decode_native(). It clears the PLC FEC queue, derives the libopus recovery window from the retained blend state, and enqueues either concrete feature vectors or skipped-positive placeholders.
func QueueProcessedFeaturesWithInitFrames ¶
func QueueProcessedFeaturesWithInitFrames(plc *lpcnetplc.State, result Result, decoded *Decoded, decodeOffsetSamples, frameSizeSamples, initFrames int) FeatureWindow
QueueProcessedFeaturesWithInitFrames mirrors the DRED prefill loop in opus_decode_native() when the caller already knows which init-frame policy should apply for the packet being scheduled.
func (FeatureWindow) FillFeatureOffsets ¶
func (w FeatureWindow) FillFeatureOffsets(dst []int32) int
FillFeatureOffsets writes the feature offsets libopus would probe, from newest to oldest, into dst and returns the number of entries written.
type Header ¶
type Header struct {
Q0 int32
DQ int32
QMax int32
ExtraOffset int32
DredOffset int32
DredFrameOffset int32
}
Header mirrors the low-cost metadata libopus decodes before the model-heavy DRED processing stage.
func ParseHeader ¶
ParseHeader decodes the lightweight libopus DRED header from a payload body with the temporary extension prefix already stripped. dredFrameOffset is in 2.5 ms units, matching libopus dred_find_payload().
func (Header) Availability ¶
func (h Header) Availability(maxDredSamples, sampleRate int) Availability
Availability reports the request-bounded DRED coverage derived from the parsed header and the opus_dred_parse() request parameters.
func (Header) EndSamples ¶
EndSamples mirrors opus_dred_parse()'s dred_end output: the number of trailing silence samples between the DRED timestamp and the last usable DRED sample.
func (Header) FillQuantizerLevels ¶
FillQuantizerLevels writes the request-bounded libopus quantizer schedule into dst and returns the number of entries written.
func (Header) MaxAvailableSamples ¶
MaxAvailableSamples mirrors opus_dred_parse()'s positive sample-count result using the request-bounded latent ceiling derived from max_dred_samples.
func (Header) OffsetSamples ¶
OffsetSamples converts the parsed DRED offset from 2.5 ms units to samples at the caller's sample rate.
func (Header) QuantizerLevel ¶
QuantizerLevel mirrors libopus compute_quantizer() for the parsed DRED quantizer metadata.
type LatentGenerator ¶
type LatentGenerator struct {
// contains filtered or unexported fields
}
LatentGenerator mirrors the libopus DRED encoder-side d-frame pipeline for already-prepared 16 kHz mono PCM. It retains LPCNet analysis and RDOVAE recurrent state so repeated use stays allocation-free.
func (*LatentGenerator) DREDOffset ¶
func (g *LatentGenerator) DREDOffset() int32
DREDOffset reports the current libopus-shaped DRED offset in 2.5 ms units.
func (*LatentGenerator) LatentOffset ¶
func (g *LatentGenerator) LatentOffset() int32
LatentOffset reports the current libopus-shaped latent offset.
func (*LatentGenerator) Loaded ¶
func (g *LatentGenerator) Loaded() bool
Loaded reports whether the latent generator has the pitch-analysis model it needs to mirror libopus DRED feature extraction.
func (*LatentGenerator) Process16k ¶
func (g *LatentGenerator) Process16k(model *rdovae.EncoderModel, pcm []float32, extraDelay int32, emit func(latents, initialState []float32)) int
Process16k mirrors the libopus encoder-side dred_compute_latents() inner 16 kHz mono d-frame path. The callback, when non-nil, is invoked once per emitted latent/state pair with slices that alias internal storage and remain valid only until the callback returns.
func (*LatentGenerator) Reset ¶
func (g *LatentGenerator) Reset()
Reset clears retained feature-extraction and RDOVAE recurrent state while preserving the bound pitch model.
func (*LatentGenerator) SetDNNBlob ¶
func (g *LatentGenerator) SetDNNBlob(blob *dnnblob.Blob) error
SetDNNBlob binds the shared pitch model family needed by the encoder-side LPCNet analysis front end and resets retained state.
func (*LatentGenerator) SetDNNBlobPreservingState ¶
func (g *LatentGenerator) SetDNNBlobPreservingState(blob *dnnblob.Blob) error
SetDNNBlobPreservingState replaces the shared pitch model family without clearing retained DRED encoder state.
type Parsed ¶
Parsed is the low-cost libopus-shaped DRED parse result retained before any model-backed processing stage.
func ParsePayload ¶
ParsePayload decodes the lightweight DRED metadata from a payload body with the temporary experimental prefix already stripped.
func (Parsed) Availability ¶
func (p Parsed) Availability(maxDredSamples, sampleRate int) Availability
Availability reports the request-bounded DRED coverage for a fully parsed payload. It refines Header.Availability by clamping MaxLatents (and the derived AvailableSamples) to the number of latents actually present in the payload.
func (Parsed) FillQuantizerLevels ¶
FillQuantizerLevels writes the request-bounded libopus quantizer schedule into dst and returns the number of entries written.
func (Parsed) ForRequest ¶
ForRequest evaluates a parsed DRED payload against an opus_dred_parse()-style request.
type Request ¶
Request mirrors the lightweight opus_dred_parse() request parameters that affect how much cached redundancy is usable.
type Result ¶
type Result struct {
Request Request
Parsed Parsed
Availability Availability
}
Result bundles parsed DRED metadata with the request-bounded coverage derived from it.
func (Result) FeatureWindow ¶
func (r Result) FeatureWindow(decodeOffsetSamples, frameSizeSamples, initFrames int) FeatureWindow
FeatureWindow mirrors the feature_offset indexing math in opus_decode_native() for a given decoded DRED result and concealment request.
func (Result) FillQuantizerLevels ¶
FillQuantizerLevels writes the request-bounded libopus quantizer schedule into dst and returns the number of entries written.
func (Result) MaxAvailableSamples ¶
MaxAvailableSamples mirrors opus_dred_parse()'s positive sample-count return for the request-bounded result.