DxDecoderWrap.cpp
8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <chrono>
#include "DxDecoderWrap.h"
#include "ColorSpace.h"
#include "sfx_depend_inc.h"
#include "opencv2/opencv.hpp"
#include <iostream>
#ifndef SFX_STD_LOG_DONE
#define SFX_STD_LOG_DONE "done"
#endif
static void CUDADecoder_Callback(const void * userPtr, void * buf, unsigned int size, int width, int height, unsigned long long timestamp){
DxDecoderWrap* _this = (DxDecoderWrap*)userPtr;
if(nullptr != _this){
_this->DxCUDADecoderCallback(buf, size, width, height, timestamp);
}
}
static int DecoderLog_Callback( const void * userPtr, DxLogLevel level, const char * log, unsigned int logLen ){
DxDecoderWrap* _this = (DxDecoderWrap*)userPtr;
if(nullptr != _this){
_this->DxDecoderLogCallback(level, log, logLen);
}
}
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();
}
DxDecoderWrap::DxDecoderWrap( )
{
m_pDec = nullptr;
m_skip = 0;
return;
}
int frame_nb = 0;
DxDecoderWrap::~DxDecoderWrap()
{
DxCloseDecoder();
std::cout << "总帧数" << frame_nb << std::endl;
return;
}
bool DxDecoderWrap::isStream(string sVideoFileName) {
string target = "file://";
int pos = sVideoFileName.find(target);
if (pos == 0) {
int n = target.size();
sVideoFileName = sVideoFileName.erase(pos, n);
}else {
// 非file 开头,直接判为视频流
return true;
}
pos = sVideoFileName.find_first_of('?');
if (pos != string::npos) {
sVideoFileName = sVideoFileName.substr(0, pos);
}
bool bReal = false;
FILE* fp=fopen(sVideoFileName.c_str(),"rb");
if(fp!=nullptr) {
bReal = false;
fclose(fp);
} else {
bReal = true;
}
return bReal;
}
int DxDecoderWrap::DxOpenDecoder(const DxConfig& cfg)
{
cout << "before check: " << cfg.uri << endl;
//判断是否为实时流
m_bReal = isStream(cfg.uri);
cout << "before check: " << cfg.uri << endl;
m_nums = 0;
m_bPause = false;
m_pDec = new SfxDecoder();
if ( nullptr == m_pDec ) {
return -1;
}
DxDecoderConfig m_cfg;
m_cfg.userPtr = this;
m_cfg.escbk = CUDADecoder_Callback;
m_cfg.logcbk = DecoderLog_Callback;
m_cfg.log_all = cfg.log_all;
m_cfg.log_user_ptr = cfg.log_user_ptr;
m_cfg.devId = cfg.devId;
m_cfg.uri = cfg.uri;
m_cfg.dec_name = cfg.name;
m_skip = cfg.skip_frame;
m_name = cfg.name;
m_devId = cfg.devId;
cout << "before open: " << m_cfg.uri << endl;
bool bRet= m_pDec->OpenDecoder(m_cfg);
if (bRet)
{
m_bRun = true;
LOG_INFO("[{}][{}]- 解码器初始化成功", m_name.c_str(), m_cfg.uri);
return 0;
}
// 创建失败,关闭解码器
m_pDec->CloseDecoder();
delete m_pDec;
m_pDec = nullptr;
LOG_ERROR("[{}][{}]- 解码器初始化失败!", m_name.c_str(), m_cfg.uri);
return -1;
}
int DxDecoderWrap::DxCloseDecoder() {
if (m_pDec)
{
m_pDec->CloseDecoder();
delete m_pDec;
m_pDec = nullptr;
}
m_queue_frames_mutex.lock();
while (m_queue_frames.size() > 0) {
DxGPUFrame decodedFrame = m_queue_frames.front();
if (decodedFrame.frame) {
cudaFree(decodedFrame.frame);
decodedFrame.frame = nullptr;
}
m_queue_frames.pop();
}
m_queue_frames_mutex.unlock();
}
bool DxDecoderWrap::DxDecoderIsFinished() {
if(m_pDec) {
return m_pDec->isFinished();
}
return true;
}
int DxDecoderWrap::DxGetResolution( int &width, int &height ) {
width = m_width;
height = m_height;
}
bool DxDecoderWrap::DxFrameIsEmpty()
{
m_queue_frames_mutex.lock();
int count = m_queue_frames.size();
m_queue_frames_mutex.unlock();
return 0 == count;
}
int DxDecoderWrap::DxLockFrame(DxGPUFrame& frame)
{
std::lock_guard<std::mutex> l(m_queue_frames_mutex);
if(m_queue_frames.size() <= 0) {
return -1;
}
frame = m_queue_frames.front();
m_queue_frames.pop();
return 0;
}
DxGPUFrame DxDecoderWrap::DxGetFrame()
{
std::lock_guard<std::mutex> l(m_queue_frames_mutex);
DxGPUFrame frame;
if(m_queue_frames.size() <= 0) {
return frame;
}
frame = m_queue_frames.front();
m_queue_frames.pop();
return frame;
}
int DxDecoderWrap::PauseDecoder()
{
m_bPause = true;
return 0;
}
int DxDecoderWrap::ResumeDecoder()
{
m_bPause = false;
return 0;
}
bool DxDecoderWrap::DxDecoderIsRun() const
{
return m_bRun;
}
unsigned int DxDecoderWrap::DxGetFrameCount() {
if(m_pDec) {
return m_pDec->GetFrameCount();
}
return 1;
}
int DxDecoderWrap::DxDecoderLogCallback(DxLogLevel level, const char * log, unsigned int logLen )
{
int index = 0;
if ( DX_LOG_LEVEL_FATAL == level )
{
return 0;
}
if (DX_LOG_LEVEL_IMPORTANT == level) {
if (0 == memcmp( log, SFX_STD_LOG_DONE, strlen(SFX_STD_LOG_DONE) + 1)) {
m_bRun = false;
return 0;
}
if (0 == memcmp( log, "progress ", strlen( "progress ") ) ) {
//printf("%s\n", log); //打印进度 格式:progress 100
}
}
if ( DX_LOG_LEVEL_CLEANUP == level ) {
LOG_INFO( "{}", log );
m_bRun = false;
}
return 0;
}
void DxDecoderWrap::DxCUDADecoderCallback( void * buf, unsigned int size, int width, int height, unsigned long long timestamp )
{
if ( 0 == size ) {
m_width = width;
m_height = height;
return;
}
m_nums++;
while ( m_bRun ) {
if ( m_bPause ) {
if ( m_bReal ) {
return;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
continue;
}
break;
}
if ( !m_bRun ) {
return;
}
if ( m_skip > 0 ) {
if ( m_nums % m_skip ) {
return;
}
}
while(m_bRun) {
m_queue_frames_mutex.lock();
if(m_queue_frames.size() < 3) {
// 入队
cudaSetDevice(m_devId);
size_t rgbSize = 3 * m_width * m_height * sizeof(unsigned char);
unsigned char *pHwData = nullptr;
cudaError_t cudaStatus = cudaMalloc((void **)&pHwData, rgbSize);
if (cudaStatus != cudaSuccess) {
m_queue_frames_mutex.unlock();
LOG_ERROR("[{}]- cudaMalloc failed !!!", m_name.c_str());
return ;
}
//Nv12ToColor24<BGR24>((unsigned char*)buf, m_width, pHwData, 3 * m_width, m_width, m_height, 0);
Nv12ToColor24<BGR24>((unsigned char*)buf, m_width, pHwData, 3*m_width, m_width, m_height, 0);
// cudaStatus = cudaMemcpy(pHwData, buf, rgbSize * sizeof(unsigned char), cudaMemcpyDeviceToDevice);
// int rgb_size = 3 * m_width * m_height;
// unsigned char* cpu_data = new unsigned char[rgb_size];
// cudaStatus = cudaMemcpy(cpu_data, pHwData, rgb_size * sizeof(unsigned char), cudaMemcpyDeviceToHost);
// // if (timestamp > 245 && timestamp < 255)
// // {
// // string file_name = "./nv12/"+to_string(timestamp) + ".txt";
// // FILE* fp=fopen(file_name.c_str(),"wb");
// // fwrite(cpu_data, rgb_size, 1, fp);
// // fclose(fp);
// // }
// cv::Mat img(m_height, m_width, CV_8UC3, cpu_data);
// cv::imwrite("test.jpg", img);
// delete[] cpu_data;
// cpu_data = nullptr;
DxGPUFrame frame;
frame.width = m_width;
frame.height = m_height;
frame.size = m_width;
frame.frame = pHwData;
frame.timestamp = timestamp;
frame.dec_name = m_name;
m_queue_frames.push(frame);
m_queue_frames_mutex.unlock();
frame_nb++;
std::cout << "in queue " << frame_nb << std::endl;
break;
} else {
m_queue_frames_mutex.unlock();
if (m_bReal) {
// 实时流不等待,直接弃帧
return;
}
// std::cout << "waiting queue " << frame_nb << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
//int width = m_frames.width;
//int height = m_frames.height;
//size_t rgbSize = 3 * width * height * sizeof(unsigned char);
//unsigned char* pHwData = nullptr;
//cudaStatus = cudaMalloc((void**)&pHwData, rgbSize);
//if (cudaStatus != cudaSuccess) {
// //LOG_ERROR("[{}]- cudaMalloc failed !!!", m_name.c_str());
// return;
//}
//Nv12ToColor24<BGR24>((unsigned char*)m_frames.frames[m_frames.write].frame, width, pHwData, 3 * width, width, height, 0);
//int rgb_size = 3 * width * height;
//unsigned char* cpu_data = new unsigned char[rgb_size];
//cudaStatus = cudaMemcpy(cpu_data, pHwData, rgb_size * sizeof(unsigned char), cudaMemcpyDeviceToHost);
//cv::Mat img(height, width, CV_8UC3, cpu_data);
////cv::imwrite("test.jpg", img);
//cv::imshow("test", img);
//cv::waitKey(1);
//delete[] cpu_data;
//cpu_data = nullptr;
//cudaFree(pHwData);
}