| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #include <fstream>
|
| #include <iostream>
|
| #include <memory>
|
| #include <cuda.h>
|
| #include "../Utils/NvCodecUtils.h"
|
| #include "NvEncoder/NvEncoderCuda.h"
|
| #include "NvEncoder/NvEncoderOutputInVidMemCuda.h"
|
| #include "../Utils/Logger.h"
|
| #include "../Utils/NvEncoderCLIOptions.h"
|
| #include "../Common/AppEncUtils.h"
|
|
|
| simplelogger::Logger *logger = simplelogger::LoggerFactory::CreateConsoleLogger();
|
|
|
|
|
|
|
|
|
| class NvCUStream
|
| {
|
| public:
|
| NvCUStream(CUcontext cuDevice, int cuStreamType, std::unique_ptr<NvEncoderOutputInVidMemCuda> &pEnc)
|
| {
|
| device = cuDevice;
|
| CUDA_DRVAPI_CALL(cuCtxPushCurrent(device));
|
|
|
|
|
| if (cuStreamType == 1)
|
| {
|
| ck(cuStreamCreate(&inputStream, CU_STREAM_DEFAULT));
|
| outputStream = inputStream;
|
| }
|
| else if (cuStreamType == 2)
|
| {
|
| ck(cuStreamCreate(&inputStream, CU_STREAM_DEFAULT));
|
| ck(cuStreamCreate(&outputStream, CU_STREAM_DEFAULT));
|
| }
|
|
|
| CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL));
|
|
|
|
|
| pEnc->SetIOCudaStreams((NV_ENC_CUSTREAM_PTR)&inputStream, (NV_ENC_CUSTREAM_PTR)&outputStream);
|
| }
|
|
|
| ~NvCUStream()
|
| {
|
| ck(cuCtxPushCurrent(device));
|
|
|
| if (inputStream == outputStream)
|
| {
|
| if (inputStream != NULL)
|
| ck(cuStreamDestroy(inputStream));
|
| }
|
| else
|
| {
|
| if (inputStream != NULL)
|
| ck(cuStreamDestroy(inputStream));
|
|
|
| if (outputStream != NULL)
|
| ck(cuStreamDestroy(outputStream));
|
| }
|
|
|
| ck(cuCtxPopCurrent(NULL));
|
| }
|
|
|
| CUstream GetOutputCUStream() { return outputStream; };
|
| CUstream GetInputCUStream() { return inputStream; };
|
|
|
| private:
|
| CUcontext device;
|
| CUstream inputStream = NULL, outputStream = NULL;
|
| };
|
|
|
|
|
| class CRC
|
| {
|
| public:
|
| CRC(CUcontext cuDevice, uint32_t bufferSize)
|
| {
|
| device = cuDevice;
|
|
|
| ck(cuCtxPushCurrent(device));
|
|
|
|
|
| ck(cuMemAlloc(&crcVidMem, bufferSize));
|
|
|
| ck(cuCtxPopCurrent(NULL));
|
| }
|
|
|
| ~CRC()
|
| {
|
| ck(cuCtxPushCurrent(device));
|
|
|
| ck(cuMemFree(crcVidMem));
|
|
|
| ck(cuCtxPopCurrent(NULL));
|
| }
|
|
|
| void GetCRC(NV_ENC_OUTPUT_PTR pVideoMemBfr, CUstream outputStream)
|
| {
|
| ComputeCRC((uint8_t *)pVideoMemBfr, (uint32_t *)crcVidMem, outputStream);
|
| }
|
|
|
| CUdeviceptr GetCRCVidMemPtr() { return crcVidMem; };
|
|
|
| private:
|
| CUcontext device;
|
| CUdeviceptr crcVidMem = 0;
|
| };
|
|
|
|
|
|
|
| class DumpVidMemOutput
|
| {
|
| public:
|
| DumpVidMemOutput(CUcontext cuDevice, uint32_t size, char *outFilePath, bool bUseCUStream)
|
| {
|
| device = cuDevice;
|
| bfrSize = size;
|
| bCRC = bUseCUStream;
|
|
|
| ck(cuCtxPushCurrent(device));
|
|
|
|
|
| ck(cuMemAllocHost((void **)&pHostMemEncOp, (bfrSize + (bCRC ? 4 : 0))));
|
|
|
| ck(cuCtxPopCurrent(NULL));
|
|
|
|
|
| if (bCRC)
|
| {
|
| crcFile = std::string(outFilePath) + "_crc.txt";
|
| fpCRCOut.open(crcFile, std::ios::out);
|
| pHostMemCRC = (uint32_t *)((uint8_t *)pHostMemEncOp + bfrSize);
|
| }
|
| }
|
|
|
| ~DumpVidMemOutput()
|
| {
|
| ck(cuCtxPushCurrent(device));
|
|
|
| ck(cuMemFreeHost(pHostMemEncOp));
|
|
|
| ck(cuCtxPopCurrent(NULL));
|
|
|
| if (bCRC)
|
| {
|
| fpCRCOut.close();
|
| std::cout << "CRC saved in file: " << crcFile << std::endl;
|
| }
|
| }
|
|
|
| void DumpOutputToFile(CUdeviceptr pEncFrameBfr, CUdeviceptr pCRCBfr, std::ofstream &fpOut, uint32_t nFrame, bool &bWriteIVFFileHeader, NV_ENC_INITIALIZE_PARAMS &pInitializeParams)
|
| {
|
| ck(cuCtxPushCurrent(device));
|
|
|
|
|
| ck(cuMemcpyDtoH(pHostMemEncOp, pEncFrameBfr, bfrSize));
|
|
|
|
|
| if (bCRC)
|
| {
|
| ck(cuMemcpyDtoH(pHostMemCRC, pCRCBfr, 4));
|
| }
|
|
|
| ck(cuCtxPopCurrent(NULL));
|
|
|
|
|
| uint32_t offset = sizeof(NV_ENC_ENCODE_OUT_PARAMS);
|
| uint32_t bitstream_size = ((NV_ENC_ENCODE_OUT_PARAMS *)pHostMemEncOp)->bitstreamSizeInBytes;
|
| uint8_t * ptr = pHostMemEncOp + offset;
|
|
|
| IVFUtils iVFUtils;
|
| int64_t pts = 0;
|
| std::vector<uint8_t> vPacket;
|
| if (pInitializeParams.encodeGUID == NV_ENC_CODEC_AV1_GUID)
|
| {
|
| if (bWriteIVFFileHeader)
|
| {
|
| iVFUtils.WriteFileHeader(vPacket, MAKE_FOURCC('A', 'V', '0', '1'), pInitializeParams.encodeWidth, pInitializeParams.encodeHeight, pInitializeParams.frameRateNum, pInitializeParams.frameRateDen, 0xFFFF);
|
| fpOut.write(reinterpret_cast<char*>(vPacket.data()), vPacket.size());
|
| bWriteIVFFileHeader = false;
|
| vPacket.clear();
|
| }
|
| iVFUtils.WriteFrameHeader(vPacket, bitstream_size, pts);
|
| fpOut.write(reinterpret_cast<char*>(vPacket.data()), vPacket.size());
|
| }
|
|
|
| fpOut.write((const char *)ptr, bitstream_size);
|
|
|
|
|
| if (bCRC)
|
| {
|
| if (!nFrame)
|
| {
|
| fpCRCOut << "Frame num" << std::setw(10) << "CRC" << std::endl;
|
| }
|
| fpCRCOut << std::dec << std::setfill(' ') << std::setw(5) << nFrame << " ";
|
| fpCRCOut << std::hex << std::setfill('0') << std::setw(8) << *pHostMemCRC << std::endl;
|
| }
|
| }
|
| private:
|
| CUcontext device;
|
| uint32_t bfrSize;
|
| uint8_t *pHostMemEncOp = NULL;
|
| uint32_t *pHostMemCRC = NULL;
|
| bool bCRC;
|
| std::string crcFile;
|
| std::ofstream fpCRCOut;
|
| };
|
|
|
| void ShowEncoderBriefHelp()
|
| {
|
| std::ostringstream oss;
|
| oss << "NVIDIA Video Encoder AppEncCuda Sample Application\n";
|
| oss << "============================================\n\n";
|
|
|
| oss << "Usage: AppEncCuda -i <input_file> [options]\n\n";
|
|
|
|
|
| oss << "Common Arguments:\n";
|
| oss << std::left << std::setw(25) << "Argument"
|
| << std::setw(12) << "Type"
|
| << "Default Value\n";
|
| oss << std::string(50, '-') << "\n";
|
|
|
| oss << std::left << std::setw(25) << "-i <path>"
|
| << std::setw(12) << "Required"
|
| << "N/A\n";
|
| oss << std::left << std::setw(25) << "-o <path>"
|
| << std::setw(12) << "Optional"
|
| << "codec-based (out.h264/hevc/av1)\n";
|
| oss << std::left << std::setw(25) << "-s <WxH>"
|
| << std::setw(12) << "Required"
|
| << "N/A\n";
|
| oss << std::left << std::setw(25) << "-if <format>"
|
| << std::setw(12) << "Optional"
|
| << "iyuv\n";
|
| oss << std::left << std::setw(25) << "-gpu <n>"
|
| << std::setw(12) << "Optional"
|
| << "0\n";
|
|
|
| oss << "\nFor detailed help, use -A/--advanced-options\n";
|
| oss << "To view encoder capabilities, use -ec/--encode-caps\n";
|
| std::cout << oss.str();
|
| exit(0);
|
| }
|
|
|
| void ShowEncoderDetailedHelp()
|
| {
|
| std::ostringstream oss;
|
| oss << "NVIDIA Video Encoder AppEncCuda Sample Application - Detailed Help\n";
|
| oss << "=======================================================\n\n";
|
|
|
| oss << "Usage: AppEncCuda -i <input_file> [options]\n\n";
|
|
|
|
|
| oss << "All Arguments:\n";
|
| oss << std::left << std::setw(25) << "Argument"
|
| << std::setw(12) << "Type"
|
| << std::setw(20) << "Default Value"
|
| << "Example\n";
|
| oss << std::string(80, '-') << "\n";
|
|
|
|
|
| oss << std::left << std::setw(25) << "-i <path>"
|
| << std::setw(12) << "Required"
|
| << std::setw(20) << "N/A"
|
| << "-i input.yuv\n";
|
| oss << std::left << std::setw(25) << "-s <WxH>"
|
| << std::setw(12) << "Required"
|
| << std::setw(20) << "N/A"
|
| << "-s 1920x1080\n";
|
|
|
|
|
| oss << std::left << std::setw(25) << "-o <path>"
|
| << std::setw(12) << "Optional"
|
| << std::setw(20) << "codec-based"
|
| << "-o output.h264\n";
|
| oss << std::left << std::setw(25) << "-if <format>"
|
| << std::setw(12) << "Optional"
|
| << std::setw(20) << "iyuv"
|
| << "-if yuv444\n";
|
| oss << std::left << std::setw(25) << "-gpu <n>"
|
| << std::setw(12) << "Optional"
|
| << std::setw(20) << "0"
|
| << "-gpu 1\n";
|
| oss << std::left << std::setw(25) << "-outputInVidMem <n>"
|
| << std::setw(12) << "Optional"
|
| << std::setw(20) << "0"
|
| << "-outputInVidMem 1\n";
|
| oss << std::left << std::setw(25) << "-cuStreamType <n>"
|
| << std::setw(12) << "Optional"
|
| << std::setw(20) << "disabled"
|
| << "-cuStreamType 1\n";
|
|
|
|
|
| oss << "\nDetailed Descriptions:\n";
|
| oss << "-------------------\n";
|
| oss << std::left << std::setw(25) << "-i" << ": Input file path\n";
|
| oss << std::left << std::setw(25) << "-o" << ": Output file path\n";
|
| oss << std::left << std::setw(25) << "-s" << ": Input resolution in WxH format\n";
|
| oss << std::left << std::setw(25) << "-if" << ": Input format (iyuv/nv12/nv16/yuv444/p010/p210/yuv444p16/bgra/bgra10/ayuv/abgr/abgr10)\n";
|
| oss << std::left << std::setw(25) << "-gpu" << ": Ordinal of GPU to use\n";
|
| oss << std::left << std::setw(25) << "-outputInVidMem" << ": Enable output in Video Memory (0/1)\n";
|
| oss << std::left << std::setw(25) << "-cuStreamType" << ": CUDA stream type for pre/post processing (0: NULL, 1: SAME, 2: DIFFERENT)\n";
|
| oss << std::left << std::setw(25) << "-h/--help" << ": Print basic usage information\n";
|
| oss << std::left << std::setw(25) << "-A/--advanced-options" << ": Print detailed usage information\n";
|
| oss << std::left << std::setw(25) << "-ec/--encode-caps" << ": Print encode capabilities of GPU\n";
|
|
|
|
|
| oss << "\nNotes:\n";
|
| oss << "------\n";
|
| oss << "* When outputInVidMem is enabled, CRC of encoded frames will be computed\n";
|
| oss << " and dumped to file with suffix '_crc.txt' added to output file\n";
|
| oss << "* Width and height must be specified for encoding\n";
|
| oss << std::endl;
|
|
|
| oss << NvEncoderInitParam().GetHelpMessage(false, false, false, false, true, false, false, false) << std::endl;
|
| oss << "\nTo view encode capabilities, use -ec/--encode-caps\n";
|
| std::cout << oss.str();
|
| exit(0);
|
| }
|
|
|
| void ShowHelpAndExit(const char *szBadOption = NULL)
|
| {
|
| if (szBadOption)
|
| {
|
| std::ostringstream oss;
|
| oss << "Error parsing \"" << szBadOption << "\"\n";
|
| oss << "Use -h/--help for basic usage or -A/--advanced-options for detailed information\n";
|
| throw std::invalid_argument(oss.str());
|
| }
|
| }
|
|
|
| void ParseCommandLine(int argc, char *argv[], char *szInputFileName, int &nWidth, int &nHeight,
|
| NV_ENC_BUFFER_FORMAT &eFormat, char *szOutputFileName, NvEncoderInitParam &initParam, int &iGpu,
|
| bool &bOutputInVidMem, int32_t &cuStreamType)
|
| {
|
| std::ostringstream oss;
|
| int i;
|
|
|
| if (argc == 1) {
|
| std::cout << "No Arguments provided! Please refer to the following for options:\n";
|
| ShowEncoderBriefHelp();
|
| }
|
|
|
| for (i = 1; i < argc; i++)
|
| {
|
| if (!_stricmp(argv[i], "-h") || !_stricmp(argv[i], "--help")) {
|
| ShowEncoderBriefHelp();
|
| }
|
| if (!_stricmp(argv[i], "-A") || !_stricmp(argv[i], "--advanced-options")) {
|
| ShowEncoderDetailedHelp();
|
| }
|
| if (!_stricmp(argv[i], "-ec") || !_stricmp(argv[i], "--encode-caps")) {
|
| ShowEncoderCapability();
|
| }
|
| 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", "nv16", "yuv444", "p010", "p210", "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_NV16,
|
| NV_ENC_BUFFER_FORMAT_YUV444,
|
| NV_ENC_BUFFER_FORMAT_YUV420_10BIT,
|
| NV_ENC_BUFFER_FORMAT_P210,
|
| 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;
|
| }
|
| if (!_stricmp(argv[i], "-outputInVidMem"))
|
| {
|
| if (++i == argc)
|
| {
|
| ShowHelpAndExit("-outputInVidMem");
|
| }
|
| bOutputInVidMem = (atoi(argv[i]) != 0) ? true : false;
|
| continue;
|
| }
|
| if (!_stricmp(argv[i], "-cuStreamType"))
|
| {
|
| if (++i == argc)
|
| {
|
| ShowHelpAndExit("-cuStreamType");
|
| }
|
| cuStreamType = atoi(argv[i]);
|
| continue;
|
| }
|
|
|
|
|
| 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());
|
| }
|
|
|
| template<class EncoderClass>
|
| void InitializeEncoder(EncoderClass &pEnc, NvEncoderInitParam encodeCLIOptions, NV_ENC_BUFFER_FORMAT eFormat)
|
| {
|
| NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER };
|
| NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER };
|
|
|
| initializeParams.encodeConfig = &encodeConfig;
|
| pEnc->CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions.GetEncodeGUID(), encodeCLIOptions.GetPresetGUID(), encodeCLIOptions.GetTuningInfo());
|
| encodeCLIOptions.SetInitParams(&initializeParams, eFormat);
|
|
|
| pEnc->CreateEncoder(&initializeParams);
|
| }
|
|
|
| void EncodeCuda(int nWidth, int nHeight, NV_ENC_BUFFER_FORMAT eFormat, NvEncoderInitParam encodeCLIOptions, CUcontext cuContext, std::ifstream &fpIn, std::ofstream &fpOut)
|
| {
|
| std::unique_ptr<NvEncoderCuda> pEnc(new NvEncoderCuda(cuContext, nWidth, nHeight, eFormat));
|
|
|
| InitializeEncoder(pEnc, encodeCLIOptions, eFormat);
|
|
|
| int nFrameSize = pEnc->GetFrameSize();
|
|
|
| uint32_t enableMVHEVC = pEnc->IsMVHEVC();
|
| int nTotalFrameSize = nFrameSize;
|
| if (enableMVHEVC)
|
| {
|
| nTotalFrameSize = nTotalFrameSize << 1;
|
| }
|
| int viewID = 0;
|
| std::streamsize nRead = 0;
|
|
|
| std::unique_ptr<uint8_t[]> pHostFrame(new uint8_t[nTotalFrameSize]);
|
| int nFrame = 0;
|
| while (true)
|
| {
|
| if (viewID == 0)
|
| {
|
|
|
| nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nTotalFrameSize).gcount();
|
| }
|
|
|
| std::vector<NvEncOutputFrame> vPacket;
|
| if (nRead == nTotalFrameSize)
|
| {
|
| const NvEncInputFrame* encoderInputFrame = pEnc->GetNextInputFrame();
|
| NvEncoderCuda::CopyToDeviceFrame(cuContext,
|
| pHostFrame.get() + viewID * nFrameSize, 0,
|
| (CUdeviceptr)encoderInputFrame->inputPtr,
|
| (int)encoderInputFrame->pitch,
|
| pEnc->GetEncodeWidth(),
|
| pEnc->GetEncodeHeight(),
|
| CU_MEMORYTYPE_HOST,
|
| encoderInputFrame->bufferFormat,
|
| encoderInputFrame->chromaOffsets,
|
| encoderInputFrame->numChromaPlanes);
|
|
|
| pEnc->EncodeFrame(vPacket);
|
| }
|
| else
|
| {
|
|
|
| pEnc->EndEncode(vPacket);
|
| }
|
| nFrame += (int)vPacket.size();
|
| for (const NvEncOutputFrame &packet : vPacket)
|
| {
|
|
|
| fpOut.write(reinterpret_cast<const char*>(packet.frame.data()), packet.frame.size());
|
| }
|
|
|
| if (nRead != nTotalFrameSize) break;
|
| if (enableMVHEVC)
|
| {
|
| viewID = viewID ^ 1;
|
| }
|
| }
|
| pEnc->DestroyEncoder();
|
|
|
| std::cout << "Total frames encoded: " << nFrame << std::endl;
|
| }
|
|
|
| void EncodeCudaOpInVidMem(int nWidth, int nHeight, NV_ENC_BUFFER_FORMAT eFormat, NvEncoderInitParam encodeCLIOptions, CUcontext cuContext, std::ifstream &fpIn, std::ofstream &fpOut, char *outFilePath, int32_t cuStreamType)
|
| {
|
| std::unique_ptr<NvEncoderOutputInVidMemCuda> pEnc(new NvEncoderOutputInVidMemCuda(cuContext, nWidth, nHeight, eFormat));
|
|
|
| InitializeEncoder(pEnc, encodeCLIOptions, eFormat);
|
|
|
| int nFrameSize = pEnc->GetFrameSize();
|
| bool bUseCUStream = cuStreamType != -1 ? true : false;
|
|
|
| NV_ENC_INITIALIZE_PARAMS initializeParams = pEnc->GetinitializeParams();
|
| bool bWriteIVFFileHeader = true;
|
|
|
| std::unique_ptr<CRC> pCRC;
|
| std::unique_ptr<NvCUStream> pCUStream;
|
| if (bUseCUStream)
|
| {
|
|
|
| pCUStream.reset(new NvCUStream(reinterpret_cast<CUcontext>(pEnc->GetDevice()), cuStreamType, pEnc));
|
|
|
|
|
| pCRC.reset(new CRC(reinterpret_cast<CUcontext>(pEnc->GetDevice()), pEnc->GetOutputBufferSize()));
|
| }
|
|
|
|
|
| std::unique_ptr<DumpVidMemOutput> pDumpVidMemOutput(new DumpVidMemOutput(reinterpret_cast<CUcontext>(pEnc->GetDevice()), pEnc->GetOutputBufferSize(), outFilePath, bUseCUStream));
|
|
|
| std::unique_ptr<uint8_t[]> pHostFrame(new uint8_t[nFrameSize]);
|
| int nFrame = 0;
|
|
|
|
|
| while (true)
|
| {
|
|
|
| std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nFrameSize).gcount();
|
|
|
| std::vector<NV_ENC_OUTPUT_PTR>pVideoMemBfr;
|
|
|
| if (nRead == nFrameSize)
|
| {
|
| const NvEncInputFrame* encoderInputFrame = pEnc->GetNextInputFrame();
|
| NvEncoderCuda::CopyToDeviceFrame(cuContext, pHostFrame.get(), 0, (CUdeviceptr)encoderInputFrame->inputPtr,
|
| (int)encoderInputFrame->pitch,
|
| pEnc->GetEncodeWidth(),
|
| pEnc->GetEncodeHeight(),
|
| CU_MEMORYTYPE_HOST,
|
| encoderInputFrame->bufferFormat,
|
| encoderInputFrame->chromaOffsets,
|
| encoderInputFrame->numChromaPlanes,
|
| false,
|
| bUseCUStream ? pCUStream->GetInputCUStream() : NULL);
|
|
|
| pEnc->EncodeFrame(pVideoMemBfr);
|
| }
|
| else
|
| {
|
| pEnc->EndEncode(pVideoMemBfr);
|
| }
|
|
|
| for (uint32_t i = 0; i < pVideoMemBfr.size(); ++i)
|
| {
|
| if (bUseCUStream)
|
| {
|
|
|
| pCRC->GetCRC(pVideoMemBfr[i], pCUStream->GetOutputCUStream());
|
| }
|
|
|
| pDumpVidMemOutput->DumpOutputToFile((CUdeviceptr)(pVideoMemBfr[i]), bUseCUStream ? pCRC->GetCRCVidMemPtr() : 0, fpOut, nFrame, bWriteIVFFileHeader, initializeParams);
|
|
|
| nFrame++;
|
| }
|
|
|
| if (nRead != nFrameSize) break;
|
| }
|
|
|
| pEnc->DestroyEncoder();
|
|
|
| std::cout << "Total frames encoded: " << nFrame << std::endl;
|
| }
|
|
|
| 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;
|
| int cuStreamType = -1;
|
| bool bOutputInVideoMem = false;
|
| ParseCommandLine(argc, argv, szInFilePath, nWidth, nHeight, eFormat, szOutFilePath, encodeCLIOptions, iGpu,
|
| bOutputInVideoMem, cuStreamType);
|
|
|
| CheckInputFile(szInFilePath);
|
| ValidateResolution(nWidth, nHeight);
|
|
|
| if (!*szOutFilePath)
|
| {
|
| sprintf(szOutFilePath, encodeCLIOptions.IsCodecH264() ? "out.h264" : encodeCLIOptions.IsCodecHEVC() ? "out.hevc" : "out.av1");
|
| }
|
|
|
| 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(NVCODEC_CUDA_CTX_CREATE(&cuContext, 0, cuDevice));
|
|
|
|
|
| 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);
|
| if (!fpOut)
|
| {
|
| std::ostringstream err;
|
| err << "Unable to open output file: " << szOutFilePath << std::endl;
|
| throw std::invalid_argument(err.str());
|
| }
|
|
|
|
|
| if (bOutputInVideoMem)
|
| {
|
| EncodeCudaOpInVidMem(nWidth, nHeight, eFormat, encodeCLIOptions, cuContext, fpIn, fpOut, szOutFilePath, cuStreamType);
|
| }
|
| else
|
| {
|
| EncodeCuda(nWidth, nHeight, eFormat, encodeCLIOptions, cuContext, fpIn, fpOut);
|
| }
|
|
|
| fpOut.close();
|
| fpIn.close();
|
|
|
| std::cout << "Bitstream saved in file " << szOutFilePath << std::endl;
|
| }
|
| catch (const std::exception &ex)
|
| {
|
| std::cout << ex.what();
|
| return 1;
|
| }
|
| return 0;
|
| }
|
|
|