---
name: "opus_encoder"
section: "3"
platform: "linux"
package: "libopus-doc"
version: "1.5.2-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/opus_encoder"
---

# opus\_encoder(3)

Opus Encoder - This page describes the process and functions used to encode Opus.

## NAME

opus\_encoder - Opus Encoder

 \- This page describes the process and functions used to encode Opus.

## SYNOPSIS

### Typedefs

typedef struct **OpusEncoder** **OpusEncoder**\
 Opus encoder state.

### Functions

int **opus\_encoder\_get\_size** (int channels)\
 Gets the size of an `OpusEncoder` structure.  **OpusEncoder** \* **opus\_encoder\_create** (**opus\_int32** Fs, int channels, int application, int \*error)\
 Allocates and initializes an encoder state.  int **opus\_encoder\_init** (**OpusEncoder** \*st, **opus\_int32** Fs, int channels, int application)\
 Initializes a previously allocated encoder state The memory pointed to by st must be at least the size returned by **opus\_encoder\_get\_size()**.  **opus\_int32** **opus\_encode** (**OpusEncoder** \*st, const **opus\_int16** \*pcm, int frame\_size, unsigned char \*data, **opus\_int32** max\_data\_bytes)\
 Encodes an Opus frame.  **opus\_int32** **opus\_encode\_float** (**OpusEncoder** \*st, const float \*pcm, int frame\_size, unsigned char \*data, **opus\_int32** max\_data\_bytes)\
 Encodes an Opus frame from floating point input.  void **opus\_encoder\_destroy** (**OpusEncoder** \*st)\
 Frees an `OpusEncoder` allocated by **opus\_encoder\_create()**.  int **opus\_encoder\_ctl** (**OpusEncoder** \*st, int request,...)\
 Perform a CTL function on an Opus encoder.

## Detailed Description

This page describes the process and functions used to encode Opus.

Since Opus is a stateful codec, the encoding process starts with creating an encoder state. This can be done with:

```
int          error;
OpusEncoder *enc;
enc = opus_encoder_create(Fs, channels, application, &error);
```

From this point, `enc` can be used for encoding an audio stream. An encoder state **must** **not** be used for more than one stream at the same time. Similarly, the encoder state **must** **not** be re-initialized for each frame.

While **opus\_encoder\_create()** allocates memory for the state, it's also possible to initialize pre-allocated memory:

```
int          size;
int          error;
OpusEncoder *enc;
size = opus_encoder_get_size(channels);
enc = malloc(size);
error = opus_encoder_init(enc, Fs, channels, application);
```

where **opus\_encoder\_get\_size()** returns the required size for the encoder state. Note that future versions of this code may change the size, so no assumptions should be made about it.

The encoder state is always continuous in memory and only a shallow copy is sufficient to copy it (e.g. memcpy())

It is possible to change some of the encoder's settings using the **opus\_encoder\_ctl()** interface. All these settings already default to the recommended value, so they should only be changed when necessary. The most common settings one may want to change are:

```
opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate));
opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal_type));
```

where

- •

  bitrate is in bits per second (b/s)

- •

  complexity is a value from 1 to 10, where 1 is the lowest complexity and 10 is the highest

- •

  signal\_type is either OPUS\_AUTO (default), OPUS\_SIGNAL\_VOICE, or OPUS\_SIGNAL\_MUSIC

See **Encoder related CTLs** and **Generic CTLs** for a complete list of parameters that can be set or queried. Most parameters can be set or changed at any time during a stream.

To encode a frame, **opus\_encode()** or **opus\_encode\_float()** must be called with exactly one frame (2.5, 5, 10, 20, 40 or 60 ms) of audio data:

```
len = opus_encode(enc, audio_frame, frame_size, packet, max_packet);
```

where

- •

  audio\_frame is the audio data in opus\_int16 (or float for **opus\_encode\_float()**)

- •

  frame\_size is the duration of the frame in samples (per channel)

- •

  packet is the byte array to which the compressed data is written

- •

  max\_packet is the maximum number of bytes that can be written in the packet (4000 bytes is recommended). Do not use max\_packet to control VBR target bitrate, instead use the **OPUS\_SET\_BITRATE** CTL.

**opus\_encode()** and **opus\_encode\_float()** return the number of bytes actually written to the packet. The return value **can be negative**, which indicates that an error has occurred. If the return value is 2 bytes or less, then the packet does not need to be transmitted (DTX).

Once the encoder state if no longer needed, it can be destroyed with

```
opus_encoder_destroy(enc);
```

If the encoder was created with **opus\_encoder\_init()** rather than **opus\_encoder\_create()**, then no action is required aside from potentially freeing the memory that was manually allocated for it (calling free(enc) for the example above)

## Typedef Documentation

### typedef struct OpusEncoder OpusEncoder

Opus encoder state. This contains the complete state of an Opus encoder. It is position independent and can be freely copied.

**See also**

**opus\_encoder\_create**,**opus\_encoder\_init**

## Function Documentation

### opus\_int32 opus\_encode (OpusEncoder \* st, const opus\_int16 \* pcm, int frame\_size, unsigned char \* data, opus\_int32 max\_data\_bytes)

Encodes an Opus frame.

**Parameters**

*st* `OpusEncoder*`: Encoder state \
 *pcm* `opus_int16*`: Input signal (interleaved if 2 channels). length is frame\_size\*channels\*sizeof(opus\_int16) \
 *frame\_size* `int`: Number of samples per channel in the input signal. This must be an Opus frame size for the encoder's sampling rate. For example, at 48 kHz the permitted values are 120, 240, 480, 960, 1920, and 2880. Passing in a duration of less than 10 ms (480 samples at 48 kHz) will prevent the encoder from using the LPC or hybrid modes. \
 *data* `unsigned char*`: Output payload. This must contain storage for at least *max\_data\_bytes*. \
 *max\_data\_bytes* `opus_int32`: Size of the allocated memory for the output payload. This may be used to impose an upper limit on the instant bitrate, but should not be used as the only bitrate control. Use **OPUS\_SET\_BITRATE** to control the bitrate.

**Returns**

The length of the encoded packet (in bytes) on success or a negative error code (see **Error codes**) on failure.

### opus\_int32 opus\_encode\_float (OpusEncoder \* st, const float \* pcm, int frame\_size, unsigned char \* data, opus\_int32 max\_data\_bytes)

Encodes an Opus frame from floating point input.

**Parameters**

*st* `OpusEncoder*`: Encoder state \
 *pcm* `float*`: Input in float format (interleaved if 2 channels), with a normal range of +/-1.0. Samples with a range beyond +/-1.0 are supported but will be clipped by decoders using the integer API and should only be used if it is known that the far end supports extended dynamic range. length is frame\_size\*channels\*sizeof(float) \
 *frame\_size* `int`: Number of samples per channel in the input signal. This must be an Opus frame size for the encoder's sampling rate. For example, at 48 kHz the permitted values are 120, 240, 480, 960, 1920, and 2880. Passing in a duration of less than 10 ms (480 samples at 48 kHz) will prevent the encoder from using the LPC or hybrid modes. \
 *data* `unsigned char*`: Output payload. This must contain storage for at least *max\_data\_bytes*. \
 *max\_data\_bytes* `opus_int32`: Size of the allocated memory for the output payload. This may be used to impose an upper limit on the instant bitrate, but should not be used as the only bitrate control. Use **OPUS\_SET\_BITRATE** to control the bitrate.

**Returns**

The length of the encoded packet (in bytes) on success or a negative error code (see **Error codes**) on failure.

### OpusEncoder \* opus\_encoder\_create (opus\_int32 Fs, int channels, int application, int \* error)

Allocates and initializes an encoder state. There are three coding modes:

**OPUS\_APPLICATION\_VOIP** gives best quality at a given bitrate for voice signals. It enhances the input signal by high-pass filtering and emphasizing formants and harmonics. Optionally it includes in-band forward error correction to protect against packet loss. Use this mode for typical VoIP applications. Because of the enhancement, even at high bitrates the output may sound different from the input.

**OPUS\_APPLICATION\_AUDIO** gives best quality at a given bitrate for most non-voice signals like music. Use this mode for music and mixed (music/voice) content, broadcast, and applications requiring less than 15 ms of coding delay.

**OPUS\_APPLICATION\_RESTRICTED\_LOWDELAY** configures low-delay mode that disables the speech-optimized mode in exchange for slightly reduced delay. This mode can only be set on an newly initialized or freshly reset encoder because it changes the codec delay.

This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution).

**Parameters**

*Fs* `opus_int32`: Sampling rate of input signal (Hz) This must be one of 8000, 12000, 16000, 24000, or 48000. \
 *channels* `int`: Number of channels (1 or 2) in input signal \
 *application* `int`: Coding mode (one of **OPUS\_APPLICATION\_VOIP**, **OPUS\_APPLICATION\_AUDIO**, or **OPUS\_APPLICATION\_RESTRICTED\_LOWDELAY**) \
 *error* `int*`: **Error codes**

**Note**

Regardless of the sampling rate and number channels selected, the Opus encoder can switch to a lower audio bandwidth or number of channels if the bitrate selected is too low. This also means that it is safe to always use 48 kHz stereo input and let the encoder optimize the encoding.

### int opus\_encoder\_ctl (OpusEncoder \* st, int request, ...)

Perform a CTL function on an Opus encoder. Generally the request and subsequent arguments are generated by a convenience macro.

**Parameters**

*st* `OpusEncoder*`: Encoder state. \
 *request* This and all remaining parameters should be replaced by one of the convenience macros in **Generic CTLs** or **Encoder related CTLs**.

**See also**

**Generic CTLs**

**Encoder related CTLs**

### void opus\_encoder\_destroy (OpusEncoder \* st)

Frees an `OpusEncoder` allocated by **opus\_encoder\_create()**.

**Parameters**

*st* `OpusEncoder*`: State to be freed.

### int opus\_encoder\_get\_size (int channels)

Gets the size of an `OpusEncoder` structure.

**Parameters**

*channels* `int`: Number of channels. This must be 1 or 2.

**Returns**

The size in bytes.

### int opus\_encoder\_init (OpusEncoder \* st, opus\_int32 Fs, int channels, int application)

Initializes a previously allocated encoder state The memory pointed to by st must be at least the size returned by **opus\_encoder\_get\_size()**. This is intended for applications which use their own allocator instead of malloc.

**See also**

**opus\_encoder\_create()**,**opus\_encoder\_get\_size()** To reset a previously initialized state, use the **OPUS\_RESET\_STATE** CTL.

**Parameters**

*st* `OpusEncoder*`: Encoder state \
 *Fs* `opus_int32`: Sampling rate of input signal (Hz) This must be one of 8000, 12000, 16000, 24000, or 48000. \
 *channels* `int`: Number of channels (1 or 2) in input signal \
 *application* `int`: Coding mode (one of OPUS\_APPLICATION\_VOIP, OPUS\_APPLICATION\_AUDIO, or OPUS\_APPLICATION\_RESTRICTED\_LOWDELAY)

**Return values**

**OPUS\_OK Success or Error codes**

## Author

**Generated automatically by Doxygen for Opus from the source code.**
