File size: 3,949 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
165
166
/*

 * SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.

 * SPDX-License-Identifier: LicenseRef-NvidiaProprietary

 *

 * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual

 * property and proprietary rights in and to this material, related

 * documentation and any modifications thereto. Any use, reproduction,

 * disclosure or distribution of this material and related documentation

 * without an express license agreement from NVIDIA CORPORATION or

 * its affiliates is strictly prohibited.

 */

#include <stdio.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <cuda.h>
#include <atomic>
#include <memory>
#include "NvEncoder/NvEncoderCuda.h"
#include "../Utils/NvEncoderCLIOptions.h"
#include "../Utils/NvCodecUtils.h"
#include <condition_variable>
#include <mutex>

#define bufSize 2

template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
	return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

class NvCUStream

{
public:
	NvCUStream(CUcontext cuDevice, int cuStreamType, std::unique_ptr<NvEncoderCuda> &pEnc)
	{
		device = cuDevice;
		CUDA_DRVAPI_CALL(cuCtxPushCurrent(device));
		// Create CUDA streams
		if (cuStreamType == 1)
		{
			ck(cuStreamCreate(&inputStream, CU_STREAM_DEFAULT));
			outputStream = inputStream;
		}
		else if (cuStreamType == 2)
		{
			ck(cuStreamCreate(&inputStream, CU_STREAM_DEFAULT));
			ck(cuStreamCreate(&outputStream, CU_STREAM_DEFAULT));
		}
		CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL));
		// Set input and output CUDA streams in driver
		pEnc->SetIOCudaStreams((NV_ENC_CUSTREAM_PTR)&inputStream, (NV_ENC_CUSTREAM_PTR)&outputStream);
	}

	~NvCUStream()
	{
		ck(cuCtxPushCurrent(device));
		if (inputStream == outputStream)
		{
			if (inputStream != NULL)
				ck(cuStreamDestroy(inputStream));
		}
		else
		{
			if (inputStream != NULL)
				ck(cuStreamDestroy(inputStream));

			if (outputStream != NULL)
				ck(cuStreamDestroy(outputStream));
		}
		ck(cuCtxPopCurrent(NULL));
	}

	CUstream GetOutputCUStream() { return outputStream; };
	CUstream GetInputCUStream() { return inputStream; };

private:
	CUcontext device;
	CUstream inputStream = NULL, outputStream = NULL;
};

struct EncodedFrameData {
	uint8_t* data;
	uint32_t size;
	uint32_t offset;
};

struct safeBuffer {
	std::atomic<bool> readyToEdit; // true = can EDIT content / false = can READ content
	std::condition_variable condVarReady;
	std::mutex mutex;
	uint8_t* data;
};

struct IOEncoderMem

{
	safeBuffer hostInBuf[bufSize];
	safeBuffer hostOutBuf;
	std::vector<EncodedFrameData> hostEncodedData;

	~IOEncoderMem() {
		for (int inBuf = 0; inBuf < bufSize; inBuf++){
			if (hostInBuf[inBuf].data) {
				cuMemFreeHost(hostInBuf[inBuf].data);
				hostInBuf[inBuf].data = nullptr;
			}
		}
		if (hostOutBuf.data) {
			cuMemFreeHost(hostOutBuf.data);
			hostOutBuf.data = nullptr;
		}
	}
};

struct ThreadData

{
	std::unique_ptr<NvEncoderCuda> encSession;
	std::unique_ptr<NvCUStream> cuStream;
	CUcontext *cuContext;

	~ThreadData() {
		cuStream.release();
		encSession.release();
	};
};

struct fileReadData

{
	ThreadData* threadData;
	IOEncoderMem* ioVideoMem;
	uint32_t vidPortionNum;
	uint32_t vidThreadIdx;
	uint32_t numFrames;
	bool isLast;
	bool isSingleThread;
	uint64_t videoSize;
	uint64_t offset;
	char* filePath;
};

struct encodeData

{
	ThreadData* threadData;
	IOEncoderMem* ioVideoMem;
	uint32_t vidPortionNum;
	uint32_t vidThreadIdx;
	uint32_t numFrames;
	bool isLast;
	bool isSingleThread;
	uint64_t videoSize;
	uint64_t offset;
	char* filePath;
};

struct fileWriteData

{
	std::ofstream* fpOut;
	IOEncoderMem* ioVideoMem;
	uint32_t vidPortionNum;
	uint32_t vidThreadIdx;
	char *outPath;
    bool isFirst;
	bool isLast;
    bool isAV1;
};