| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <cuda.h>
|
| #include <iostream>
|
| #include "NvDecoder/NvDecoder.h"
|
| #include "../Utils/NvCodecUtils.h"
|
| #include "../Utils/FFmpegDemuxer.h"
|
| #include "../Common/AppDecUtils.h"
|
| #include "../Utils/ColorSpace.h"
|
|
|
| #include "FramePresenter.h"
|
| #include "FramePresenterGLUT.h"
|
|
|
| #if !defined (_WIN32)
|
| #include "FramePresenterGLX.h"
|
| #endif
|
|
|
| simplelogger::Logger *logger = simplelogger::LoggerFactory::CreateConsoleLogger();
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| int Decode(CUcontext cuContext, char *szInFilePath) {
|
|
|
| FFmpegDemuxer demuxer(szInFilePath);
|
| NvDecoder dec(cuContext, true, FFmpeg2NvCodecId(demuxer.GetVideoCodec()));
|
|
|
|
|
| int nWidth = (demuxer.GetWidth() + 1) & ~1;
|
| int nPitch = nWidth * 4;
|
|
|
| #if defined (_WIN32)
|
| FramePresenterGLUT gInstance(cuContext, nWidth, demuxer.GetHeight());
|
| #else
|
| FramePresenterGLX gInstance(nWidth, demuxer.GetHeight());
|
| #endif
|
|
|
| int &nFrame = gInstance.nFrame;
|
|
|
|
|
| if (!gInstance.isVendorNvidia()) {
|
| std::cout<<"\nFailed to find NVIDIA libraries\n";
|
| return 0;
|
| }
|
|
|
| CUdeviceptr dpFrame;
|
| int nVideoBytes = 0, nFrameReturned = 0, iMatrix = 0;
|
| uint8_t *pVideo = NULL;
|
| uint8_t *pFrame;
|
| do {
|
| demuxer.Demux(&pVideo, &nVideoBytes);
|
| nFrameReturned = dec.Decode(pVideo, nVideoBytes);
|
| if (!nFrame && nFrameReturned)
|
| LOG(INFO) << dec.GetVideoInfo();
|
|
|
| for (int i = 0; i < nFrameReturned; i++) {
|
| pFrame = dec.GetFrame();
|
| gInstance.GetDeviceFrameBuffer(&dpFrame, &nPitch);
|
| iMatrix = dec.GetVideoFormatInfo().video_signal_description.matrix_coefficients;
|
|
|
| if (dec.GetBitDepth() == 8) {
|
| if (dec.GetOutputFormat() == cudaVideoSurfaceFormat_YUV444)
|
| YUV444ToColor32<BGRA32>(pFrame, dec.GetWidth(), (uint8_t *)dpFrame, nPitch, dec.GetWidth(), dec.GetHeight(), iMatrix);
|
| else if (dec.GetOutputFormat() == cudaVideoSurfaceFormat_NV12)
|
| Nv12ToColor32<BGRA32>(pFrame, dec.GetWidth(), (uint8_t *)dpFrame, nPitch, dec.GetWidth(), dec.GetHeight(), iMatrix);
|
| else
|
| Nv16ToColor32<BGRA32>(pFrame, dec.GetWidth(), (uint8_t *)dpFrame, nPitch, dec.GetWidth(), dec.GetHeight(), iMatrix);
|
| }
|
| else {
|
| if (dec.GetOutputFormat() == cudaVideoSurfaceFormat_YUV444_16Bit)
|
| YUV444P16ToColor32<BGRA32>(pFrame, 2 * dec.GetWidth(), (uint8_t *)dpFrame, nPitch, dec.GetWidth(), dec.GetHeight(), iMatrix);
|
| else if (dec.GetOutputFormat() == cudaVideoSurfaceFormat_P016)
|
| P016ToColor32<BGRA32>(pFrame, 2 * dec.GetWidth(), (uint8_t *)dpFrame, nPitch, dec.GetWidth(), dec.GetHeight(), iMatrix);
|
| else
|
| P216ToColor32<BGRA32>(pFrame, 2 * dec.GetWidth(), (uint8_t *)dpFrame, nPitch, dec.GetWidth(), dec.GetHeight(), iMatrix);
|
| }
|
|
|
| gInstance.ReleaseDeviceFrameBuffer();
|
|
|
| }
|
| nFrame += nFrameReturned;
|
| } while (nVideoBytes);
|
|
|
| std::cout << "Total frame decoded: " << nFrame << std::endl;
|
| return 1;
|
| }
|
|
|
| int main(int argc, char **argv)
|
| {
|
| char szInFilePath[256] = "";
|
| int iGpu = 0;
|
| try
|
| {
|
| ParseCommandLine(argc, argv, szInFilePath, NULL, iGpu, NULL, NULL);
|
| 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, CU_CTX_SCHED_BLOCKING_SYNC);
|
|
|
| std::cout << "Decode with NvDecoder." << std::endl;
|
| Decode(cuContext, szInFilePath);
|
|
|
| ck(cuCtxDestroy(cuContext));
|
| }
|
| catch(const std::exception& ex)
|
| {
|
| std::cout << ex.what();
|
| exit(1);
|
| }
|
| return 0;
|
| }
|
|
|