其实对于网上的libswresample音频重采样的博客以及demo都非常多,我也不想重复写这些东西,网上一搜一大把,但是我在最近项目中出现了重采样后出现杂音的问题,有些细节大家可能没有注意到,本文主要介绍这些大家容易忽略的细节,而很多时候,正是由于这些细节出现了问题
下面主要写一些关键函数,介绍libswresample重采样过程的关键步骤:
初始化
这是第一步,其实也最简单,网上随便搜索都有,随便选用什么方法
SwrContext *swr_ctx = nullptr;
#if LIBSWRESAMPLE_VERSION_MINOR >= 17 // 根据版本不同,选用适当函数
if (nullptr == swr_ctx)
{
swr_ctx = swr_alloc();
}
av_opt_set_int(swr_ctx, "in_channel_layout", src_ch_layout, 0);
av_opt_set_int(swr_ctx, "out_channel_layout", dst_ch_layout, 0);
av_opt_set_int(swr_ctx, "in_sample_rate", src_frame.format.sample_rate, 0);
av_opt_set_int(swr_ctx, "out_sample_rate", dst_frame.format.sample_rate, 0);
av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
#else
swr_ctx = swr_alloc_set_opts(nullptr,
dst_ch_layout,
dst_sample_fmt,
dst_frame.format.sample_rate,
src_ch_layout,
src_sample_fmt,
src_frame.format.sample_rate,
0,
nullptr);
#endif
if (!swr_ctx || swr_init(swr_ctx) < 0)
{
swr_free(&swr_ctx);
return false;
}
分配样本数据内存空间
使用av_samples_alloc_array_and_samples、av_samples_alloc等工具函数。
不同平面数据转换
这是第二步,其实很多demo里并没有,sample有两种类型的存储方式:平面(planar)和打包(packed)格式,所以涉及音频数据的存储,如果不确定源是什么格式,最好加上这个函数,对数据进行转换之后再重采样
static void setup_array(uint8_t* out[SWR_CH_MAX], AVFrame* in_frame, int format, int samples)
{
if (av_sample_fmt_is_planar((AVSampleFormat)format))
{
int i;
int plane_size = av_get_bytes_per_sample((AVSampleFormat)(format & 0xFF)) * samples;
format &= 0xFF;
//从decoder出来的frame中的data数据不是连续分布的,所以不能这样写:in_frame->data[0]+i*plane_size;
for (i = 0; i < in_frame->channels; i++)
{
out[i] = in_frame->data[i];
}
}
else
{
out[0] = in_frame->data[0];
}
}
重采样转换
这一步是最关键的,虽然大家都知道核心函数是这个swr_convert,但是我重采样后音频出现杂音也是在这一步,先贴代码:
int nb = swr_convert(swr_ctx, temp_data, dst_nb_samples, &src_data_ptr, src_nb_samples);
if (nb < 0)
{
break;
}
int out_buffer_size = av_samples_get_buffer_size(NULL, dst_frame.format.channels, nb, dst_sample_fmt, 1);
dst_frame.data.resize(temp_size);
memcpy(dst_frame.data.data(), *temp_data, out_buffer_size);
int nb1 = 0, total_offset = out_buffer_size;
while ((nb1 = swr_convert(swr_ctx, temp_data, dst_nb_samples, NULL, 0)) > 0)
{
int out_buffer_size1 = av_samples_get_buffer_size(NULL, dst_frame.format.channels, nb1, dst_sample_fmt, 1);
memcpy(dst_frame.data.data() + total_offset, *temp_data, out_buffer_size1);
total_offset += out_buffer_size1;
}
从代码中可以看出我调用了两次swr_convert,其实正常情况下一般调用一次就可以了,但是有时比如我在项目中就出现了有数据存在缓冲区中,第一次调用之后并没有返回所有的音频数据,所以转换后的音频数据是缺失了的,所以我重采样过后出现了杂音,后面看swr_convert API的注释发现有这么一句:
in and in_count can be set to 0 to flush the last few samples out at the
end.
所以又调用了一次swr_convert函数来刷新下音频数据,这下音频数据就正常了,再也没有杂音。
释放资源:
通过swr_free()释放swr_ctx以及 av_freep来释放。
if (temp_data != nullptr) av_freep(&temp_data[0]);
if (swr_ctx != nullptr) swr_free(&swr_ctx);