VpcUtils.cpp
8.68 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "VpcUtils.h"
#include "depend_headers.h"
#define ALIGN_UP(val, align) (((val) % (align) == 0) ? (val) : (((val) / (align) + 1) * (align)))
#define CHECK_AND_RETURN(ret, message) \
if(ret != 0) {LOG_ERROR("{}", message); return ret;}
#define CHECK_NOT_RETURN(ret, message) \
if(ret != 0) {LOG_ERROR("{}", message);}
#define CHECK_AND_RETURN_NOVALUE(ret, message) \
if(ret != 0) {LOG_ERROR("{}", message); return;}
#define CHECK_AND_BREAK(ret, message) \
if(ret != 0) {LOG_ERROR("{}", message); break;}
VpcUtils::VpcUtils(){
}
VpcUtils::~VpcUtils(){
release();
}
int VpcUtils::init(int devId){
m_devId = devId;
aclError ret = aclrtCreateContext(&context_, m_devId);
if (ret != ACL_ERROR_NONE) {
LOG_ERROR("[{}]-aclrtCreateContext failed !", m_dec_name);
return false;
}
do
{
dvppChannelDesc_ = acldvppCreateChannelDesc();
ret = acldvppCreateChannel(dvppChannelDesc_);
CHECK_AND_BREAK(ret, "acldvppCreateChannel failed !");
ret = acldvppSetChannelDescMode(dvppChannelDesc_, DVPP_CHNMODE_VPC);
CHECK_AND_BREAK(ret, "acldvppSetChannelDescMode failed !");
} while (0);
return ret;
}
void VpcUtils::release() {
if(context_){
aclrtSetCurrentContext(context_);
if (dvppChannelDesc_) {
(void)acldvppDestroyChannel(dvppChannelDesc_);
(void)acldvppDestroyChannelDesc(dvppChannelDesc_);
dvppChannelDesc_ = nullptr;
}
aclrtDestroyContext(context_);
}
}
DvppDataMemory* VpcUtils::convert2bgr(acldvppPicDesc *inputDesc_, int out_width, int out_height, bool key_frame){
aclrtSetCurrentContext(context_);
int out_buf_width = ALIGN_UP(out_width, 16) * 3;
int out_buf_height = ALIGN_UP(out_height, 2);
int out_buf_size = out_buf_width * out_buf_height;
DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, "", to_string(m_devId), key_frame, 0);
void *outBufferDev_ = (void*)rgbMem->getMem();
acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
acldvppSetPicDescData(outputDesc_, outBufferDev_);
acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888);
acldvppSetPicDescWidth(outputDesc_, out_width);
acldvppSetPicDescHeight(outputDesc_, out_height);
acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
acldvppSetPicDescSize(outputDesc_, out_buf_size);
aclError ret = ACL_ERROR_NONE;
aclrtStream stream_{nullptr};
aclrtCreateStream(&stream_);
do{
// 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size);
break;
}
ret = aclrtSynchronizeStream(stream_);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size);
break;
}
}while(0);
if(nullptr != stream_){
aclrtDestroyStream(stream_);
stream_ = nullptr;
}
acldvppDestroyPicDesc(outputDesc_);
if(ret != ACL_ERROR_NONE){
delete rgbMem;
rgbMem = nullptr;
}
return rgbMem;
}
DvppDataMemory* VpcUtils::convert2bgr(DvppDataMemory* inMem){
aclrtSetCurrentContext(context_);
int out_width = inMem->getWidth();
int out_height = inMem->getHeight();
acldvppPicDesc *inputDesc_= acldvppCreatePicDesc();
acldvppSetPicDescData(inputDesc_, inMem->getMem());
acldvppSetPicDescFormat(inputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(inputDesc_, out_width);
acldvppSetPicDescHeight(inputDesc_, out_height);
acldvppSetPicDescWidthStride(inputDesc_, inMem->getWidthStride());
acldvppSetPicDescHeightStride(inputDesc_, inMem->getHeightStride());
acldvppSetPicDescSize(inputDesc_, inMem->getSize());
int out_buf_width = ALIGN_UP(out_width, 16) * 3;
int out_buf_height = ALIGN_UP(out_height, 2);
int out_buf_size = out_buf_width * out_buf_height;
DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, inMem->getId(), inMem->getDeviceId(), false, inMem->getFrameNb());
void *outBufferDev_ = (void*)rgbMem->getMem();
acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
acldvppSetPicDescData(outputDesc_, outBufferDev_);
acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888);
acldvppSetPicDescWidth(outputDesc_, out_width);
acldvppSetPicDescHeight(outputDesc_, out_height);
acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
acldvppSetPicDescSize(outputDesc_, out_buf_size);
aclError ret = ACL_ERROR_NONE;
aclrtStream stream_{nullptr};
aclrtCreateStream(&stream_);
do{
// 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size);
break;
}
ret = aclrtSynchronizeStream(stream_);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size);
break;
}
}while(0);
if(nullptr != stream_){
aclrtDestroyStream(stream_);
stream_ = nullptr;
}
acldvppDestroyPicDesc(outputDesc_);
acldvppDestroyPicDesc(inputDesc_);
if(ret != ACL_ERROR_NONE){
delete rgbMem;
rgbMem = nullptr;
}
return rgbMem;
}
DvppDataMemory* VpcUtils::resize(acldvppPicDesc *inputDesc_, int out_width, int out_height){
aclrtSetCurrentContext(context_);
int out_buf_width = ALIGN_UP(out_width, 16);
int out_buf_height = ALIGN_UP(out_height, 2);
int out_buf_size = out_buf_width * out_buf_height * 3 / 2;
DvppDataMemory* rgbMem = new DvppDataMemory(1, out_width, out_buf_width, out_height, out_buf_height, out_buf_size, "", "", false, 0);
void *outBufferDev_ = (void*)rgbMem->getMem();
acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
acldvppSetPicDescData(outputDesc_, outBufferDev_);
acldvppSetPicDescFormat(outputDesc_, acldvppGetPicDescFormat(inputDesc_));
acldvppSetPicDescWidth(outputDesc_, out_width);
acldvppSetPicDescHeight(outputDesc_, out_height);
acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
acldvppSetPicDescSize(outputDesc_, out_buf_size);
acldvppResizeConfig *resizeConfig_ = acldvppCreateResizeConfig();
aclError ret = ACL_ERROR_NONE;
aclrtStream stream_{nullptr};
aclrtCreateStream(&stream_);
do{
// 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc_, outputDesc_, resizeConfig_, stream_);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("acldvppVpcResizeAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size);
break;
}
ret = aclrtSynchronizeStream(stream_);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size);
break;
}
}while(0);
if(nullptr != stream_){
aclrtDestroyStream(stream_);
stream_ = nullptr;
}
acldvppDestroyResizeConfig(resizeConfig_);
acldvppDestroyPicDesc(outputDesc_);
if(ret != ACL_ERROR_NONE){
delete rgbMem;
rgbMem = nullptr;
}
return rgbMem;
}