Debian Codegraph @

xbmc/cores/VideoPlayer/VideoRenderers/HwDecRender/RendererVDPAU.cpp

raw
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 #include "RendererVDPAU.h"
10
11 #include "../RenderFactory.h"
12 #include "ServiceBroker.h"
13 #include "cores/VideoPlayer/DVDCodecs/Video/VDPAU.h"
14 #include "settings/Settings.h"
15 #include "settings/SettingsComponent.h"
16 #include "utils/GLUtils.h"
17 #include "utils/log.h"
18
19 using namespace VDPAU;
20
21 CBaseRenderer* CRendererVDPAU::Create(CVideoBuffer *buffer)
22 {
23 CVdpauRenderPicture *vb = dynamic_cast<CVdpauRenderPicture*>(buffer);
24 if (vb)
25 return new CRendererVDPAU();
26
27 return nullptr;
28 }
29
30 bool CRendererVDPAU::Register()
31 {
32 VIDEOPLAYER::CRendererFactory::RegisterRenderer("vdpau", CRendererVDPAU::Create);
33 return true;
34 }
35
36 CRendererVDPAU::CRendererVDPAU() = default;
37
38 CRendererVDPAU::~CRendererVDPAU()
39 {
40 for (int i = 0; i < NUM_BUFFERS; ++i)
41 {
42 DeleteTexture(i);
43 }
44 m_interopState.Finish();
45 }
46
47 bool CRendererVDPAU::Configure(const VideoPicture &picture, float fps, unsigned int orientation)
48 {
49 CVdpauRenderPicture *pic = dynamic_cast<CVdpauRenderPicture*>(picture.videoBuffer);
50 if (pic->procPic.isYuv)
51 m_isYuv = true;
52 else
53 m_isYuv = false;
54
55 if (!m_interopState.Init(pic->device, pic->procFunc, pic->ident))
56 return false;
57
58 for (auto &tex : m_vdpauTextures)
59 {
60 tex.Init(m_interopState.GetInterop());
61 }
62 for (auto &fence : m_fences)
63 {
64 fence = {};
65 }
66
67 return CLinuxRendererGL::Configure(picture, fps, orientation);
68 }
69
70 bool CRendererVDPAU::ConfigChanged(const VideoPicture &picture)
71 {
72 CVdpauRenderPicture *pic = dynamic_cast<CVdpauRenderPicture*>(picture.videoBuffer);
73 if (pic->procPic.isYuv && !m_isYuv)
74 return true;
75
76 if (m_interopState.NeedInit(pic->device, pic->procFunc, pic->ident))
77 return true;
78
79 return false;
80 }
81
82 bool CRendererVDPAU::NeedBuffer(int idx)
83 {
84 if (glIsSync(m_fences[idx]))
85 {
86 GLint state;
87 GLsizei length;
88 glGetSynciv(m_fences[idx], GL_SYNC_STATUS, 1, &length, &state);
89 if (state == GL_SIGNALED)
90 {
91 glDeleteSync(m_fences[idx]);
92 m_fences[idx] = {};
93 }
94 else
95 {
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 bool CRendererVDPAU::Flush(bool saveBuffers)
104 {
105 for (int i = 0; i < NUM_BUFFERS; i++)
106 m_vdpauTextures[i].Unmap();
107
108 return CLinuxRendererGL::Flush(saveBuffers);
109 }
110
111
112 void CRendererVDPAU::ReleaseBuffer(int idx)
113 {
114 if (glIsSync(m_fences[idx]))
115 {
116 glDeleteSync(m_fences[idx]);
117 m_fences[idx] = {};
118 }
119 m_vdpauTextures[idx].Unmap();
120 CLinuxRendererGL::ReleaseBuffer(idx);
121 }
122
123 bool CRendererVDPAU::Supports(ERENDERFEATURE feature) const
124 {
125 if(feature == RENDERFEATURE_BRIGHTNESS ||
126 feature == RENDERFEATURE_CONTRAST)
127 {
128 if (!m_isYuv && !CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_VIDEOSCREEN_LIMITEDRANGE))
129 return true;
130
131 return (m_renderMethod & RENDER_GLSL);
132 }
133 else if (feature == RENDERFEATURE_NOISE ||
134 feature == RENDERFEATURE_SHARPNESS)
135 {
136 if (!m_isYuv)
137 return true;
138 }
139 else if (feature == RENDERFEATURE_STRETCH ||
140 feature == RENDERFEATURE_ZOOM ||
141 feature == RENDERFEATURE_VERTICAL_SHIFT ||
142 feature == RENDERFEATURE_PIXEL_RATIO ||
143 feature == RENDERFEATURE_POSTPROCESS ||
144 feature == RENDERFEATURE_ROTATION ||
145 feature == RENDERFEATURE_NONLINSTRETCH)
146 return true;
147
148 return false;
149 }
150
151 bool CRendererVDPAU::Supports(ESCALINGMETHOD method) const
152 {
153 if (m_isYuv)
154 return CLinuxRendererGL::Supports(method);
155
156 if (method == VS_SCALINGMETHOD_NEAREST)
157 return true;
158
159 if (method == VS_SCALINGMETHOD_LINEAR ||
160 method == VS_SCALINGMETHOD_AUTO)
161 return true;
162
163 if(method == VS_SCALINGMETHOD_CUBIC_B_SPLINE
164 || method == VS_SCALINGMETHOD_CUBIC_MITCHELL
165 || method == VS_SCALINGMETHOD_CUBIC_CATMULL
166 || method == VS_SCALINGMETHOD_CUBIC_0_075
167 || method == VS_SCALINGMETHOD_CUBIC_0_1
168 || method == VS_SCALINGMETHOD_LANCZOS2
169 || method == VS_SCALINGMETHOD_SPLINE36_FAST
170 || method == VS_SCALINGMETHOD_LANCZOS3_FAST
171 || method == VS_SCALINGMETHOD_SPLINE36
172 || method == VS_SCALINGMETHOD_LANCZOS3)
173 {
174 // if scaling is below level, avoid hq scaling
175 float scaleX = fabs(((float)m_sourceWidth - m_destRect.Width())/m_sourceWidth)*100;
176 float scaleY = fabs(((float)m_sourceHeight - m_destRect.Height())/m_sourceHeight)*100;
177 int minScale = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt("videoplayer.hqscalers");
178 return !(scaleX < minScale && scaleY < minScale);
179 }
180
181 return false;
182 }
183
184 EShaderFormat CRendererVDPAU::GetShaderFormat()
185 {
186 EShaderFormat ret = SHADER_NONE;
187
188 if (m_isYuv)
189 ret = SHADER_NV12;
190
191 return ret;
192 }
193
194 bool CRendererVDPAU::LoadShadersHook()
195 {
196 if (!m_isYuv)
197 {
198 CLog::Log(LOGINFO, "GL: Using VDPAU render method");
199 m_renderMethod = RENDER_CUSTOM;
200 m_fullRange = false;
201 return true;
202 }
203 return false;
204 }
205
206 bool CRendererVDPAU::RenderHook(int idx)
207 {
208 UpdateVideoFilter();
209
210 if (m_isYuv)
211 {
212 switch(m_renderQuality)
213 {
214 case RQ_LOW:
215 case RQ_SINGLEPASS:
216 if (m_currentField == FIELD_FULL)
217 RenderProgressiveWeave(idx, m_currentField);
218 else
219 RenderSinglePass(idx, m_currentField);
220 VerifyGLState();
221 break;
222
223 case RQ_MULTIPASS:
224 if (m_currentField == FIELD_FULL)
225 RenderProgressiveWeave(idx, m_currentField);
226 else
227 {
228 RenderToFBO(idx, m_currentField);
229 RenderFromFBO();
230 }
231 VerifyGLState();
232 break;
233 }
234 }
235 else
236 {
237 RenderRGB(idx, m_currentField);
238 }
239
240 return true;
241 }
242
243 void CRendererVDPAU::AfterRenderHook(int idx)
244 {
245 if (glIsSync(m_fences[idx]))
246 {
247 glDeleteSync(m_fences[idx]);
248 m_fences[idx] = None;
249 }
250 m_fences[idx] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
251 }
252
253 bool CRendererVDPAU::CreateTexture(int index)
254 {
255 if (!m_isYuv)
256 return CreateVDPAUTexture(index);
257 else
258 return CreateVDPAUTexture420(index);
259 }
260
261 void CRendererVDPAU::DeleteTexture(int index)
262 {
263 ReleaseBuffer(index);
264
265 if (!m_isYuv)
266 DeleteVDPAUTexture(index);
267 else
268 DeleteVDPAUTexture420(index);
269 }
270
271 bool CRendererVDPAU::UploadTexture(int index)
272 {
273 if (!m_isYuv)
274 return UploadVDPAUTexture(index);
275 else
276 return UploadVDPAUTexture420(index);
277 }
278
279 bool CRendererVDPAU::CreateVDPAUTexture(int index)
280 {
281 CPictureBuffer &buf = m_buffers[index];
282 YuvImage &im = buf.image;
283 CYuvPlane &plane = buf.fields[FIELD_FULL][0];
284
285 DeleteVDPAUTexture(index);
286
287 memset(&im, 0, sizeof(im));
288 plane = {};
289 im.height = m_sourceHeight;
290 im.width = m_sourceWidth;
291
292 plane.texwidth = im.width;
293 plane.texheight = im.height;
294
295 plane.pixpertex_x = 1;
296 plane.pixpertex_y = 1;
297
298 plane.id = 1;
299 return true;
300 }
301
302 void CRendererVDPAU::DeleteVDPAUTexture(int index)
303 {
304 CPictureBuffer &buf = m_buffers[index];
305 CYuvPlane &plane = buf.fields[FIELD_FULL][0];
306
307 plane.id = 0;
308 }
309
310 bool CRendererVDPAU::UploadVDPAUTexture(int index)
311 {
312 CPictureBuffer &buf = m_buffers[index];
313 VDPAU::CVdpauRenderPicture *pic = dynamic_cast<VDPAU::CVdpauRenderPicture*>(buf.videoBuffer);
314
315 CYuvPlane &plane = buf.fields[FIELD_FULL][0];
316
317 if (!pic)
318 {
319 return false;
320 }
321
322 if (!m_vdpauTextures[index].Map(pic))
323 return false;
324
325 // in stereoscopic mode sourceRect may only
326 // be a part of the source video surface
327 plane.rect = m_sourceRect;
328
329 // clip rect
330 if (pic->crop.x1 > plane.rect.x1)
331 plane.rect.x1 = pic->crop.x1;
332 if (pic->crop.x2 < plane.rect.x2)
333 plane.rect.x2 = pic->crop.x2;
334 if (pic->crop.y1 > plane.rect.y1)
335 plane.rect.y1 = pic->crop.y1;
336 if (pic->crop.y2 < plane.rect.y2)
337 plane.rect.y2 = pic->crop.y2;
338
339 plane.texheight = m_vdpauTextures[index].m_texHeight;
340 plane.texwidth = m_vdpauTextures[index].m_texWidth;
341
342 if (m_textureTarget == GL_TEXTURE_2D)
343 {
344 plane.rect.y1 /= plane.texheight;
345 plane.rect.y2 /= plane.texheight;
346 plane.rect.x1 /= plane.texwidth;
347 plane.rect.x2 /= plane.texwidth;
348 }
349
350 // set texture
351 plane.id = m_vdpauTextures[index].m_texture;
352
353 return true;
354 }
355
356 bool CRendererVDPAU::CreateVDPAUTexture420(int index)
357 {
358 CPictureBuffer &buf = m_buffers[index];
359 YuvImage &im = buf.image;
360 CYuvPlane (&planes)[YuvImage::MAX_PLANES] = buf.fields[0];
361 GLuint *pbo = buf.pbo;
362
363 DeleteVDPAUTexture420(index);
364
365 memset(&im, 0, sizeof(im));
366 memset(&planes, 0, sizeof(CYuvPlane[YuvImage::MAX_PLANES]));
367
368 im.cshift_x = 1;
369 im.cshift_y = 1;
370
371 im.plane[0] = nullptr;
372 im.plane[1] = nullptr;
373 im.plane[2] = nullptr;
374
375 for(int p=0; p<3; p++)
376 {
377 pbo[p] = None;
378 }
379
380 planes[0].id = 1;
381
382 return true;
383 }
384
385 void CRendererVDPAU::DeleteVDPAUTexture420(int index)
386 {
387 CPictureBuffer &buf = m_buffers[index];
388
389 buf.fields[0][0].id = 0;
390 buf.fields[1][0].id = 0;
391 buf.fields[1][1].id = 0;
392 buf.fields[2][0].id = 0;
393 buf.fields[2][1].id = 0;
394 }
395
396 bool CRendererVDPAU::UploadVDPAUTexture420(int index)
397 {
398 CPictureBuffer &buf = m_buffers[index];
399 YuvImage &im = buf.image;
400
401 VDPAU::CVdpauRenderPicture *pic = dynamic_cast<VDPAU::CVdpauRenderPicture*>(buf.videoBuffer);
402
403 if (!pic)
404 {
405 return false;
406 }
407
408 if (!m_vdpauTextures[index].Map(pic))
409 return false;
410
411 im.height = m_vdpauTextures[index].m_texHeight;
412 im.width = m_vdpauTextures[index].m_texWidth;
413
414 // YUV
415 for (int f = FIELD_TOP; f<=FIELD_BOT ; f++)
416 {
417 CYuvPlane (&planes)[YuvImage::MAX_PLANES] = buf.fields[f];
418
419 planes[0].texwidth = im.width;
420 planes[0].texheight = im.height >> 1;
421
422 planes[1].texwidth = planes[0].texwidth >> im.cshift_x;
423 planes[1].texheight = planes[0].texheight >> im.cshift_y;
424 planes[2].texwidth = planes[1].texwidth;
425 planes[2].texheight = planes[1].texheight;
426
427 for (int p = 0; p < 3; p++)
428 {
429 planes[p].pixpertex_x = 1;
430 planes[p].pixpertex_y = 1;
431 }
432 }
433 // crop
434 // m_sourceRect.x1 += vdpau->crop.x1;
435 // m_sourceRect.x2 -= vdpau->crop.x2;
436 // m_sourceRect.y1 += vdpau->crop.y1;
437 // m_sourceRect.y2 -= vdpau->crop.y2;
438
439 // set textures
440 buf.fields[1][0].id = m_vdpauTextures[index].m_textureTopY;
441 buf.fields[1][1].id = m_vdpauTextures[index].m_textureTopUV;
442 buf.fields[1][2].id = m_vdpauTextures[index].m_textureTopUV;
443 buf.fields[2][0].id = m_vdpauTextures[index].m_textureBotY;
444 buf.fields[2][1].id = m_vdpauTextures[index].m_textureBotUV;
445 buf.fields[2][2].id = m_vdpauTextures[index].m_textureBotUV;
446
447 for (int f = FIELD_TOP; f <= FIELD_BOT; f++)
448 {
449 for (int p=0; p<2; p++)
450 {
451 glBindTexture(m_textureTarget, buf.fields[f][p].id);
452 glTexParameteri(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
453 glTexParameteri(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
454 glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
455 glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
456
457 glBindTexture(m_textureTarget,0);
458 VerifyGLState();
459 }
460 }
461 CalculateTextureSourceRects(index, 3);
462 return true;
463 }