| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #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();
|
|
|
| class FileDataProvider : public FFmpegDemuxer::DataProvider {
|
| public:
|
| FileDataProvider(const char *szInFilePath) {
|
| fpIn.open(szInFilePath, std::ifstream::in | std::ifstream::binary);
|
| if (!fpIn)
|
| {
|
| std::cout << "Unable to open input file: " << szInFilePath << std::endl;
|
| return;
|
| }
|
| }
|
| ~FileDataProvider() {
|
| fpIn.close();
|
| }
|
|
|
| int GetData(uint8_t *pBuf, int nBuf) {
|
| if (fpIn.eof())
|
| {
|
| return AVERROR_EOF;
|
| }
|
|
|
|
|
| return (int)fpIn.read(reinterpret_cast<char*>(pBuf), nBuf).gcount();
|
| }
|
|
|
| private:
|
| std::ifstream fpIn;
|
| };
|
|
|
| int main(int argc, char *argv[])
|
| {
|
| char szInFilePath[256] = "", szOutFilePath[256] = "out.yuv";
|
| int iGpu = 0;
|
| try
|
| {
|
| ParseCommandLine(argc, argv, szInFilePath, szOutFilePath, iGpu);
|
| 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);
|
|
|
| FileDataProvider dp(szInFilePath);
|
|
|
|
|
|
|
|
|
|
|
| FFmpegDemuxer demuxer(&dp);
|
| NvDecoder dec(cuContext, false, FFmpeg2NvCodecId(demuxer.GetVideoCodec()));
|
|
|
| int nFrame = 0;
|
| uint8_t *pVideo = NULL;
|
| int nVideoBytes = 0;
|
| uint8_t *pFrame;
|
| int nFrameReturned = 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());
|
| }
|
| do
|
| {
|
| demuxer.Demux(&pVideo, &nVideoBytes);
|
| nFrameReturned = dec.Decode(pVideo, nVideoBytes);
|
| if (!nFrame && nFrameReturned)
|
| LOG(INFO) << dec.GetVideoInfo();
|
|
|
| nFrame += nFrameReturned;
|
| for (int i = 0; i < nFrameReturned; i++) {
|
| pFrame = dec.GetFrame();
|
| fpOut.write(reinterpret_cast<char*>(pFrame), dec.GetFrameSize());
|
| }
|
| } while (nVideoBytes);
|
| fpOut.close();
|
| const char *aszDecodeOutFormat[] = { "NV12", "P016", "YUV444", "YUV444P16", "NV16", "P216"};
|
| std::cout << "Total frame decoded: " << nFrame << std::endl << "Saved in file " << szOutFilePath << " in format " << aszDecodeOutFormat[dec.GetOutputFormat()] << std::endl;
|
| }
|
| catch (const std::exception& ex)
|
| {
|
| std::cout << ex.what();
|
| exit(1);
|
| }
|
| return 0;
|
| }
|
|
|