Recommended Libopus Output Buffer Size

This article provides a clear guide on the recommended memory buffer sizes to allocate for libopus output. It covers the specific byte requirements for both encoded compressed packets and decoded PCM audio output, ensuring your application avoids buffer overflows while optimizing memory usage.

Encoded Packet Buffer Size (Encoder Output)

When allocating memory buffers to receive encoded Opus packets from the libopus encoder (using functions like opus_encode or opus_encode_float), the official recommendation is to allocate 4,000 bytes per packet.

While the maximum theoretical size of a standard single-frame Opus packet is 1,275 bytes (and a multi-frame packet can theoretically reach up to 6,120 bytes), the libopus documentation specifically recommends a 4,000-byte buffer. This size is large enough to prevent any potential buffer overflow during encoding under any bitrate or frame size configuration, without wasting excessive memory.

If you are constraint-optimized for network transmission (such as UDP/RTP streaming), you may target a payload size under the standard MTU limit of 1,500 bytes. However, for internal memory allocation before transmission, the 4,000-byte buffer remains the safest standard.

Decoded PCM Buffer Size (Decoder Output)

If you are allocating a buffer to receive decoded PCM audio from the libopus decoder (using opus_decode or opus_decode_float), the buffer size is determined by the maximum possible frame duration and your audio settings.

Opus supports a maximum frame size of 120 milliseconds. To calculate the required buffer size, use the following specifications based on the maximum sampling rate of 48 kHz:

Memory Allocation in Bytes

Depending on your output format, allocate the following buffer sizes to safely handle a 120 ms stereo frame:

Allocating these maximum sizes ensures your application can decode any valid incoming Opus packet without risk of out-of-bounds memory writes.