File size: 4,830 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
/*

* 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 AppDecMem.cpp
//! \brief Source file for AppDecMem sample
//!
//! This sample application is similar to AppDec. It illustrates how to demux and decode media content from memory buffer.
//! It allocates AVIOContext explicitely and also defines method to read data packets from input file.
//! For simplicity, this application reads the input stream and stores it in a buffer before invoking the demuxer.
//---------------------------------------------------------------------------

#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();
    }
    // Fill in the buffer owned by the demuxer/decoder
    int GetData(uint8_t *pBuf, int nBuf) {
        if (fpIn.eof())
        {
            return AVERROR_EOF;
        }

        // We read a file for this example. You may get your data from network or somewhere else
        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);
        // Instead of passing in a media file path, here we pass in a DataProvider, which reads from the file.
        // Note that the data is passed into the demuxer chunk-by-chunk sequentially. If the meta data is at the end of the file
        // (as for MP4) and the buffer isn't large enough to hold the whole file, the file may never get demuxed.
        // Allocated buffer size is 32 MB. Please increase buffer size (avioc_buffer_size) in CreateFormatContext(DataProvider *pDataProvider)
        // if this limitation is faced.
        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;
}