pub struct Encoder { /* private fields */ }
Expand description
OPUS encoder
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn new(
channels: Channels,
rate: SampleRate,
app: Application,
) -> Result<Self, ErrorCode>
pub fn new( channels: Channels, rate: SampleRate, app: Application, ) -> Result<Self, ErrorCode>
Creates new encoder instance
Sourcepub fn channels(&self) -> Channels
pub fn channels(&self) -> Channels
Returns channels number
When encoding, it is used to determine frame size as input.len() / channels
Sourcepub fn encode_to(
&mut self,
input: &[u16],
output: &mut [MaybeUninit<u8>],
) -> Result<usize, ErrorCode>
pub fn encode_to( &mut self, input: &[u16], output: &mut [MaybeUninit<u8>], ) -> Result<usize, ErrorCode>
Encodes an Opus frame, returning number of bytes written.
If more than 1 channel is configured, then input must be interleaved.
Input size must correspond to sampling rate. For example, at 48 kHz allowed frame sizes 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.
Sourcepub fn encode_to_slice(
&mut self,
input: &[u16],
output: &mut [u8],
) -> Result<usize, ErrorCode>
pub fn encode_to_slice( &mut self, input: &[u16], output: &mut [u8], ) -> Result<usize, ErrorCode>
Encodes an Opus frame, returning number of bytes written.
Refer to encode_to
for details
Sourcepub fn encode_to_vec(
&mut self,
input: &[u16],
output: &mut Vec<u8>,
) -> Result<usize, ErrorCode>
pub fn encode_to_vec( &mut self, input: &[u16], output: &mut Vec<u8>, ) -> Result<usize, ErrorCode>
Encodes an Opus frame, returning number of bytes written.
Vector will be written into spare capacity, modifying its length on success.
It is user responsibility to reserve correct amount of space
Refer to encode_to
for details
Sourcepub fn encode_float_to(
&mut self,
input: &[f32],
output: &mut [MaybeUninit<u8>],
) -> Result<usize, ErrorCode>
pub fn encode_float_to( &mut self, input: &[f32], output: &mut [MaybeUninit<u8>], ) -> Result<usize, ErrorCode>
Encodes an Opus frame, returning number of bytes written.
If more than 1 channel is configured, then input must be interleaved.
Input size must correspond to sampling rate. For example, at 48 kHz allowed frame sizes 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.
§Note
When using float API, input with a normal range of +/-1.0 should be preferred. 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
Sourcepub fn encode_float_to_slice(
&mut self,
input: &[f32],
output: &mut [u8],
) -> Result<usize, ErrorCode>
pub fn encode_float_to_slice( &mut self, input: &[f32], output: &mut [u8], ) -> Result<usize, ErrorCode>
Encodes an Opus frame, returning number of bytes written.
Refer to encode_to
for details
Sourcepub fn encode_float_to_vec(
&mut self,
input: &[f32],
output: &mut Vec<u8>,
) -> Result<usize, ErrorCode>
pub fn encode_float_to_vec( &mut self, input: &[f32], output: &mut Vec<u8>, ) -> Result<usize, ErrorCode>
Encodes an Opus frame, returning number of bytes written.
Vector will be written into spare capacity, modifying its length on success.
It is user responsibility to reserve correct amount of space
Refer to encode_to
for details
Sourcepub fn get_look_ahead(&mut self) -> Result<u32, ErrorCode>
pub fn get_look_ahead(&mut self) -> Result<u32, ErrorCode>
Gets the total samples of delay added by the entire codec.
From the perspective of a decoding application the real data begins this many samples late.
Sourcepub fn get_bitrate(&mut self) -> Result<Bitrate, ErrorCode>
pub fn get_bitrate(&mut self) -> Result<Bitrate, ErrorCode>
Gets the encoder’s bitrate configuration.
Sourcepub fn set_bitrate(&mut self, value: Bitrate) -> Result<(), ErrorCode>
pub fn set_bitrate(&mut self, value: Bitrate) -> Result<(), ErrorCode>
Configures the encoder’s bitrate
Sourcepub fn get_vbr(&mut self) -> Result<bool, ErrorCode>
pub fn get_vbr(&mut self) -> Result<bool, ErrorCode>
Determine if variable bitrate (VBR) is enabled in the encoder.
Sourcepub fn set_vbr(&mut self, value: bool) -> Result<(), ErrorCode>
pub fn set_vbr(&mut self, value: bool) -> Result<(), ErrorCode>
Enables or disables variable bitrate (VBR) in the encoder.
The configured bitrate may not be met exactly because frames must be an integer number of bytes in length.
Sourcepub fn get_vbr_constraint(&mut self) -> Result<bool, ErrorCode>
pub fn get_vbr_constraint(&mut self) -> Result<bool, ErrorCode>
Determine if constrained VBR is enabled in the encoder.
Sourcepub fn set_vbr_constraint(&mut self, value: bool) -> Result<(), ErrorCode>
pub fn set_vbr_constraint(&mut self, value: bool) -> Result<(), ErrorCode>
Enables or disables constrained VBR in the encoder.
This setting is ignored when the encoder is in CBR mode.
§Note
Only the MDCT mode of Opus currently heeds the constraint. Speech mode ignores it completely, hybrid mode may fail to obey it if the LPC layer uses more bitrate than the constraint would have permitted.
Sourcepub fn get_force_channels(&mut self) -> Result<Option<Channels>, ErrorCode>
pub fn get_force_channels(&mut self) -> Result<Option<Channels>, ErrorCode>
Gets the encoder’s forced channel configuration (if set).
Sourcepub fn set_force_channels(
&mut self,
value: Option<Channels>,
) -> Result<(), ErrorCode>
pub fn set_force_channels( &mut self, value: Option<Channels>, ) -> Result<(), ErrorCode>
Configures mono/stereo forcing in the encoder (or disables it by specifying None).
This can force the encoder to produce packets encoded as either mono or stereo, regardless of the format of the input audio. This is useful when the caller knows that the input signal is currently a mono source embedded in a stereo stream.
Sourcepub fn get_complexity(&mut self) -> Result<u8, ErrorCode>
pub fn get_complexity(&mut self) -> Result<u8, ErrorCode>
Gets the encoder’s complexity configuration.
Sourcepub fn set_complexity(&mut self, value: u8) -> Result<(), ErrorCode>
pub fn set_complexity(&mut self, value: u8) -> Result<(), ErrorCode>
Configures the encoder’s computational complexity.
The supported range is 0-10 inclusive with 10 representing the highest complexity.
Sourcepub fn get_signal(&mut self) -> Result<Signal, ErrorCode>
pub fn get_signal(&mut self) -> Result<Signal, ErrorCode>
Gets the encoder’s configured signal type.
Sourcepub fn set_signal(&mut self, value: Signal) -> Result<(), ErrorCode>
pub fn set_signal(&mut self, value: Signal) -> Result<(), ErrorCode>
Configures the type of signal being encoded.
This is a hint which helps the encoder’s mode selection.
Sourcepub fn get_application(&mut self) -> Result<Application, ErrorCode>
pub fn get_application(&mut self) -> Result<Application, ErrorCode>
Gets the encoder’s configured application.
Sourcepub fn set_application(&mut self, value: Application) -> Result<(), ErrorCode>
pub fn set_application(&mut self, value: Application) -> Result<(), ErrorCode>
Configures the encoder’s intended application.
The initial value is a mandatory argument to encoder constructor.
Sourcepub fn get_bandwidth(&mut self) -> Result<Bandwidth, ErrorCode>
pub fn get_bandwidth(&mut self) -> Result<Bandwidth, ErrorCode>
Gets the encoder’s configured bandpass
Sourcepub fn set_bandwidth(&mut self, value: Bandwidth) -> Result<(), ErrorCode>
pub fn set_bandwidth(&mut self, value: Bandwidth) -> Result<(), ErrorCode>
Sets the encoder’s bandpass to a specific value.
This prevents the encoder from automatically selecting the bandpass based on the available
bitrate. If an application knows the bandpass of the input audio it is providing, it should
normally use set_max_bandwidth
instead, which still gives the encoder the freedom to
reduce the bandpass when the bitrate becomes too low, for better overall quality.
Sourcepub fn get_max_bandwidth(&mut self) -> Result<Bandwidth, ErrorCode>
pub fn get_max_bandwidth(&mut self) -> Result<Bandwidth, ErrorCode>
Gets the encoder’s configured maximum allowed bandpass.
Sourcepub fn set_max_bandwidth(&mut self, value: Bandwidth) -> Result<(), ErrorCode>
pub fn set_max_bandwidth(&mut self, value: Bandwidth) -> Result<(), ErrorCode>
Configures the maximum bandpass that the encoder will select automatically.
Applications should normally use this instead of set_bandwidth
(leaving that set to the
default, Bandwidth::Auto
). This allows the application to set an upper bound based on the type of
input it is providing, but still gives the encoder the freedom to reduce the bandpass when
the bitrate becomes too low, for better overall quality.
Sourcepub fn get_inband_fec(&mut self) -> Result<InbandFec, ErrorCode>
pub fn get_inband_fec(&mut self) -> Result<InbandFec, ErrorCode>
Gets encoder’s configured use of inband forward error correction.
Sourcepub fn set_inband_fec(&mut self, value: InbandFec) -> Result<(), ErrorCode>
pub fn set_inband_fec(&mut self, value: InbandFec) -> Result<(), ErrorCode>
Configures the encoder’s use of inband forward error correction (FEC).
§Note
This is only applicable to the LPC layer
Sourcepub fn get_packet_loss(&mut self) -> Result<u8, ErrorCode>
pub fn get_packet_loss(&mut self) -> Result<u8, ErrorCode>
Gets the encoder’s configured packet loss percentage.
Sourcepub fn set_packet_loss(&mut self, value: u8) -> Result<(), ErrorCode>
pub fn set_packet_loss(&mut self, value: u8) -> Result<(), ErrorCode>
Configures the encoder’s expected packet loss percentage (Allowed values are 0..=100).
Higher values trigger progressively more loss resistant behavior in the encoder at the expense of quality at a given bitrate in the absence of packet loss, but greater quality under loss.
Sourcepub fn get_prediction_disabled(&mut self) -> Result<bool, ErrorCode>
pub fn get_prediction_disabled(&mut self) -> Result<bool, ErrorCode>
Gets the encoder’s configured prediction status.
Sourcepub fn set_prediction_disabled(&mut self, value: bool) -> Result<(), ErrorCode>
pub fn set_prediction_disabled(&mut self, value: bool) -> Result<(), ErrorCode>
If set to true
, disables almost all use of prediction, making frames almost completely independent.
This reduces quality.
Sourcepub fn get_lsb_depth(&mut self) -> Result<u8, ErrorCode>
pub fn get_lsb_depth(&mut self) -> Result<u8, ErrorCode>
Gets the encoder’s configured signal depth.
Sourcepub fn set_lsb_depth(&mut self, value: u8) -> Result<(), ErrorCode>
pub fn set_lsb_depth(&mut self, value: u8) -> Result<(), ErrorCode>
Configures the depth of signal being encoded (Defaults to 24) in range 8 to 24.
This is a hint which helps the encoder identify silence and near-silence. It represents the number of significant bits of linear intensity below which the signal contains ignorable quantization or other noise.
For example, 14 would be an appropriate setting for G.711 u-law input.
16 would be appropriate for 16-bit linear pcm input with encode_float
.
When using encode
instead of encode_float
, or when libopus is compiled for
fixed-point, the encoder uses the minimum of the value set here and the value 16.
Sourcepub fn get_frame_duration(&mut self) -> Result<FrameDuration, ErrorCode>
pub fn get_frame_duration(&mut self) -> Result<FrameDuration, ErrorCode>
Gets the encoder’s configured use of variable duration frames.
Sourcepub fn set_frame_duration(
&mut self,
value: FrameDuration,
) -> Result<(), ErrorCode>
pub fn set_frame_duration( &mut self, value: FrameDuration, ) -> Result<(), ErrorCode>
Configures the encoder’s use of variable duration frames.
When variable duration is enabled, the encoder is free to use a shorter frame size than the
one requested in the encode
call. It is then the user’s responsibility to verify how
much audio was encoded by checking the ToC byte of the encoded packet. The part of the audio
that was not encoded needs to be resent to the encoder for the next call. Do not use this
option unless you really know what you are doing.
Sourcepub fn get_dred_duration(&mut self) -> Result<u8, ErrorCode>
pub fn get_dred_duration(&mut self) -> Result<u8, ErrorCode>
Gets the encoder’s configured Deep Redundancy (DRED) maximum number of frames.
Sourcepub fn set_dred_duration(&mut self, value: u8) -> Result<(), ErrorCode>
pub fn set_dred_duration(&mut self, value: u8) -> Result<(), ErrorCode>
Configures value of Deep Redundancy (DRED) in range 0..=104
If non-zero, enables DRED and use the specified maximum number of 10-ms redundant frames.
Sourcepub fn get_sample_rate(&mut self) -> Result<SampleRate, ErrorCode>
pub fn get_sample_rate(&mut self) -> Result<SampleRate, ErrorCode>
Gets configured sample rate of this instance
Sourcepub fn set_dtx(&mut self, value: bool) -> Result<(), ErrorCode>
pub fn set_dtx(&mut self, value: bool) -> Result<(), ErrorCode>
Configures the encoder’s use of discontinuous transmission (DTX).
This is only applicable to the LPC layer
Sourcepub fn get_phase_inversion_disabled(&mut self) -> Result<bool, ErrorCode>
pub fn get_phase_inversion_disabled(&mut self) -> Result<bool, ErrorCode>
Gets the encoder’s configured phase inversion status.