-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppEncCuda.cpp
340 lines (306 loc) · 12.2 KB
/
AppEncCuda.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
/*
* Copyright 2017-2018 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*
#include <fstream>
#include <iostream>
#include <memory>
#include <cuda.h>
#include "NvEncoder/NvEncoderCuda.h"
#include "../Utils/Logger.h"
#include "../Utils/NvEncoderCLIOptions.h"
#include "../Utils/NvCodecUtils.h"
simplelogger::Logger *logger = simplelogger::LoggerFactory::CreateConsoleLogger();
void EncodeCuda(CUcontext cuContext, char *szInFilePath, int nWidth, int nHeight, NV_ENC_BUFFER_FORMAT eFormat,
char *szOutFilePath, NvEncoderInitParam *pEncodeCLIOptions)
{
std::ifstream fpIn(szInFilePath, std::ifstream::in | std::ifstream::binary);
if (!fpIn)
{
std::ostringstream err;
err << "Unable to open input file: " << szInFilePath << std::endl;
throw std::invalid_argument(err.str());
}
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());
}
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, pEncodeCLIOptions->GetEncodeGUID(), pEncodeCLIOptions->GetPresetGUID());
pEncodeCLIOptions->SetInitParams(&initializeParams, eFormat);
enc.CreateEncoder(&initializeParams);
int nFrameSize = enc.GetFrameSize();
std::cout << "FRAME LENGTH";
std::cout << nFrameSize;
std::unique_ptr<uint8_t[]> pHostFrame(new uint8_t[nFrameSize]);
int nFrame = 0;
while (true)
{
// Load the next frame from disk
std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nFrameSize).gcount();
// For receiving encoded packets
std::vector<std::vector<uint8_t>> vPacket;
if (nRead == nFrameSize)
{
const NvEncInputFrame* encoderInputFrame = enc.GetNextInputFrame();
NvEncoderCuda::CopyToDeviceFrame(cuContext, pHostFrame.get(), 0, (CUdeviceptr)encoderInputFrame->inputPtr,
(int)encoderInputFrame->pitch,
enc.GetEncodeWidth(),
enc.GetEncodeHeight(),
CU_MEMORYTYPE_HOST,
encoderInputFrame->bufferFormat,
encoderInputFrame->chromaOffsets,
encoderInputFrame->numChromaPlanes);
enc.EncodeFrame(vPacket);
}
else
{
enc.EndEncode(vPacket);
}
nFrame += (int)vPacket.size();
for (std::vector<uint8_t> &packet : vPacket)
{
// For each encoded packet
fpOut.write(reinterpret_cast<char*>(packet.data()), packet.size());
}
if (nRead != nFrameSize) break;
}
enc.DestroyEncoder();
fpOut.close();
fpIn.close();
std::cout << "Total frames encoded: " << nFrame << std::endl << "Saved in file " << szOutFilePath << std::endl;
}
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());
}
*
* This sample application illustrates encoding of frames in CUDA device buffers.
* The application reads the image data from file and loads it to CUDA input
* buffers obtained from the encoder using NvEncoder::GetNextInputFrame().
* The encoder subsequently maps the CUDA buffers for encoder using NvEncodeAPI
* and submits them to NVENC hardware for encoding as part of EncodeFrame() function.
*
int main(int argc, char **argv)
{
char szInFilePath[256] = "",
szOutFilePath[256] = "";
int nWidth = 0, nHeight = 0;
NV_ENC_BUFFER_FORMAT eFormat = NV_ENC_BUFFER_FORMAT_IYUV;
int iGpu = 0;
try
{
NvEncoderInitParam encodeCLIOptions;
ParseCommandLine(argc, argv, szInFilePath, nWidth, nHeight, eFormat, szOutFilePath, encodeCLIOptions, iGpu);
CheckInputFile(szInFilePath);
ValidateResolution(nWidth, nHeight);
if (!*szOutFilePath)
{
sprintf(szOutFilePath, encodeCLIOptions.IsCodecH264() ? "out.h264" : "out.hevc");
}
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));
EncodeCuda(cuContext, szInFilePath, nWidth, nHeight, eFormat, szOutFilePath, &encodeCLIOptions);
}
catch (const std::exception &ex)
{
std::cout << ex.what();
return 1;
}
return 0;
}
*/