Blame view

src/dvpp/DvppDec.cpp 13 KB
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
1
2
3
4
  #include "DvppDec.h"
  #include "DvppSourceManager.h"
  
  #define CHECK_AND_RETURN(ret, message)    \
42424bf2   Hu Chunming   代码优化
5
              if(ret != 0) {LOG_ERROR("[{}]- {}", m_dec_name, message); return ret;}
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
6
  #define CHECK_NOT_RETURN(ret, message)    \
42424bf2   Hu Chunming   代码优化
7
              if(ret != 0) {LOG_ERROR("[{}]- {}", m_dec_name, message);}
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
8
  #define CHECK_AND_RETURN_NOVALUE(ret, message)    \
42424bf2   Hu Chunming   代码优化
9
10
11
              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;}
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  
  struct Vdec_CallBack_UserData {
      uint64_t frameId;
      long startTime;
      long sendTime;
      // void* vdecOutputBuf;
  	DvppDec* self;
  	shared_ptr<MemNode> inBufNode;
      Vdec_CallBack_UserData() {
          frameId = 0;
      }
  };
  
  #ifdef TEST_DECODER
  static void *vdecHostAddr = nullptr;
  #endif
  
  static const int g_pkt_size = 1024 * 1024;
  
   DvppDec::DvppDec(){
      m_decode_thread = 0;
   }
  
   DvppDec::~DvppDec(){
  
   }
  
   bool DvppDec::init_vdpp(DvppDecConfig cfg){
      cout << "Init device....\n";
  
      m_dvpp_deviceId = atoi(cfg.dev_id.c_str());
      
      if(cfg.codec_id == 0){
          // 66Baseline77Main>=100High
          if(cfg.profile == 77){
              enType = H264_MAIN_LEVEL;
          }else if(cfg.profile < 77){
              enType = H264_BASELINE_LEVEL;
          }else{
              enType = H264_HIGH_LEVEL;
          }
      }else if(cfg.codec_id == 1){
          // h265只有main
          enType = H265_MAIN_LEVEL;
      }else {
          cout << "codec_id is not supported!" << endl;
          return false;
      }
  
      post_decoded_cbk = cfg.post_decoded_cbk;
      m_pktQueueptr = cfg.pktQueueptr;
  
  	// DvppSourceManager 创建时包含 aclInit,析构时包含 aclFinalize
  	DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
  	m_context = pSrcMgr->getContext(m_dvpp_deviceId);
  	m_dvpp_channel = pSrcMgr->getChannel(m_dvpp_deviceId);
  	if(m_dvpp_channel < 0){
  		cout << "该设备channel已经用完了" << endl;
  		return false;
  	}
  
      cout << "devProgram start, device: " << m_dvpp_deviceId << endl;
      int ret = aclrtSetCurrentContext(m_context);
      if (ret != ACL_ERROR_NONE) { 
  		cout << "aclrtSetCurrentContext failed" << endl;
  		return false;
  	}
  	
  	// queue_size 最小应大于16,否则关键帧之间距离太远的时候会导致回调函数与循环队列卡死
  	for (size_t i = 0; i < 20; i++){
  		void *vdecInputbuf = nullptr;
  		int ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size);
  		if(ret != ACL_ERROR_NONE){
  		    cout << "acldvppMalloc failed" << endl;
  			return false;;
  		}
  		m_vec_vdec.push_back(vdecInputbuf);
      }
  
  	if(!m_vdecQueue.init(m_vec_vdec)){
  		return false;
  	}
  
      ret = picConverter.init(m_context);
  	if(!ret){
  		picConverter.release();
  	}
  
      m_vdec_out_size = cfg.width * cfg.height * 3 / 2;
      m_dec_name = cfg.dec_name;
  
  	cout << "init vdpp success!" << endl;
  	return true;
  }
  
  bool DvppDec::start(){
  	m_bRunning = true;
  
  	pthread_create(&m_decode_thread,0,
          [](void* arg)
          {
              DvppDec* a=(DvppDec*)arg;
              a->decode_thread();
              return (void*)0;
          }
      ,this);
  
  	return true;
  }
  
  static void *ReportThd(void *arg)
  {
      DvppDec *self = (DvppDec *)arg;
  	if(nullptr != self){
  		self->doProcessReport();
  	}
      return (void *)0;
  }
  
  void DvppDec::doProcessReport(){
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
132
133
134
135
136
137
  
  	CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
      // 阻塞等待vdec线程开始
  
      int ret;
      while (!m_bExitReportThd) {
6042f34b   Hu Chunming   代码优化
138
          aclrtProcessReport(1000);
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
139
      }
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
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
  }
  
  static int count_frame = 0;
  static long lastts = 0;
  static void VdecCallback(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData)
  {
  	cout << "VdecCallback: " << UtilTools::get_cur_time_ms() - lastts << endl;
  	lastts = UtilTools::get_cur_time_ms();
  
  	Vdec_CallBack_UserData *userData = (Vdec_CallBack_UserData *) pUserData;
  	DvppDec* self = userData->self;
  	if(self != nullptr){
  
  		self->doVdppVdecCallBack(input, output);
  	}
  	
  	delete userData;
  	userData = nullptr;
  }
  
  void DvppDec::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output){
  
  	CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
  
  	void *inputDataDev = acldvppGetStreamDescData(input);
  	void *outputDataDev = acldvppGetPicDescData(output);
  	uint32_t outputSize = acldvppGetPicDescSize(output);
  	uint32_t width = acldvppGetPicDescWidth(output);
  	uint32_t height = acldvppGetPicDescHeight(output);
  	
  	cout << "width = " << width << "  height = " << height << "  data_size:" << outputSize << endl;
  
  	if (!m_bPause)
  	{
  		DvppRgbMemory* rgbMem = picConverter.convert2bgr(output, width, height, false);
          post_decoded_cbk(m_postDecArg, rgbMem);
  #ifdef TEST_DECODER
  		if(rgbMem != nullptr){
  			// 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)
  			{
6042f34b   Hu Chunming   代码优化
188
  				string file_name = "./yuv_pic/vdec_out_"+ m_dec_name +".rgb" ;
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  				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
  
  	}else{
  		std::this_thread::sleep_for(std::chrono::milliseconds(3));
  	}
  
      acldvppFree((uint8_t*)outputDataDev);
      outputDataDev = nullptr;
  
  	m_vdecQueue.addHead();
  
  	CHECK_AND_RETURN_NOVALUE(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
  	CHECK_AND_RETURN_NOVALUE(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
  
  	cout << "callback exit." << endl;
  }
  
  void DvppDec::close(){
  	m_bRunning=false;
  
  	if(m_decode_thread != 0){
  		pthread_join(m_decode_thread,0);
  	}
  }
  
  bool DvppDec::sendVdecEos(aclvdecChannelDesc *vdecChannelDesc){
      // create stream desc
      acldvppStreamDesc *streamInputDesc = acldvppCreateStreamDesc();
      if (streamInputDesc == nullptr) {
          cout << "fail to create input stream desc" << endl;
          return false;
      }
      aclError ret = acldvppSetStreamDescEos(streamInputDesc, 1);
      if (ret != ACL_SUCCESS) {
          cout << "fail to set eos for stream desc, errorCode = " << static_cast<int32_t>(ret) << endl;
          (void)acldvppDestroyStreamDesc(streamInputDesc);
          return false;
      }
  
      // send vdec eos frame. when all vdec callback are completed, aclvdecSendFrame can be returned.
      cout << "send eos" << endl;
      ret = aclvdecSendFrame(vdecChannelDesc, streamInputDesc, nullptr, nullptr, nullptr);
      if (ret != ACL_SUCCESS) {
          cout << "fail to send eos frame, ret=" << ret << endl;
          (void)acldvppDestroyStreamDesc(streamInputDesc);
          return false;
      }
      (void)acldvppDestroyStreamDesc(streamInputDesc);
  
      return true;
  }
  
  void DvppDec::releaseResource(){
  
  	for(int i = 0; i < m_vec_vdec.size(); i++){
  		if(m_vec_vdec[i] != nullptr){
42424bf2   Hu Chunming   代码优化
257
  			acldvppFree(m_vec_vdec[i]);
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
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
  			m_vec_vdec[i] = nullptr;
  		}
  	}
  	m_vec_vdec.clear();
  
  	DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
  	pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
  }
  
  void DvppDec::decode_thread(){
  
      long startTime = UtilTools::get_cur_time_ms();
  
  	int ret = -1;
  
      // dvpp解码参数
  	CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
  
  	pthread_t report_thread;
  	ret = pthread_create(&report_thread, nullptr, ReportThd, (void *)this);
  	if(ret != 0){
  		cout << "pthread_create failed" << endl;
  		return;
  	}
  
      // 创建aclvdecChannelDesc类型的数据
      aclvdecChannelDesc *vdecChannelDesc = aclvdecCreateChannelDesc();
      if (vdecChannelDesc == nullptr) { 
  		cout << "aclvdecCreateChannelDesc failed"; 
  		return;
  	}
      // 创建 channel dec结构体
      // 通道IDdvpp层面为0~31
      CHECK_AND_RETURN_NOVALUE(aclvdecSetChannelDescChannelId(vdecChannelDesc, m_dvpp_channel), "aclvdecSetChannelDescChannelId failed");
      CHECK_AND_RETURN_NOVALUE(aclvdecSetChannelDescThreadId(vdecChannelDesc, report_thread), "aclvdecSetChannelDescThreadId failed");
      CHECK_AND_RETURN_NOVALUE(aclvdecSetChannelDescCallback(vdecChannelDesc, VdecCallback), "aclvdecSetChannelDescCallback failed");
      CHECK_AND_RETURN_NOVALUE(aclvdecSetChannelDescEnType(vdecChannelDesc, enType), "aclvdecSetChannelDescEnType failed");
      CHECK_AND_RETURN_NOVALUE(aclvdecSetChannelDescOutPicFormat(vdecChannelDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420), "aclvdecSetChannelDescOutPicFormat failed");
      CHECK_AND_RETURN_NOVALUE(aclvdecCreateChannel(vdecChannelDesc), "aclvdecCreateChannel failed");
  
      uint64_t frame_count = 0;
      bool bBreak = false;
  	while (m_bRunning)
  	{
          int ret = sentFrame(vdecChannelDesc, frame_count);
          if(ret == 2){
              break;
              bBreak = true;
          }else if(ret == 1){
              continue;
          }
  
          frame_count++;
  	}
  
      // 尽量保证数据全部解码完成
42424bf2   Hu Chunming   代码优化
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
      int sum = 0;
      if(!bBreak){
          while(!m_pktQueueptr->isEmpty()){
              int ret = sentFrame(vdecChannelDesc, frame_count);
              if(ret == 2){
                  break;
              }
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              sum++;
              if(sum > 40){
                  // 避免卡死
                  break;
              }
          }
      }
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
329
      
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  	sendVdecEos(vdecChannelDesc);
  
  	CHECK_NOT_RETURN(aclvdecDestroyChannel(vdecChannelDesc), "aclvdecDestroyChannel failed");
      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");
  
  	cout << "decode thread exit." << endl;
  }
  
  int DvppDec::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){
  
      AVPacket * pkt = m_pktQueueptr->getHead();
      if(pkt == nullptr){
          std::this_thread::sleep_for(std::chrono::milliseconds(10));
          // cout << "getTail failed" << endl;
          // continue;
          return 1;
      }
      // 解码
      void *vdecInputbuf = m_vdecQueue.getTail();
      if(vdecInputbuf == nullptr){
          std::this_thread::sleep_for(std::chrono::milliseconds(3));
          // cout << "getTail failed" << endl;
          // continue;
          return 1;
      }
      
      int ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE);
      if(ACL_ERROR_NONE != ret){
          cout << "aclrtMemcpy failed" << endl;
          // break;
          return 2;
      }
  
      void *vdecOutputBuf = nullptr;
      ret = acldvppMalloc((void **)&vdecOutputBuf, m_vdec_out_size);
      if(ret != ACL_ERROR_NONE){
          cout << "acldvppMalloc failed" << endl;
          // break;
          return 2;
      }
  
42424bf2   Hu Chunming   代码优化
376
377
378
379
      acldvppStreamDesc *input_stream_desc = nullptr;
      acldvppPicDesc *output_pic_desc = nullptr;
      do{
           input_stream_desc = acldvppCreateStreamDesc();
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
380
          if (input_stream_desc == nullptr) { cout << "acldvppCreateStreamDesc error" << endl; }
42424bf2   Hu Chunming   代码优化
381
          output_pic_desc = acldvppCreatePicDesc();
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
382
          if (output_pic_desc == nullptr) { cout<< "acldvppCreatePicDesc error" << endl; }
42424bf2   Hu Chunming   代码优化
383
384
385
386
387
388
389
390
391
392
393
          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;
          // user_data->startTime = startTime;
          user_data->sendTime = UtilTools::get_cur_time_ms();
          user_data->self = this;
6042f34b   Hu Chunming   代码优化
394
395
396
397
398
399
400
401
          
          ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data));
          if(ret != ACL_ERROR_NONE){
              cout << "aclvdecSendFrame failed" << endl;
              m_pktQueueptr->addHead();
              av_packet_unref(pkt);
              break;
          }
42424bf2   Hu Chunming   代码优化
402
403
404
  
          m_vdecQueue.addTail();
  
42424bf2   Hu Chunming   代码优化
405
406
407
408
409
410
          return 0;
      }while (0);
  
      if(input_stream_desc){
          CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed");
      }
6042f34b   Hu Chunming   代码优化
411
      if(output_pic_desc){
42424bf2   Hu Chunming   代码优化
412
413
414
415
416
417
418
          CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed");
      }
  
      acldvppFree(vdecOutputBuf);
  	vdecOutputBuf = nullptr;
  
      return 1;
63e6f7bc   Hu Chunming   完成dvpp。但是nv和gb281...
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  }
  
  
  void DvppDec::setPostDecArg(const void* postDecArg){
  	m_postDecArg = postDecArg;
  }
  
  void DvppDec::pause(){
      m_bPause = true;
  }
  
  void DvppDec::resume(){
      m_bPause = false;
  }