网站做视频在线观看网址,wordpress page模板页,电商网络推广是什么,高校网站建设建议libswresample Audio合成和重采样
libswresample库用来进行audio数据的合成和重采样操作。调用流程#xff1a;
调用 swr_alloc 创建SwrContext结构体。设置SwrContext参数#xff0c;有两种方法#xff1a; 调用av_opt_set_xx函数逐项设置参数#xff1b;swr_alloc_set_…libswresample Audio合成和重采样
libswresample库用来进行audio数据的合成和重采样操作。调用流程
调用 swr_alloc 创建SwrContext结构体。设置SwrContext参数有两种方法 调用av_opt_set_xx函数逐项设置参数swr_alloc_set_opts2同时设置多个参数如果传入参数为nullswr_alloc_set_opts2中会调用swr_alloc创建SwrContext结构体.调用swr_init利用设置的参数初始化SwrContext结构体的内部参数。调用 swr_convert 或 swr_convert_frame 转换audio数据。数据转换完成后调用 swr_free 释放资源。如果需要多次此结构体可以调用 swr_close 清理当前上下文然后重复步骤2.
如果基于AVFrame进行audio数据的合成和重采样操作。调用流程
调用 swr_alloc 创建SwrContext结构体。设置输入AVFrame中的channel_layout, sample_rate和format调用swr_convert_frame进行数据转换。数据转换完成后调用 swr_free 释放资源。如果数据格式发生变化可以swr_config_frame重新设置参数然后重复步骤3.
函数
SwrContext结构体处理函数
struct SwrContext *swr_alloc(void) 创建SwrContext结构体在swr_init调用之前必须要设置转换参数。 int swr_init(struct SwrContext *s) 设置完参数后初始化上下文变量。 int swr_is_initialized(struct SwrContext *s) 检测是否已经初始化0为没有初始化。正数为已经初始化。 int swr_alloc_set_opts2(struct SwrContext **ps, const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx) 使用AVChannelLayout直接创建SwrContext结构体并设置输入、输出audio数据的参数。log_offset为转换时log levellog_ctx为log的上下文。 void swr_free(struct SwrContext **s) 释放SwrContext结构体。 void swr_close(struct SwrContext *s) 清理swr_init时设置的内部参数没有清理用户设置的参数。调用此函数后可以修改参数然后重新调用swr_init。
转换函数
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in , int in_count) 转换audio数据。当in 和 in_count 设置为0时表示输入数据已结束会将剩余的少量数据输出到out中。返回值为每个channel的采样数量。负数表示错误。 int64_t swr_next_pts(struct SwrContext *s, int64_t pts) 获取输入的pts对应的输出pts的值单位1/(in_sample_rate * out_sample_rate) 。swr_set_compensation 是swr_next_pts内部调用的函数不建议在其他地方调用。
底层可选设置函数
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map) 设置一个输入channel的映射数组不需要输出的channel设置为-1. int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout, double center_mix_level, double surround_mix_level, double lfe_mix_level, double maxval, double rematrix_volume, double *matrix, ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context) 创建一个channel的合成矩阵。一般只是内部使用也可用来创建自定义混合矩阵。 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride) 将swr_build_matrix2创建的合成矩阵设置到SwrContext中。
采样处理函数
int swr_drop_output(struct SwrContext *s, int count) 丢弃指定数量的输出数据 int swr_inject_silence(struct SwrContext *s, int count) 注入指定数量的静音输出数据如果需要强制补偿swr_next_pts 调用 swr_drop_output 或 swr_inject_silence 进行数据对齐。 int64_t swr_get_delay(struct SwrContext *s, int64_t base) 获取下一个输入采样数据相对于下一个输出采样数据将经历的延迟。延时单位根据base的值进行计算为 1/base int swr_get_out_samples(struct SwrContext *s, int in_samples) 根据输入的输入采样率in_samples计算输出采样率的上限SwrContext的内部状态不同即使in_samples的值相同也有可能返回不同的值。
配置信息函数
unsigned swresample_version(void) 返回swresample的版本 const char *swresample_configuration(void) 返回编译时的配置信息 const char *swresample_license(void) 返回swresample的授权信息
基于AVFrame的处理函数 int swr_convert_frame(SwrContext *swr,AVFrame *output, const AVFrame *input); 转换输入AVFrame中的数据并将其写入输出AVFrame。输入和输出AVFrames必须有channel_layout, sample_rate和format。如果输出AVFrame没有分配nb_samples的数据指针将使用av_frame_get_buffer()分配数据并设置字段。输出AVFrame可以为NULL或分配的样本比所需的少。在这样的情况下将添加未写入输出的所有剩余数据到内部FIFO缓冲区在下次调用该函数或swr_convert时返回如果转换采样率可能会有数据留在内部重采样延迟缓冲器。调用Swr_get_delay()可以获取剩余的数量。如果需要获取剩余数据请调用此函数或将swr_convert的输入设置为NULL。如果SwrContext配置不匹配输出和输入AVFrame设置不会转换数据并会报错。如果SwrContext没有初始化此函数会利用输入、输出AVFrame中的参数初始化SwrContext并进行数据转换。此函数不会创建SwrContext也不会检测swr的值调用之前必须通过确保swr的值为合法值 int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in); 使用输入的AVFrame重新配置SwrContext的用户参数。此函数调用后必须调用swr_init初始化SwrContext。
结构体
SwrContext定义在swresample_internel.h中属于非公开结构体因此不同版本的定义可能不同。swr_alloc、swr_get_class及SwrContext结构体对应的AVClass在libswresample/options.c中定义此类的option项较多请自行参考此文件。