-
Notifications
You must be signed in to change notification settings - Fork 1
/
video_encoder.cpp
555 lines (461 loc) · 20.1 KB
/
video_encoder.cpp
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//
// Created by fmthhadmin on 21.05.19.
//
// Include files to use OpenCV API.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
// Include files to use the PYLON API.
#include <pylon/PylonIncludes.h>
#include <fstream>
// Use sstream to create image names including integer
#include <sstream>
//For cvtColor()
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <vector>
#include <iostream>
#include <memory>
#include <cuda.h>
#include "NvEncoder/NvEncoderCuda.h"
#include "../Utils/Logger.h"
#include "../Utils/NvEncoderCLIOptions.h"
#include "../Utils/NvCodecUtils.h"
// Namespace for using pylon objects.
using namespace Pylon;
// Namespace for using GenApi objects
using namespace GenApi;
// Namespace for using opencv objects.
using namespace cv;
// Namespace for using cout.
using namespace std;
// Number of images to be grabbed.
static const uint32_t c_countOfImagesToGrab = 1000;
simplelogger::Logger *logger = simplelogger::LoggerFactory::CreateConsoleLogger();
int status = 0; //1 means start encoding 2 means stop encoding
template <typename T>
std::vector<T> flatten(const std::vector<std::vector<T>> & vec) {
std::vector<T> result;
for (const auto & v : vec)
result.insert(result.end(), v.begin(), v.end());
return result;
}
void ShowEncoderCapability()
{
ck(cuInit(0));
int nGpu = 0;
ck(cuDeviceGetCount(&nGpu));
std::cout << "Encoder Capability" << std::endl << std::endl;
for (int iGpu = 0; iGpu < nGpu; iGpu++) {
CUdevice cuDevice = 0;
ck(cuDeviceGet(&cuDevice, iGpu));
char szDeviceName[80];
ck(cuDeviceGetName(szDeviceName, sizeof(szDeviceName), cuDevice));
CUcontext cuContext = NULL;
ck(cuCtxCreate(&cuContext, 0, cuDevice));
NvEncoderCuda enc(cuContext, 1280, 720, NV_ENC_BUFFER_FORMAT_NV12);
std::cout << "GPU " << iGpu << " - " << szDeviceName << std::endl << std::endl;
std::cout << "\tH264:\t\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_H264_GUID,
NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES) ? "yes" : "no" ) << std::endl <<
"\tH264_444:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_H264_GUID,
NV_ENC_CAPS_SUPPORT_YUV444_ENCODE) ? "yes" : "no" ) << std::endl <<
"\tH264_ME:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_H264_GUID,
NV_ENC_CAPS_SUPPORT_MEONLY_MODE) ? "yes" : "no" ) << std::endl <<
"\tH264_WxH:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_H264_GUID,
NV_ENC_CAPS_WIDTH_MAX) ) << "*" <<
( enc.GetCapabilityValue(NV_ENC_CODEC_H264_GUID, NV_ENC_CAPS_HEIGHT_MAX) ) << std::endl <<
"\tHEVC:\t\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES) ? "yes" : "no" ) << std::endl <<
"\tHEVC_Main10:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_SUPPORT_10BIT_ENCODE) ? "yes" : "no" ) << std::endl <<
"\tHEVC_Lossless:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE) ? "yes" : "no" ) << std::endl <<
"\tHEVC_SAO:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_SUPPORT_SAO) ? "yes" : "no" ) << std::endl <<
"\tHEVC_444:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_SUPPORT_YUV444_ENCODE) ? "yes" : "no" ) << std::endl <<
"\tHEVC_ME:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_SUPPORT_MEONLY_MODE) ? "yes" : "no" ) << std::endl <<
"\tHEVC_WxH:\t" << " " <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID,
NV_ENC_CAPS_WIDTH_MAX) ) << "*" <<
( enc.GetCapabilityValue(NV_ENC_CODEC_HEVC_GUID, NV_ENC_CAPS_HEIGHT_MAX) ) << std::endl;
std::cout << std::endl;
enc.DestroyEncoder();
ck(cuCtxDestroy(cuContext));
}
}
void ShowHelpAndExit(const char *szBadOption = NULL)
{
bool bThrowError = false;
std::ostringstream oss;
if (szBadOption)
{
bThrowError = true;
oss << "Error parsing \"" << szBadOption << "\"" << std::endl;
}
oss << "Options:" << std::endl
<< "-i Input file path" << std::endl
<< "-o Output file path" << std::endl
<< "-s Input resolution in this form: WxH" << std::endl
<< "-if Input format: iyuv nv12 yuv444 p010 yuv444p16 bgra bgra10 ayuv abgr abgr10" << std::endl
<< "-gpu Ordinal of GPU to use" << std::endl
;
oss << NvEncoderInitParam().GetHelpMessage() << std::endl;
if (bThrowError)
{
throw std::invalid_argument(oss.str());
}
else
{
std::cout << oss.str();
ShowEncoderCapability();
exit(0);
}
}
void ParseCommandLine(int argc, char *argv[], char *szInputFileName, int &nWidth, int &nHeight,
NV_ENC_BUFFER_FORMAT &eFormat, char *szOutputFileName, NvEncoderInitParam &initParam, int &iGpu)
{
std::ostringstream oss;
int i;
for (i = 1; i < argc; i++)
{
if (!_stricmp(argv[i], "-h"))
{
ShowHelpAndExit();
}
if (!_stricmp(argv[i], "-i"))
{
if (++i == argc)
{
ShowHelpAndExit("-i");
}
sprintf(szInputFileName, "%s", argv[i]);
continue;
}
if (!_stricmp(argv[i], "-o"))
{
if (++i == argc)
{
ShowHelpAndExit("-o");
}
sprintf(szOutputFileName, "%s", argv[i]);
continue;
}
if (!_stricmp(argv[i], "-s"))
{
if (++i == argc || 2 != sscanf(argv[i], "%dx%d", &nWidth, &nHeight))
{
ShowHelpAndExit("-s");
}
continue;
}
std::vector<std::string> vszFileFormatName =
{
"iyuv", "nv12", "yv12", "yuv444", "p010", "yuv444p16", "bgra", "bgra10", "ayuv", "abgr", "abgr10"
};
NV_ENC_BUFFER_FORMAT aFormat[] =
{
NV_ENC_BUFFER_FORMAT_IYUV,
NV_ENC_BUFFER_FORMAT_NV12,
NV_ENC_BUFFER_FORMAT_YV12,
NV_ENC_BUFFER_FORMAT_YUV444,
NV_ENC_BUFFER_FORMAT_YUV420_10BIT,
NV_ENC_BUFFER_FORMAT_YUV444_10BIT,
NV_ENC_BUFFER_FORMAT_ARGB,
NV_ENC_BUFFER_FORMAT_ARGB10,
NV_ENC_BUFFER_FORMAT_AYUV,
NV_ENC_BUFFER_FORMAT_ABGR,
NV_ENC_BUFFER_FORMAT_ABGR10,
};
if (!_stricmp(argv[i], "-if"))
{
if (++i == argc) {
ShowHelpAndExit("-if");
}
auto it = std::find(vszFileFormatName.begin(), vszFileFormatName.end(), argv[i]);
if (it == vszFileFormatName.end())
{
ShowHelpAndExit("-if");
}
eFormat = aFormat[it - vszFileFormatName.begin()];
continue;
}
if (!_stricmp(argv[i], "-gpu"))
{
if (++i == argc)
{
ShowHelpAndExit("-gpu");
}
iGpu = atoi(argv[i]);
continue;
}
// Regard as encoder parameter
if (argv[i][0] != '-')
{
ShowHelpAndExit(argv[i]);
}
oss << argv[i] << " ";
while (i + 1 < argc && argv[i + 1][0] != '-')
{
oss << argv[++i] << " ";
}
}
initParam = NvEncoderInitParam(oss.str().c_str());
}
int encodeVideo(char* cameraSerialNumber, int argc, char** argv) {
// The exit code of the sample application.
int exitCode = 0;
/******************************************************
*
* Parsing command prompt input
* Setting output file paths
*
* ***************************************************/
/*******************************************************************************************************************************************************************/
char szInFilePath[256] = "",
szOutFilePath[256] = "";
int nWidth = 0, nHeight = 0;
NV_ENC_BUFFER_FORMAT eFormat = NV_ENC_BUFFER_FORMAT_IYUV;
int iGpu = 0;
NvEncoderInitParam encodeCLIOptions;
//Parsing command line arguments
ParseCommandLine(argc, argv, szInFilePath, nWidth, nHeight, eFormat, szOutFilePath, encodeCLIOptions, iGpu);
CheckInputFile(szInFilePath);
ValidateResolution(nWidth, nHeight);
//Forming the output file path for camera using camera serial number
char endFile[20];
std::strcat(endFile, cameraSerialNumber);
std::strcat(endFile,".264");
std::strcat(szOutFilePath,endFile);
if (!*szOutFilePath) {
sprintf(szOutFilePath, encodeCLIOptions.IsCodecH264() ? "out.h264" : "out.hevc");
}
/*******************************************************************************************************************************************************************/
/******************************************************
*
* Boiler plate program to initialise CUDA Encoder
*
* ***************************************************/
/*******************************************************************************************************************************************************************/
//Encoder initialization. Boiler plate code
ck(cuInit(0));
int nGpu = 0;
ck(cuDeviceGetCount(&nGpu));
if (iGpu < 0 || iGpu >= nGpu) {
std::cout << "GPU ordinal out of range. Should be within [" << 0 << ", " << nGpu - 1 << "]" << std::endl;
return 1;
}
CUdevice cuDevice = 0;
ck(cuDeviceGet(&cuDevice, iGpu));
char szDeviceName[80];
ck(cuDeviceGetName(szDeviceName, sizeof(szDeviceName), cuDevice));
std::cout << "GPU in use: " << szDeviceName << std::endl;
CUcontext cuContext = NULL;
ck(cuCtxCreate(&cuContext, 0, cuDevice));
NvEncoderCuda enc(cuContext, nWidth, nHeight, eFormat);
NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER };
NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER };
initializeParams.encodeConfig = &encodeConfig;
enc.CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions.GetEncodeGUID(), encodeCLIOptions.GetPresetGUID());
encodeCLIOptions.SetInitParams(&initializeParams, eFormat);
enc.CreateEncoder(&initializeParams);
int nFrameSize = enc.GetFrameSize();
std::cout << "FRAME LENGTH";
std::cout << nFrameSize << "\n";
/*******************************************************************************************************************************************************************/
/******************************************************
*
* Setting up the Basler Camera to capture frames
*
* ***************************************************/
/*******************************************************************************************************************************************************************/
// Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
// is initialized during the lifetime of this object.
Pylon::PylonAutoInitTerm autoInitTerm;
try
{
// Create an instant camera object with the camera device found first.
cout << "Creating Camera..." << endl;
// CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
// or use a device info object to use a specific camera
CDeviceInfo info;
info.SetSerialNumber(cameraSerialNumber);
CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice(info));
cout << "Camera Created." << endl;
// Print the model name of the camera.
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
// The parameter MaxNumBuffer can be used to control the count of buffers
// allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer = 10;
// create pylon image format converter and pylon image
CImageFormatConverter formatConverter;
formatConverter.OutputPixelFormat= PixelType_BGR8packed;
CPylonImage pylonImage;
// Create an OpenCV image
Mat openCvImage;
Mat yuvImage;
// Start the grabbing of c_countOfImagesToGrab images.
// The camera device is parameterized with a default configuration which
// sets up free-running continuous acquisition.
camera.StartGrabbing();
// Output for ca
CGrabResultPtr ptrGrabResult;
//Encoder output file
std::ofstream fpOut(szOutFilePath, std::ios::out | std::ios::binary | std::ofstream::trunc);
if (!fpOut)
{
std::ostringstream err;
err << "Unable to open output file: " << szOutFilePath << std::endl;
throw std::invalid_argument(err.str());
}
//Keeps count of the number of frames encoded so far
int counter = 0;
int nFrame = 0;
// Camera.StopGrabbing() is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while ( camera.IsGrabbing())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded())
{
// Access the image data.
// cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
//cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
//cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;
// Convert the grabbed buffer to pylon imag
formatConverter.Convert(pylonImage, ptrGrabResult);
// Create an OpenCV image out of pylon image
openCvImage= cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());
//Converting the opencv image to YUV420 format. If you specify the input format in command prompt to something else
//Don't forget to change the Constant in cvtColor to convert opencvImage to the desired format
cvtColor(openCvImage, yuvImage, CV_BGR2YUV_I420);
// Create a display window
namedWindow( cameraSerialNumber, WINDOW_NORMAL);//AUTOSIZE //FREERATIO
// Display the current image with opencv
imshow(cameraSerialNumber, openCvImage);
/******************************************************
*
* The captured frame stored in the yuvImage is passed to the encoder and it encodes it
*
***************************************************/
//starts encoding if user inputs 1 as input
if(status != 0) {
try
{
// For receiving encoded packets
std::vector<std::vector<uint8_t>> vPacket;
if (status == 1)
{
const NvEncInputFrame* encoderInputFrame = enc.GetNextInputFrame();
NvEncoderCuda::CopyToDeviceFrame(cuContext, (uint8_t*)yuvImage.data, 0, (CUdeviceptr)encoderInputFrame->inputPtr,
(int)encoderInputFrame->pitch,
enc.GetEncodeWidth(),
enc.GetEncodeHeight(),
CU_MEMORYTYPE_HOST,
encoderInputFrame->bufferFormat,
encoderInputFrame->chromaOffsets,
encoderInputFrame->numChromaPlanes);
enc.EncodeFrame(vPacket);
}
else
{
cout << "Stopping encoding for: " << cameraSerialNumber;
enc.EndEncode(vPacket);
camera.StopGrabbing();
}
nFrame += (int)vPacket.size();
for (std::vector<uint8_t> &packet : vPacket)
{
// For each encoded packet
fpOut.write(reinterpret_cast<char*>(packet.data()), packet.size());
}
}
catch (const std::exception &ex)
{
std::cout << ex.what();
}
}
waitKey(1);
}
else
{
cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;
}
counter += 1;
}
std::cout << "Total frames encoded: " << nFrame << std::endl << "Saved in file " << szOutFilePath << std::endl;
enc.DestroyEncoder();
fpOut.close();
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "An exception occurred." << endl
<< e.GetDescription() << endl;
exitCode = 1;
}
// Comment the following two lines to disable waiting on exit.
cerr << endl << "Press Enter to exit." << endl;
while( cin.get() != '\n');
return exitCode;
}
int msleep(unsigned long milisec)
{
struct timespec req={0};
time_t sec=(int)(milisec/1000);
milisec=milisec-(sec*1000);
req.tv_sec=sec;
req.tv_nsec=milisec*1000000L;
while(nanosleep(&req,&req)==-1)
continue;
return 1;
}
void getUserInput() {
msleep(3000);
cout << "\n" << "Press 1 to start encoding";
std::cin >> status;
cout << "Press 2 to stop encoding";
std::cin >> status;
}
/******************************************************
*
* Main function
* Separate threads are created for each camera
* Manually edit the ids
* You can find the ids for the Basler cameras in PylonViewerApp
*
***************************************************/
int main(int argc, char** argv)
{
char* camera1SerialNumber = "40024100";
char* camera2SerialNumber = "40024099";
char* camera3SerialNumber = "40024969";
char* camera4SerialNumber = "40024971";
char* camera5SerialNumber = "40024299";
std::thread t1(encodeVideo,std::ref(camera1SerialNumber),std::ref(argc),std::ref(argv));
std::thread t2(encodeVideo,std::ref(camera2SerialNumber),std::ref(argc),std::ref(argv));
std::thread t3(encodeVideo,std::ref(camera3SerialNumber),std::ref(argc),std::ref(argv));
std::thread t4(encodeVideo,std::ref(camera4SerialNumber),std::ref(argc),std::ref(argv));
std::thread t5(encodeVideo,std::ref(camera5SerialNumber),std::ref(argc),std::ref(argv));
std::thread t6(getUserInput);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
return 1;
}