1 Star 1 Fork 1

Devin / XPlay3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
XResample.cpp 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
Devin 提交于 2021-10-23 13:08 . 提交工程
#include "XResample.h"
XResample::XResample()
{
}
XResample::~XResample()
{
}
bool XResample::Open(AVCodecParameters *_pavcodeParam,bool _blIsClearPara)
{
if(_pavcodeParam == nullptr)
{
qDebug("_pavcodeParam == nullptr");
return false;
}
m_qmutResample.lock();
//音频重采样上下文分配空间
m_swrContext = swr_alloc_set_opts(m_swrContext,
av_get_default_channel_layout(2), //输出格式
(AVSampleFormat)m_iOutFromat, //输出样本格式 AV_SAMPLE_FMT_S16
_pavcodeParam->sample_rate, //输出采样率
av_get_default_channel_layout(_pavcodeParam->channels),
(AVSampleFormat)_pavcodeParam->format,
_pavcodeParam->sample_rate,
0,0
);
//判断是否需要释放param空间
if(_blIsClearPara)
{
avcodec_parameters_free(&_pavcodeParam);
}
//重采样初始化
int iResult = swr_init(m_swrContext);
if(iResult != 0)
{
m_qmutResample.unlock();
char buf[1024] = {0};
av_strerror(iResult, buf, sizeof(buf) - 1);
qDebug()<< "swr_init error:"<<buf;
return false;
}
m_qmutResample.unlock();
return true;
}
void XResample::Close()
{
m_qmutResample.lock();
if(m_swrContext)
{
swr_free(&m_swrContext);
}
m_qmutResample.unlock();
}
int XResample::Resample(AVFrame *_pavFrame, unsigned char *_pcOutData)
{
if(_pavFrame == nullptr)
{
return 0;
}
if(_pcOutData == nullptr)
{
av_frame_free(&_pavFrame);
return 0;
}
m_qmutResample.lock();
uint8_t *pData[2] = {0};
pData[0] = (uint8_t *)_pcOutData;
int iResult = swr_convert(m_swrContext,
pData, //输出
_pavFrame->nb_samples,
(const uint8_t **)_pavFrame->data, //输入
_pavFrame->nb_samples);
if(iResult <= 0)
{
m_qmutResample.unlock();
char buf[1024] = {0};
av_strerror(iResult, buf, sizeof(buf) - 1);
qDebug()<< "swr_init error:"<<buf;
return 0;
}
int iOutSize = iResult * _pavFrame->channels * av_get_bytes_per_sample((AVSampleFormat)m_iOutFromat);
m_qmutResample.unlock();
return iOutSize;
}
1
https://gitee.com/yixinglong/xplay3.git
git@gitee.com:yixinglong/xplay3.git
yixinglong
xplay3
XPlay3
master

搜索帮助