File size: 7,490 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | /*
* This copyright notice applies to this header file only:
*
* Copyright (c) 2010-2024 NVIDIA Corporation
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the software, and to permit persons to whom the
* software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <vector>
#include <stdint.h>
#include <mutex>
#include <unordered_map>
#include <d3d12.h>
#include "NvEncoder.h"
#include <wrl.h>
#define ALIGN_UP(s,a) (((s) + (a) - 1) & ~((a) - 1))
using Microsoft::WRL::ComPtr;
class NvEncoderD3D12 : public NvEncoder
{
public:
/**
* @brief NvEncoderD3D12 class constructor.
*/
NvEncoderD3D12(ID3D12Device* pD3D12Device, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat,
uint32_t nExtraOutputDelay = 3);
/**
* @brief NvEncoderD3D12 class virtual destructor.
*/
virtual ~NvEncoderD3D12();
/**
* @brief This function is used to initialize the encoder session.
* Application must call this function to initialize the encoder, before
* starting to encode any frames.
*/
void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) override;
/**
* @brief This function is used to get the input buffer size, needed
* to allocate upload buffer.
*/
uint32_t GetInputSize();
/**
* @brief This function is used to get the fence corresponding to
* input buffers.
*/
ID3D12Fence* GetInpFence();
/**
* @brief This function is used to get the pointer to fence value corresponding to
* input buffers.
*/
uint64_t* GetInpFenceValPtr();
/**
* @brief This function is used to do CPU wait on fence and corresponding fence value.
*/
void CPUWaitForFencePoint(ID3D12Fence* pFence, uint64_t nFenceValue);
/**
* @brief This function is used to get the total number of input buffers.
*/
uint32_t GetNumBfrs() { return m_nEncoderBuffer;};
/**
* @brief This function is used to encode a frame.
* Applications must call EncodeFrame() function to encode the uncompressed
* data, which has been copied to an input buffer obtained from the
* GetNextInputFrame() function.
*/
void EncodeFrame(std::vector<NvEncOutputFrame>& vPacket, NV_ENC_PIC_PARAMS* pPicParams = nullptr) override;
/**
* @brief This function to flush the encoder queue.
* The encoder might be queuing frames for B picture encoding or lookahead;
* the application must call EndEncode() to get all the queued encoded frames
* from the encoder. The application must call this function before destroying
* an encoder session. Video memory buffer pointer containing compressed data
* is returned in pOutputBuffer.
*/
void EndEncode(std::vector<NvEncOutputFrame> &vPacket) override;
/**
* @brief This function is used to destroy the encoder session.
* Application must call this function to destroy the encoder session and
* clean up any allocated resources. The application must call EndEncode()
* function to get any queued encoded frames before calling DestroyEncoder().
*/
void DestroyEncoder() override;
protected:
/**
* @brief This function is used to release the input buffers allocated for encoding.
* This function is an override of virtual function NvEncoder::ReleaseInputBuffers().
*/
virtual void ReleaseInputBuffers() override;
private:
/**
* @brief This function is used to allocate input buffers for encoding.
* This function is an override of virtual function NvEncoder::AllocateInputBuffers().
* This function creates ID3D12Resource which is used to accept input data.
* To obtain handle to input buffers application must call NvEncoder::GetNextInputFrame()
*/
virtual void AllocateInputBuffers(int32_t numInputBuffers) override;
/**
* @brief This function is used to allocate output buffers for storing encode output.
*/
void AllocateOutputBuffers(uint32_t numOutputBuffers);
/**
* @brief This function is used to release output buffers.
*/
void ReleaseOutputBuffers();
/**
* @brief This function is used to map the input and output buffers to NvEncodeAPI.
*/
void MapResources(uint32_t bfrIdx);
/**
* @brief This function is used to register output buffers with NvEncodeAPI.
*/
void RegisterOutputResources(uint32_t bfrSize);
/**
* @brief This function is used to unregister output resources which had been previously registered for encoding
* using RegisterOutputResources() function.
*/
void UnregisterOutputResources();
/**
* @brief This function is used to get the size of output buffer required to be
* allocated in order to store the output.
*/
uint32_t GetOutputBufferSize();
/**
* @brief This function is used to register input buffers with NvEncodeAPI.
*/
void RegisterInputResources(int width, int height, NV_ENC_BUFFER_FORMAT bufferFormat);
/**
* @brief This is a private function which is used to get video memory buffer pointer containing compressed data
* from the encoder HW. This is called by EncodeFrame() function. If there is buffering enabled, this may return
* without any output data.
*/
void GetEncodedPacket(std::vector<NV_ENC_OUTPUT_RESOURCE_D3D12*>& vOutputBuffer, std::vector<NvEncOutputFrame>& vPacket, bool bOutputDelay);
/**
* @brief This is a private function to release ID3D12Texture2D textures used for encoding.
*/
void ReleaseD3D12Resources();
/**
* @brief This function is used to flush the encoder queue.
*/
void FlushEncoder();
protected:
std::vector<NV_ENC_INPUT_RESOURCE_D3D12*> m_vInputRsrc;
std::vector<NV_ENC_OUTPUT_RESOURCE_D3D12*> m_vOutputRsrc;
ComPtr<ID3D12Fence> m_pInputFence;
ComPtr<ID3D12Fence> m_pOutputFence;
uint64_t m_nInputFenceVal;
uint64_t m_nOutputFenceVal;
HANDLE m_event;
std::vector<ComPtr<ID3D12Resource>> m_vInputBuffers;
std::vector<ComPtr<ID3D12Resource>> m_vOutputBuffers;
std::vector<NV_ENC_REGISTERED_PTR> m_vRegisteredResourcesOutputBuffer;
std::vector<NV_ENC_OUTPUT_PTR> m_vMappedOutputBuffers;
ID3D12Device *m_pD3D12Device = nullptr;
};
|