本篇介绍音频重采样技术,音频重采样其实跟视频转码有异曲同工之妙,可以理解为将源pcm转码为目标pcm供编码器编码使用,因为有时候只需要采样率很低的音频。
技术简介
使用ffmpeg的filter功能实现
使用模块(库)
使用ffmpeg的avfilter-7.dll
主要流程和代码
1、初始化重采样器
int AudioResampler::init(const AUDIO_FILTER_CTX& outCtx, const AUDIO_FILTER_CTX* inCtx0, const AUDIO_FILTER_CTX* inCtx1)
{
int err = ERROR_CODE_OK;
if (m_inited) {
return err;
}
if (inCtx0 == nullptr) {
err = ERROR_CODE_PARAMS_ERROR;
return err;
}
do {
m_filterGraph = avfilter_graph_alloc();
if (m_filterGraph == nullptr) {
err = ERROR_CODE_FILTER_ALLOC_GRAPH_FAILED;
break;
}
m_filterInCtxs = new AUDIO_FILTER_CTX;
m_filterInCtxs[0] = *inCtx0;
m_filterOutCtx = outCtx;
m_filterInCtxs[0].filterInout = avfilter_inout_alloc();
m_filterOutCtx.filterInout = avfilter_inout_alloc();
char filterInArg[512] = {
0 };
sprintf_s(filterInArg, sizeof(filterInArg), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x",
m_filterInCtxs[0].timebase.num, m_filterInCtxs[0].timebase.den, m_filterInCtxs[0].samplerate,
av_get_sample_fmt_name(m_filterInCtxs[0].sampleFmt), av_get_default_channel_layout(m_filterInCtxs[0].channels));
int ret = avfilter_graph_create_filter(&m_filterInCtxs[0].filterCtx, avfilter_get_by_name("abuffer"), "in", filterInArg,