09c2d08c
Hu Chunming
arm交付版
|
1
2
3
4
5
6
|
#include "DvppDecoder.h"
#include "DvppSourceManager.h"
struct Vdec_CallBack_UserData {
uint64_t frameId;
|
746db74c
Hu Chunming
实现recode
|
7
|
unsigned long long frame_nb;
|
09c2d08c
Hu Chunming
arm交付版
|
8
9
10
11
12
13
|
long startTime;
long sendTime;
// void* vdecOutputBuf;
DvppDecoder* self;
Vdec_CallBack_UserData() {
frameId = 0;
|
746db74c
Hu Chunming
实现recode
|
14
|
frame_nb = 0;
|
09c2d08c
Hu Chunming
arm交付版
|
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
|
}
};
DvppDecoder::DvppDecoder(){
m_read_thread = 0;
m_decode_thread = 0;
m_cached_mem = nullptr;
fmt_ctx = nullptr;
m_bRunning = false;
stream = nullptr;
video_index = -1;
pix_fmt = AV_PIX_FMT_NONE;
m_dec_name = "";
m_bPause = false;
m_bReal = true;
m_bFinished = false;
m_dec_keyframe = false;
m_fps = 0.0;
m_bSnapShoting = false;
}
DvppDecoder::~DvppDecoder(){
}
bool DvppDecoder::init(FFDecConfig cfg){
m_dec_name = cfg.dec_name;
AVCodecContext* avctx = init_FFmpeg(cfg);
if(avctx == nullptr){
return false;
}
bool bRet = init_vdpp(cfg, avctx);
if(!bRet){
return false;
}
m_cfg = cfg;
decode_finished_cbk = cfg.decode_finished_cbk;
m_bFinished = false;
return true;
}
AVCodecContext* DvppDecoder::init_FFmpeg(FFDecConfig config){
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
avcodec_register_all();
#endif
avformat_network_init();
const char* uri = config.uri.c_str();
fstream infile(uri);
if (infile.is_open()){
m_bReal = false;
infile.close();
} else {
m_bReal = true;
}
// 打开输入视频文件
AVDictionary *options = nullptr;
av_dict_set( &options, "bufsize", "655360", 0 );
av_dict_set( &options, "rtsp_transport", config.force_tcp ? "tcp" : "udp", 0 );
av_dict_set( &options, "stimeout", "30000000", 0 ); // 单位为 百万分之一秒
const char* input_file = uri;
do{
fmt_ctx = avformat_alloc_context();
if (avformat_open_input(&fmt_ctx, input_file, nullptr, &options) != 0) {
LOG_ERROR("[{}]- Cannot open input file: {}", m_dec_name, input_file);
break;
}
av_dump_format(fmt_ctx, 0, input_file, 0);
// 查找流信息
if (avformat_find_stream_info(fmt_ctx, nullptr) < 0) {
LOG_ERROR("[{}]- Cannot find input stream information!", m_dec_name);
break;
}
// 查找视频流信息
AVCodec *decoder = nullptr;
video_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
if (video_index < 0) {
LOG_ERROR("[{}]- Cannot find a video stream in the input file!", m_dec_name);
break;
}
AVCodec *vcodec = avcodec_find_decoder(decoder->id);
avctx = avcodec_alloc_context3(vcodec);
if(avctx == nullptr){
LOG_ERROR("[{}]- alloc AVCodecContext failed!", m_dec_name);
break;
}
// 得到视频流对象
AVStream* stream = fmt_ctx->streams[video_index];
AVCodecParameters *codecpar = stream->codecpar;
if (avcodec_parameters_to_context(avctx, codecpar) < 0)
break;
const AVBitStreamFilter * filter = nullptr;
if(codecpar->codec_id == AV_CODEC_ID_H264){
filter = av_bsf_get_by_name("h264_mp4toannexb");
}else if(codecpar->codec_id == AV_CODEC_ID_HEVC){
filter = av_bsf_get_by_name("hevc_mp4toannexb");
}else {
LOG_ERROR("[{}]- codec_id is not supported!", m_dec_name);
break;
}
int ret = av_bsf_alloc(filter, &h264bsfc);
if (ret < 0){
break;
}
avcodec_parameters_copy(h264bsfc->par_in, codecpar);
av_bsf_init(h264bsfc);
frame_width = codecpar->width;
frame_height = codecpar->height;
pix_fmt = (AVPixelFormat)codecpar->format;
m_fps = av_q2d(stream ->avg_frame_rate);
|
5a84488e
Hu Chunming
添加农村事件开关
|
153
|
#ifdef USE_VILLAGE
|
1b57a1c5
Hu Chunming
代码优化,避免可能的崩溃
|
154
155
156
157
|
bool bRet = m_recoderManager.init(stream, avctx);
if (!bRet){
LOG_ERROR("[{}]- m_recoderManager 初始化失败!", m_dec_name);
}
|
5a84488e
Hu Chunming
添加农村事件开关
|
158
|
#endif
|
746db74c
Hu Chunming
实现recode
|
159
|
|
09c2d08c
Hu Chunming
arm交付版
|
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
|
LOG_INFO("[{}]- init ffmpeg success! input:{} frame_width:{} frame_height:{} fps:{} ", m_dec_name, input_file, frame_width, frame_height, m_fps);
return avctx;
}while(0);
release_ffmpeg();
LOG_ERROR("[{}]- init ffmpeg failed ! input:{} ", m_dec_name, input_file);
return nullptr;
}
bool DvppDecoder::init_vdpp(FFDecConfig cfg, AVCodecContext* avctx) {
LOG_INFO("[{}]- Init device start...", m_dec_name);
m_dvpp_deviceId = atoi(cfg.gpuid.c_str());
if(avctx->codec_id == AV_CODEC_ID_H264){
// 66:Baseline,77:Main,>=100:High
if(avctx->profile == 77){
enType = H264_MAIN_LEVEL;
}else if(avctx->profile < 77){
enType = H264_BASELINE_LEVEL;
}else{
enType = H264_HIGH_LEVEL;
}
}else if(avctx->codec_id == AV_CODEC_ID_HEVC){
// h265只有main
enType = H265_MAIN_LEVEL;
}else {
LOG_ERROR("[{}]- codec_id is not supported!", m_dec_name);
return false;
}
post_decoded_cbk = cfg.post_decoded_cbk;
do{
aclError ret = aclrtSetDevice(m_dvpp_deviceId);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name);
return false;
}
ret = aclrtCreateContext(&m_context, m_dvpp_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_dvpp_deviceId);
if(m_dvpp_channel < 0){
LOG_ERROR("[{}]-该设备channel已经用完了!", m_dec_name);
return false;
}
m_vdec_out_size = avctx->width * avctx->height * 3 / 2;
LOG_INFO("[{}]- init vdpp success! device:{} channel:{}", m_dec_name, m_dvpp_deviceId, m_dvpp_channel);
return true;
}while(0);
release_dvpp();
return false;
}
bool DvppDecoder::isSurport(FFDecConfig& cfg){
return true;
}
bool DvppDecoder::start(){
m_bRunning = true;
pthread_create(&m_read_thread,0,
[](void* arg)
{
DvppDecoder* a=(DvppDecoder*)arg;
a->read_thread();
return (void*)0;
}
,this);
return true;
}
void DvppDecoder::close(){
m_bRunning=false;
if(m_read_thread != 0){
pthread_join(m_read_thread,0);
}
|
09c2d08c
Hu Chunming
arm交付版
|
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
|
}
void DvppDecoder::setPostDecArg(const void* postDecArg){
m_postDecArg = postDecArg;
}
void DvppDecoder::setFinishedDecArg(const void* finishedDecArg){
m_finishedDecArg = finishedDecArg;
}
void DvppDecoder::pause(){
m_bPause = true;
}
void DvppDecoder::resume(){
m_bPause = false;
}
void DvppDecoder::setDecKeyframe(bool bKeyframe){
m_dec_keyframe = bKeyframe;
}
bool DvppDecoder::isRunning(){
return m_bRunning;
}
bool DvppDecoder::isFinished(){
return m_bFinished;
}
bool DvppDecoder::isPausing(){
return m_bPause;
}
bool DvppDecoder::getResolution(int &width, int &height){
width = frame_width;
height = frame_height;
return true;
}
float DvppDecoder::fps(){
return m_fps;
}
DeviceMemory* DvppDecoder::snapshot(){
// 注意内部有锁
// 开始抓拍
m_bSnapShoting = true;
std::unique_lock<std::mutex> locker(m_cached_mutex);
while (m_cached_mem == nullptr)
m_cached_cond.wait_for(locker, std::chrono::seconds(1)); // Unlock mutex and wait to be notified
locker.unlock();
DeviceMemory* mem = m_cached_mem;
m_cached_mem = nullptr;
return mem;
}
int DvppDecoder::getCachedQueueLength(){
return 0;
}
void DvppDecoder::release_ffmpeg() {
m_dec_keyframe = false;
if(h264bsfc){
av_bsf_free(&h264bsfc);
h264bsfc = nullptr;
}
if (fmt_ctx){
avformat_close_input(&fmt_ctx);
fmt_ctx = nullptr;
}
if(avctx){
avcodec_free_context(&avctx);
avctx = nullptr;
}
}
void DvppDecoder::read_thread() {
int frame_count = 0;
int ret = -1;
pthread_create(&m_decode_thread,0,
[](void* arg)
{
DvppDecoder* a=(DvppDecoder*)arg;
a->decode_thread();
return (void*)0;
}
,this);
AVPacket* pkt = nullptr;
|
746db74c
Hu Chunming
实现recode
|
348
|
unsigned long long frame_nb = 0;
|
09c2d08c
Hu Chunming
arm交付版
|
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
|
while (m_bRunning){
if (!m_bReal){
if (m_bPause){
std::this_thread::sleep_for(std::chrono::milliseconds(3));
continue;
}
}
m_pktQueue_mutex.lock();
if(m_pktQueue.size() > 10){
m_pktQueue_mutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
continue;
}
m_pktQueue_mutex.unlock();
pkt = av_packet_alloc();
av_init_packet( pkt );
int result = av_read_frame(fmt_ctx, pkt);
if (result == AVERROR_EOF || result < 0){
av_packet_free(&pkt);
pkt = nullptr;
LOG_ERROR("[{}]- Failed to read frame!", m_dec_name);
break;
}
if (m_dec_keyframe && !(pkt->flags & AV_PKT_FLAG_KEY)) {
av_packet_free(&pkt);
pkt = nullptr;
continue;
}
if (video_index == pkt->stream_index){
|
09c2d08c
Hu Chunming
arm交付版
|
385
386
387
388
389
390
391
392
393
394
395
|
ret = av_bsf_send_packet(h264bsfc, pkt);
if(ret < 0) {
LOG_ERROR("[{}]- av_bsf_send_packet error!", m_dec_name);
av_packet_free(&pkt);
pkt = nullptr;
continue;
}
bool bPushed = false;
while ((ret = av_bsf_receive_packet(h264bsfc, pkt)) == 0) {
if(pkt->size > g_pkt_size){
|
785da442
Hu Chunming
日志优化
|
396
|
LOG_ERROR("[{}]- pkt size 大于最大预设值, 为 {}!", m_dec_name, pkt->size);
|
09c2d08c
Hu Chunming
arm交付版
|
397
398
399
400
401
402
403
|
break;
}
if(!m_bRunning){
break;
}
|
bf661eb0
Hu Chunming
录像文件保存优化
|
404
|
frame_nb++;
|
5a84488e
Hu Chunming
添加农村事件开关
|
405
|
#ifdef USE_VILLAGE
|
bf661eb0
Hu Chunming
录像文件保存优化
|
406
|
m_recoderManager.cache_pkt(pkt, frame_nb);
|
5a84488e
Hu Chunming
添加农村事件开关
|
407
|
#endif
|
bf661eb0
Hu Chunming
录像文件保存优化
|
408
|
|
09c2d08c
Hu Chunming
arm交付版
|
409
|
m_pktQueue_mutex.lock();
|
746db74c
Hu Chunming
实现recode
|
410
411
412
413
|
DataPacket* data_pkt = new DataPacket();
data_pkt->pkt = pkt;
data_pkt->frame_nb = frame_nb;
m_pktQueue.push(data_pkt);
|
09c2d08c
Hu Chunming
arm交付版
|
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
m_pktQueue_mutex.unlock();
bPushed = true;
frame_count++;
}
if(!bPushed){
av_packet_free(&pkt);
pkt = nullptr;
}
} else {
// 音频等其他分量的情形
av_packet_free(&pkt);
pkt = nullptr;
}
}
m_bRunning=false;
if(m_decode_thread != 0){
pthread_join(m_decode_thread,0);
}
m_pktQueue_mutex.lock();
while(m_pktQueue.size() > 0){
|
746db74c
Hu Chunming
实现recode
|
439
440
441
|
DataPacket* data_pkt = m_pktQueue.front();
delete data_pkt;
data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
442
443
444
445
446
447
448
449
|
m_pktQueue.pop();
}
m_pktQueue_mutex.unlock();
if(decode_finished_cbk) {
decode_finished_cbk(m_finishedDecArg);
}
|
e01a0397
Hu Chunming
代码优化;
|
450
451
|
m_recoderManager.close();
|
09c2d08c
Hu Chunming
arm交付版
|
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
LOG_INFO("[{}]- read thread exit.", m_dec_name);
m_bFinished = true;
release_ffmpeg();
}
static void *ReportThd(void *arg)
{
DvppDecoder *self = (DvppDecoder *)arg;
if(nullptr != self){
self->doProcessReport();
}
return (void *)0;
}
void DvppDecoder::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 ;
}
CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(ctx), "aclrtSetCurrentContext failed");
// 阻塞等待vdec线程开始
while (!m_bExitReportThd) {
aclrtProcessReport(1000);
}
ret = aclrtDestroyContext(ctx);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("aclrtDestroyContext failed !");
}
LOG_INFO("doProcessReport exit.");
}
static void VdecCallback(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData)
{
Vdec_CallBack_UserData *userData = (Vdec_CallBack_UserData *) pUserData;
if(nullptr != userData){
DvppDecoder* self = userData->self;
if(self != nullptr){
|
746db74c
Hu Chunming
实现recode
|
504
|
self->doVdppVdecCallBack(input, output, userData->frame_nb);
|
09c2d08c
Hu Chunming
arm交付版
|
505
506
507
508
509
510
|
}
delete userData;
userData = nullptr;
}
}
|
746db74c
Hu Chunming
实现recode
|
511
|
void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output, unsigned long long frame_nb){
|
09c2d08c
Hu Chunming
arm交付版
|
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
m_vdecQueue_mutex.lock();
if(m_vdecQueue.size() > 0){
void* inputData = m_vdecQueue.front();
acldvppFree(inputData);
inputData = nullptr;
m_vdecQueue.pop();
}
m_vdecQueue_mutex.unlock();
CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
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;
}
if(width > 0 && height > 0 && outputSize > 0){
|
746db74c
Hu Chunming
实现recode
|
542
|
DvppDataMemory* mem = new DvppDataMemory(width, width_stride, height, height_stride, outputSize, m_dec_name, to_string(m_dvpp_deviceId), false, frame_nb, (unsigned char *)outputDataDev);
|
09c2d08c
Hu Chunming
arm交付版
|
543
544
545
546
547
548
549
550
551
552
553
554
|
if(mem){
if(post_decoded_cbk) {
post_decoded_cbk(m_postDecArg, mem);
} else {
delete mem;
mem = nullptr;
}
if(m_bSnapShoting){
// 缓存snapshot
std::unique_lock<std::mutex> locker(m_cached_mutex);
|
746db74c
Hu Chunming
实现recode
|
555
|
m_cached_mem = new DvppDataMemory(-1, width, width_stride, height, height_stride, outputSize, m_dec_name, to_string(m_dvpp_deviceId), false, 0);
|
09c2d08c
Hu Chunming
arm交付版
|
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
if(m_cached_mem != nullptr){
aclrtMemcpy(m_cached_mem->getMem(), outputSize, (unsigned char *)outputDataDev, outputSize, ACL_MEMCPY_DEVICE_TO_DEVICE);
}
locker.unlock();
m_cached_cond.notify_one();
m_bSnapShoting = false;
}
} else {
LOG_ERROR("[{}]- DvppDataMemory 创建失败! ", m_dec_name, ret);
acldvppFree(outputDataDev);
outputDataDev = nullptr;
}
} else {
LOG_WARN("[{}]- decode result error, width:{} width_stride:{} height:{} height_stride:{} size:{}", m_dec_name, width, width_stride, height, height_stride, outputSize);
acldvppFree(outputDataDev);
outputDataDev = nullptr;
}
// DvppDataMemory* rgbMem = picConverter.convert2bgr(output, width, height, false);
// if(rgbMem != nullptr){
// #ifdef TEST_DECODER
// // D2H
// if(vdecHostAddr == nullptr){
// CHECK_NOT_RETURN(aclrtMallocHost(&vdecHostAddr, width * height * 3), "aclrtMallocHost failed");
// }
// uint32_t data_size = rgbMem->getSize();
// CHECK_AND_RETURN_NOVALUE(aclrtMemcpy(vdecHostAddr, data_size, rgbMem->getMem(), data_size, ACL_MEMCPY_DEVICE_TO_HOST), "D2H aclrtMemcpy failed");
// // 保存vdec结果
// if(count_frame > 45 && count_frame < 50)
// {
// string file_name = "./yuv_pic/vdec_out_"+ m_dec_name +".rgb" ;
// FILE *outputFile = fopen(file_name.c_str(), "a");
// if(outputFile){
// fwrite(vdecHostAddr, data_size, sizeof(char), outputFile);
// fclose(outputFile);
// }
// }
// else if(count_frame > 50 && vdecHostAddr != nullptr){
// CHECK_NOT_RETURN(aclrtFreeHost(vdecHostAddr), "aclrtFreeHost failed");
// vdecHostAddr = nullptr;
// }
// count_frame++;
// #endif
// post_decoded_cbk(m_postDecArg, rgbMem);
// }else{
// LOG_ERROR("[{}]- convert2bgr failed !", m_dec_name);
// }
}while(0);
CHECK_AND_RETURN_NOVALUE(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
CHECK_AND_RETURN_NOVALUE(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
}
void DvppDecoder::decode_thread(){
long startTime = UtilTools::get_cur_time_ms();
int ret = -1;
m_bExitReportThd = false;
pthread_t report_thread;
ret = pthread_create(&report_thread, nullptr, ReportThd, (void *)this);
if(ret != 0){
LOG_ERROR("[{}]- pthread_create failed", m_dec_name);
return;
}
aclrtSetDevice(m_dvpp_deviceId);
aclrtContext ctx;
ret = aclrtCreateContext(&ctx, m_dvpp_deviceId);
if (ret != ACL_ERROR_NONE) {
// cout << "aclrtCreateContext failed " << endl;
LOG_ERROR("aclrtCreateContext failed !");
return ;
}
// 创建aclvdecChannelDesc类型的数据
aclvdecChannelDesc *vdecChannelDesc = aclvdecCreateChannelDesc();
if (vdecChannelDesc == nullptr) {
LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name);
return;
}
do{
// 创建 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");
uint64_t frame_count = 0;
bool bBreak = false;
while (m_bRunning)
{
if (m_bPause){
std::this_thread::sleep_for(std::chrono::milliseconds(3));
continue;
}
int ret = sentFrame(vdecChannelDesc, frame_count);
if(ret == 2){
bBreak = true;
break;
}else if(ret == 1){
continue;
}
frame_count++;
}
// 尽量保证数据全部解码完成
int sum = 0;
if(!bBreak){
aclrtSetDevice(m_dvpp_deviceId);
aclrtSetCurrentContext(ctx);
while(m_pktQueue.size() > 0){
int ret = sentFrame(vdecChannelDesc, frame_count);
if(ret == 2){
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(3));
sum++;
if(sum > 40){
// 避免卡死
break;
}
}
}
sendVdecEos(vdecChannelDesc);
CHECK_NOT_RETURN(aclvdecDestroyChannel(vdecChannelDesc), "aclvdecDestroyChannel failed");
}while(0);
CHECK_NOT_RETURN(aclvdecDestroyChannelDesc(vdecChannelDesc), "aclvdecDestroyChannelDesc failed");
// report_thread 需后于destroy退出
m_bRunning = false;
m_bExitReportThd = true;
CHECK_NOT_RETURN(pthread_join(report_thread, nullptr), "pthread_join failed");
// 最后清理一遍未解码的数据
m_vdecQueue_mutex.lock();
if(m_vdecQueue.size() > 0){
void* inputData = m_vdecQueue.front();
acldvppFree(inputData);
inputData = nullptr;
m_vdecQueue.pop();
}
m_vdecQueue_mutex.unlock();
release_dvpp();
ret = aclrtDestroyContext(ctx);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("aclrtDestroyContext failed !");
}
LOG_INFO("[{}]- decode thread exit.", m_dec_name);
}
|
746db74c
Hu Chunming
实现recode
|
721
722
723
|
// #include <fstream>
// #include <iostream>
// #include <cstring>
|
09c2d08c
Hu Chunming
arm交付版
|
724
|
|
746db74c
Hu Chunming
实现recode
|
725
726
|
// static int nRecoder = 0;
// std::ofstream outfile;
|
09c2d08c
Hu Chunming
arm交付版
|
727
728
729
730
731
732
733
734
735
736
737
738
|
int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){
// 此处需要判断 m_vdecQueue 队列长度,避免占用过多显存
m_vdecQueue_mutex.lock();
if(m_vdecQueue.size() > 20){
m_vdecQueue_mutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
return 1;
}
m_vdecQueue_mutex.unlock();
|
746db74c
Hu Chunming
实现recode
|
739
|
DataPacket * data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
740
741
742
743
744
745
|
m_pktQueue_mutex.lock();
if(m_pktQueue.size() <= 0){
m_pktQueue_mutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return 1;
}
|
746db74c
Hu Chunming
实现recode
|
746
|
data_pkt = m_pktQueue.front();
|
09c2d08c
Hu Chunming
arm交付版
|
747
748
749
750
751
752
753
754
|
m_pktQueue.pop();
m_pktQueue_mutex.unlock();
// 解码
void *vdecInputbuf = nullptr;
int ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size);
if(ACL_ERROR_NONE != ret){
LOG_ERROR("[{}]- acldvppMalloc failed!, ret:{}", m_dec_name, ret);
|
746db74c
Hu Chunming
实现recode
|
755
756
|
delete data_pkt;
data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
|
return 2;
}
// if(nRecoder == 0){
// outfile.open("pkt.bin", std::ios::binary | std::ios::app);
// if (!outfile) {
// std::cerr << "Failed to open file!" << std::endl;
// return 2;
// }
// }
// outfile.write((const char*)pkt->data, pkt->size);
// nRecoder ++ ;
// if(nRecoder >= 2000){
// outfile.close();
// return 2;
// }
|
746db74c
Hu Chunming
实现recode
|
777
|
AVPacket* pkt = data_pkt->pkt;
|
09c2d08c
Hu Chunming
arm交付版
|
778
779
780
|
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);
|
746db74c
Hu Chunming
实现recode
|
781
782
|
delete data_pkt;
data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
783
784
785
786
787
788
789
|
return 2;
}
void *vdecOutputBuf = nullptr;
ret = acldvppMalloc((void **)&vdecOutputBuf, m_vdec_out_size);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]- acldvppMalloc failed", m_dec_name);
|
746db74c
Hu Chunming
实现recode
|
790
791
|
delete data_pkt;
data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
return 2;
}
acldvppStreamDesc *input_stream_desc = nullptr;
acldvppPicDesc *output_pic_desc = nullptr;
do{
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_count;
|
746db74c
Hu Chunming
实现recode
|
816
|
user_data->frame_nb = data_pkt->frame_nb;
|
09c2d08c
Hu Chunming
arm交付版
|
817
818
819
820
|
// user_data->startTime = startTime;
user_data->sendTime = UtilTools::get_cur_time_ms();
user_data->self = this;
ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data));
|
746db74c
Hu Chunming
实现recode
|
821
822
|
delete data_pkt;
data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
if(ret != ACL_ERROR_NONE){
delete user_data;
user_data = nullptr;
LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);
break;
}
m_vdecQueue_mutex.lock();
m_vdecQueue.push(vdecInputbuf);
m_vdecQueue_mutex.unlock();
return 0;
}while (0);
|
746db74c
Hu Chunming
实现recode
|
837
838
839
|
if(data_pkt != nullptr){
delete data_pkt;
data_pkt = nullptr;
|
09c2d08c
Hu Chunming
arm交付版
|
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
}
// 报错情形
if(input_stream_desc){
CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed");
}
if(output_pic_desc){
CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed");
}
if (vdecOutputBuf){
acldvppFree(vdecOutputBuf);
vdecOutputBuf = nullptr;
}
return 1;
}
bool DvppDecoder::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;
}
void DvppDecoder::release_dvpp(){
if(m_context){
aclError ret = aclrtDestroyContext(m_context);
if(ret != ACL_ERROR_NONE){
LOG_ERROR("[{}]- aclrtDestroyContext failed !", m_dec_name);
}
}
DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
|
746db74c
Hu Chunming
实现recode
|
894
895
896
897
|
}
void DvppDecoder::doRecode(RecoderInfo& recoderInfo) {
m_recoderManager.create_recode_task2(recoderInfo);
|
d9fc3e82
Hu Chunming
recode添加colse功能和mq功能
|
898
899
900
901
|
}
void DvppDecoder::set_mq_callback(mq_callback_t cb) {
m_recoderManager.set_mq_callback(cb);
|
09c2d08c
Hu Chunming
arm交付版
|
902
|
}
|