ffmpeg 音频重采样

一、重采样参数
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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

都市无名者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值