int opus_encoder_get_size(int channels);
功能:获取一个 OpusEncoder 结构体的大小
参数:
channels:通道数,必须1或2;
返回值:以字节为单位的大小
//---------------------------------------
OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error);
功能:分配并初始化编码器状态
参数:
Fs:采样率,必须是 8000, 12000, 16000, 24000, 48000 之一;
channels:声道数,1或2;
application:OPUS_APPLICATION_VOIP 或 OPUS_APPLICATION_AUDIO 或 PUS_APPLICATION_RESTRICTED_LOWDELAY
error:输出参数,错误码;
返回值:成功返回编码器指针,失败返回NULL;
//---------------------------------------
int opus_encoder_init(OpusEncoder *st, opus_int32 Fs, int channels, int application);
功能:初始化之前分配的编码器状态;
//---------------------------------------
opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size, unsigned char *data, opus_int32 max_data_bytes);
功能:编码一个Opus帧;
//---------------------------------------
opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size, unsigned char *data, opus_int32 max_data_bytes);
功能:从浮点输入编码一个Opus帧
//---------------------------------------
void opus_encoder_destroy(OpusEncoder *st);
功能:释放 opus_encoder_create 分配的 OpusEncoder 指针;
//---------------------------------------
int opus_encoder_ctl(OpusEncoder *st, int request, ...);
功能:在Opus编码器上执行控制功能。
✨3.2 opus 解码相关库函数
c复制代码
int opus_decoder_get_size(int channels);
功能:获取一个 OpusDecoder 结构体的大小
参数:
channels:通道数,必须1或2;
返回值:以字节为单位的大小
//---------------------------------------
OpusDecoder *opus_decoder_create(opus_int32 Fs,int channels,int *error);
功能:分配并初始化解码器状态
参数:
Fs:采样率,必须是 8000, 12000, 16000, 24000, 48000 之一;
channels:声道数,1或2;
error:输出参数,错误码;
返回值:成功返回解码器指针,失败返回NULL;
//---------------------------------------
int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels);
功能:初始化之前分配的解码器状态;
//---------------------------------------
int opus_decode(OpusDecoder *st, const unsigned char *data, opus_int32 len,
opus_int16 *pcm, int frame_size, int decode_fec);
功能:解码一个Opus数据包。
//---------------------------------------
int opus_decode_float(OpusDecoder *st, const unsigned char *data, opus_int32 len,
float *pcm, int frame_size, int decode_fec);
功能:用浮点输出解码一个Opus数据包。
//---------------------------------------
int opus_decoder_ctl(OpusDecoder *st, int request, ...);
功能:在Opus解码器上执行控制功能。
//---------------------------------------
void opus_decoder_destroy(OpusDecoder *st);
功能:释放 opus_decoder_create分配的 OpusDecoder 指针;
//---------------------------------------
int opus_packet_parse(const unsigned char *data,opus_int32 len, unsigned char *out_toc,
const unsigned char *frames[48],opus_int16 size[48],int *payload_offset);
//---------------------------------------
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(const unsigned char *data) OPUS_ARG_NONNULL(1);
/** Gets the number of samples per frame from an Opus packet.
* @param [in] data <tt>char*</tt>: Opus packet.
* This must contain at least one byte of
* data.
* @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
* This must be a multiple of 400, or
* inaccurate results will be returned.
* @returns Number of samples per frame.
*/
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs) OPUS_ARG_NONNULL(1);
/** Gets the number of channels from an Opus packet.
* @param [in] data <tt>char*</tt>: Opus packet
* @returns Number of channels
* @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
*/
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsigned char *data) OPUS_ARG_NONNULL(1);
/** Gets the number of frames in an Opus packet.
* @param [in] packet <tt>char*</tt>: Opus packet
* @param [in] len <tt>opus_int32</tt>: Length of packet
* @returns Number of frames
* @retval OPUS_BAD_ARG Insufficient data was passed to the function
* @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
*/
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1);
/** Gets the number of samples of an Opus packet.
* @param [in] packet <tt>char*</tt>: Opus packet
* @param [in] len <tt>opus_int32</tt>: Length of packet
* @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
* This must be a multiple of 400, or
* inaccurate results will be returned.
* @returns Number of samples
* @retval OPUS_BAD_ARG Insufficient data was passed to the function
* @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
*/
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, opus_int32 Fs) OPUS_ARG_NONNULL(1);
/** Gets the number of samples of an Opus packet.
* @param [in] dec <tt>OpusDecoder*</tt>: Decoder state
* @param [in] packet <tt>char*</tt>: Opus packet
* @param [in] len <tt>opus_int32</tt>: Length of packet
* @returns Number of samples
* @retval OPUS_BAD_ARG Insufficient data was passed to the function
* @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
*/
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
/** 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.
* @param [in,out] pcm <tt>float*</tt>: Input PCM and modified PCM
* @param [in] frame_size <tt>int</tt> Number of samples per channel to process
* @param [in] channels <tt>int</tt>: Number of channels
* @param [in,out] softclip_mem <tt>float*</tt>: State memory for the soft clipping process (one float per channel, initialized to zero)
*/
OPUS_EXPORT void opus_pcm_soft_clip(float *pcm, int frame_size, int channels, float *softclip_mem);
🎄四、opus 解码流程及源码
opus 解码流程:
1、创建 opus 解码器:opus_decoder_create;
2、配置 opus 解码器:opus_decoder_ctl;
3、取出 opus 包;
4、使用 opus_decode 函数对 opus 包解码;
示例代码:
c复制代码
/**
* @file opusDec.h
* @author wkd_007
* @date 2023-10-27 15:47:38
*/
#ifndef __OPUS_DEC_H__
#define __OPUS_DEC_H__
#include "opus/opus.h"
#define MAX_OPUS_DEC_FRAME 48000 // opus解码最大采样点个数,如果个数时间小于120ms,可能停止解码,这里设置1000ms的个数
#define OPUS_DEC_CHANNELS 2
class COpusDec
{
public:
COpusDec();
~COpusDec();
int CreateOpusDecoder(int sampleRate, int channels);
int OpusDecode(unsigned char* in_data, int in_len, short *out_buf);
private:
OpusDecoder *decoder; // opus 解码器指针
int sample_rate; // 采样率
int channel_num; // 通道数
};
#endif// __OPUS_DEC_H__