Can I get a concrete in-depth explanation of how audio encoders work, eg for a modern…
- User
Can I get a concrete in-depth explanation of how audio encoders work, eg for a modern codec like AAC (of the most modern profile used in music streaming) or Opus?
In particular, I understand how video codecs are designed, how the ideal is an exhaustive search for RD-optimal decisions, how in practice this is approximated with two-pass encoding or lookahead, and how psychovisual-RD or psychovisual-optimizations can be integrated.
I don't understand the same for audio encoding; it appears that they all use one-pass encoding and at best ABR modes even though this seems unoptimal. I know psychoacoustics is much more important to audio quality but it also doesn't seem that encoders implement any formal models of it in a well-founded or RD-optimal-considered way. Maybe there isn't a high quality metric for it? In general, doesn't the one-pass encode mean noise is pushed forward so that there are post-echo but not pre-echo artifacts? Is it considered safer because audio codecs are much less reliant on "inter-prediction"?
- Assistant
Great question — this dives deep into a subtle and complex area of media coding. You’re absolutely right to compare audio and video encoding — at surface level they’re similar, but structural and perceptual differences lead to significantly different designs and compromises.
Let’s go deep step by step: We'll focus on AAC (modern flavor: xHE-AAC) and Opus, then discuss:
- Why one-pass is dominant in audio encoders
- How psychoacoustic models really work in audio encoders
- Rate-Distortion (RD) behavior and why audio differs from video
- The challenges of a "rigorous" RD-optimal or two-pass audio encoder
- Pre-echo, post-echo, and time masking
Overview of Modern Audio Codecs
xHE-AAC (Extended High-Efficiency AAC)
This is the most modern AAC profile; used in streaming (e.g., YouTube, DRM), radio, voice apps. It includes dynamic range control (DRC), speech/music classification, and has SBR (Spectral Band Replication) for low-bitrate quality.
Opus
A hybrid codec: SILK (linear-predictive, great for speech) + CELT (low-delay MDCT-based transform codec, like AAC). Opus dynamically switches or blends between them. Used in WebRTC, Discord, and streaming platforms.
1. Why One-Pass Encoding is Typical in Audio
In contrast to video, audio is inherently more localized in time:
- Tonal and transient content typically lasts under 100–200ms.
- Human auditory memory is extremely short (on the order of tens of milliseconds for noise).
- There’s relatively little temporal prediction in audio codecs: no motion vectors, P-frames, no reference frame trees.
So: bitrate decisions can be localized. This reduces the benefit of full two-pass strategies that are essential in video where macro/motion decisions can ripple across seconds.
ABR vs. CBR vs. VBR
Most encoders use ABR (Average Bitrate) or constrained VBR:
- They operate using limited lookahead, just enough to buffer short-term bitrate fluctuations. Much like video encoder HRD constraints, just simpler.
- The encoder might allocate more bits for transient regions and fewer for stationary ones based on instantaneous analysis.
So even though it's one-pass, it's adaptive and often perceptually driven.
2. Psychoacoustic Models in Practice
Psychoacoustics is central to audio coding. All modern encoders implement some variant of a psychoacoustic model. For AAC:
Encoder Psychoacoustic Steps:
- FFT Analysis of audio frame (e.g., 1024 samples)
- Masking Threshold Computation: Estimate the minimum masking threshold per critical band using:
- Tonal/Noise masking models (e.g., MPEG-1 Model 1/2/3)
- Spreading functions (maskers spread across frequencies non-uniformly)
- Bark scale or ERB-rate scale frequency bins
- Signal-to-Mask Ratio (SMR): This gives a per-band perceptual SNR target
- The encoder chooses quantization such that quantization noise stays under the masking threshold in each band
Opus follows similar principles, though its CELT mode mainly uses a flat masking curve plus tonality probabilistic estimation.
But here's the crux:
These masking models are empirical approximations, based on lab testing, not theoretical RD optimization.
They're not derived from formal perceptual distortion metrics in the same way, say, PSNR or SSIM is for video. No gold standard perceptual model exists for audio — PEAQ, ViSQOL, and others are imperfect and can’t reliably guide an encoder in real-time.
3. Rate-Distortion and Audio: Why It's Not Formalized
Video has clear distortion metrics (SSIM, VMAF), and coding errors are spatially coherent — visual humans notice block edges, color shifts, motion artifacts; each of which has reasonably quantifiable impact.
Audio distortion is psychoacoustic, and most human perception filters out tons of quantization noise if it's masked correctly.
Thus:
- There’s no good RD function of the form
D(x, x')that correlates with perceptual error. - Psychoacoustic models approximate D only heuristically
- Formal RD optimization, like in video (e.g., RD-curves via Q-step trials), isn't as effective in audio because:
- The distortion function is not continuous or differentiable.
- Artifacts like birdies, pre-echo, or tonal smearing occur sharply once a threshold is crossed.
4. Why Not Two-Pass Audio Encoding?
It can be done — there are some research and closed-source encoders (e.g. Dolby's) which implement bit reservoir and psychoacoustic-aware two-pass estimation. But it's rare. Why?
- Short memory of ear: No need to balance quality across minutes; just tens or hundreds of milliseconds.
- Bit Reservoirs exist: AAC's "bit reservoir" acts like a mini two-pass internal buffer — it borrows bits from easy frames to help hard frames.
- Computation vs. Benefit tradeoff: Audio encodes in real-time with minimal hardware (phones, radios, web apps). So complexity budgets are tight.
Note: We'll return to Opus here — Opus has unusual two-stage encoding, where it:
- First estimates spectral envelope and quantizes/stores energy per band
- Then allocates bitrate between bands and quantizes the fine details
This is a kind of efficient "intermediate-first" coding — not quite two-pass, but better than greedy, pure one-pass coding. Still, it doesn't require knowledge of future frames.
5. Pre-Echo and Time Masking Effects
Pre-echo is an artifact where noise appears before the onset of a transient (e.g., a castanet click). It's especially salient when quantizing transients with long MDCT windows due to the transform's inherent "ringing" or temporal smearing.
How it's handled:
- Window Switching: AAC and Opus/CELT switch to short blocks (e.g., 128-sample) when transients are detected.
- They also apply "attack detection" with simple energy delta metrics.
- Bit allocation gives transients higher priority, often over-allocating just to avoid pre-echo.
- Pre-echo is not masked well (the brain is bad at "hearing noise before the signal starts"), so encoders err on the side of extra bits.
This is the time domain analog of temporal perceptual masking — the ear accepts artifacts after a loud event more than before it.
Post-echo is typically less objectionable and is tolerated within limits.
Future Possibilities: RD-Aware Audio Encoding?
In research and some test encoders:
- Two-pass or iterative encoding has been explored
- Learnable models (e.g., deep learning masking threshold estimators) could improve "D(x, x')" estimation
- Content-adaptive encoding (long-term energy, genre classification) might eventually guide long-term bit allocation
- Libraries like Dolby, Fraunhofer fdk-aac, or Apple’s encoders (not public) likely have proprietary enhancements beyond simple masking models
But the lack of a "ground truth" perceptual metric remains the blocker to true RD-optimal audio encoding.
Summary
Video Audio RD extensions ease distortion control No formal distortion model; relies on masking Two-pass and inter prediction crucial One-pass works well; little inter-frame prediction Perceptual metrics (VMAF, MS-SSIM) help optimize No equivalent perceptual metric usable in encoder Temporal correlation (motion) dominates Audio is more localized; short-term masking dominates High complexity justified (VFX, movies) Needs low complexity (streaming, phones)
TL;DR
Audio encoders like AAC and Opus use perceptual models to shape quantization below audibility thresholds. Rather than RD optimization with explicit distortion metrics, they rely on empirical psychoacoustic heuristics and flexible short-term bit budgeting. One-pass is sufficient because of the short memory of human hearing, low inter-frame dependency, and lack of a perceptually faithful distortion metric. Some encoders use clever internal stages or reservoirs to "simulate" limited lookahead.
If you'd like, I can walk through a pseudocode-level logic of a full encode pipeline too — just ask!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.