一、重采样参数
1. sample rate(采样率):采样设备每秒抽取样本的次数
2. sample format(采样格式)和量化精度:每种⾳频格式有不同的量化精度(位宽),位数越多,表示值就越精确,声⾳表现⾃然就越精准。
3. channel layout(通道布局,也就是声道数):这个就是采样的声道数
通过swr_alloc_set_opts设置
SwrContext *swr = swr_alloc_set_opts(NULL, // we're allocating a new context
AV_CH_LAYOUT_STEREO, // out_ch_layout
AV_SAMPLE_FMT_S16, // out_sample_fmt
44100, // out_sample_rate
AV_CH_LAYOUT_5POINT1, // in_ch_layout
AV_SAMPLE_FMT_FLTP, // in_sample_fmt
48000, // in_sample_rate
0, // log_offset
NULL); // log_ctx
二、基本用法
申请结构体—>设置相关参数—>初始化—>转换—->释放结构体
2.1 申请结构体
struct SwrContext* au_convert_ctx;
au_convert_ctx = swr_alloc();
2.2 设置相关参数
AVChannelLayout outChannelLayout = AV_CHANNEL_LAYOUT_STEREO;
AVChannelLayout inChannelLayout = pCodecCtx->ch_layout;
outChannelLayout.nb_channels = 2;
inChannelLayout.nb_channels = pCodecCtx->ch_layout.nb_channels;
swr_alloc_set_opts2(&au_convert_ctx, &outChannelLayout, out_sample_fmt, out_sample_rate,
&inChannelLayout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL);
需要注意AV_CHANNEL_LAYOUT_STEREO是一个宏定义,在头文件channel_layout.h中定义。
2.3 初始化
swr_init(au_convert_ctx);
2.4 转换
解码完成后调用swr_convert重采样
swr_convert()函数的定义如下:
* @param out 输出缓冲区,当PCM数据为Packed包装格式时,只有out[0]会填充有数据。
* @param out_count 每个通道可存储输出PCM数据的sample数量。
* @param in 输入缓冲区,当PCM数据为Packed包装格式时,只有in[0]需要填充有数据。
* @param in_count 输入PCM数据中每个通道可用的sample数量。
*
* @return 返回每个通道输出的sample数量,发生错误的时候返回负数。
*/
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
const uint8_t **in , int in_count);
2.5 释放结构体
swr_free(&au_convert_ctx);