| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <vector>
|
| #include <cuda.h>
|
| #include "NvDecoder/NvDecoder.h"
|
| #include "../Utils/NvCodecUtils.h"
|
| #include "../Utils/FFmpegDemuxer.h"
|
| #include "../Common/AppDecUtils.h"
|
|
|
| simplelogger::Logger *logger = simplelogger::LoggerFactory::CreateConsoleLogger();
|
|
|
| int main(int argc, char *argv[])
|
| {
|
| char szInFilePath[256] = "", szOutFilePath[256] = "out.yuv";
|
| int iGpu = 0;
|
| bool bVerbose = false;
|
|
|
|
|
| bool force_zero_latency = false;
|
| try
|
| {
|
| ParseCommandLine(argc, argv, szInFilePath, szOutFilePath, iGpu, &bVerbose, NULL, &force_zero_latency);
|
| CheckInputFile(szInFilePath);
|
|
|
| ck(cuInit(0));
|
| int nGpu = 0;
|
| ck(cuDeviceGetCount(&nGpu));
|
| if (iGpu < 0 || iGpu >= nGpu)
|
| {
|
| std::ostringstream err;
|
| err << "GPU ordinal out of range. Should be within [" << 0 << ", " << nGpu - 1 << "]" << std::endl;
|
| throw std::invalid_argument(err.str());
|
| }
|
|
|
| CUcontext cuContext = NULL;
|
| createCudaContext(&cuContext, iGpu, 0);
|
|
|
| FFmpegDemuxer demuxer(szInFilePath);
|
|
|
|
|
|
|
| NvDecoder dec(cuContext, false, FFmpeg2NvCodecId(demuxer.GetVideoCodec()), true, false, NULL, NULL, false, 0, 0, 1000, force_zero_latency);
|
|
|
| int nFrame = 0;
|
| uint8_t *pVideo = NULL;
|
| int nVideoBytes = 0;
|
| 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());
|
| }
|
|
|
| int n = 0;
|
| bool bOneInOneOut = true;
|
| int nFrameReturned = 0;
|
| int64_t timestamp = 0;
|
| do {
|
| demuxer.Demux(&pVideo, &nVideoBytes);
|
|
|
| nFrameReturned = dec.Decode(pVideo, nVideoBytes, CUVID_PKT_ENDOFPICTURE, n++);
|
| if (!nFrame && nFrameReturned)
|
| LOG(INFO) << dec.GetVideoInfo();
|
|
|
| nFrame += nFrameReturned;
|
|
|
| if (bVerbose)
|
| {
|
| std::cout << "Decode: nVideoBytes=" << nVideoBytes << ", nFrameReturned=" << nFrameReturned << ", total=" << nFrame << std::endl;
|
| }
|
| if (nVideoBytes && nFrameReturned != 1)
|
| {
|
| bOneInOneOut = false;
|
| }
|
| for (int i = 0; i < nFrameReturned; i++)
|
| {
|
| fpOut.write(reinterpret_cast<char*>(dec.GetFrame(×tamp)), dec.GetFrameSize());
|
| if (bVerbose)
|
| {
|
| std::cout << "Timestamp: " << timestamp << std::endl;
|
| }
|
| }
|
| } while (nVideoBytes);
|
|
|
| fpOut.close();
|
| std::cout << "One packet in and one frame out: " << (bOneInOneOut ? "true" : "false") << std::endl;
|
| }
|
| catch(const std::exception& ex)
|
| {
|
| std::cout << ex.what();
|
| exit(1);
|
| }
|
| return 0;
|
| }
|
|
|