From b920a16325328226f2d8ff78b6376900af398953 Mon Sep 17 00:00:00 2001 From: Thomas Vilte Date: Tue, 11 Aug 2026 19:34:22 -0300 Subject: [PATCH] Fit the TOC byte inside the bitrate budget --- encoder.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/encoder.go b/encoder.go index c44cedd..b4dab9f 100644 --- a/encoder.go +++ b/encoder.go @@ -557,8 +557,15 @@ func splitChannels(in []float32, numChannels, frameSamples int) [][]float32 { return ch } +// tocHeaderBytes is the single table-of-contents byte every packet starts with. +const tocHeaderBytes = 1 + +// frameBytes returns the CELT payload budget. The packet carries a TOC byte in +// front of it, so the payload gets one byte less than the frame's share of the +// bitrate — otherwise every packet overshoots the target by a byte, which is +// 400 bps at 20 ms. func (e *Encoder) frameBytes() int { - return int(int64(e.bitrate) * frame20msNS / 1000000000 / 8) + return int(int64(e.bitrate)*frame20msNS/1000000000/8) - tocHeaderBytes } func (e *Encoder) frameSampleCount() int {