Blame view

src/decoder/dvpp/DvppRtpDecoder.cpp 26 KB
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
1
2
3
  #include "DvppRtpDecoder.h"
  
  #include "DvppSourceManager.h"
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
  
  
  #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;}
  
  
  
  
  struct Vdec_CallBack_UserData {
      uint64_t frameId;
      uint64_t frame_nb;
      long startTime;
      long sendTime;
  	DvppRtpDecoder* self;
  
      Vdec_CallBack_UserData() {
          frameId = 0;
          frame_nb = 0;
      }
  };
  
  
  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)
  {
      DvppRtpDecoder *self = (DvppRtpDecoder *)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){
          DvppRtpDecoder* self = userData->self;
          if(self != nullptr){
              self->doVdppVdecCallBack(input, output, userData);
          }
          delete userData;
  	    userData = nullptr;
      }
  }
  
  static int avio_read_packet(void* opaque, uint8_t* buf, int buffsize){
  	DvppRtpDecoder* rtpDecoder = (DvppRtpDecoder*)opaque;
  	if(rtpDecoder) {
  		return rtpDecoder->ReadBuffer(buf, buffsize);
  	}
  
  	LOG_ERROR("rtpDecoder is null");
  
  	return 0;
  }
  
  DvppRtpDecoder::DvppRtpDecoder(){
      m_read_thread = nullptr;
  
      fmt_ctx = nullptr;
  	m_bRunning = false;
  
      mVideoIndex = -1;
      pix_fmt = AV_PIX_FMT_NONE;
      m_dec_name = "";
  
  	m_bPause = false;
  
  	m_bFinished = false;
  	m_dec_keyframe = false;
  	m_fps = 0.0;
  }
  
  DvppRtpDecoder::~DvppRtpDecoder() {
      Close();
  
      LOG_DEBUG("[{}]- ~DvppRtpDecoder() in_count:{}  out_count:{}", m_dec_name, m_in_count, m_out_count);
  }
  
  bool DvppRtpDecoder::Init(FFDecConfig cfg) {
  
      m_dec_name = cfg.dec_name;
      m_frameSkip = cfg.skip_frame;
  
      m_cfg = cfg;
  
  	m_bResize = m_cfg.resize;
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
  	bool bRet = init_dvpp(cfg);
  	if(!bRet){
  		return false;
  	}
  
  	m_bFinished = false;
  
  	return true;
  }
  
  void DvppRtpDecoder::calcOutResolution(int width, int height) {
      if(m_bResize) {
          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;
              }
          }
  
          out_frame_width = outWidth;
          out_frame_height = outHeight;
      } else {
          out_frame_width = width;
          out_frame_height = height;
      }
  }
  
  int DvppRtpDecoder::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;
  }
  
   bool DvppRtpDecoder::init_dvpp(FFDecConfig cfg) {
  
      LOG_INFO("[{}]- Init device start...", m_dec_name);
  
      m_dvpp_deviceId = atoi(cfg.gpuid.c_str());
  
      post_decoded_cbk = cfg.post_decoded_cbk;
  
      do{
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
          // 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);
              break;
          }
  
          m_vpcUtils.init(m_dvpp_deviceId);
  
          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 DvppRtpDecoder::isSurport(FFDecConfig& cfg){
      return true;
  }
  
  bool DvppRtpDecoder::start(){
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
212
213
  
      m_bRunning = true;
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
214
215
216
217
218
      
      if(!probe()) {
  		return false;
  	}
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
      m_read_thread = new std::thread([](void* arg)
          {
              DvppRtpDecoder* a=(DvppRtpDecoder*)arg;
              a->read_thread();
              return (void*)0;
          }, this);
  
  	return true;
  }
  
  void DvppRtpDecoder::Close(){
      m_bRunning=false;
  
  	if(m_read_thread != nullptr){
          m_read_thread->join();
          delete m_read_thread;
          m_read_thread = nullptr;
  	}
  
  	m_recoderManager.close();
      
      release_ffmpeg();
      release_dvpp();
  }
  
  void DvppRtpDecoder::setPostDecArg(const void* postDecArg){
      m_postDecArg = postDecArg;
  }
  
  void DvppRtpDecoder::setFinishedDecArg(const void* finishedDecArg){
      m_finishedDecArg = finishedDecArg;
  }
  
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
252
253
254
255
256
257
  void DvppRtpDecoder::SetFinishedCallback(CallBack_DecodeFinished cb, void* param)
  {
      m_finish_cbk = cb;
  	m_finishParam = param;
  }
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
  void DvppRtpDecoder::pause(){
      m_bPause = true;
  }
  
  void DvppRtpDecoder::resume(){
      m_bPause = false;
  }
  
  void DvppRtpDecoder::setDecKeyframe(bool bKeyframe){
      m_dec_keyframe = bKeyframe;
  }
  
  bool DvppRtpDecoder::isRunning(){
      return m_bRunning;
  }
  
  bool DvppRtpDecoder::isFinished(){
      return m_bFinished;
  }
  
  bool DvppRtpDecoder::isPausing(){
      return m_bPause;
  }
  
  bool DvppRtpDecoder::getResolution(int &width, int &height){
      width = frame_width;
  	height = frame_height;
  	return true;
  }
  
  bool DvppRtpDecoder::getOutResolution( int &width, int &height ) {  
      width = out_frame_width;
      height = out_frame_height;
      return true;
  }
  
  float DvppRtpDecoder::fps(){
      return m_fps;
  }
  
  static int snap_count = 0;
  
  DeviceMemory* DvppRtpDecoder::snapshot(){
  
01fb8719   Hu Chunming   修复aclrtSetDevice造...
302
      aclError ret = aclrtSetDevice(m_dvpp_deviceId);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
303
      if(ret != ACL_ERROR_NONE){
01fb8719   Hu Chunming   修复aclrtSetDevice造...
304
          LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
          return nullptr;
      }
  
      // 注意有锁
      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();
  
          // snap_count++;
          // LOG_INFO("[{}]- snap_count:{} ", m_dec_name, snap_count);
          break;
      }
  
01fb8719   Hu Chunming   修复aclrtSetDevice造...
333
334
335
336
337
338
      ret = aclrtResetDevice(m_dvpp_deviceId);
      if(ret != ACL_ERROR_NONE){
          LOG_ERROR("[{}]-aclrtResetDevice failed !", m_dec_name);
          return nullptr;
      }
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
      return snapshot_mem;
  }
  
  void DvppRtpDecoder::release_ffmpeg() {
  	m_dec_keyframe = false;
  	if(h264bsfc){
  		av_bsf_free(&h264bsfc);
  		h264bsfc = nullptr;
  	}
      if(avctx){
          avcodec_free_context(&avctx);
          avctx = nullptr;
      }
      if (fmt_ctx){
  		avformat_close_input(&fmt_ctx);
  		fmt_ctx = nullptr;
  	}
  
      LOG_DEBUG("[{}]- release_ffmpeg", m_dec_name);
  }
  
  void DvppRtpDecoder::CacheBuffer(uint8_t* recvBuf, int recvBufSize) {
3c9776e9   Hu Chunming   代码优化
361
362
363
  	if ((m_bufferSize + recvBufSize) < MAX_RTP_BUFFER_SIZE) {
          memcpy(m_buffer + m_bufferSize, recvBuf, recvBufSize);
          m_bufferSize += recvBufSize;
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
364
      } else {
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
365
366
367
368
369
  		LOG_WARN("recvBufSize = {} over MAX_RTP_BUFFER_SIZE ", recvBufSize);
  	}
  }
  
  int DvppRtpDecoder::ReadBuffer(uint8_t* buf, int buffsize) {
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
370
  
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
371
      int count = 0;
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
372
373
374
375
      while(m_bufferSize < buffsize){
          if(!m_bRunning){
              return AVERROR_EXIT;
          }
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
376
377
          std::this_thread::sleep_for(std::chrono::milliseconds(10));
          count++;
341effc6   Hu Chunming   等待时间优化
378
379
          if (count >= m_buffer_waiting_time) {
              // 等待
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
380
              return AVERROR(EIO);
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
381
382
383
          }
      }
  
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
384
385
386
      memcpy(buf, m_buffer, buffsize);
      m_bufferSize = m_bufferSize - buffsize;
      memmove(m_buffer, m_buffer + buffsize, m_bufferSize);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
387
  
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
388
      // printf("m_bufferSize=%d  buffsize=%d\n", m_bufferSize.load(), buffsize);
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
389
  
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
390
  	return buffsize;
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
391
392
393
  }
  
  bool DvppRtpDecoder::probe() {
341effc6   Hu Chunming   等待时间优化
394
395
  
      m_buffer_waiting_time = 3000;   //probe只等待30s,避免卡主任务添加
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
396
      
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
397
  	// todo: 此处可能有泄露
3c9776e9   Hu Chunming   代码优化
398
399
  	unsigned char* avioBuff = (unsigned char*)av_malloc(2048);  // 32768 < MAX_RTP_BUFFER_SIZE - RTP_HEADER_SIZE
  	AVIOContext  *ioCtx = avio_alloc_context(avioBuff, 2048, 0, this, avio_read_packet, NULL, NULL);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
400
401
402
  
  	do{
  		fmt_ctx = avformat_alloc_context();
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
403
  
341effc6   Hu Chunming   等待时间优化
404
405
          // fmt_ctx->probesize = 10000000;//5 000 000
          // fmt_ctx->flags |= AVFMT_FLAG_NOBUFFER;
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
406
407
408
          av_opt_set(fmt_ctx->priv_data,"preset","ultrafast",0);
      
          //AV_TIME_BASE = 1000 000
341effc6   Hu Chunming   等待时间优化
409
          fmt_ctx->max_analyze_duration = 100 * AV_TIME_BASE;
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
410
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
411
412
413
414
  		fmt_ctx->pb = ioCtx;
  
  
  		AVDictionary* net_options{nullptr};//网络连接参数
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
415
416
417
418
  		// av_dict_set(&net_options, "fflags", "nobuffer", 0); //不缓存直接解码
          av_dict_set( &net_options, "bufsize", "655360", 0 );
          av_dict_set( &net_options, "stimeout", "30000000", 0 ); // 单位为 百万分之一秒
          av_dict_set( &net_options, "max_delay", "500000", 0); //设置最大时延
341effc6   Hu Chunming   等待时间优化
419
420
          av_dict_set( &net_options, "probesize", "50M", 0); //设置最大时延
           
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
421
422
  
  		//打开流
341effc6   Hu Chunming   等待时间优化
423
          int ret = avformat_open_input(&fmt_ctx, 0, 0, &net_options);
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
424
  		if (ret != 0) {
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
425
426
427
  			LOG_ERROR("avformat_open_input error: {}", ret);
  			break;
  		}
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
428
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
429
  		//获取流信息
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
430
  		if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
431
432
433
  			LOG_ERROR("avformat_find_stream_info error");
  			break;
  		}
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
434
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  		//获取视频流
  		mVideoIndex = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  		if (mVideoIndex < 0)
  		{
  			LOG_ERROR("av_find_best_stream error");
  			break;
  		}
  		//获取解码信息
  		AVStream* stream = fmt_ctx->streams[mVideoIndex];
  		AVCodecParameters *codecpar = stream->codecpar;
  		const AVCodec* videoCodec = avcodec_find_decoder(codecpar->codec_id);
  		if (!videoCodec){
  			LOG_ERROR("avcodec_find_decoder error");
  			break;
  		}
  		avctx = avcodec_alloc_context3(videoCodec);
  
  		//codecpar为解码器上下文赋值
  		if (avcodec_parameters_to_context(avctx, codecpar) != 0)
  		{
  			LOG_ERROR("avcodec_parameters_to_context error");
  			break;
  		}
  
  		int enType = getVdecType(codecpar->codec_id, codecpar->profile);
  		if(-1 == enType) {
  			break;
  		}
  		m_enType = static_cast<acldvppStreamFormat>(enType);
  
  		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;
  		}
  
  		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;
  
  		calcOutResolution(frame_width, frame_height);
  
  		if (stream->avg_frame_rate.den) {
  			m_fps = av_q2d(stream ->avg_frame_rate);
  		} else {
  			m_fps = 0.0;
  		}
  
  		m_vdec_out_size = frame_width * frame_height * 3 / 2;
  
  		if (avctx->gop_size > 0) {
  			m_cache_gop = avctx->gop_size + 1;
  		} else {
  			m_cache_gop = 20;
  		}
  
  	#ifdef USE_VILLAGE
  		bool bRet = m_recoderManager.init(frame_width, frame_height, m_fps, avctx->bit_rate);
  		if (!bRet){
  			LOG_ERROR("[{}]- m_recoderManager 初始化失败!", m_dec_name);
  		}  
  	#endif
  
  		LOG_INFO("[{}]- init ffmpeg success! src:({}, {}) out:({}, {}) fps:{} ", m_dec_name, frame_width, frame_height, out_frame_width, out_frame_height, m_fps);
  
  		return true;
  	} while(0);
  
  	release_ffmpeg();
  
  	return false;
  }
  
  void DvppRtpDecoder::read_thread() {
  
  	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;
  	}
  
01fb8719   Hu Chunming   修复aclrtSetDevice造...
532
533
      CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed!");
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
534
535
536
      aclvdecChannelDesc *vdecChannelDesc = nullptr;
  
      do {
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
          vdecChannelDesc = aclvdecCreateChannelDesc();
          if (vdecChannelDesc == nullptr) { 
              LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name);
              break;
          }
  
          // 创建 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");
  
341effc6   Hu Chunming   等待时间优化
552
553
          m_buffer_waiting_time = 30000;   //read最多等待5分钟没有数据
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
554
555
556
557
558
559
560
561
562
          unsigned long long frame_nb = 0;
          while (m_bRunning){
  
              AVPacket* 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;
c2ff6d2a   Hu Chunming   初步实现ffmepg接收rtp流
563
564
                  // LOG_WARN("[{}]- Failed to read frame!", m_dec_name);
                  continue;
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
565
566
              }
  
3c9776e9   Hu Chunming   代码优化
567
568
569
570
              // av_packet_free(&pkt);
              // pkt = nullptr;
              // continue;
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
571
572
573
              if (m_DvppCacheCounter.load() > m_cache_gop){
                  // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免
                  // 实时流在这里处理是为了避免长时间不读取数据导致数据中断
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
574
575
                  av_packet_free(&pkt);
                  pkt = nullptr;
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
                  std::this_thread::sleep_for(std::chrono::milliseconds(10));
                  continue;
              }
  
              if (m_dec_keyframe && !(pkt->flags & AV_PKT_FLAG_KEY)) {
                  av_packet_free(&pkt);
                  pkt = nullptr;
                  continue;
              }
  
              if (mVideoIndex == 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_free(&pkt);
                      pkt = nullptr;
                      continue;
                  }
  
                  frame_nb++;
                  int nSended = -1;
                  while ((ret = av_bsf_receive_packet(h264bsfc, pkt)) == 0) {
                      if(!m_bRunning){
                          break;
                      }
                      nSended = sendPkt(vdecChannelDesc, pkt, frame_nb);
                  }
  
                  if(nSended < 0) {
                      // 执行出错,强行结束整个任务
                      m_bRunning=false;
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
608
609
                      av_packet_free(&pkt);
                      pkt = nullptr;
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
610
611
612
613
614
                      break;
                  }
  
      #ifdef USE_VILLAGE
                  m_recoderManager.cache_pkt(pkt, frame_nb, m_dec_name);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
615
      #endif
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
616
              }
01fb8719   Hu Chunming   修复aclrtSetDevice造...
617
618
619
  
              av_packet_free(&pkt);
              pkt = nullptr;
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
          }
  
          if (vdecChannelDesc) {
              sendVdecEos(vdecChannelDesc);
          }
  
          while(m_bRunning && m_decoded_data_queue.size() > 0) {
              std::this_thread::sleep_for(std::chrono::milliseconds(5));
          }
  
      } while (0);
  
      if (vdecChannelDesc) {
          CHECK_NOT_RETURN(aclvdecDestroyChannel(vdecChannelDesc), "aclvdecDestroyChannel failed");
          CHECK_NOT_RETURN(aclvdecDestroyChannelDesc(vdecChannelDesc), "aclvdecDestroyChannelDesc failed");
          vdecChannelDesc = nullptr;
      }
  
      m_bRunning=false;
  
      m_bExitReportThd = true;
  	CHECK_NOT_RETURN(pthread_join(report_thread, nullptr), "report_thread join failed");
  
      m_bFinished = true;
  
      LOG_INFO("[{}]- read thread exit.", m_dec_name);
  
e5c14c8e   Hu Chunming   修复解码器异常退出时,接收器还在正...
647
648
      if(m_finish_cbk) {
          m_finish_cbk(m_finishParam);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
      }
  }
  
  int DvppRtpDecoder::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;
  }
  
  void DvppRtpDecoder::doProcessReport(){
  
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
739
      aclrtContext ctx;
01fb8719   Hu Chunming   修复aclrtSetDevice造...
740
      aclError ret = aclrtCreateContext(&ctx, m_dvpp_deviceId);
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
      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 DvppRtpDecoder::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){
              
              // 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)) {
                  
                  mem = m_vpcUtils.resize(output, out_frame_width, out_frame_height);
                  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_mtx.lock();
                  m_decoded_data_queue.push(mem);
                  m_decoded_data_queue_mtx.unlock();
                  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);
  
01fb8719   Hu Chunming   修复aclrtSetDevice造...
832
833
834
835
  	CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
  	CHECK_NOT_RETURN(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
  
      CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed");
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
836
837
838
839
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
  }
  
  bool DvppRtpDecoder::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;
  }
  
  DvppDataMemory* DvppRtpDecoder::GetFrame() {
      DvppDataMemory* mem = nullptr;
      m_decoded_data_queue_mtx.lock();
      if (m_decoded_data_queue.size() > 0) {
          mem = m_decoded_data_queue.front();
          m_decoded_data_queue.pop();
      }
      m_decoded_data_queue_mtx.unlock();
  
      return mem;
  }
  
  void DvppRtpDecoder::release_dvpp(){
c027963f   Hu Chunming   ffmpeg6.1.1版本的接收rtp
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
      
      if(m_dvpp_channel >= 0){
          DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
  	    pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
          m_dvpp_channel = -1;
      }
  }
  
  void DvppRtpDecoder::doRecode(RecoderInfo& recoderInfo) {
      m_recoderManager.create_recode_task(recoderInfo);
  }
  
  void DvppRtpDecoder::set_mq_callback(mq_callback_t cb) {
      m_recoderManager.set_mq_callback(cb);
  }