convert.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #pragma once
  29. #include <dxgi1_2.h>
  30. #include <d3d11_2.h>
  31. #include <unordered_map>
  32. class D3dConverter {
  33. /// Simple Preprocessor class
  34. /// Uses DXVAHD VideoProcessBlt to perform colorspace conversion
  35. private:
  36. /// D3D11 device to be used for Processing
  37. ID3D11Device* m_pDev = nullptr;
  38. /// D3D11 device context to be used for Processing
  39. ID3D11DeviceContext* m_pCtx = nullptr;
  40. /// D3D11 video device to be used for Processing, obtained from d3d11 device
  41. ID3D11VideoDevice* m_pVid = nullptr;
  42. /// D3D11 video device context to be used for Processing, obtained from d3d11 device
  43. ID3D11VideoContext* m_pVidCtx = nullptr;
  44. /// DXVAHD video processor configured for processing.
  45. /// Needs to be reconfigured based on input and output textures for each Convert() call
  46. ID3D11VideoProcessor* m_pVP = nullptr;
  47. /// DXVAHD VpBlt output target. Obtained from the output texture passed to Convert()
  48. ID3D11VideoProcessorOutputView* m_pVPOut = nullptr;
  49. /// D3D11 video processor enumerator. Required to configure Video processor streams
  50. ID3D11VideoProcessorEnumerator* m_pVPEnum = nullptr;
  51. /// Mapping of Texture2D handle and corresponding Video Processor output view handle
  52. /// Optimization to avoid having to create video processor output views in each Convert() call
  53. std::unordered_map<ID3D11Texture2D*, ID3D11VideoProcessorOutputView*> viewMap;
  54. /// Input and Output Texture2D properties.
  55. /// Required to optimize Video Processor stream usage
  56. D3D11_TEXTURE2D_DESC m_inDesc = {0};
  57. D3D11_TEXTURE2D_DESC m_outDesc = {0};
  58. D3D11_VIDEO_PROCESSOR_COLOR_SPACE _inColorSpace;
  59. D3D11_VIDEO_PROCESSOR_COLOR_SPACE _outColorSpace;
  60. public:
  61. /// Initialize Video Context
  62. HRESULT Open(ID3D11Device* pDev, ID3D11DeviceContext* pCtx,
  63. const D3D11_VIDEO_PROCESSOR_COLOR_SPACE& inColorSpace, D3D11_VIDEO_PROCESSOR_COLOR_SPACE& outColorSpace);
  64. /// Perform Colorspace conversion
  65. HRESULT Convert(ID3D11Texture2D* pIn, ID3D11Texture2D* pOut);
  66. /// Release all resources
  67. void Close();
  68. public:
  69. /// Constructor
  70. // RGBToNV12(ID3D11Device *pDev, ID3D11DeviceContext *pCtx);
  71. /// Destructor. Release all resources before destroying object
  72. ~D3dConverter()
  73. {
  74. Close();
  75. }
  76. };