File size: 6,893 Bytes
ea55f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*

* Copyright 2017-2024 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.

*

*/

//---------------------------------------------------------------------------
//! \file AppDecD3D.cpp
//! \brief Source file for AppDecD3D sample
//!
//! This sample application illustrates the decoding of media file and display of decoded frames in a window.
//! This is done by CUDA interop with D3D(both D3D9 and D3D11).
//! For a detailed list of supported codecs on your NVIDIA GPU, refer : https://developer.nvidia.com/nvidia-video-codec-sdk#NVDECFeatures


#include <cuda.h>
#include <iostream>
#include "NvDecoder/NvDecoder.h"
#include "../Utils/NvCodecUtils.h"
#include "../Utils/FFmpegDemuxer.h"
#include "FramePresenterD3D9.h"
#include "FramePresenterD3D11.h"
#include "../Common/AppDecUtils.h"
#include "../Utils/ColorSpace.h"

simplelogger::Logger *logger = simplelogger::LoggerFactory::CreateConsoleLogger();

/**

*   @brief Function template to decode media file pointed to by szInFilePath parameter.

           The decoded frames are displayed by using the D3D-CUDA interop.

           In this app FramePresenterType is either FramePresenterD3D9 or FramePresenterD3D11.

           The presentation rate is based on per frame time stamp.

*   @param  cuContext - Handle to CUDA context

*   @param  szInFilePath - Path to file to be decoded

*   @return 0 on success

*/
template<class FramePresenterType, typename = std::enable_if<std::is_base_of<FramePresenterD3D, FramePresenterType>::value>>
int NvDecD3D(CUcontext cuContext, char *szInFilePath)
{
    unsigned int timescale = 1000; // get timestamp in milisecond
    FFmpegDemuxer demuxer(szInFilePath, timescale);
    NvDecoder dec(cuContext, true, FFmpeg2NvCodecId(demuxer.GetVideoCodec()), false, false, NULL, NULL, false, 0, 0, timescale);
    int nRGBWidth = (demuxer.GetWidth() + 1) & ~1;
    FramePresenterType presenter(cuContext, nRGBWidth, demuxer.GetHeight());
    CUdeviceptr dpFrame = 0;
    ck(cuMemAlloc(&dpFrame, nRGBWidth * demuxer.GetHeight() * 4));
    int nVideoBytes = 0, nFrameReturned = 0, nFrame = 0;
    uint8_t *pVideo = NULL, *pFrame;
    int64_t pts, timestamp = 0;
    bool m_bFirstFrame = true;
    int64_t firstPts = 0, startTime = 0;
    LARGE_INTEGER m_Freq;
    int iMatrix = 0;

    QueryPerformanceFrequency(&m_Freq);

    do
    {
        demuxer.Demux(&pVideo, &nVideoBytes, &pts);
        nFrameReturned = dec.Decode(pVideo, nVideoBytes, 0, pts);
        if (!nFrame && nFrameReturned)
            LOG(INFO) << dec.GetVideoInfo();

        for (int i = 0; i < nFrameReturned; i++)
        {
            pFrame = dec.GetFrame(&timestamp);
            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, 4 * nRGBWidth, dec.GetWidth(), dec.GetHeight(), iMatrix);
                else if (dec.GetOutputFormat() == cudaVideoSurfaceFormat_NV12)
                    Nv12ToColor32<BGRA32>(pFrame, dec.GetWidth(), (uint8_t *)dpFrame, 4 * nRGBWidth, dec.GetWidth(), dec.GetHeight(), iMatrix);
                else
                    Nv16ToColor32<BGRA32>(pFrame, dec.GetWidth(), (uint8_t *)dpFrame, 4 * nRGBWidth, dec.GetWidth(), dec.GetHeight(), iMatrix);
            }
            else
            {
                if(dec.GetOutputFormat() == cudaVideoSurfaceFormat_YUV444_16Bit)
                    YUV444P16ToColor32<BGRA32>(pFrame, 2 * dec.GetWidth(), (uint8_t *)dpFrame, 4 * nRGBWidth, dec.GetWidth(), dec.GetHeight(), iMatrix);
                else if (dec.GetOutputFormat() == cudaVideoSurfaceFormat_P016)
                    P016ToColor32<BGRA32>(pFrame, 2 * dec.GetWidth(), (uint8_t *)dpFrame, 4 * nRGBWidth, dec.GetWidth(), dec.GetHeight(), iMatrix);
                else
                    P216ToColor32<BGRA32>(pFrame, 2 * dec.GetWidth(), (uint8_t *)dpFrame, 4 * nRGBWidth, dec.GetWidth(), dec.GetHeight(), iMatrix);
            }

            LARGE_INTEGER counter;
            if (m_bFirstFrame)
            {
                firstPts = timestamp;
                QueryPerformanceCounter(&counter);
                startTime = 1000 * counter.QuadPart / m_Freq.QuadPart;
                m_bFirstFrame = false;
            }

            QueryPerformanceCounter(&counter);
            int64_t curTime = timescale * counter.QuadPart / m_Freq.QuadPart;

            int64_t expectedRenderTime = timestamp - firstPts + startTime;
            int64_t delay = expectedRenderTime - curTime;
            if (timestamp == 0)
                delay = 0;
            if (delay < 0)
                continue;

            presenter.PresentDeviceFrame((uint8_t *)dpFrame, nRGBWidth * 4, delay);
        }
        nFrame += nFrameReturned;
    } while (nVideoBytes);
    ck(cuMemFree(dpFrame));
    std::cout << "Total frame decoded: " << nFrame << std::endl;
    return 0;
}

int main(int argc, char **argv) 

{
    char szInFilePath[256] = "";
    int iGpu = 0;
    int iD3d = 0;
    try
    {
        ParseCommandLine(argc, argv, szInFilePath, NULL, iGpu, NULL, &iD3d);
        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());
        }
        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, CU_CTX_SCHED_BLOCKING_SYNC, cuDevice));

        switch (iD3d) {
        default:
        case 9:
            std::cout << "Display with D3D9." << std::endl;
            return NvDecD3D<FramePresenterD3D9>(cuContext, szInFilePath);
        case 11:
            std::cout << "Display with D3D11." << std::endl;
            return NvDecD3D<FramePresenterD3D11>(cuContext, szInFilePath);
        }
    }
    catch (const std::exception& ex)
    {
        std::cout << ex.what();
        exit(1);
    }
    return 0;
}