forked from c12121234/MyFFmpegPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpegresample.cpp
More file actions
78 lines (70 loc) · 1.95 KB
/
ffmpegresample.cpp
File metadata and controls
78 lines (70 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "ffmpegresample.h"
extern "C"
{
#include "libswresample/swresample.h"
#include "libavcodec/avcodec.h"
}
#include <QDebug>
FFmpegResample::FFmpegResample():
m_pSwrctx(nullptr)
, m_nOutFormat(1)
{
}
FFmpegResample::~FFmpegResample()
{
}
bool FFmpegResample::Open(AVCodecParameters *pPara, bool bClear)
{
if(!pPara)
return false;
m_Mutex.lock();
//如果m_pSwrctx為null,會自動分配空間
m_pSwrctx = swr_alloc_set_opts(m_pSwrctx,
av_get_default_channel_layout(2), //輸出格式
(AVSampleFormat)m_nOutFormat, //輸出樣本格式
pPara->sample_rate, //輸出採樣率
av_get_default_channel_layout(pPara->channels), //輸入格式
(AVSampleFormat)pPara->format,
pPara->sample_rate,
0,0);
if(bClear)
avcodec_parameters_free(&pPara);
int nRet = swr_init(m_pSwrctx);
m_Mutex.unlock();
if(nRet!=0)
{
char buff[1024] = {0};
av_strerror(nRet,buff,sizeof(buff)-1);
qInfo()<<"swr_init failed! :"<<buff;
return false;
}
unsigned char* pPCM = nullptr;
return true;
}
void FFmpegResample::Close()
{
m_Mutex.lock();
if(m_pSwrctx)
swr_free(&m_pSwrctx);
m_Mutex.unlock();
}
int FFmpegResample::Resample(AVFrame *pInputData, unsigned char *pOutputData)
{
if(!pInputData)
return 0;
if(!pOutputData)
{
av_frame_free(&pInputData);
return 0;
}
uint8_t* data[2] = {0};
data[0] = pOutputData;
int nRet = swr_convert(m_pSwrctx,
data,pInputData->nb_samples,
(const uint8_t**)pInputData->data,pInputData->nb_samples);
int nOutSize = nRet * pInputData->channels * av_get_bytes_per_sample((AVSampleFormat)m_nOutFormat);
av_frame_free(&pInputData);
if(nRet<=0)
return nRet;
return nOutSize;
}