xbmc
WinVideoFilter.h
1 /*
2  * Copyright (C) 2007-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "cores/IPlayer.h"
12 #include "cores/VideoPlayer/VideoRenderers/VideoShaders/ConversionMatrix.h"
13 #include "guilib/D3DResource.h"
14 #include "utils/Geometry.h"
15 
16 #include <array>
17 #include <vector>
18 
19 #include <DirectXMath.h>
20 #include <wrl/client.h>
21 
22 extern "C" {
23 #include <libavutil/pixfmt.h>
24 #include <libavutil/mastering_display_metadata.h>
25 }
26 class CRenderBuffer;
27 
28 using namespace DirectX;
29 
31 {
32 protected:
33  CWinShader() = default;
34  virtual ~CWinShader() = default;
35 
36  virtual bool CreateVertexBuffer(unsigned int vertCount, unsigned int vertSize);
37  virtual bool LockVertexBuffer(void **data);
38  virtual bool UnlockVertexBuffer();
39  virtual bool LoadEffect(const std::string& filename, DefinesMap* defines);
40  virtual bool Execute(const std::vector<CD3DTexture*>& targets, unsigned int vertexIndexStep);
41  virtual void SetStepParams(unsigned stepIndex) { }
42  virtual bool CreateInputLayout(D3D11_INPUT_ELEMENT_DESC *layout, unsigned numElements);
43 
44  CD3DEffect m_effect;
45  CD3DTexture* m_target = nullptr;
46 
47 private:
48  void SetTarget(CD3DTexture* target);
49 
50  CD3DBuffer m_vb;
51  CD3DBuffer m_ib;
52  unsigned int m_vbsize = 0;
53  unsigned int m_vertsize = 0;
54  Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout = nullptr;
55 };
56 
57 class COutputShader : public CWinShader
58 {
59 public:
60  explicit COutputShader() = default;
61  ~COutputShader() = default;
62 
63  void ApplyEffectParameters(CD3DEffect &effect, unsigned sourceWidth, unsigned sourceHeight);
64  void GetDefines(DefinesMap &map) const;
65  bool Create(bool useLUT,
66  bool useDithering,
67  int ditherDepth,
68  bool toneMapping,
69  ETONEMAPMETHOD toneMethod,
70  bool HLGtoPQ);
71  void Render(CD3DTexture& sourceTexture, CRect sourceRect, const CPoint points[4]
72  , CD3DTexture& target, unsigned range = 0, float contrast = 0.5f, float brightness = 0.5f);
73  void Render(CD3DTexture& sourceTexture, CRect sourceRect, CRect destRect
74  , CD3DTexture& target, unsigned range = 0, float contrast = 0.5f, float brightness = 0.5f);
75  void SetLUT(int lutSize, ID3D11ShaderResourceView *pLUTView);
76  void SetDisplayMetadata(bool hasDisplayMetadata, AVMasteringDisplayMetadata displayMetadata,
77  bool hasLightMetadata, AVContentLightMetadata lightMetadata);
78  void SetToneMapParam(ETONEMAPMETHOD method, float param);
79  std::string GetDebugInfo();
80 
81  static bool CreateLUTView(int lutSize, uint16_t* lutData, bool isRGB, ID3D11ShaderResourceView** ppLUTView);
82 
83 private:
84  struct Vertex
85  {
86  float x, y, z;
87  float tu, tv;
88  };
89 
90  bool HasLUT() const { return m_lutSize && m_pLUTView; }
91  void PrepareParameters(unsigned sourceWidth, unsigned sourceHeight, CRect sourceRect, const CPoint points[4]);
92  void SetShaderParameters(CD3DTexture &sourceTexture, unsigned range, float contrast, float brightness);
93  void CreateDitherView();
94 
95  bool m_useLut = false;
96  bool m_useDithering = false;
97  bool m_toneMapping = false;
98  bool m_useHLGtoPQ = false;
99 
100  bool m_hasDisplayMetadata = false;
101  bool m_hasLightMetadata = false;
102  unsigned m_sourceWidth = 0;
103  unsigned m_sourceHeight = 0;
104  int m_lutSize = 0;
105  int m_ditherDepth = 0;
106  ETONEMAPMETHOD m_toneMappingMethod = VS_TONEMAPMETHOD_OFF;
107  float m_toneMappingParam = 1.0f;
108  float m_toneMappingDebug = .0f;
109 
110  CRect m_sourceRect = {};
111  CPoint m_destPoints[4] = {};
112  Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pDitherView = nullptr;
113  Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pLUTView = nullptr;
114 
115  AVMasteringDisplayMetadata m_displayMetadata = {};
116  AVContentLightMetadata m_lightMetadata = {};
117 };
118 
120 {
121 public:
122  explicit CYUV2RGBShader() = default;
123  ~CYUV2RGBShader() = default;
124 
125  bool Create(AVPixelFormat fmt,
126  AVColorPrimaries dstPrimaries,
127  AVColorPrimaries srcPrimaries,
128  const std::shared_ptr<COutputShader>& pOutShader = nullptr);
129  void Render(CRect sourceRect, CPoint dest[], CRenderBuffer* videoBuffer, CD3DTexture& target);
130  void SetParams(float contrast, float black, bool limited);
131  void SetColParams(AVColorSpace colSpace, int bits, bool limited, int texBits);
132 
133 protected:
134  void PrepareParameters(CRenderBuffer* videoBuffer, CRect sourceRect, CPoint dest[]);
135  void SetShaderParameters(CRenderBuffer* videoBuffer);
136 
137 private:
138  struct Vertex {
139  float x, y, z;
140  float tu, tv; // Y Texture coordinates
141  float tu2, tv2; // U and V Textures coordinates
142  };
143 
144  unsigned int m_sourceWidth = 0;
145  unsigned int m_sourceHeight = 0;
146  CRect m_sourceRect = {};
147  CPoint m_dest[4] = {};
148  AVPixelFormat m_format = AV_PIX_FMT_NONE;
149  std::array<float, 2> m_texSteps = {};
150  std::shared_ptr<COutputShader> m_pOutShader = nullptr;
151  CConvertMatrix m_convMatrix;
152  bool m_colorConversion{false};
153 };
154 
156 {
157 public:
158  virtual ~CConvolutionShader() = default;
159  virtual bool Create(ESCALINGMETHOD method, const std::shared_ptr<COutputShader>& pOutShader = nullptr) = 0;
160  virtual void Render(CD3DTexture& sourceTexture, CD3DTexture& target,
161  CRect sourceRect, CRect destRect, bool useLimitRange) = 0;
162 protected:
163  struct Vertex {
164  float x, y, z;
165  float tu, tv;
166  };
167 
168  CConvolutionShader() = default;
169 
170  virtual bool ChooseKernelD3DFormat();
171  virtual bool CreateHQKernel(ESCALINGMETHOD method);
172  virtual void SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps, int texStepsCount, bool useLimitRange) = 0;
173 
174  bool m_floattex = false;
175  bool m_rgba = false;
176  DXGI_FORMAT m_KernelFormat = DXGI_FORMAT_UNKNOWN;
177  CD3DTexture m_HQKernelTexture;
178  std::shared_ptr<COutputShader> m_pOutShader = nullptr;
179 };
180 
182 {
183 public:
184  explicit CConvolutionShader1Pass() = default;
185 
186  bool Create(ESCALINGMETHOD method, const std::shared_ptr<COutputShader>& pOutputShader = nullptr) override;
187  void Render(CD3DTexture& sourceTexture, CD3DTexture& target,
188  CRect sourceRect, CRect destRect, bool useLimitRange) override;
189 
190 protected:
191  void PrepareParameters(unsigned int sourceWidth, unsigned int sourceHeight,
192  CRect sourceRect, CRect destRect);
193  void SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps,
194  int texStepsCount, bool useLimitRange) override;
195 
196 private:
197  unsigned int m_sourceWidth = 0;
198  unsigned int m_sourceHeight = 0;
199  CRect m_sourceRect = {};
200  CRect m_destRect = {};
201 };
202 
204 {
205 public:
206  explicit CConvolutionShaderSeparable() = default;
207  ~CConvolutionShaderSeparable() = default;
208 
209  bool Create(ESCALINGMETHOD method, const std::shared_ptr<COutputShader>& pOutputShader = nullptr) override;
210  void Render(CD3DTexture& sourceTexture, CD3DTexture& target,
211  CRect sourceRect, CRect destRect, bool useLimitRange) override;
212 
213 protected:
214  bool ChooseIntermediateD3DFormat();
215  bool CreateIntermediateRenderTarget(unsigned int width, unsigned int height);
216  bool ClearIntermediateRenderTarget();
217  void PrepareParameters(unsigned int sourceWidth, unsigned int sourceHeight,
218  unsigned int destWidth, unsigned int destHeight,
219  CRect sourceRect, CRect destRect);
220  void SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps,
221  int texStepsCount, bool useLimitRange) override;
222  void SetStepParams(unsigned iPass) override;
223 
224 private:
225  CD3DTexture m_IntermediateTarget;
226  DXGI_FORMAT m_IntermediateFormat = DXGI_FORMAT_UNKNOWN;
227  unsigned int m_sourceWidth = 0;
228  unsigned int m_sourceHeight = 0;
229  unsigned int m_destWidth = 0;
230  unsigned int m_destHeight = 0;
231  CRect m_sourceRect = {};
232  CRect m_destRect = {};
233 };
234 
235 class CTestShader : public CWinShader
236 {
237 public:
238  virtual bool Create();
239 };
Definition: D3DResource.h:92
Definition: WinVideoFilter.h:181
Definition: WinVideoFilter.h:119
Definition: RendererBase.h:57
Definition: D3DResource.h:176
Definition: WinVideoFilter.h:235
Helper class used for YUV to RGB conversions.
Definition: ConversionMatrix.h:144
Definition: WinVideoFilter.h:30
Definition: WinVideoFilter.h:155
Definition: WinVideoFilter.h:163
Definition: WinVideoFilter.h:57
Definition: D3DResource.h:217
Definition: WinVideoFilter.h:203