-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXResample.cpp
More file actions
76 lines (69 loc) · 1.63 KB
/
Copy pathXResample.cpp
File metadata and controls
76 lines (69 loc) · 1.63 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
#include "XResample.h"
#include "AVFrameRAII.h"
extern "C" {
#include <libswresample/swresample.h>
#include <libavcodec/avcodec.h>
}
#pragma comment(lib,"swresample.lib")
#include <iostream>
using namespace std;
void XResample::Close()
{
lock_guard<mutex> lck(mux);
if (actx)
swr_free(&actx);
}
//输出参数和输入参数一致除了采样格式,输出为S16
bool XResample::Open(AVCodecParameters* para, bool isClearPara)
{
if (!para)return false;
mux.lock();
//音频重采样 上下文初始化
//如果actx为NULL会分配空间
actx = swr_alloc_set_opts(actx,
av_get_default_channel_layout(2), //输出格式
(AVSampleFormat)outFormat, //输出样本格式 1 AV_SAMPLE_FMT_S16
para->sample_rate, //输出采样率
av_get_default_channel_layout(para->channels),//输入格式
(AVSampleFormat)para->format,
para->sample_rate,
0, 0
);
if (isClearPara)
avcodec_parameters_free(¶);
int re = swr_init(actx);
mux.unlock();
if (re != 0)
{
char buf[1024] = { 0 };
av_strerror(re, buf, sizeof(buf) - 1);
cout << "swr_init failed! :" << buf << endl;
return false;
}
return true;
}
//返回重采样后大小,不管成功与否都释放indata空间
int XResample::Resample(shared_ptr<AVFrameRAII> indata, unsigned char* d)
{
// 容错
if (!indata) return 0;
if (!d)
{
return 0;
}
uint8_t* data[2] = { 0 };
data[0] = d;
int re = swr_convert(actx,
data, indata->get_frame()->nb_samples, //输出
(const uint8_t**)indata->get_frame()->data, indata->get_frame()->nb_samples //输入
);
if (re <= 0)return re;
int outSize = re * indata->get_frame()->channels * av_get_bytes_per_sample((AVSampleFormat)outFormat);
return outSize;
}
XResample::XResample()
{
}
XResample::~XResample()
{
}