-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXResample.cpp
More file actions
96 lines (77 loc) · 2.26 KB
/
XResample.cpp
File metadata and controls
96 lines (77 loc) · 2.26 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "XResample.h"
extern "C" {
#include <libswresample/swresample.h>
#include <libavcodec//avcodec.h>
}
using namespace std;
XResample::XResample()
{
}
bool XResample::Open(AVCodecParameters *para, bool isClearPara)
{
if(!para)
return false;
mux.lock();
int ret = -1;
// 音频重采样 上下文初始化
actx = swr_alloc();
unsigned char *pcm = NULL;
ret = swr_alloc_set_opts2(
&actx, // 重采样上下文
¶->ch_layout, // 输出格式
static_cast<AVSampleFormat>(outFormat), // 输出样本格式
para->sample_rate, // 输出采样率
¶->ch_layout, // 输入格式
static_cast<AVSampleFormat>(para->format), // 输入样本格式
para->sample_rate, // 输入采样率
0, 0
);
if(isClearPara)
avcodec_parameters_free(¶);
if(ret != 0) {
mux.unlock();
char buf[1024] = {0};
av_strerror(ret, buf, sizeof(buf) - 1);
cout << "swr_alloc_set_opts2 failed!" << endl;
return false;
}
cout << "swr_alloc_set_opts2 success " << ret << endl;
ret = swr_init(actx);
mux.unlock();
if(ret != 0) {
char buf[1024] = {0};
av_strerror(ret, buf, sizeof(buf) - 1);
cout << "swr_init failed!" << endl;
getchar();
return -1;
}
return true;
}
void XResample::Close()
{
mux.lock();
if(actx)
swr_free(&actx);
mux.unlock();
}
int XResample::Resample(AVFrame *indata, unsigned char *d)
{
if(!indata)
return 0;
if(!d)
{
av_frame_free(&indata);
return 0;
}
uint8_t *data[2] = {0};
data[0] = d;
int ret = swr_convert(actx,
data, indata->nb_samples,
const_cast<const uint8_t**>(indata->data), indata->nb_samples
);
int outSize = ret * indata->ch_layout.nb_channels * av_get_bytes_per_sample(static_cast<AVSampleFormat>(outFormat));
av_frame_free(&indata);
if(ret < 0)
return ret;
return outSize;
}