DvppStreamDecoder.cpp
13.7 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "DvppStreamDecoder.h"
struct Vdec_CallBack_UserData {
uint64_t frameId;
uint64_t frame_nb;
long startTime;
long sendTime;
DvppDecoder* self;
Vdec_CallBack_UserData() {
frameId = 0;
frame_nb = 0;
}
};
static void *ReportThd(void *arg)
{
DvppStreamDecoder *self = (DvppStreamDecoder *)arg;
if(nullptr != self){
self->doProcessReport();
}
return (void *)0;
}
static void VdecCallback(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData)
{
Vdec_CallBack_UserData *userData = (Vdec_CallBack_UserData *) pUserData;
if(nullptr != userData){
DvppStreamDecoder* self = userData->self;
if(self != nullptr){
self->doVdppVdecCallBack(input, output, userData);
}
delete userData;
userData = nullptr;
}
}
DvppStreamDecoder::DvppStreamDecoder(/* args */)
{
}
DvppStreamDecoder::~DvppStreamDecoder()
{
}
bool DvppStreamDecoder::init_vdpp(FFDecConfig cfg) {
LOG_INFO("[{}]- Init device start...", m_dec_name);
m_deviceId = atoi(cfg.gpuid.c_str());
do{
aclError ret = aclrtSetDevice(m_deviceId);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name);
return false;
}
ret = aclrtCreateContext(&m_context, m_deviceId);
if (ret != ACL_ERROR_NONE) {
LOG_ERROR("[{}]-aclrtCreateContext failed !", m_dec_name);
return false;
}
// DvppSourceManager 创建时包含 aclInit,析构时包含 aclFinalize
DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
m_dvpp_channel = pSrcMgr->getChannel(m_deviceId);
if(m_dvpp_channel < 0){
LOG_ERROR("[{}]-该设备channel已经用完了!", m_dec_name);
return false;
}
m_vpcUtils.init(m_deviceId);
LOG_INFO("[{}]- init vdpp success! device:{} channel:{}", m_dec_name, m_deviceId, m_dvpp_channel);
return true;
}while(0);
release_dvpp();
return false;
}
void DvppStreamDecoder::release_dvpp(){
if(m_context){
aclError ret = aclrtDestroyContext(m_context);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]- aclrtDestroyContext failed !", m_dec_name);
}
m_context = nullptr;
}
if(m_dvpp_channel >= 0){
DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
pSrcMgr->releaseChannel(m_deviceId, m_dvpp_channel);
m_dvpp_channel = -1;
}
}
int DvppStreamDecoder::getVdecType(int videoType)
{
int streamFormat = H264_MAIN_LEVEL;
// VDEC only support H265 main level,264 baseline level,main level,high level
if (videoType == 1) {
streamFormat = H265_MAIN_LEVEL;
} else if (videoType == 0) {
streamFormat = H264_MAIN_LEVEL;
} else {
streamFormat = -1;
LOG_ERROR("Not support stream, type {}", videoType);
}
return streamFormat;
}
void DvppStreamDecoder::doProcessReport(){
aclError ret = aclrtSetDevice(m_dvpp_deviceId);
if(ret != ACL_ERROR_NONE){
// cout << "aclrtSetDevice failed" << endl;
LOG_ERROR("aclrtSetDevice failed !");
return ;
}
aclrtContext ctx;
ret = aclrtCreateContext(&ctx, m_dvpp_deviceId);
if (ret != ACL_ERROR_NONE) {
// cout << "aclrtCreateContext failed " << endl;
LOG_ERROR("aclrtCreateContext failed !");
return ;
}
while (!m_bExitReportThd) {
aclrtProcessReport(1000);
}
ret = aclrtDestroyContext(ctx);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("aclrtDestroyContext failed !");
}
LOG_INFO("doProcessReport exit.");
}
void DvppStreamDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData) {
// 内部缓存计数减1
m_DvppCacheCounter--;
if(nullptr == pUserData){
return;
}
Vdec_CallBack_UserData *userData = (Vdec_CallBack_UserData *) pUserData;
uint64_t frame_nb = userData->frame_nb;
m_out_count++;
CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
void *inputDataDev = acldvppGetStreamDescData(input);
acldvppFree(inputDataDev);
inputDataDev = nullptr;
void *outputDataDev = acldvppGetPicDescData(output);
uint32_t outputSize = acldvppGetPicDescSize(output);
uint32_t width = acldvppGetPicDescWidth(output);
uint32_t width_stride = acldvppGetPicDescWidthStride(output);
uint32_t height = acldvppGetPicDescHeight(output);
uint32_t height_stride = acldvppGetPicDescHeightStride(output);
do{
int ret = acldvppGetPicDescRetCode(output);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]- decode result error, retCode:{} ", m_dec_name, ret);
acldvppFree(outputDataDev);
outputDataDev = nullptr;
break;
}
bool bCached = false;
if(width > 0 && height > 0 && outputSize > 0){
if (!m_bReal) {
while(!m_bExitReportThd) {
// 非实时流,即为文件情形,因为不存在花屏问题,为保证不丢帧,这里做个循环等待
m_decoded_data_queue_mtx.lock();
if(m_decoded_data_queue.size() >= 5){
m_decoded_data_queue_mtx.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
continue;
}
m_decoded_data_queue_mtx.unlock();
break;
}
}
// cout << m_dec_name << " 解码时间间隔: " << get_cur_time_ms() - last_ts << endl;
// last_ts = get_cur_time_ms();
// 换成解码后数据, 这里这样做的是为了保证解码一直持续进行,避免后续操作阻碍文件读取和解码从而导致花屏
DvppDataMemory* mem = nullptr;
if (m_bResize && (width > 1920 || height > 1080)) {
float srcRatio = width / (float)height;
float stdRatio = 1920.0 / 1080.0f ;
int outWidth = 1920;
int outHeight = 1080;
if (srcRatio > stdRatio) {
outHeight = static_cast<int>(outWidth * (float)height / width) ;
if (outHeight % 2 == 1)
{
outHeight += 1;
}
} else if (srcRatio < stdRatio) {
outWidth = static_cast<int>(outHeight * (float)width / height) ;
if (outWidth % 2 == 1)
{
outWidth += 1;
}
}
mem = m_vpcUtils.resize(output, outWidth, outHeight);
if (mem) {
acldvppFree(outputDataDev);
outputDataDev = nullptr;
mem->setDeviceId(to_string(m_dvpp_deviceId));
mem->setId(m_dec_name);
mem->setFrameNb(frame_nb);
}
} else {
mem = new DvppDataMemory(width, width_stride, height, height_stride, outputSize, m_dec_name, to_string(m_dvpp_deviceId), false, frame_nb, (unsigned char *)outputDataDev);
}
if(mem){
m_decoded_data_queue.push(mem);
bCached = true;
}
}
if(!bCached) {
LOG_WARN("[{}]- decode result warning, width:{} width_stride:{} height:{} height_stride:{} size:{}", m_dec_name, width, width_stride, height, height_stride, outputSize);
acldvppFree(outputDataDev);
outputDataDev = nullptr;
}
}while(0);
CHECK_AND_RETURN_NOVALUE(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
CHECK_AND_RETURN_NOVALUE(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
}
bool DvppStreamDecoder::sendVdecEos(aclvdecChannelDesc *vdecChannelDesc) {
// create stream desc
acldvppStreamDesc *streamInputDesc = acldvppCreateStreamDesc();
if (streamInputDesc == nullptr) {
LOG_ERROR("[{}]- fail to create input stream desc", m_dec_name);
return false;
}
aclError ret = acldvppSetStreamDescEos(streamInputDesc, 1);
if (ret != ACL_SUCCESS) {
LOG_ERROR("[{}]- fail to set eos for stream desc, errorCode = {}", m_dec_name, static_cast<int32_t>(ret));
(void)acldvppDestroyStreamDesc(streamInputDesc);
return false;
}
// send vdec eos frame. when all vdec callback are completed, aclvdecSendFrame can be returned.
LOG_INFO("[{}]- send eos", m_dec_name);
ret = aclvdecSendFrame(vdecChannelDesc, streamInputDesc, nullptr, nullptr, nullptr);
(void)acldvppDestroyStreamDesc(streamInputDesc);
if (ret != ACL_SUCCESS) {
LOG_ERROR("[{}]- fail to send eos frame, ret={}", m_dec_name, ret);
return false;
}
return true;
}
int DvppStreamDecoder::sendPkt(aclvdecChannelDesc *vdecChannelDesc, AVPacket* pkt, unsigned long long frame_nb) {
void *vdecInputbuf = nullptr;
void *vdecOutputBuf = nullptr;
acldvppStreamDesc *input_stream_desc = nullptr;
acldvppPicDesc *output_pic_desc = nullptr;
do{
int ret = acldvppMalloc((void **)&vdecInputbuf, pkt->size);
if(ACL_ERROR_NONE != ret){
LOG_ERROR("[{}]- acldvppMalloc failed!, ret:{}", m_dec_name, ret);
break;
}
ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE);
if(ACL_ERROR_NONE != ret){
LOG_ERROR("[{}]- aclrtMemcpy failed", m_dec_name);
break;
}
ret = acldvppMalloc((void **)&vdecOutputBuf, m_vdec_out_size);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]- acldvppMalloc failed", m_dec_name);
break;
}
input_stream_desc = acldvppCreateStreamDesc();
if (input_stream_desc == nullptr) {
LOG_ERROR("[{}]- acldvppCreateStreamDesc failed", m_dec_name);
break;
}
output_pic_desc = acldvppCreatePicDesc();
if (output_pic_desc == nullptr) {
LOG_ERROR("[{}]- acldvppCreatePicDesc failed", m_dec_name);
break;
}
CHECK_AND_BREAK(acldvppSetStreamDescData(input_stream_desc, vdecInputbuf), "acldvppSetStreamDescData failed");
CHECK_AND_BREAK(acldvppSetStreamDescSize(input_stream_desc, pkt->size), "acldvppSetStreamDescSize failed");
CHECK_AND_BREAK(acldvppSetPicDescData(output_pic_desc, vdecOutputBuf), "acldvppSetPicDescData failed");
CHECK_AND_BREAK(acldvppSetPicDescSize(output_pic_desc, m_vdec_out_size), "acldvppSetPicDescSize failed");
Vdec_CallBack_UserData *user_data = NULL;
user_data = new Vdec_CallBack_UserData;
user_data->frameId = frame_nb;
user_data->frame_nb = frame_nb;
// user_data->startTime = startTime;
user_data->sendTime = UtilTools::get_cur_time_ms();
user_data->self = this;
m_in_count++;
// 内部缓存计数加1
m_DvppCacheCounter++;
ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data));
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);
delete user_data;
user_data = nullptr;
return -2;
}
return 0;
}while (0);
if (vdecInputbuf){
acldvppFree(vdecInputbuf);
vdecInputbuf = nullptr;
}
// 报错情形
if(input_stream_desc){
CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed");
}
if (vdecOutputBuf){
acldvppFree(vdecOutputBuf);
vdecOutputBuf = nullptr;
}
if(output_pic_desc){
CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed");
}
return -1;
}
int DvppStreamDecoder::Decode(int videoType, char* data, int len, int isKey, uint64_t pts) {
if (vdecChannelDesc == nullptr) {
vdecChannelDesc = aclvdecCreateChannelDesc();
if (vdecChannelDesc == nullptr) {
LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name);
return;
}
pthread_t report_thread;
int ret = pthread_create(&report_thread, nullptr, ReportThd, (void *)this);
if(ret != 0){
LOG_ERROR("[{}]- pthread_create failed", m_dec_name);
return;
}
acldvppStreamFormat enType = getVdecType(videoType);
// 创建 channel dec结构体
// 通道ID在dvpp层面为0~31
CHECK_AND_BREAK(aclvdecSetChannelDescChannelId(vdecChannelDesc, m_dvpp_channel), "aclvdecSetChannelDescChannelId failed");
CHECK_AND_BREAK(aclvdecSetChannelDescThreadId(vdecChannelDesc, report_thread), "aclvdecSetChannelDescThreadId failed");
CHECK_AND_BREAK(aclvdecSetChannelDescCallback(vdecChannelDesc, VdecCallback), "aclvdecSetChannelDescCallback failed");
CHECK_AND_BREAK(aclvdecSetChannelDescEnType(vdecChannelDesc, enType), "aclvdecSetChannelDescEnType failed");
CHECK_AND_BREAK(aclvdecSetChannelDescOutPicFormat(vdecChannelDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420), "aclvdecSetChannelDescOutPicFormat failed");
CHECK_AND_BREAK(aclvdecCreateChannel(vdecChannelDesc), "aclvdecCreateChannel failed");
}
if (vdecChannelDesc)
{
m_frame_nb++;
AVPacket* pkt = av_packet_alloc();
av_init_packet(pkt);
pkt->size = len;
pkt->data = (uint8_t*)data;
// dvpp 解码
int nSended = sendPkt(vdecChannelDesc, pkt, m_frame_nb);
av_packet_free(&pkt);
pkt = nullptr;
}
}