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  float GetLuminanceValue() const;
95 
96  bool m_useLut = false;
97  bool m_useDithering = false;
98  bool m_toneMapping = false;
99  bool m_useHLGtoPQ = false;
100 
101  bool m_hasDisplayMetadata = false;
102  bool m_hasLightMetadata = false;
103  unsigned m_sourceWidth = 0;
104  unsigned m_sourceHeight = 0;
105  int m_lutSize = 0;
106  int m_ditherDepth = 0;
107  ETONEMAPMETHOD m_toneMappingMethod = VS_TONEMAPMETHOD_OFF;
108  float m_toneMappingParam = 1.0f;
109  float m_toneMappingDebug = .0f;
110 
111  CRect m_sourceRect = {};
112  CPoint m_destPoints[4] = {};
113  Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pDitherView = nullptr;
114  Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pLUTView = nullptr;
115 
116  AVMasteringDisplayMetadata m_displayMetadata = {};
117  AVContentLightMetadata m_lightMetadata = {};
118 };
119 
121 {
122 public:
123  explicit CYUV2RGBShader() = default;
124  ~CYUV2RGBShader() = default;
125 
126  bool Create(AVPixelFormat fmt,
127  AVColorPrimaries dstPrimaries,
128  AVColorPrimaries srcPrimaries,
129  const std::shared_ptr<COutputShader>& pOutShader = nullptr);
130  void Render(CRect sourceRect, CPoint dest[], CRenderBuffer* videoBuffer, CD3DTexture& target);
131  void SetParams(float contrast, float black, bool limited);
132  void SetColParams(AVColorSpace colSpace, int bits, bool limited, int texBits);
133 
134 protected:
135  void PrepareParameters(CRenderBuffer* videoBuffer, CRect sourceRect, CPoint dest[]);
136  void SetShaderParameters(CRenderBuffer* videoBuffer);
137 
138 private:
139  struct Vertex {
140  float x, y, z;
141  float tu, tv; // Y Texture coordinates
142  float tu2, tv2; // U and V Textures coordinates
143  };
144 
145  unsigned int m_sourceWidth = 0;
146  unsigned int m_sourceHeight = 0;
147  CRect m_sourceRect = {};
148  CPoint m_dest[4] = {};
149  AVPixelFormat m_format = AV_PIX_FMT_NONE;
150  std::array<float, 2> m_texSteps = {};
151  std::shared_ptr<COutputShader> m_pOutShader = nullptr;
152  CConvertMatrix m_convMatrix;
153  bool m_colorConversion{false};
154 };
155 
157 {
158 public:
159  virtual ~CConvolutionShader() = default;
160  virtual bool Create(ESCALINGMETHOD method, const std::shared_ptr<COutputShader>& pOutShader = nullptr) = 0;
161  virtual void Render(CD3DTexture& sourceTexture, CD3DTexture& target,
162  CRect sourceRect, CRect destRect, bool useLimitRange) = 0;
163 protected:
164  struct Vertex {
165  float x, y, z;
166  float tu, tv;
167  };
168 
169  CConvolutionShader() = default;
170 
171  virtual bool ChooseKernelD3DFormat();
172  virtual bool CreateHQKernel(ESCALINGMETHOD method);
173  virtual void SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps, int texStepsCount, bool useLimitRange) = 0;
174 
175  bool m_floattex = false;
176  bool m_rgba = false;
177  DXGI_FORMAT m_KernelFormat = DXGI_FORMAT_UNKNOWN;
178  CD3DTexture m_HQKernelTexture;
179  std::shared_ptr<COutputShader> m_pOutShader = nullptr;
180 };
181 
183 {
184 public:
185  explicit CConvolutionShader1Pass() = default;
186 
187  bool Create(ESCALINGMETHOD method, const std::shared_ptr<COutputShader>& pOutputShader = nullptr) override;
188  void Render(CD3DTexture& sourceTexture, CD3DTexture& target,
189  CRect sourceRect, CRect destRect, bool useLimitRange) override;
190 
191 protected:
192  void PrepareParameters(unsigned int sourceWidth, unsigned int sourceHeight,
193  CRect sourceRect, CRect destRect);
194  void SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps,
195  int texStepsCount, bool useLimitRange) override;
196 
197 private:
198  unsigned int m_sourceWidth = 0;
199  unsigned int m_sourceHeight = 0;
200  CRect m_sourceRect = {};
201  CRect m_destRect = {};
202 };
203 
205 {
206 public:
207  explicit CConvolutionShaderSeparable() = default;
208  ~CConvolutionShaderSeparable() = default;
209 
210  bool Create(ESCALINGMETHOD method, const std::shared_ptr<COutputShader>& pOutputShader = nullptr) override;
211  void Render(CD3DTexture& sourceTexture, CD3DTexture& target,
212  CRect sourceRect, CRect destRect, bool useLimitRange) override;
213 
214 protected:
215  bool ChooseIntermediateD3DFormat();
216  bool CreateIntermediateRenderTarget(unsigned int width, unsigned int height);
217  bool ClearIntermediateRenderTarget();
218  void PrepareParameters(unsigned int sourceWidth, unsigned int sourceHeight,
219  unsigned int destWidth, unsigned int destHeight,
220  CRect sourceRect, CRect destRect);
221  void SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps,
222  int texStepsCount, bool useLimitRange) override;
223  void SetStepParams(unsigned iPass) override;
224 
225 private:
226  CD3DTexture m_IntermediateTarget;
227  DXGI_FORMAT m_IntermediateFormat = DXGI_FORMAT_UNKNOWN;
228  unsigned int m_sourceWidth = 0;
229  unsigned int m_sourceHeight = 0;
230  unsigned int m_destWidth = 0;
231  unsigned int m_destHeight = 0;
232  CRect m_sourceRect = {};
233  CRect m_destRect = {};
234 };
235 
236 class CTestShader : public CWinShader
237 {
238 public:
239  virtual bool Create();
240 };
Definition: D3DResource.h:90
Definition: WinVideoFilter.h:182
Definition: WinVideoFilter.h:120
Definition: RendererBase.h:56
Definition: D3DResource.h:174
Definition: WinVideoFilter.h:236
Helper class used for YUV to RGB conversions.
Definition: ConversionMatrix.h:144
Definition: WinVideoFilter.h:30
Definition: WinVideoFilter.h:156
Definition: WinVideoFilter.h:164
Definition: WinVideoFilter.h:57
Definition: D3DResource.h:212
Definition: WinVideoFilter.h:204