This commit is contained in:
mofeng-git
2025-12-28 18:19:16 +08:00
commit d143d158e4
771 changed files with 220548 additions and 0 deletions

View File

@@ -0,0 +1,147 @@
#ifndef DDA_H
#define DDA_H
#include "DDAImpl.h"
#include "Defs.h"
class DemoApplication {
/// Demo Application Core class
#define returnIfError(x) \
if (FAILED(x)) { \
printf(__FUNCTION__ ": Line %d, File %s Returning error 0x%08x\n", \
__LINE__, __FILE__, x); \
return x; \
}
private:
IDXGIFactory1 *factory1_ = nullptr;
IDXGIAdapter1 *adapter1_ = nullptr;
IDXGIAdapter *adapter_ = nullptr;
/// DDA wrapper object, defined in DDAImpl.h
DDAImpl *pDDAWrapper = nullptr;
/// D3D11 device context used for the operations demonstrated in this
/// application
ID3D11Device *pD3DDev = nullptr;
/// D3D11 device context
ID3D11DeviceContext *pCtx = nullptr;
/// D3D11 RGB Texture2D object that recieves the captured image from DDA
ID3D11Texture2D *pDupTex2D = nullptr;
/// D3D11 YUV420 Texture2D object that sends the image to NVENC for video
/// encoding
ID3D11Texture2D *pEncBuf = nullptr;
ID3D10Multithread *hmt = NULL;
int64_t m_luid = 0;
private:
/// Initialize DXGI pipeline
HRESULT InitDXGI() {
HRESULT hr = S_OK;
hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void **)&factory1_);
if (FAILED(hr)) {
return hr;
}
UINT i = 0;
while (!FAILED(factory1_->EnumAdapters1(i, &adapter1_))) {
i++;
DXGI_ADAPTER_DESC1 desc = DXGI_ADAPTER_DESC1();
adapter1_->GetDesc1(&desc);
if ((((int64_t)desc.AdapterLuid.HighPart << 32) |
desc.AdapterLuid.LowPart) == m_luid) {
break;
}
SAFE_RELEASE(adapter1_);
}
if (!adapter1_) {
return S_FALSE;
}
hr = adapter1_->QueryInterface(__uuidof(IDXGIAdapter), (void **)&adapter_);
if (FAILED(hr)) {
return hr;
}
/// Feature levels supported
D3D_FEATURE_LEVEL FeatureLevels[] = {D3D_FEATURE_LEVEL_11_0};
UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
D3D_FEATURE_LEVEL FeatureLevel = D3D_FEATURE_LEVEL_11_0;
/// Create device
hr = D3D11CreateDevice(adapter1_, D3D_DRIVER_TYPE_UNKNOWN, nullptr,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT |
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
FeatureLevels, NumFeatureLevels, D3D11_SDK_VERSION,
&pD3DDev, &FeatureLevel, &pCtx);
if (SUCCEEDED(hr)) {
// Device creation succeeded, no need to loop anymore
hr = pCtx->QueryInterface(IID_PPV_ARGS(&hmt));
if (SUCCEEDED(hr)) {
hr = hmt->SetMultithreadProtected(TRUE);
}
}
return hr;
}
/// Initialize DDA handler
HRESULT InitDup() {
HRESULT hr = S_OK;
if (!pDDAWrapper) {
pDDAWrapper = new DDAImpl(pD3DDev, pCtx);
hr = pDDAWrapper->Init();
returnIfError(hr);
}
return hr;
}
public:
HRESULT Init() {
HRESULT hr = S_OK;
hr = InitDXGI();
returnIfError(hr);
hr = InitDup();
returnIfError(hr);
return hr;
}
ID3D11Device *Device() { return pD3DDev; }
int width() { return pDDAWrapper->getWidth(); }
int height() { return pDDAWrapper->getHeight(); }
/// Capture a frame using DDA
ID3D11Texture2D *Capture(int wait) {
HRESULT hr = pDDAWrapper->GetCapturedFrame(&pDupTex2D,
wait); // Release after preproc
if (FAILED(hr)) {
return NULL;
}
return pDupTex2D;
}
/// Release all resources
void Cleanup(bool bDelete = true) {
if (pDDAWrapper) {
pDDAWrapper->Cleanup();
delete pDDAWrapper;
pDDAWrapper = nullptr;
}
SAFE_RELEASE(pDupTex2D);
if (bDelete) {
SAFE_RELEASE(factory1_);
SAFE_RELEASE(adapter_);
SAFE_RELEASE(adapter1_);
SAFE_RELEASE(pD3DDev);
SAFE_RELEASE(pCtx);
SAFE_RELEASE(hmt)
}
}
DemoApplication(int64_t luid) { m_luid = luid; }
~DemoApplication() { Cleanup(true); }
};
#endif // DDA_H

View File

@@ -0,0 +1,170 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Defs.h"
#include "DDAImpl.h"
#include <iomanip>
/// Initialize DDA
HRESULT DDAImpl::Init()
{
IDXGIOutput * pOutput = nullptr;
IDXGIDevice2* pDevice = nullptr;
IDXGIFactory1* pFactory = nullptr;
IDXGIAdapter *pAdapter = nullptr;
IDXGIOutput1* pOut1 = nullptr;
/// Release all temporary refs before exit
#define CLEAN_RETURN(x) \
SAFE_RELEASE(pDevice);\
SAFE_RELEASE(pFactory);\
SAFE_RELEASE(pOutput);\
SAFE_RELEASE(pOut1);\
SAFE_RELEASE(pAdapter);\
return x;
HRESULT hr = S_OK;
/// To create a DDA object given a D3D11 device, we must first get to the DXGI Adapter associated with that device
if (FAILED(hr = pD3DDev->QueryInterface(__uuidof(IDXGIDevice2), (void**)&pDevice)))
{
CLEAN_RETURN(hr);
}
if (FAILED(hr = pDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&pAdapter)))
{
CLEAN_RETURN(hr);
}
/// Once we have the DXGI Adapter, we enumerate the attached display outputs, and select which one we want to capture
/// This sample application always captures the primary display output, enumerated at index 0.
if (FAILED(hr = pAdapter->EnumOutputs(0, &pOutput)))
{
CLEAN_RETURN(hr);
}
if (FAILED(hr = pOutput->QueryInterface(__uuidof(IDXGIOutput1), (void**)&pOut1)))
{
CLEAN_RETURN(hr);
}
/// Ask DXGI to create an instance of IDXGIOutputDuplication for the selected output. We can now capture this display output
if (FAILED(hr = pOut1->DuplicateOutput(pDevice, &pDup)))
{
CLEAN_RETURN(hr);
}
DXGI_OUTDUPL_DESC outDesc;
ZeroMemory(&outDesc, sizeof(outDesc));
pDup->GetDesc(&outDesc);
height = outDesc.ModeDesc.Height;
width = outDesc.ModeDesc.Width;
CLEAN_RETURN(hr);
}
/// Acquire a new frame from DDA, and return it as a Texture2D object.
/// 'wait' specifies the time in milliseconds that DDA shoulo wait for a new screen update.
HRESULT DDAImpl::GetCapturedFrame(ID3D11Texture2D **ppTex2D, int wait, bool fail_if_equal)
{
HRESULT hr = S_OK;
DXGI_OUTDUPL_FRAME_INFO frameInfo;
ZeroMemory(&frameInfo, sizeof(frameInfo));
int acquired = 0;
#define RETURN_ERR(x) {printf(__FUNCTION__": %d : Line %d return 0x%x\n", frameno, __LINE__, x);return x;}
if (pResource)
{
pDup->ReleaseFrame();
pResource->Release();
pResource = nullptr;
}
hr = pDup->AcquireNextFrame(wait, &frameInfo, &pResource);
if (FAILED(hr))
{
if (hr == DXGI_ERROR_WAIT_TIMEOUT)
{
// printf(__FUNCTION__": %d : Wait for %d ms timed out\n", frameno, wait);
}
if (hr == DXGI_ERROR_INVALID_CALL)
{
printf(__FUNCTION__": %d : Invalid Call, previous frame not released?\n", frameno);
}
if (hr == DXGI_ERROR_ACCESS_LOST)
{
printf(__FUNCTION__": %d : Access lost, frame needs to be released?\n", frameno);
}
// RETURN_ERR(hr);
return hr;
}
if (fail_if_equal)
{
if (frameInfo.AccumulatedFrames == 0 || frameInfo.LastPresentTime.QuadPart == 0)
{
// No image update, only cursor moved.
RETURN_ERR(DXGI_ERROR_WAIT_TIMEOUT);
}
}
if (!pResource)
{
printf(__FUNCTION__": %d : Null output resource. Return error.\n", frameno);
return E_UNEXPECTED;
}
if (FAILED(hr = pResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)ppTex2D)))
{
return hr;
}
LARGE_INTEGER pts = frameInfo.LastPresentTime; MICROSEC_TIME(pts, qpcFreq);
LONGLONG interval = pts.QuadPart - lastPTS.QuadPart;
// printf(__FUNCTION__": %d : Accumulated Frames %u PTS Interval %lld PTS %lld\n", frameno, frameInfo.AccumulatedFrames, interval * 1000, frameInfo.LastPresentTime.QuadPart);
lastPTS = pts; // store microsec value
frameno += frameInfo.AccumulatedFrames;
return hr;
}
/// Release all resources
int DDAImpl::Cleanup()
{
if (pResource)
{
pDup->ReleaseFrame();
SAFE_RELEASE(pResource);
}
width = height = frameno = 0;
SAFE_RELEASE(pDup);
SAFE_RELEASE(pCtx);
SAFE_RELEASE(pD3DDev);
return 0;
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
#include <dxgi1_2.h>
#include <d3d11_2.h>
class DDAImpl
{
/// Thin wrapper around IDXGIOutputDuplication interface
/// Manages IDXGIOutputDuplication object lifecycle
/// Interacts with IDXGIOuputDuplication to acquire new frames
private:
/// The DDA object
IDXGIOutputDuplication* pDup = nullptr;
/// The D3D11 device used by the DDA session
ID3D11Device* pD3DDev = nullptr;
/// The D3D11 Device Context used by the DDA session
ID3D11DeviceContext* pCtx = nullptr;
/// The resource used to acquire a new captured frame from DDA
IDXGIResource *pResource = nullptr;
/// Output width obtained from DXGI_OUTDUPL_DESC
DWORD width = 0;
/// Output height obtained from DXGI_OUTDUPL_DESC
DWORD height = 0;
/// Running count of no. of accumulated desktop updates
int frameno = 0;
/// DXGI_OUTDUPL_FRAME_INFO::latPresentTime from the last Acquired frame
LARGE_INTEGER lastPTS = { 0 };
/// Clock frequency from QueryPerformaceFrequency()
LARGE_INTEGER qpcFreq = { 0 };
/// Default constructor
DDAImpl() {}
public:
/// Initialize DDA
HRESULT Init();
/// Acquire a new frame from DDA, and return it as a Texture2D object.
/// 'wait' specifies the time in milliseconds that DDA shoulo wait for a new screen update.
HRESULT GetCapturedFrame(ID3D11Texture2D **pTex2D, int wait, bool fail_if_equal = false);
/// Release all resources
int Cleanup();
/// Return output height to caller
inline DWORD getWidth() { return width; }
/// Return output width to caller
inline DWORD getHeight() { return height; }
public:
/// Constructor
DDAImpl(ID3D11Device *pDev, ID3D11DeviceContext* pDevCtx)
: pD3DDev(pDev)
, pCtx(pDevCtx)
{
pD3DDev->AddRef();
pCtx->AddRef();
QueryPerformanceFrequency(&qpcFreq);
}
/// Destructor. Release all resources before destroying the object
~DDAImpl() { Cleanup(); }
};

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#pragma warning(disable:4996)
#pragma warning(disable:4838)
#if !defined(SAFE_RELEASE)
#define SAFE_RELEASE(X) if(X){X->Release(); X=nullptr;}
#endif
#if !defined(PRINTERR1)
#define PRINTERR1(x) printf(__FUNCTION__": Error 0x%08x at line %d in file %s\n", x, __LINE__, __FILE__);
#endif
#if !defined(PRINTERR)
#define PRINTERR(x,y) printf(__FUNCTION__": Error 0x%08x in %s at line %d in file %s\n", x, y, __LINE__, __FILE__);
#endif
#define MICROSEC_TIME(x,f)\
x.QuadPart *= 1000000;\
x.QuadPart /= f.QuadPart;

View File

@@ -0,0 +1,169 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Defs.h"
#include "Preproc.h"
/// Constructor
RGBToNV12::RGBToNV12(ID3D11Device *pDev, ID3D11DeviceContext *pCtx)
: m_pDev(pDev)
, m_pCtx(pCtx)
{
m_pDev->AddRef();
m_pCtx->AddRef();
}
/// Initialize Video Context
HRESULT RGBToNV12::Init()
{
/// Obtain Video device and Video device context
HRESULT hr = m_pDev->QueryInterface(__uuidof(ID3D11VideoDevice), (void**)&m_pVid);
if (FAILED(hr))
{
PRINTERR(hr, "QAI for ID3D11VideoDevice");
}
hr = m_pCtx->QueryInterface(__uuidof(ID3D11VideoContext), (void**)&m_pVidCtx);
if (FAILED(hr))
{
PRINTERR(hr, "QAI for ID3D11VideoContext");
}
return hr;
}
/// Release all Resources
void RGBToNV12::Cleanup()
{
for (auto& it : viewMap)
{
ID3D11VideoProcessorOutputView* pVPOV = it.second;
pVPOV->Release();
}
SAFE_RELEASE(m_pVP);
SAFE_RELEASE(m_pVPEnum);
SAFE_RELEASE(m_pVidCtx);
SAFE_RELEASE(m_pVid);
SAFE_RELEASE(m_pCtx);
SAFE_RELEASE(m_pDev);
}
/// Perform Colorspace conversion
HRESULT RGBToNV12::Convert(ID3D11Texture2D* pRGB, ID3D11Texture2D*pYUV)
{
HRESULT hr = S_OK;
ID3D11VideoProcessorInputView* pVPIn = nullptr;
D3D11_TEXTURE2D_DESC inDesc = { 0 };
D3D11_TEXTURE2D_DESC outDesc = { 0 };
pRGB->GetDesc(&inDesc);
pYUV->GetDesc(&outDesc);
/// Check if VideoProcessor needs to be reconfigured
/// Reconfiguration is required if input/output dimensions have changed
if (m_pVP)
{
if (m_inDesc.Width != inDesc.Width ||
m_inDesc.Height != inDesc.Height ||
m_outDesc.Width != outDesc.Width ||
m_outDesc.Height != outDesc.Height)
{
SAFE_RELEASE(m_pVPEnum);
SAFE_RELEASE(m_pVP);
}
}
if (!m_pVP)
{
/// Initialize Video Processor
m_inDesc = inDesc;
m_outDesc = outDesc;
D3D11_VIDEO_PROCESSOR_CONTENT_DESC contentDesc =
{
D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE,
{ 1, 1 }, inDesc.Width, inDesc.Height,
{ 1, 1 }, outDesc.Width, outDesc.Height,
D3D11_VIDEO_USAGE_PLAYBACK_NORMAL
};
hr = m_pVid->CreateVideoProcessorEnumerator(&contentDesc, &m_pVPEnum);;
if (FAILED(hr))
{
PRINTERR(hr, "CreateVideoProcessorEnumerator");
}
hr = m_pVid->CreateVideoProcessor(m_pVPEnum, 0, &m_pVP);;
if (FAILED(hr))
{
PRINTERR(hr, "CreateVideoProcessor");
}
}
/// Obtain Video Processor Input view from input texture
D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC inputVD = { 0, D3D11_VPIV_DIMENSION_TEXTURE2D,{ 0,0 } };
hr = m_pVid->CreateVideoProcessorInputView(pRGB, m_pVPEnum, &inputVD, &pVPIn);
if (FAILED(hr))
{
PRINTERR(hr, "CreateVideoProcessInputView");
return hr;
}
/// Obtain Video Processor Output view from output texture
ID3D11VideoProcessorOutputView* pVPOV = nullptr;
auto it = viewMap.find(pYUV);
/// Optimization: Check if we already created a video processor output view for this texture
if (it == viewMap.end())
{
/// We don't have a video processor output view for this texture, create one now.
D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC ovD = { D3D11_VPOV_DIMENSION_TEXTURE2D };
hr = m_pVid->CreateVideoProcessorOutputView(pYUV, m_pVPEnum, &ovD, &pVPOV);
if (FAILED(hr))
{
SAFE_RELEASE(pVPIn);
PRINTERR(hr, "CreateVideoProcessorOutputView");
return hr;
}
viewMap.insert({ pYUV, pVPOV });
}
else
{
pVPOV = it->second;
}
/// Create a Video Processor Stream to run the operation
D3D11_VIDEO_PROCESSOR_STREAM stream = { TRUE, 0, 0, 0, 0, nullptr, pVPIn, nullptr };
/// Perform the Colorspace conversion
hr = m_pVidCtx->VideoProcessorBlt(m_pVP, pVPOV, 0, 1, &stream);
if (FAILED(hr))
{
SAFE_RELEASE(pVPIn);
PRINTERR(hr, "VideoProcessorBlt");
return hr;
}
SAFE_RELEASE(pVPIn);
return hr;
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <dxgi1_2.h>
#include <d3d11_2.h>
#include <unordered_map>
using namespace std;
class RGBToNV12
{
/// Simple Preprocessor class
/// Uses DXVAHD VideoProcessBlt to perform colorspace conversion
private:
/// D3D11 device to be used for Processing
ID3D11Device *m_pDev = nullptr;
/// D3D11 device context to be used for Processing
ID3D11DeviceContext* m_pCtx = nullptr;
/// D3D11 video device to be used for Processing, obtained from d3d11 device
ID3D11VideoDevice* m_pVid = nullptr;
/// D3D11 video device context to be used for Processing, obtained from d3d11 device
ID3D11VideoContext* m_pVidCtx = nullptr;
/// DXVAHD video processor configured for processing.
/// Needs to be reconfigured based on input and output textures for each Convert() call
ID3D11VideoProcessor* m_pVP = nullptr;
/// DXVAHD VpBlt output target. Obtained from the output texture passed to Convert()
ID3D11VideoProcessorOutputView* m_pVPOut = nullptr;
/// D3D11 video processor enumerator. Required to configure Video processor streams
ID3D11VideoProcessorEnumerator* m_pVPEnum = nullptr;
/// Mapping of Texture2D handle and corresponding Video Processor output view handle
/// Optimization to avoid having to create video processor output views in each Convert() call
std::unordered_map<ID3D11Texture2D*, ID3D11VideoProcessorOutputView*> viewMap;
/// Input and Output Texture2D properties.
/// Required to optimize Video Processor stream usage
D3D11_TEXTURE2D_DESC m_inDesc = { 0 };
D3D11_TEXTURE2D_DESC m_outDesc = { 0 };
private:
/// Default Constructor
RGBToNV12()
{
}
public:
/// Initialize Video Context
HRESULT Init();
/// Perform Colorspace conversion
HRESULT Convert(ID3D11Texture2D* pRGB, ID3D11Texture2D*pYUV);
/// Release all resources
void Cleanup();
public:
/// Constructor
RGBToNV12(ID3D11Device *pDev, ID3D11DeviceContext *pCtx);
/// Destructor. Release all resources before destroying object
~RGBToNV12()
{
Cleanup();
}
};