Typedefs | Functions

Opus Encoder

Since Opus is a stateful codec, the encoding process starts with creating an encoder state. More...

Typedefs

typedef struct OpusEncoder OpusEncoder
 Opus encoder state.

Functions

int opus_encoder_get_size (int channels)
OpusEncoderopus_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 the size returned by opus_encoder_get_size.
int opus_encode (OpusEncoder *st, const opus_int16 *pcm, int frame_size, unsigned char *data, int max_data_bytes)
 Encodes an Opus frame.
int opus_encode_float (OpusEncoder *st, const float *pcm, int frame_size, unsigned char *data, int 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

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 assuptions 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

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

opus_encode() and opus_encode_frame() 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 1 byte, 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

int opus_encode ( OpusEncoder st,
const opus_int16 pcm,
int  frame_size,
unsigned char *  data,
int  max_data_bytes 
)

Encodes an Opus frame.

The passed frame_size must an opus frame size for the encoder's sampling rate. For example, at 48kHz the permitted values are 120, 240, 480, or 960. Passing in a duration of less than 10ms (480 samples at 48kHz) will prevent the encoder from using the LPC or hybrid modes.

Parameters:
[in] st OpusEncoder*: Encoder state
[in] pcm opus_int16*: Input signal (interleaved if 2 channels). length is frame_size*channels*sizeof(opus_int16)
[in] frame_size int: Number of samples per frame of input signal
[out] data char*: Output payload (at least max_data_bytes long)
[in] max_data_bytes int: Allocated memory for payload; don't use for controlling bitrate
Returns:
length of the data payload (in bytes) or Error codes
int opus_encode_float ( OpusEncoder st,
const float *  pcm,
int  frame_size,
unsigned char *  data,
int  max_data_bytes 
)

Encodes an Opus frame from floating point input.

The passed frame_size must an opus frame size for the encoder's sampling rate. For example, at 48kHz the permitted values are 120, 240, 480, or 960. Passing in a duration of less than 10ms (480 samples at 48kHz) will prevent the encoder from using the LPC or hybrid modes.

Parameters:
[in] st OpusEncoder*: Encoder state
[in] pcm float*: Input signal (interleaved if 2 channels). length is frame_size*channels*sizeof(float)
[in] frame_size int: Number of samples per frame of input signal
[out] data char*: Output payload (at least max_data_bytes long)
[in] max_data_bytes int: Allocated memory for payload; don't use for controlling bitrate
Returns:
length of the data payload (in bytes) or Error codes
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 is useful when the caller knows that the speech-optimized modes will not be needed (use with caution).

Parameters:
[in] Fs opus_int32: Sampling rate of input signal (Hz)
[in] channels int: Number of channels (1/2) in input signal
[in] application int: Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY)
[out] error int*: Error codes
Note:
Regardless of the sampling rate and number channels selected, the Opus encoder can switch to a lower audio 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.

See also:
Encoder related CTLs
void opus_encoder_destroy ( OpusEncoder st  ) 

Frees an OpusEncoder allocated by opus_encoder_create.

Parameters:
[in] st OpusEncoder*: State to be freed.
int opus_encoder_get_size ( int  channels  ) 
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 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:
[in] st OpusEncoder*: Encoder state
[in] Fs opus_int32: Sampling rate of input signal (Hz)
[in] channels int: Number of channels (1/2) in input signal
[in] application int: Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY)
Return values:
OPUS_OK Success or Error codes
 All Files Functions Typedefs Defines