Skip to content

Fit the TOC byte inside the bitrate budget - #199

Merged
thomas-vilte merged 1 commit into
pion:mainfrom
thomas-vilte:fix/packet-bytes
Aug 12, 2026
Merged

Fit the TOC byte inside the bitrate budget#199
thomas-vilte merged 1 commit into
pion:mainfrom
thomas-vilte:fix/packet-bytes

Conversation

@thomas-vilte

Copy link
Copy Markdown
Member

Description

frameBytes() returns the amount of bitrate available to the frame, and that value is used as the CELT payload budget — but the packet also has the TOC byte at the front:

out[0] = byte(e.tocHeader())
n, err := e.celtEncoder.EncodeFrame(channels, out[1:frameBytes+1], frameBytes, ...)

So each packet was actually frameBytes+1 bytes instead of frameBytes.

Measured over 200 frames at each bitrate, before the change:

bitrate target pion output
24000 60 61.00
32000 80 81.00
48000 120 121.00
64000 160 161.00
96000 240 241.00
128000 320 321.00

It's exactly one extra byte per frame, or 400 bps at 20 ms: 1.7% above the target at 24 kbps and 0.4% at 96 kbps. So a CBR 24 kbps stream was actually sending 24.4 kbps.

libopus passes CELT the payload size without the TOC byte. I verified this by instrumenting run_prefilter at 24 kbps: nbAvailableBytes is 59 in libopus where pion was using 60.

The fix is to subtract the TOC byte from the payload budget. After the change, the packet sizes match the target exactly at all six bitrates.

Side effect

frameBytes is also the budget used by the allocator, so this shifts some of the rate-dependent decisions as well. That's expected — they should now match libopus at the same bitrate.

In particular, the equiv_rate used by the intensity-band and trim decisions now matches the reference.

Reference issue

Part of #34.

@thomas-vilte
thomas-vilte requested a review from FrantaBOT August 11, 2026 22:37
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.07%. Comparing base (a38c746) to head (b920a16).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #199      +/-   ##
==========================================
- Coverage   93.10%   93.07%   -0.03%     
==========================================
  Files          58       58              
  Lines       10263    10263              
==========================================
- Hits         9555     9552       -3     
- Misses        500      502       +2     
- Partials      208      209       +1     
Flag Coverage Δ
go 93.07% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@thomas-vilte
thomas-vilte merged commit ebe48be into pion:main Aug 12, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants