.TH "opus_decoder" 3 "Fri Feb 11 2022" "Version 1.3.1" "Opus" \" -*- nroff -*- .ad l .nh .SH NAME opus_decoder \- Opus Decoder .PP \- This page describes the process and functions used to decode Opus\&. .SH SYNOPSIS .br .PP .SS "Typedefs" .in +1c .ti -1c .RI "typedef struct \fBOpusDecoder\fP \fBOpusDecoder\fP" .br .RI "Opus decoder state\&. " .in -1c .SS "Functions" .in +1c .ti -1c .RI "int \fBopus_decoder_get_size\fP (int channels)" .br .RI "Gets the size of an \fCOpusDecoder\fP structure\&. " .ti -1c .RI "\fBOpusDecoder\fP * \fBopus_decoder_create\fP (\fBopus_int32\fP Fs, int channels, int *error)" .br .RI "Allocates and initializes a decoder state\&. " .ti -1c .RI "int \fBopus_decoder_init\fP (\fBOpusDecoder\fP *st, \fBopus_int32\fP Fs, int channels)" .br .RI "Initializes a previously allocated decoder state\&. " .ti -1c .RI "int \fBopus_decode\fP (\fBOpusDecoder\fP *st, const unsigned char *data, \fBopus_int32\fP len, \fBopus_int16\fP *pcm, int frame_size, int decode_fec)" .br .RI "Decode an Opus packet\&. " .ti -1c .RI "int \fBopus_decode_float\fP (\fBOpusDecoder\fP *st, const unsigned char *data, \fBopus_int32\fP len, float *pcm, int frame_size, int decode_fec)" .br .RI "Decode an Opus packet with floating point output\&. " .ti -1c .RI "int \fBopus_decoder_ctl\fP (\fBOpusDecoder\fP *st, int request,\&.\&.\&.)" .br .RI "Perform a CTL function on an Opus decoder\&. " .ti -1c .RI "void \fBopus_decoder_destroy\fP (\fBOpusDecoder\fP *st)" .br .RI "Frees an \fCOpusDecoder\fP allocated by \fBopus_decoder_create()\fP\&. " .ti -1c .RI "int \fBopus_packet_parse\fP (const unsigned char *data, \fBopus_int32\fP len, unsigned char *out_toc, const unsigned char *frames[48], \fBopus_int16\fP size[48], int *payload_offset)" .br .RI "Parse an opus packet into one or more frames\&. " .ti -1c .RI "int \fBopus_packet_get_bandwidth\fP (const unsigned char *data)" .br .RI "Gets the bandwidth of an Opus packet\&. " .ti -1c .RI "int \fBopus_packet_get_samples_per_frame\fP (const unsigned char *data, \fBopus_int32\fP Fs)" .br .RI "Gets the number of samples per frame from an Opus packet\&. " .ti -1c .RI "int \fBopus_packet_get_nb_channels\fP (const unsigned char *data)" .br .RI "Gets the number of channels from an Opus packet\&. " .ti -1c .RI "int \fBopus_packet_get_nb_frames\fP (const unsigned char packet[], \fBopus_int32\fP len)" .br .RI "Gets the number of frames in an Opus packet\&. " .ti -1c .RI "int \fBopus_packet_get_nb_samples\fP (const unsigned char packet[], \fBopus_int32\fP len, \fBopus_int32\fP Fs)" .br .RI "Gets the number of samples of an Opus packet\&. " .ti -1c .RI "int \fBopus_decoder_get_nb_samples\fP (const \fBOpusDecoder\fP *dec, const unsigned char packet[], \fBopus_int32\fP len)" .br .RI "Gets the number of samples of an Opus packet\&. " .ti -1c .RI "void \fBopus_pcm_soft_clip\fP (float *pcm, int frame_size, int channels, float *softclip_mem)" .br .RI "Applies soft-clipping to bring a float signal within the [-1,1] range\&. " .in -1c .SH "Detailed Description" .PP This page describes the process and functions used to decode Opus\&. The decoding process also starts with creating a decoder state\&. This can be done with: .PP .nf int error; OpusDecoder *dec; dec = opus_decoder_create(Fs, channels, &error); .fi .PP where .PD 0 .IP "\(bu" 2 Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000 .IP "\(bu" 2 channels is the number of channels (1 or 2) .IP "\(bu" 2 error will hold the error code in case of failure (or \fBOPUS_OK\fP on success) .IP "\(bu" 2 the return value is a newly created decoder state to be used for decoding .PP While \fBopus_decoder_create()\fP allocates memory for the state, it's also possible to initialize pre-allocated memory: .PP .nf int size; int error; OpusDecoder *dec; size = opus_decoder_get_size(channels); dec = malloc(size); error = opus_decoder_init(dec, Fs, channels); .fi .PP where \fBopus_decoder_get_size()\fP returns the required size for the decoder state\&. Note that future versions of this code may change the size, so no assuptions should be made about it\&. .PP The decoder state is always continuous in memory and only a shallow copy is sufficient to copy it (e\&.g\&. memcpy()) .PP To decode a frame, \fBopus_decode()\fP or \fBopus_decode_float()\fP must be called with a packet of compressed audio data: .PP .nf frame_size = opus_decode(dec, packet, len, decoded, max_size, 0); .fi .PP where .PP .PD 0 .IP "\(bu" 2 packet is the byte array containing the compressed data .IP "\(bu" 2 len is the exact number of bytes contained in the packet .IP "\(bu" 2 decoded is the decoded audio data in opus_int16 (or float for \fBopus_decode_float()\fP) .IP "\(bu" 2 max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array .PP \fBopus_decode()\fP and \fBopus_decode_float()\fP return the number of samples (per channel) decoded from the packet\&. If that value is negative, then an error has occurred\&. This can occur if the packet is corrupted or if the audio buffer is too small to hold the decoded audio\&. .PP Opus is a stateful codec with overlapping blocks and as a result Opus packets are not coded independently of each other\&. Packets must be passed into the decoder serially and in the correct order for a correct decode\&. Lost packets can be replaced with loss concealment by calling the decoder with a null pointer and zero length for the missing packet\&. .PP A single codec state may only be accessed from a single thread at a time and any required locking must be performed by the caller\&. Separate streams must be decoded with separate decoder states and can be decoded in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK defined\&. .SH "Typedef Documentation" .PP .SS "typedef struct \fBOpusDecoder\fP \fBOpusDecoder\fP" .PP Opus decoder state\&. This contains the complete state of an Opus decoder\&. It is position independent and can be freely copied\&. .PP \fBSee also\fP .RS 4 \fBopus_decoder_create\fP,\fBopus_decoder_init\fP .RE .PP .SH "Function Documentation" .PP .SS "int opus_decode (\fBOpusDecoder\fP * st, const unsigned char * data, \fBopus_int32\fP len, \fBopus_int16\fP * pcm, int frame_size, int decode_fec)" .PP Decode an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIst\fP \fCOpusDecoder*\fP: Decoder state .br \fIdata\fP \fCchar*\fP: Input payload\&. Use a NULL pointer to indicate packet loss .br \fIlen\fP \fCopus_int32\fP: Number of bytes in payload* .br \fIpcm\fP \fCopus_int16*\fP: Output signal (interleaved if 2 channels)\&. length is frame_size*channels*sizeof(opus_int16) .br \fIframe_size\fP Number of samples per channel of available space in \fIpcm\fP\&. If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will not be capable of decoding some packets\&. In the case of PLC (data==NULL) or FEC (decode_fec=1), then frame_size needs to be exactly the duration of audio that is missing, otherwise the decoder will not be in the optimal state to decode the next incoming packet\&. For the PLC and FEC cases, frame_size \fBmust\fP be a multiple of 2\&.5 ms\&. .br \fIdecode_fec\fP \fCint\fP: Flag (0 or 1) to request that any in-band forward error correction data be decoded\&. If no such data is available, the frame is decoded as if it were lost\&. .RE .PP \fBReturns\fP .RS 4 Number of decoded samples or \fBError codes\fP .RE .PP .SS "int opus_decode_float (\fBOpusDecoder\fP * st, const unsigned char * data, \fBopus_int32\fP len, float * pcm, int frame_size, int decode_fec)" .PP Decode an Opus packet with floating point output\&. .PP \fBParameters\fP .RS 4 \fIst\fP \fCOpusDecoder*\fP: Decoder state .br \fIdata\fP \fCchar*\fP: Input payload\&. Use a NULL pointer to indicate packet loss .br \fIlen\fP \fCopus_int32\fP: Number of bytes in payload .br \fIpcm\fP \fCfloat*\fP: Output signal (interleaved if 2 channels)\&. length is frame_size*channels*sizeof(float) .br \fIframe_size\fP Number of samples per channel of available space in \fIpcm\fP\&. If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will not be capable of decoding some packets\&. In the case of PLC (data==NULL) or FEC (decode_fec=1), then frame_size needs to be exactly the duration of audio that is missing, otherwise the decoder will not be in the optimal state to decode the next incoming packet\&. For the PLC and FEC cases, frame_size \fBmust\fP be a multiple of 2\&.5 ms\&. .br \fIdecode_fec\fP \fCint\fP: Flag (0 or 1) to request that any in-band forward error correction data be decoded\&. If no such data is available the frame is decoded as if it were lost\&. .RE .PP \fBReturns\fP .RS 4 Number of decoded samples or \fBError codes\fP .RE .PP .SS "\fBOpusDecoder\fP * opus_decoder_create (\fBopus_int32\fP Fs, int channels, int * error)" .PP Allocates and initializes a decoder state\&. .PP \fBParameters\fP .RS 4 \fIFs\fP \fCopus_int32\fP: Sample rate to decode at (Hz)\&. This must be one of 8000, 12000, 16000, 24000, or 48000\&. .br \fIchannels\fP \fCint\fP: Number of channels (1 or 2) to decode .br \fIerror\fP \fCint*\fP: \fBOPUS_OK\fP Success or \fBError codes\fP .RE .PP Internally Opus stores data at 48000 Hz, so that should be the default value for Fs\&. However, the decoder can efficiently decode to buffers at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use data at the full sample rate, or knows the compressed data doesn't use the full frequency range, it can request decoding at a reduced rate\&. Likewise, the decoder is capable of filling in either mono or interleaved stereo pcm buffers, at the caller's request\&. .SS "int opus_decoder_ctl (\fBOpusDecoder\fP * st, int request, \&.\&.\&.)" .PP Perform a CTL function on an Opus decoder\&. Generally the request and subsequent arguments are generated by a convenience macro\&. .PP \fBParameters\fP .RS 4 \fIst\fP \fCOpusDecoder*\fP: Decoder state\&. .br \fIrequest\fP This and all remaining parameters should be replaced by one of the convenience macros in \fBGeneric CTLs\fP or \fBDecoder related CTLs\fP\&. .RE .PP \fBSee also\fP .RS 4 \fBGeneric CTLs\fP .PP \fBDecoder related CTLs\fP .RE .PP .SS "void opus_decoder_destroy (\fBOpusDecoder\fP * st)" .PP Frees an \fCOpusDecoder\fP allocated by \fBopus_decoder_create()\fP\&. .PP \fBParameters\fP .RS 4 \fIst\fP \fCOpusDecoder*\fP: State to be freed\&. .RE .PP .SS "int opus_decoder_get_nb_samples (const \fBOpusDecoder\fP * dec, const unsigned char packet[], \fBopus_int32\fP len)" .PP Gets the number of samples of an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIdec\fP \fCOpusDecoder*\fP: Decoder state .br \fIpacket\fP \fCchar*\fP: Opus packet .br \fIlen\fP \fCopus_int32\fP: Length of packet .RE .PP \fBReturns\fP .RS 4 Number of samples .RE .PP \fBReturn values\fP .RS 4 \fIOPUS_BAD_ARG\fP Insufficient data was passed to the function .br \fIOPUS_INVALID_PACKET\fP The compressed data passed is corrupted or of an unsupported type .RE .PP .SS "int opus_decoder_get_size (int channels)" .PP Gets the size of an \fCOpusDecoder\fP structure\&. .PP \fBParameters\fP .RS 4 \fIchannels\fP \fCint\fP: Number of channels\&. This must be 1 or 2\&. .RE .PP \fBReturns\fP .RS 4 The size in bytes\&. .RE .PP .SS "int opus_decoder_init (\fBOpusDecoder\fP * st, \fBopus_int32\fP Fs, int channels)" .PP Initializes a previously allocated decoder state\&. The state must be at least the size returned by \fBopus_decoder_get_size()\fP\&. This is intended for applications which use their own allocator instead of malloc\&. .PP \fBSee also\fP .RS 4 \fBopus_decoder_create\fP,\fBopus_decoder_get_size\fP To reset a previously initialized state, use the \fBOPUS_RESET_STATE\fP CTL\&. .RE .PP \fBParameters\fP .RS 4 \fIst\fP \fCOpusDecoder*\fP: Decoder state\&. .br \fIFs\fP \fCopus_int32\fP: Sampling rate to decode to (Hz)\&. This must be one of 8000, 12000, 16000, 24000, or 48000\&. .br \fIchannels\fP \fCint\fP: Number of channels (1 or 2) to decode .RE .PP \fBReturn values\fP .RS 4 \fI\fBOPUS_OK\fP\fP Success or \fBError codes\fP .RE .PP .SS "int opus_packet_get_bandwidth (const unsigned char * data)" .PP Gets the bandwidth of an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIdata\fP \fCchar*\fP: Opus packet .RE .PP \fBReturn values\fP .RS 4 \fIOPUS_BANDWIDTH_NARROWBAND\fP Narrowband (4kHz bandpass) .br \fIOPUS_BANDWIDTH_MEDIUMBAND\fP Mediumband (6kHz bandpass) .br \fIOPUS_BANDWIDTH_WIDEBAND\fP Wideband (8kHz bandpass) .br \fIOPUS_BANDWIDTH_SUPERWIDEBAND\fP Superwideband (12kHz bandpass) .br \fIOPUS_BANDWIDTH_FULLBAND\fP Fullband (20kHz bandpass) .br \fIOPUS_INVALID_PACKET\fP The compressed data passed is corrupted or of an unsupported type .RE .PP .SS "int opus_packet_get_nb_channels (const unsigned char * data)" .PP Gets the number of channels from an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIdata\fP \fCchar*\fP: Opus packet .RE .PP \fBReturns\fP .RS 4 Number of channels .RE .PP \fBReturn values\fP .RS 4 \fIOPUS_INVALID_PACKET\fP The compressed data passed is corrupted or of an unsupported type .RE .PP .SS "int opus_packet_get_nb_frames (const unsigned char packet[], \fBopus_int32\fP len)" .PP Gets the number of frames in an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIpacket\fP \fCchar*\fP: Opus packet .br \fIlen\fP \fCopus_int32\fP: Length of packet .RE .PP \fBReturns\fP .RS 4 Number of frames .RE .PP \fBReturn values\fP .RS 4 \fIOPUS_BAD_ARG\fP Insufficient data was passed to the function .br \fIOPUS_INVALID_PACKET\fP The compressed data passed is corrupted or of an unsupported type .RE .PP .SS "int opus_packet_get_nb_samples (const unsigned char packet[], \fBopus_int32\fP len, \fBopus_int32\fP Fs)" .PP Gets the number of samples of an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIpacket\fP \fCchar*\fP: Opus packet .br \fIlen\fP \fCopus_int32\fP: Length of packet .br \fIFs\fP \fCopus_int32\fP: Sampling rate in Hz\&. This must be a multiple of 400, or inaccurate results will be returned\&. .RE .PP \fBReturns\fP .RS 4 Number of samples .RE .PP \fBReturn values\fP .RS 4 \fIOPUS_BAD_ARG\fP Insufficient data was passed to the function .br \fIOPUS_INVALID_PACKET\fP The compressed data passed is corrupted or of an unsupported type .RE .PP .SS "int opus_packet_get_samples_per_frame (const unsigned char * data, \fBopus_int32\fP Fs)" .PP Gets the number of samples per frame from an Opus packet\&. .PP \fBParameters\fP .RS 4 \fIdata\fP \fCchar*\fP: Opus packet\&. This must contain at least one byte of data\&. .br \fIFs\fP \fCopus_int32\fP: Sampling rate in Hz\&. This must be a multiple of 400, or inaccurate results will be returned\&. .RE .PP \fBReturns\fP .RS 4 Number of samples per frame\&. .RE .PP .SS "int opus_packet_parse (const unsigned char * data, \fBopus_int32\fP len, unsigned char * out_toc, const unsigned char * frames[48], \fBopus_int16\fP size[48], int * payload_offset)" .PP Parse an opus packet into one or more frames\&. Opus_decode will perform this operation internally so most applications do not need to use this function\&. This function does not copy the frames, the returned pointers are pointers into the input packet\&. .PP \fBParameters\fP .RS 4 \fIdata\fP \fCchar*\fP: Opus packet to be parsed .br \fIlen\fP \fCopus_int32\fP: size of data .br \fIout_toc\fP \fCchar*\fP: TOC pointer .br \fIframes\fP \fCchar*[48]\fP encapsulated frames .br \fIsize\fP \fCopus_int16[48]\fP sizes of the encapsulated frames .br \fIpayload_offset\fP \fCint*\fP: returns the position of the payload within the packet (in bytes) .RE .PP \fBReturns\fP .RS 4 number of frames .RE .PP .SS "void opus_pcm_soft_clip (float * pcm, int frame_size, int channels, float * softclip_mem)" .PP Applies soft-clipping to bring a float signal within the [-1,1] range\&. If the signal is already in that range, nothing is done\&. If there are values outside of [-1,1], then the signal is clipped as smoothly as possible to both fit in the range and avoid creating excessive distortion in the process\&. .PP \fBParameters\fP .RS 4 \fIpcm\fP \fCfloat*\fP: Input PCM and modified PCM .br \fIframe_size\fP \fCint\fP Number of samples per channel to process .br \fIchannels\fP \fCint\fP: Number of channels .br \fIsoftclip_mem\fP \fCfloat*\fP: State memory for the soft clipping process (one float per channel, initialized to zero) .RE .PP .SH "Author" .PP Generated automatically by Doxygen for Opus from the source code\&.