Blame view

src/decoder/dvpp/DvppDecoder.cpp 25.8 KB
09c2d08c   Hu Chunming   arm交付版
1
2
3
4
  #include "DvppDecoder.h"
  #include "DvppSourceManager.h"
  
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
5
6
7
8
9
10
11
12
13
14
15
  #define CHECK_AND_RETURN(ret, message)    \
              if(ret != 0) {LOG_ERROR("[{}]- {}", m_dec_name, message); return ret;}
  #define CHECK_NOT_RETURN(ret, message)    \
              if(ret != 0) {LOG_ERROR("[{}]- {}", m_dec_name, message);}
  #define CHECK_AND_RETURN_NOVALUE(ret, message)    \
              if(ret != 0) {LOG_ERROR("[{}]- {}", m_dec_name, message); return;}
  #define CHECK_AND_BREAK(ret, message)    \
              if(ret != 0) {LOG_ERROR("[{}]- {}", m_dec_name, message); break;}
  
  
  
09c2d08c   Hu Chunming   arm交付版
16
17
  struct Vdec_CallBack_UserData {
      uint64_t frameId;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
18
      uint64_t frame_nb;
09c2d08c   Hu Chunming   arm交付版
19
20
      long startTime;
      long sendTime;
09c2d08c   Hu Chunming   arm交付版
21
  	DvppDecoder* self;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
22
  
09c2d08c   Hu Chunming   arm交付版
23
24
      Vdec_CallBack_UserData() {
          frameId = 0;
746db74c   Hu Chunming   实现recode
25
          frame_nb = 0;
09c2d08c   Hu Chunming   arm交付版
26
27
28
      }
  };
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
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
  
  static long get_cur_time_ms() {
      chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
          = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
      return tpMicro.time_since_epoch().count();
  }
  
  static void *ReportThd(void *arg)
  {
      DvppDecoder *self = (DvppDecoder *)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){
          DvppDecoder* self = userData->self;
          if(self != nullptr){
              self->doVdppVdecCallBack(input, output, userData);
          }
          delete userData;
  	    userData = nullptr;
      }
  }
  
  
09c2d08c   Hu Chunming   arm交付版
59
  DvppDecoder::DvppDecoder(){
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
60
      m_read_thread = nullptr;
09c2d08c   Hu Chunming   arm交付版
61
62
63
64
  
      fmt_ctx = nullptr;
  	m_bRunning = false;
  
09c2d08c   Hu Chunming   arm交付版
65
66
67
68
69
70
71
72
73
74
      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;
09c2d08c   Hu Chunming   arm交付版
75
76
77
  }
  
  DvppDecoder::~DvppDecoder(){
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
78
79
80
      close();
  
      LOG_DEBUG("[{}]- ~DvppDecoder() in_count:{}  out_count:{}", m_dec_name, m_in_count, m_out_count);
09c2d08c   Hu Chunming   arm交付版
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
  }
  
  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){
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
108
109
110
111
112
113
114
115
116
      const char* input_file = config.uri.c_str();
      FILE* infile = fopen(input_file, "r");
      if(nullptr != infile) {
          m_bReal = false;
          fclose(infile);
          infile = nullptr;
      } else {
          m_bReal = true;
      }
09c2d08c   Hu Chunming   arm交付版
117
118
119
120
121
122
  
  	// 打开输入视频文件
  	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 ); // 单位为 百万分之一秒
09c2d08c   Hu Chunming   arm交付版
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  
      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;
          }
09c2d08c   Hu Chunming   arm交付版
145
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
146
          avctx = avcodec_alloc_context3(decoder);
09c2d08c   Hu Chunming   arm交付版
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
          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;
  		}
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
168
169
170
171
172
173
          int enType = getVdecType(codecpar->codec_id, codecpar->profile);
          if(-1 == enType) {
              break;
          }
          m_enType = static_cast<acldvppStreamFormat>(enType);
  
09c2d08c   Hu Chunming   arm交付版
174
175
176
177
178
179
180
181
182
183
184
  		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;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
185
186
187
188
189
190
  
          if (stream->avg_frame_rate.den) {
              m_fps = av_q2d(stream ->avg_frame_rate);
          } else {
              m_fps = 0.0;
          }
09c2d08c   Hu Chunming   arm交付版
191
  
5a84488e   Hu Chunming   添加农村事件开关
192
  #ifdef USE_VILLAGE
1b57a1c5   Hu Chunming   代码优化,避免可能的崩溃
193
194
195
196
          bool bRet = m_recoderManager.init(stream, avctx);
          if (!bRet){
              LOG_ERROR("[{}]- m_recoderManager 初始化失败!", m_dec_name);
          }  
5a84488e   Hu Chunming   添加农村事件开关
197
  #endif
746db74c   Hu Chunming   实现recode
198
  
09c2d08c   Hu Chunming   arm交付版
199
200
201
202
203
204
205
206
207
208
209
210
          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;
  }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
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
  int DvppDecoder::getVdecType(int videoType, int profile)
  {
      int streamFormat = H264_MAIN_LEVEL;
  
      // VDEC only support H265 main level264 baseline levelmain levelhigh level
      if (videoType == AV_CODEC_ID_HEVC) {
          streamFormat = H265_MAIN_LEVEL;
      } else if (videoType == AV_CODEC_ID_H264) {
          switch (profile) {
              case FF_PROFILE_H264_BASELINE:
                  streamFormat = H264_BASELINE_LEVEL;
                  break;
              case FF_PROFILE_H264_MAIN:
                  streamFormat = H264_MAIN_LEVEL;
                  break;
              case FF_PROFILE_H264_HIGH:
              case FF_PROFILE_H264_HIGH_10:
              case FF_PROFILE_H264_HIGH_10_INTRA:
              case FF_PROFILE_H264_MULTIVIEW_HIGH:
              case FF_PROFILE_H264_HIGH_422:
              case FF_PROFILE_H264_HIGH_422_INTRA:
              case FF_PROFILE_H264_STEREO_HIGH:
              case FF_PROFILE_H264_HIGH_444:
              case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
              case FF_PROFILE_H264_HIGH_444_INTRA:
                  streamFormat = H264_HIGH_LEVEL;
                  break;
              default:
                  LOG_INFO("Not support h264 profile {}, use as mp", profile);
                  streamFormat = H264_MAIN_LEVEL;
                  break;
          }
      } else {
          streamFormat = -1;
          LOG_ERROR("Not support stream, type {},  profile {}", videoType, profile);
      }
  
      return streamFormat;
  }
  
09c2d08c   Hu Chunming   arm交付版
251
252
253
254
255
   bool DvppDecoder::init_vdpp(FFDecConfig cfg, AVCodecContext* avctx) {
  
      LOG_INFO("[{}]- Init device start...", m_dec_name);
  
      m_dvpp_deviceId = atoi(cfg.gpuid.c_str());
09c2d08c   Hu Chunming   arm交付版
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  
      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;
          }
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
279
          m_vdec_out_size = frame_width * frame_height * 3 / 2;
09c2d08c   Hu Chunming   arm交付版
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  
          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;
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
297
      m_read_thread = new std::thread([](void* arg)
09c2d08c   Hu Chunming   arm交付版
298
299
300
301
          {
              DvppDecoder* a=(DvppDecoder*)arg;
              a->read_thread();
              return (void*)0;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
302
          }, this);
09c2d08c   Hu Chunming   arm交付版
303
304
305
306
307
308
309
  
  	return true;
  }
  
  void DvppDecoder::close(){
      m_bRunning=false;
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
310
311
312
313
  	if(m_read_thread != nullptr){
          m_read_thread->join();
          delete m_read_thread;
          m_read_thread = nullptr;
09c2d08c   Hu Chunming   arm交付版
314
  	}
09c2d08c   Hu Chunming   arm交付版
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
  }
  
  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;
  }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
359
360
  static int snap_count = 0;
  
09c2d08c   Hu Chunming   arm交付版
361
  DeviceMemory* DvppDecoder::snapshot(){
09c2d08c   Hu Chunming   arm交付版
362
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
363
364
365
366
367
      int ret = aclrtSetCurrentContext(m_context);
      if(ret != ACL_ERROR_NONE){
          LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name);
          return nullptr;
      }
09c2d08c   Hu Chunming   arm交付版
368
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
      // 注意有锁
      DeviceMemory* snapshot_mem = nullptr;
      int loop_times = 0;
      while(m_bRunning) {
          m_decoded_data_queue_mtx.lock();
          if(m_decoded_data_queue.size() <= 0) {
              m_decoded_data_queue_mtx.unlock();
              loop_times++;
              if(loop_times > 100) {
                  // 1s都没截取到图,退出
                  break;
              }
              std::this_thread::sleep_for(std::chrono::milliseconds(10));
              continue;
          }
  
          DvppDataMemory* mem = m_decoded_data_queue.front();
          snapshot_mem = new DvppDataMemory(mem);
          m_decoded_data_queue_mtx.unlock();
09c2d08c   Hu Chunming   arm交付版
388
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
389
390
391
392
393
394
          // snap_count++;
          // LOG_INFO("[{}]- snap_count:{} ", m_dec_name, snap_count);
          break;
      }
  
      return snapshot_mem;
09c2d08c   Hu Chunming   arm交付版
395
396
397
398
399
400
401
402
403
404
405
406
  }
  
  int DvppDecoder::getCachedQueueLength(){
      return 0;
  }
  
  void DvppDecoder::release_ffmpeg() {
  	m_dec_keyframe = false;
  	if(h264bsfc){
  		av_bsf_free(&h264bsfc);
  		h264bsfc = nullptr;
  	}
09c2d08c   Hu Chunming   arm交付版
407
408
409
410
      if(avctx){
          avcodec_free_context(&avctx);
          avctx = nullptr;
      }
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
411
412
413
414
415
416
      if (fmt_ctx){
  		avformat_close_input(&fmt_ctx);
  		fmt_ctx = nullptr;
  	}
  
      LOG_DEBUG("[{}]- release_ffmpeg", m_dec_name);
09c2d08c   Hu Chunming   arm交付版
417
418
419
420
  }
  
  void DvppDecoder::read_thread() {
  
09c2d08c   Hu Chunming   arm交付版
421
422
  	int ret = -1;
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
423
424
425
426
427
428
429
430
431
432
      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;
  	}
  
      m_bExitDisplayThd = false;
      std::thread display_thread = std::thread(
09c2d08c   Hu Chunming   arm交付版
433
434
435
          [](void* arg)
          {
              DvppDecoder* a=(DvppDecoder*)arg;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
436
              a->display_thread();
09c2d08c   Hu Chunming   arm交付版
437
              return (void*)0;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
          },
          this
      );
  
      aclrtContext ctx = nullptr;
      aclrtStream stream = nullptr;
      aclvdecChannelDesc *vdecChannelDesc = nullptr;
  
      do {
          CHECK_AND_BREAK(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed");
          CHECK_AND_BREAK(aclrtCreateContext(&ctx, m_dvpp_deviceId), "aclrtCreateContext failed");
          CHECK_AND_BREAK(aclrtCreateStream(&stream), "aclrtCreateStream failed");
  
          vdecChannelDesc = aclvdecCreateChannelDesc();
          if (vdecChannelDesc == nullptr) { 
              LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name);
              break;
09c2d08c   Hu Chunming   arm交付版
455
          }
09c2d08c   Hu Chunming   arm交付版
456
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
457
458
459
460
461
462
463
464
          // 创建 channel dec结构体
          // 通道IDdvpp层面为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, m_enType), "aclvdecSetChannelDescEnType failed");
          CHECK_AND_BREAK(aclvdecSetChannelDescOutPicFormat(vdecChannelDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420), "aclvdecSetChannelDescOutPicFormat failed");
          CHECK_AND_BREAK(aclvdecCreateChannel(vdecChannelDesc), "aclvdecCreateChannel failed");
09c2d08c   Hu Chunming   arm交付版
465
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
466
467
468
469
          AVPacket* pkt = av_packet_alloc();
          av_init_packet( pkt );
          unsigned long long frame_nb = 0;
          while (m_bRunning){
09c2d08c   Hu Chunming   arm交付版
470
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
471
472
473
474
475
476
477
478
479
480
481
482
483
484
              if (!m_bReal){
                  if (m_bPause){
                      std::this_thread::sleep_for(std::chrono::milliseconds(10));
                      continue;
                  }
                  
                  // 非实时流,即为文件情形,因为不存在花屏问题,为保证不丢帧,这里做个循环等待
                  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();
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
485
486
487
488
489
490
  
                  if(m_DvppCacheCounter.load() > 20) {
                      // 解码器解码不过来
                      std::this_thread::sleep_for(std::chrono::milliseconds(10));
                      continue;
                  }
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
491
              }
09c2d08c   Hu Chunming   arm交付版
492
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
493
494
495
496
497
498
              int result = av_read_frame(fmt_ctx, pkt);
              if (result == AVERROR_EOF || result < 0){
                  av_packet_unref(pkt);
                  LOG_WARN("[{}]- Failed to read frame!", m_dec_name);
                  break;
              }
09c2d08c   Hu Chunming   arm交付版
499
  
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
500
501
502
503
504
505
506
              if (m_bReal && m_DvppCacheCounter.load() > 20){
                  // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免
                  // 实时流在这里处理是为了避免长时间不读取数据导致数据中断
                  std::this_thread::sleep_for(std::chrono::milliseconds(10));
                  continue;
              }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
507
508
              if (m_dec_keyframe && !(pkt->flags & AV_PKT_FLAG_KEY)) {
                  av_packet_unref(pkt);
09c2d08c   Hu Chunming   arm交付版
509
510
511
                  continue;
              }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
512
513
514
515
516
517
518
              if (video_index == pkt->stream_index){
  
                  ret = av_bsf_send_packet(h264bsfc, pkt);
                  if(ret < 0) {
                      LOG_ERROR("[{}]- av_bsf_send_packet error!", m_dec_name);
                      av_packet_unref(pkt);
                      continue;
09c2d08c   Hu Chunming   arm交付版
519
520
                  }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
521
522
                  int nSended = -1;
                  while ((ret = av_bsf_receive_packet(h264bsfc, pkt)) == 0) {
09c2d08c   Hu Chunming   arm交付版
523
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
524
525
526
                      if(!m_bRunning){
                          break;
                      }
bf661eb0   Hu Chunming   录像文件保存优化
527
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
528
529
530
531
532
                      frame_nb++;
      #ifdef USE_VILLAGE
                      m_recoderManager.cache_pkt(pkt, frame_nb);
      #endif
                      nSended = sendPkt(vdecChannelDesc, pkt, frame_nb);
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
533
                  }
09c2d08c   Hu Chunming   arm交付版
534
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
535
536
537
538
539
540
541
542
                  if(nSended < 0) {
                      break;
                  }
              } 
              
              // 音频等其他分量的情形
              av_packet_unref(pkt);
          }
09c2d08c   Hu Chunming   arm交付版
543
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
544
545
          av_packet_free(&pkt);
          pkt = nullptr;
09c2d08c   Hu Chunming   arm交付版
546
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
547
548
549
550
551
552
553
          if (vdecChannelDesc) {
              sendVdecEos(vdecChannelDesc);
          }
  
          while(m_bRunning && m_decoded_data_queue.size() > 0) {
              std::this_thread::sleep_for(std::chrono::milliseconds(5));
          }
09c2d08c   Hu Chunming   arm交付版
554
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
555
556
557
558
559
560
      } while (0);
  
      if (vdecChannelDesc) {
          CHECK_NOT_RETURN(aclvdecDestroyChannel(vdecChannelDesc), "aclvdecDestroyChannel failed");
          CHECK_NOT_RETURN(aclvdecDestroyChannelDesc(vdecChannelDesc), "aclvdecDestroyChannelDesc failed");
          vdecChannelDesc = nullptr;
09c2d08c   Hu Chunming   arm交付版
561
      }
09c2d08c   Hu Chunming   arm交付版
562
  
b17e0e69   Zhao Shuaihua   闯红灯增加场景自适应、轨迹约束;逆...
563
564
      m_bRunning=false;
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
565
566
567
568
569
570
571
572
573
574
575
576
      m_bExitReportThd = true;
  	CHECK_NOT_RETURN(pthread_join(report_thread, nullptr), "report_thread join failed");
  
      m_bExitDisplayThd = true;
      display_thread.join();
  
       if(stream){
          CHECK_NOT_RETURN(aclrtDestroyStream(stream), "aclrtDestroyStream failed");
      }
  
      if (ctx){
          CHECK_NOT_RETURN(aclrtDestroyContext(ctx), "aclrtDestroyContext failed");
09c2d08c   Hu Chunming   arm交付版
577
      }
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
578
      
09c2d08c   Hu Chunming   arm交付版
579
  
e01a0397   Hu Chunming   代码优化;
580
      m_recoderManager.close();
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
581
582
583
584
585
      
      release_ffmpeg();
      release_dvpp();
  
      m_bFinished = true;
e01a0397   Hu Chunming   代码优化;
586
  
09c2d08c   Hu Chunming   arm交付版
587
      LOG_INFO("[{}]- read thread exit.", m_dec_name);
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
588
589
590
591
  
      if(decode_finished_cbk) {
          decode_finished_cbk(m_finishedDecArg);
      }
09c2d08c   Hu Chunming   arm交付版
592
593
  }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
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
  int DvppDecoder::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++;
  
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
644
645
          // 内部缓存计数加1
          m_DvppCacheCounter++;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
646
647
648
649
650
          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;
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
651
              return -2;
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
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
          }
  
          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;
09c2d08c   Hu Chunming   arm交付版
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
  }
  
  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 ;
      }
  
09c2d08c   Hu Chunming   arm交付版
696
697
698
699
700
701
702
703
704
705
706
      while (!m_bExitReportThd) {
          aclrtProcessReport(1000);
      }
  
      ret = aclrtDestroyContext(ctx);
      if(ret != ACL_ERROR_NONE){
          LOG_ERROR("aclrtDestroyContext failed !");
      }
      LOG_INFO("doProcessReport exit.");
  }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
707
  void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData){
09c2d08c   Hu Chunming   arm交付版
708
  
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
709
710
711
      // 内部缓存计数减1
      m_DvppCacheCounter--;
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
712
713
      if(nullptr == pUserData){
          return;
09c2d08c   Hu Chunming   arm交付版
714
      }
09c2d08c   Hu Chunming   arm交付版
715
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
716
717
      Vdec_CallBack_UserData *userData = (Vdec_CallBack_UserData *) pUserData;
      uint64_t frame_nb = userData->frame_nb;
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
718
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
719
      m_out_count++;
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
720
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
721
722
723
724
      void *inputDataDev = acldvppGetStreamDescData(input);
      acldvppFree(inputDataDev);
      inputDataDev = nullptr;
  
09c2d08c   Hu Chunming   arm交付版
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
      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;
          }
  
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
741
          bool bCached = false;
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
742
          if(width > 0 && height > 0 && outputSize > 0){
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
743
  
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
744
              if (!m_bReal) {
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
745
                  while(!m_bExitReportThd) {
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
746
747
                      // 非实时流,即为文件情形,因为不存在花屏问题,为保证不丢帧,这里做个循环等待
                      m_decoded_data_queue_mtx.lock();
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
748
                      if(m_decoded_data_queue.size() >= 5){
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
749
750
751
                          m_decoded_data_queue_mtx.unlock();
                          std::this_thread::sleep_for(std::chrono::milliseconds(5));
                          continue;
09c2d08c   Hu Chunming   arm交付版
752
                      }
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
753
754
                      m_decoded_data_queue_mtx.unlock();
                      break;
09c2d08c   Hu Chunming   arm交付版
755
                  }
09c2d08c   Hu Chunming   arm交付版
756
757
              }
              
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
758
759
760
761
              // cout << m_dec_name << " 解码时间间隔: " << get_cur_time_ms() - last_ts << endl;
              // last_ts = get_cur_time_ms();
  
              // 换成解码后数据, 这里这样做的是为了保证解码一直持续进行,避免后续操作阻碍文件读取和解码从而导致花屏
473215b4   Zhao Shuaihua   修复子sdk显存重复释放问题
762
763
764
765
              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);
              if(mem){
                  m_decoded_data_queue.push(mem);
                  bCached = true;
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
766
              }
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
767
  
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
768
769
770
          } 
          
          if(!bCached) {
7c962865   Zhao Shuaihua   增加不减速(517)功能, mas...
771
              LOG_WARN("[{}]- decode result warning, width:{} width_stride:{} height:{} height_stride:{} size:{}", m_dec_name, width, width_stride, height, height_stride, outputSize);
09c2d08c   Hu Chunming   arm交付版
772
773
774
              acldvppFree(outputDataDev);
              outputDataDev = nullptr;
          }
09c2d08c   Hu Chunming   arm交付版
775
776
777
778
779
780
      }while(0);
  
  	CHECK_AND_RETURN_NOVALUE(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
  	CHECK_AND_RETURN_NOVALUE(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
  }
  
09c2d08c   Hu Chunming   arm交付版
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
  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;
  }
  
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
807
808
  void DvppDecoder::display_thread(){
      LOG_INFO("[{}]- display_thread start...", m_dec_name);
b17e0e69   Zhao Shuaihua   闯红灯增加场景自适应、轨迹约束;逆...
809
      while(!m_bExitDisplayThd) {
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
810
811
812
813
814
815
          m_decoded_data_queue_mtx.lock();
          if(m_decoded_data_queue.size() <= 0) {
              m_decoded_data_queue_mtx.unlock();
              std::this_thread::sleep_for(std::chrono::milliseconds(5));
              continue;
          }
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
816
  
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
817
818
819
820
821
822
823
824
825
826
827
828
          DvppDataMemory* mem = m_decoded_data_queue.front();
          m_decoded_data_queue.pop();
          m_decoded_data_queue_mtx.unlock();
  
          if(post_decoded_cbk) {
              post_decoded_cbk(m_postDecArg, mem);
          } else {
              delete mem;
              mem = nullptr;
          }
      }
  
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
829
      m_decoded_data_queue_mtx.lock();
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
830
831
832
833
834
835
      while (m_decoded_data_queue.size() > 0){
          DvppDataMemory* mem = m_decoded_data_queue.front();
          m_decoded_data_queue.pop();
          delete mem;
          mem = nullptr;
      }
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
836
      m_decoded_data_queue_mtx.unlock();
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
837
      
b17e0e69   Zhao Shuaihua   闯红灯增加场景自适应、轨迹约束;逆...
838
      LOG_INFO("[{}]- display_thread exit.", m_dec_name);
c5fce6ce   Zhao Shuaihua   增加二轮车玩手机505、加装雨棚5...
839
840
  }
  
09c2d08c   Hu Chunming   arm交付版
841
842
843
844
845
846
  void DvppDecoder::release_dvpp(){
      if(m_context){
          aclError ret = aclrtDestroyContext(m_context);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("[{}]- aclrtDestroyContext failed !", m_dec_name);
          }
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
847
          m_context = nullptr;
09c2d08c   Hu Chunming   arm交付版
848
849
      }
      
d51ea997   Zhao Shuaihua   升级玩手机、雨棚模型;模型批处理;...
850
851
852
853
854
      if(m_dvpp_channel >= 0){
          DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
  	    pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
          m_dvpp_channel = -1;
      }
746db74c   Hu Chunming   实现recode
855
856
857
  }
  
  void DvppDecoder::doRecode(RecoderInfo& recoderInfo) {
d15d6736   Hu Chunming   优化代码;避免可能的泄漏
858
      m_recoderManager.create_recode_task(recoderInfo);
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
859
860
861
862
  }
  
  void DvppDecoder::set_mq_callback(mq_callback_t cb) {
      m_recoderManager.set_mq_callback(cb);
09c2d08c   Hu Chunming   arm交付版
863
  }