Can Libopus Natively Encode 24-bit Audio?
This article examines whether the standard libopus API can natively ingest and encode 24-bit audio data. While the Opus codec is fully capable of delivering high-resolution audio quality, its programming interface does not directly accept 24-bit integer PCM inputs. Below, we explain the limitations of the standard libopus API and detail the precise steps developers must take to encode 24-bit audio without losing quality.
Supported Input Formats in the Libopus API
The standard libopus library provides two primary functions for ingesting and encoding audio data:
opus_encode(): This function natively ingests 16-bit signed integer PCM data (opus_int16).opus_encode_float(): This function natively ingests 32-bit floating-point PCM data (float).
There is no native API function—such as an
opus_encode_int24()—that accepts raw 24-bit packed integers
or 24-bit PCM signals. If you attempt to pass raw 24-bit audio directly
into the standard API, the library will not be able to parse it,
resulting in severe audio distortion or program errors.
How to Encode 24-bit Audio Using Libopus
To encode a 24-bit audio source using libopus, you must perform an
upstream format conversion before passing the data to the encoder. To
preserve the dynamic range and precision of your 24-bit source, you
should convert the audio to 32-bit floating-point values and use the
opus_encode_float() function.
The Conversion Process
- Unpack the 24-bit Data: 24-bit audio is often stored in a 3-byte packed format (PCM_24) or sign-extended inside a 32-bit integer container (PCM_32). Read these samples into your application as standard integers.
- Normalize to Float: Convert the integer values to
floating-point numbers scaled between
-1.0and1.0. For a signed 24-bit integer (which ranges from-8,388,608to8,388,607), divide each sample by8,388,608.0. - Encode: Pass the resulting float array to
opus_encode_float().
By converting the 24-bit input to a 32-bit float, you ensure that the input signal-to-noise ratio is maintained, allowing the Opus codec to utilize its full fidelity capabilities during the encoding process.