PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/xbmc/cores/dvdplayer/DVDCodecs/Video/VDPAU.cpp

http://github.com/xbmc/xbmc
C++ | 3624 lines | 3084 code | 410 blank | 130 comment | 596 complexity | 8e1d48e662ca0fb9bcbbc97751e74240 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "system.h"
  21. #ifdef HAVE_LIBVDPAU
  22. #include <dlfcn.h>
  23. #include "windowing/WindowingFactory.h"
  24. #include "VDPAU.h"
  25. #include "guilib/GraphicContext.h"
  26. #include "guilib/TextureManager.h"
  27. #include "cores/VideoRenderers/RenderManager.h"
  28. #include "DVDVideoCodecFFmpeg.h"
  29. #include "DVDClock.h"
  30. #include "settings/Settings.h"
  31. #include "settings/AdvancedSettings.h"
  32. #include "settings/MediaSettings.h"
  33. #include "Application.h"
  34. #include "utils/MathUtils.h"
  35. #include "utils/TimeUtils.h"
  36. #include "DVDCodecs/DVDCodecUtils.h"
  37. #include "cores/VideoRenderers/RenderFlags.h"
  38. using namespace VDPAU;
  39. #define NUM_RENDER_PICS 7
  40. #define ARSIZE(x) (sizeof(x) / sizeof((x)[0]))
  41. CDecoder::Desc decoder_profiles[] = {
  42. {"MPEG1", VDP_DECODER_PROFILE_MPEG1},
  43. {"MPEG2_SIMPLE", VDP_DECODER_PROFILE_MPEG2_SIMPLE},
  44. {"MPEG2_MAIN", VDP_DECODER_PROFILE_MPEG2_MAIN},
  45. {"H264_BASELINE",VDP_DECODER_PROFILE_H264_BASELINE},
  46. {"H264_MAIN", VDP_DECODER_PROFILE_H264_MAIN},
  47. {"H264_HIGH", VDP_DECODER_PROFILE_H264_HIGH},
  48. {"VC1_SIMPLE", VDP_DECODER_PROFILE_VC1_SIMPLE},
  49. {"VC1_MAIN", VDP_DECODER_PROFILE_VC1_MAIN},
  50. {"VC1_ADVANCED", VDP_DECODER_PROFILE_VC1_ADVANCED},
  51. #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
  52. {"MPEG4_PART2_ASP", VDP_DECODER_PROFILE_MPEG4_PART2_ASP},
  53. #endif
  54. };
  55. const size_t decoder_profile_count = sizeof(decoder_profiles)/sizeof(CDecoder::Desc);
  56. static struct SInterlaceMapping
  57. {
  58. const EINTERLACEMETHOD method;
  59. const VdpVideoMixerFeature feature;
  60. } g_interlace_mapping[] =
  61. { {VS_INTERLACEMETHOD_VDPAU_TEMPORAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
  62. , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
  63. , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
  64. , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF, VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
  65. , {VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE , VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE}
  66. , {VS_INTERLACEMETHOD_NONE , (VdpVideoMixerFeature)-1}
  67. };
  68. static float studioCSCKCoeffs601[3] = {0.299, 0.587, 0.114}; //BT601 {Kr, Kg, Kb}
  69. static float studioCSCKCoeffs709[3] = {0.2126, 0.7152, 0.0722}; //BT709 {Kr, Kg, Kb}
  70. //-----------------------------------------------------------------------------
  71. //-----------------------------------------------------------------------------
  72. CVDPAUContext *CVDPAUContext::m_context = 0;
  73. CCriticalSection CVDPAUContext::m_section;
  74. Display *CVDPAUContext::m_display = 0;
  75. void *CVDPAUContext::m_dlHandle = 0;
  76. CVDPAUContext::CVDPAUContext()
  77. {
  78. m_context = 0;
  79. m_refCount = 0;
  80. }
  81. void CVDPAUContext::Release()
  82. {
  83. CSingleLock lock(m_section);
  84. m_refCount--;
  85. if (m_refCount <= 0)
  86. {
  87. Close();
  88. delete this;
  89. m_context = 0;
  90. }
  91. }
  92. void CVDPAUContext::Close()
  93. {
  94. CLog::Log(LOGNOTICE, "VDPAU::Close - closing decoder context");
  95. DestroyContext();
  96. }
  97. bool CVDPAUContext::EnsureContext(CVDPAUContext **ctx)
  98. {
  99. CSingleLock lock(m_section);
  100. if (m_context)
  101. {
  102. m_context->m_refCount++;
  103. *ctx = m_context;
  104. return true;
  105. }
  106. m_context = new CVDPAUContext();
  107. *ctx = m_context;
  108. {
  109. CSingleLock gLock(g_graphicsContext);
  110. if (!m_context->LoadSymbols() || !m_context->CreateContext())
  111. {
  112. delete m_context;
  113. m_context = 0;
  114. return false;
  115. }
  116. }
  117. m_context->m_refCount++;
  118. *ctx = m_context;
  119. return true;
  120. }
  121. VDPAU_procs& CVDPAUContext::GetProcs()
  122. {
  123. return m_vdpProcs;
  124. }
  125. VdpVideoMixerFeature* CVDPAUContext::GetFeatures()
  126. {
  127. return m_vdpFeatures;
  128. }
  129. int CVDPAUContext::GetFeatureCount()
  130. {
  131. return m_featureCount;
  132. }
  133. bool CVDPAUContext::LoadSymbols()
  134. {
  135. if (!m_dlHandle)
  136. {
  137. m_dlHandle = dlopen("libvdpau.so.1", RTLD_LAZY);
  138. if (!m_dlHandle)
  139. {
  140. const char* error = dlerror();
  141. if (!error)
  142. error = "dlerror() returned NULL";
  143. CLog::Log(LOGERROR,"VDPAU::LoadSymbols: Unable to get handle to lib: %s", error);
  144. return false;
  145. }
  146. }
  147. char* error;
  148. (void)dlerror();
  149. dl_vdp_device_create_x11 = (VdpStatus (*)(Display*, int, VdpDevice*, VdpStatus (**)(VdpDevice, VdpFuncId, void**)))dlsym(m_dlHandle, (const char*)"vdp_device_create_x11");
  150. error = dlerror();
  151. if (error)
  152. {
  153. CLog::Log(LOGERROR,"(VDPAU) - %s in %s",error,__FUNCTION__);
  154. m_vdpDevice = VDP_INVALID_HANDLE;
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool CVDPAUContext::CreateContext()
  160. {
  161. CLog::Log(LOGNOTICE,"VDPAU::CreateContext - creating decoder context");
  162. int mScreen;
  163. { CSingleLock lock(g_graphicsContext);
  164. if (!m_display)
  165. m_display = XOpenDisplay(NULL);
  166. mScreen = g_Windowing.GetCurrentScreen();
  167. }
  168. VdpStatus vdp_st;
  169. // Create Device
  170. vdp_st = dl_vdp_device_create_x11(m_display,
  171. mScreen,
  172. &m_vdpDevice,
  173. &m_vdpProcs.vdp_get_proc_address);
  174. CLog::Log(LOGNOTICE,"vdp_device = 0x%08x vdp_st = 0x%08x",m_vdpDevice,vdp_st);
  175. if (vdp_st != VDP_STATUS_OK)
  176. {
  177. CLog::Log(LOGERROR,"(VDPAU) unable to init VDPAU - vdp_st = 0x%x. Falling back.",vdp_st);
  178. m_vdpDevice = VDP_INVALID_HANDLE;
  179. return false;
  180. }
  181. QueryProcs();
  182. SpewHardwareAvailable();
  183. return true;
  184. }
  185. void CVDPAUContext::QueryProcs()
  186. {
  187. VdpStatus vdp_st;
  188. #define VDP_PROC(id, proc) \
  189. do { \
  190. vdp_st = m_vdpProcs.vdp_get_proc_address(m_vdpDevice, id, (void**)&proc); \
  191. if (vdp_st != VDP_STATUS_OK) \
  192. { \
  193. CLog::Log(LOGERROR, "CVDPAUContext::GetProcs - failed to get proc id"); \
  194. } \
  195. } while(0);
  196. VDP_PROC(VDP_FUNC_ID_GET_ERROR_STRING , m_vdpProcs.vdp_get_error_string);
  197. VDP_PROC(VDP_FUNC_ID_DEVICE_DESTROY , m_vdpProcs.vdp_device_destroy);
  198. VDP_PROC(VDP_FUNC_ID_GENERATE_CSC_MATRIX , m_vdpProcs.vdp_generate_csc_matrix);
  199. VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_CREATE , m_vdpProcs.vdp_video_surface_create);
  200. VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY , m_vdpProcs.vdp_video_surface_destroy);
  201. VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_put_bits_y_cb_cr);
  202. VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_get_bits_y_cb_cr);
  203. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_output_surface_put_bits_y_cb_cr);
  204. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE , m_vdpProcs.vdp_output_surface_put_bits_native);
  205. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_CREATE , m_vdpProcs.vdp_output_surface_create);
  206. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY , m_vdpProcs.vdp_output_surface_destroy);
  207. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE , m_vdpProcs.vdp_output_surface_get_bits_native);
  208. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, m_vdpProcs.vdp_output_surface_render_output_surface);
  209. VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED , m_vdpProcs.vdp_output_surface_put_bits_indexed);
  210. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_CREATE , m_vdpProcs.vdp_video_mixer_create);
  211. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES , m_vdpProcs.vdp_video_mixer_set_feature_enables);
  212. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_DESTROY , m_vdpProcs.vdp_video_mixer_destroy);
  213. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_RENDER , m_vdpProcs.vdp_video_mixer_render);
  214. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES , m_vdpProcs.vdp_video_mixer_set_attribute_values);
  215. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_PARAMETER_SUPPORT , m_vdpProcs.vdp_video_mixer_query_parameter_support);
  216. VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_FEATURE_SUPPORT , m_vdpProcs.vdp_video_mixer_query_feature_support);
  217. VDP_PROC(VDP_FUNC_ID_DECODER_CREATE , m_vdpProcs.vdp_decoder_create);
  218. VDP_PROC(VDP_FUNC_ID_DECODER_DESTROY , m_vdpProcs.vdp_decoder_destroy);
  219. VDP_PROC(VDP_FUNC_ID_DECODER_RENDER , m_vdpProcs.vdp_decoder_render);
  220. VDP_PROC(VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES , m_vdpProcs.vdp_decoder_query_caps);
  221. #undef VDP_PROC
  222. }
  223. VdpDevice CVDPAUContext::GetDevice()
  224. {
  225. return m_vdpDevice;
  226. }
  227. void CVDPAUContext::DestroyContext()
  228. {
  229. if (!m_vdpProcs.vdp_device_destroy)
  230. return;
  231. m_vdpProcs.vdp_device_destroy(m_vdpDevice);
  232. m_vdpDevice = VDP_INVALID_HANDLE;
  233. }
  234. void CVDPAUContext::SpewHardwareAvailable() //CopyrighVDPAUt (c) 2008 Wladimir J. van der Laan -- VDPInfo
  235. {
  236. VdpStatus rv;
  237. CLog::Log(LOGNOTICE,"VDPAU Decoder capabilities:");
  238. CLog::Log(LOGNOTICE,"name level macbs width height");
  239. CLog::Log(LOGNOTICE,"------------------------------------");
  240. for(unsigned int x=0; x<decoder_profile_count; ++x)
  241. {
  242. VdpBool is_supported = false;
  243. uint32_t max_level, max_macroblocks, max_width, max_height;
  244. rv = m_vdpProcs.vdp_decoder_query_caps(m_vdpDevice, decoder_profiles[x].id,
  245. &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
  246. if(rv == VDP_STATUS_OK && is_supported)
  247. {
  248. CLog::Log(LOGNOTICE,"%-16s %2i %5i %5i %5i\n", decoder_profiles[x].name,
  249. max_level, max_macroblocks, max_width, max_height);
  250. }
  251. }
  252. CLog::Log(LOGNOTICE,"------------------------------------");
  253. m_featureCount = 0;
  254. #define CHECK_SUPPORT(feature) \
  255. do { \
  256. VdpBool supported; \
  257. if(m_vdpProcs.vdp_video_mixer_query_feature_support(m_vdpDevice, feature, &supported) == VDP_STATUS_OK && supported) { \
  258. CLog::Log(LOGNOTICE, "Mixer feature: "#feature); \
  259. m_vdpFeatures[m_featureCount++] = feature; \
  260. } \
  261. } while(false)
  262. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION);
  263. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_SHARPNESS);
  264. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL);
  265. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL);
  266. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE);
  267. #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
  268. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1);
  269. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2);
  270. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3);
  271. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4);
  272. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5);
  273. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6);
  274. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7);
  275. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8);
  276. CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9);
  277. #endif
  278. #undef CHECK_SUPPORT
  279. }
  280. bool CVDPAUContext::Supports(VdpVideoMixerFeature feature)
  281. {
  282. for(int i = 0; i < m_featureCount; i++)
  283. {
  284. if(m_vdpFeatures[i] == feature)
  285. return true;
  286. }
  287. return false;
  288. }
  289. //-----------------------------------------------------------------------------
  290. // VDPAU Video Surface states
  291. //-----------------------------------------------------------------------------
  292. #define SURFACE_USED_FOR_REFERENCE 0x01
  293. #define SURFACE_USED_FOR_RENDER 0x02
  294. void CVideoSurfaces::AddSurface(VdpVideoSurface surf)
  295. {
  296. CSingleLock lock(m_section);
  297. m_state[surf] = SURFACE_USED_FOR_REFERENCE;
  298. }
  299. void CVideoSurfaces::ClearReference(VdpVideoSurface surf)
  300. {
  301. CSingleLock lock(m_section);
  302. if (m_state.find(surf) == m_state.end())
  303. {
  304. CLog::Log(LOGWARNING, "CVideoSurfaces::ClearReference - surface invalid");
  305. return;
  306. }
  307. m_state[surf] &= ~SURFACE_USED_FOR_REFERENCE;
  308. if (m_state[surf] == 0)
  309. {
  310. m_freeSurfaces.push_back(surf);
  311. }
  312. }
  313. bool CVideoSurfaces::MarkRender(VdpVideoSurface surf)
  314. {
  315. CSingleLock lock(m_section);
  316. if (m_state.find(surf) == m_state.end())
  317. {
  318. CLog::Log(LOGWARNING, "CVideoSurfaces::MarkRender - surface invalid");
  319. return false;
  320. }
  321. std::list<VdpVideoSurface>::iterator it;
  322. it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
  323. if (it != m_freeSurfaces.end())
  324. {
  325. m_freeSurfaces.erase(it);
  326. }
  327. m_state[surf] |= SURFACE_USED_FOR_RENDER;
  328. return true;
  329. }
  330. void CVideoSurfaces::ClearRender(VdpVideoSurface surf)
  331. {
  332. CSingleLock lock(m_section);
  333. if (m_state.find(surf) == m_state.end())
  334. {
  335. CLog::Log(LOGWARNING, "CVideoSurfaces::ClearRender - surface invalid");
  336. return;
  337. }
  338. m_state[surf] &= ~SURFACE_USED_FOR_RENDER;
  339. if (m_state[surf] == 0)
  340. {
  341. m_freeSurfaces.push_back(surf);
  342. }
  343. }
  344. bool CVideoSurfaces::IsValid(VdpVideoSurface surf)
  345. {
  346. CSingleLock lock(m_section);
  347. if (m_state.find(surf) != m_state.end())
  348. return true;
  349. else
  350. return false;
  351. }
  352. VdpVideoSurface CVideoSurfaces::GetFree(VdpVideoSurface surf)
  353. {
  354. CSingleLock lock(m_section);
  355. if (m_state.find(surf) != m_state.end())
  356. {
  357. std::list<VdpVideoSurface>::iterator it;
  358. it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
  359. if (it == m_freeSurfaces.end())
  360. {
  361. CLog::Log(LOGWARNING, "CVideoSurfaces::GetFree - surface not free");
  362. }
  363. else
  364. {
  365. m_freeSurfaces.erase(it);
  366. m_state[surf] = SURFACE_USED_FOR_REFERENCE;
  367. return surf;
  368. }
  369. }
  370. if (!m_freeSurfaces.empty())
  371. {
  372. VdpVideoSurface freeSurf = m_freeSurfaces.front();
  373. m_freeSurfaces.pop_front();
  374. m_state[freeSurf] = SURFACE_USED_FOR_REFERENCE;
  375. return freeSurf;
  376. }
  377. return VDP_INVALID_HANDLE;
  378. }
  379. VdpVideoSurface CVideoSurfaces::GetAtIndex(int idx)
  380. {
  381. if (idx >= m_state.size())
  382. return VDP_INVALID_HANDLE;
  383. std::map<VdpVideoSurface, int>::iterator it = m_state.begin();
  384. for(int i = 0; i < idx; i++)
  385. ++it;
  386. return it->first;
  387. }
  388. VdpVideoSurface CVideoSurfaces::RemoveNext(bool skiprender)
  389. {
  390. CSingleLock lock(m_section);
  391. VdpVideoSurface surf;
  392. std::map<VdpVideoSurface, int>::iterator it;
  393. for(it = m_state.begin(); it != m_state.end(); ++it)
  394. {
  395. if (skiprender && it->second & SURFACE_USED_FOR_RENDER)
  396. continue;
  397. surf = it->first;
  398. m_state.erase(surf);
  399. std::list<VdpVideoSurface>::iterator it2;
  400. it2 = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
  401. if (it2 != m_freeSurfaces.end())
  402. m_freeSurfaces.erase(it2);
  403. return surf;
  404. }
  405. return VDP_INVALID_HANDLE;
  406. }
  407. void CVideoSurfaces::Reset()
  408. {
  409. CSingleLock lock(m_section);
  410. m_freeSurfaces.clear();
  411. m_state.clear();
  412. }
  413. int CVideoSurfaces::Size()
  414. {
  415. CSingleLock lock(m_section);
  416. return m_state.size();
  417. }
  418. //-----------------------------------------------------------------------------
  419. // CVDPAU
  420. //-----------------------------------------------------------------------------
  421. CDecoder::CDecoder() : m_vdpauOutput(&m_inMsgEvent)
  422. {
  423. m_vdpauConfig.videoSurfaces = &m_videoSurfaces;
  424. m_vdpauConfigured = false;
  425. m_hwContext.bitstream_buffers_allocated = 0;
  426. m_DisplayState = VDPAU_OPEN;
  427. m_vdpauConfig.context = 0;
  428. }
  429. bool CDecoder::Open(AVCodecContext* avctx, const enum PixelFormat, unsigned int surfaces)
  430. {
  431. if(avctx->coded_width == 0
  432. || avctx->coded_height == 0)
  433. {
  434. CLog::Log(LOGWARNING,"(VDPAU) no width/height available, can't init");
  435. return false;
  436. }
  437. m_vdpauConfig.numRenderBuffers = surfaces;
  438. m_decoderThread = CThread::GetCurrentThreadId();
  439. if ((avctx->codec_id == AV_CODEC_ID_MPEG4) && !g_advancedSettings.m_videoAllowMpeg4VDPAU)
  440. return false;
  441. if (!CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
  442. return false;
  443. m_DisplayState = VDPAU_OPEN;
  444. m_vdpauConfigured = false;
  445. if (!m_dllAvUtil.Load())
  446. return false;
  447. m_presentPicture = 0;
  448. {
  449. VdpDecoderProfile profile = 0;
  450. if(avctx->codec_id == AV_CODEC_ID_H264)
  451. profile = VDP_DECODER_PROFILE_H264_HIGH;
  452. #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
  453. else if(avctx->codec_id == AV_CODEC_ID_MPEG4)
  454. profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
  455. #endif
  456. if(profile)
  457. {
  458. if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
  459. CLog::Log(LOGWARNING,"(VDPAU) width %i might not be supported because of hardware bug", avctx->width);
  460. /* attempt to create a decoder with this width/height, some sizes are not supported by hw */
  461. VdpStatus vdp_st;
  462. vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(), profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
  463. if(vdp_st != VDP_STATUS_OK)
  464. {
  465. CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) checking for decoder support\n", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
  466. return false;
  467. }
  468. m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
  469. CheckStatus(vdp_st, __LINE__);
  470. }
  471. /* finally setup ffmpeg */
  472. memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
  473. m_hwContext.render = CDecoder::Render;
  474. m_hwContext.bitstream_buffers_allocated = 0;
  475. avctx->get_buffer = CDecoder::FFGetBuffer;
  476. avctx->reget_buffer = CDecoder::FFGetBuffer;
  477. avctx->release_buffer = CDecoder::FFReleaseBuffer;
  478. avctx->draw_horiz_band = CDecoder::FFDrawSlice;
  479. avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
  480. avctx->hwaccel_context = &m_hwContext;
  481. avctx->thread_count = 1;
  482. g_Windowing.Register(this);
  483. return true;
  484. }
  485. return false;
  486. }
  487. CDecoder::~CDecoder()
  488. {
  489. Close();
  490. }
  491. void CDecoder::Close()
  492. {
  493. CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
  494. g_Windowing.Unregister(this);
  495. CSingleLock lock(m_DecoderSection);
  496. FiniVDPAUOutput();
  497. m_vdpauOutput.Dispose();
  498. if (m_hwContext.bitstream_buffers_allocated)
  499. {
  500. m_dllAvUtil.av_freep(&m_hwContext.bitstream_buffers);
  501. }
  502. m_dllAvUtil.Unload();
  503. if (m_vdpauConfig.context)
  504. m_vdpauConfig.context->Release();
  505. m_vdpauConfig.context = 0;
  506. }
  507. long CDecoder::Release()
  508. {
  509. // check if we should do some pre-cleanup here
  510. // a second decoder might need resources
  511. if (m_vdpauConfigured == true)
  512. {
  513. CSingleLock lock(m_DecoderSection);
  514. CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
  515. Message *reply;
  516. if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
  517. &reply,
  518. 2000))
  519. {
  520. bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
  521. reply->Release();
  522. if (!success)
  523. {
  524. CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
  525. m_DisplayState = VDPAU_ERROR;
  526. }
  527. }
  528. else
  529. {
  530. CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
  531. m_DisplayState = VDPAU_ERROR;
  532. }
  533. VdpVideoSurface surf;
  534. while((surf = m_videoSurfaces.RemoveNext(true)) != VDP_INVALID_HANDLE)
  535. {
  536. m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
  537. }
  538. }
  539. return IHardwareDecoder::Release();
  540. }
  541. long CDecoder::ReleasePicReference()
  542. {
  543. return IHardwareDecoder::Release();
  544. }
  545. void CDecoder::SetWidthHeight(int width, int height)
  546. {
  547. m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
  548. //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
  549. //this requires the least amount of gpu memory bandwidth
  550. if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
  551. {
  552. //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
  553. if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
  554. {
  555. m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
  556. m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
  557. }
  558. else //scale height to the desktop size if the aspect ratio is smaller than the desktop
  559. {
  560. m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
  561. m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
  562. }
  563. }
  564. else
  565. { //let opengl scale
  566. m_vdpauConfig.outWidth = width;
  567. m_vdpauConfig.outHeight = height;
  568. }
  569. CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
  570. }
  571. void CDecoder::OnLostDevice()
  572. {
  573. CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
  574. int count = g_graphicsContext.exit();
  575. CSingleLock lock(m_DecoderSection);
  576. FiniVDPAUOutput();
  577. if (m_vdpauConfig.context)
  578. m_vdpauConfig.context->Release();
  579. m_vdpauConfig.context = 0;
  580. m_DisplayState = VDPAU_LOST;
  581. lock.Leave();
  582. m_DisplayEvent.Reset();
  583. g_graphicsContext.restore(count);
  584. }
  585. void CDecoder::OnResetDevice()
  586. {
  587. CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
  588. int count = g_graphicsContext.exit();
  589. CSingleLock lock(m_DecoderSection);
  590. if (m_DisplayState == VDPAU_LOST)
  591. {
  592. m_DisplayState = VDPAU_RESET;
  593. lock.Leave();
  594. m_DisplayEvent.Set();
  595. }
  596. g_graphicsContext.restore(count);
  597. }
  598. int CDecoder::Check(AVCodecContext* avctx)
  599. {
  600. EDisplayState state;
  601. { CSingleLock lock(m_DecoderSection);
  602. state = m_DisplayState;
  603. }
  604. if (state == VDPAU_LOST)
  605. {
  606. CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
  607. if (!m_DisplayEvent.WaitMSec(4000))
  608. {
  609. CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
  610. state = VDPAU_RESET;
  611. }
  612. else
  613. {
  614. CSingleLock lock(m_DecoderSection);
  615. state = m_DisplayState;
  616. }
  617. }
  618. if (state == VDPAU_RESET || state == VDPAU_ERROR)
  619. {
  620. CSingleLock lock(m_DecoderSection);
  621. FiniVDPAUOutput();
  622. if (m_vdpauConfig.context)
  623. m_vdpauConfig.context->Release();
  624. m_vdpauConfig.context = 0;
  625. if (CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
  626. {
  627. m_DisplayState = VDPAU_OPEN;
  628. m_vdpauConfigured = false;
  629. }
  630. if (state == VDPAU_RESET)
  631. return VC_FLUSHED;
  632. else
  633. return VC_ERROR;
  634. }
  635. return 0;
  636. }
  637. bool CDecoder::IsVDPAUFormat(PixelFormat format)
  638. {
  639. if (format == AV_PIX_FMT_VDPAU)
  640. return true;
  641. else
  642. return false;
  643. }
  644. bool CDecoder::Supports(VdpVideoMixerFeature feature)
  645. {
  646. return m_vdpauConfig.context->Supports(feature);
  647. }
  648. bool CDecoder::Supports(EINTERLACEMETHOD method)
  649. {
  650. if(method == VS_INTERLACEMETHOD_VDPAU_BOB
  651. || method == VS_INTERLACEMETHOD_AUTO)
  652. return true;
  653. if (method == VS_INTERLACEMETHOD_RENDER_BOB)
  654. return true;
  655. if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
  656. return false;
  657. for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
  658. {
  659. if(p->method == method)
  660. return Supports(p->feature);
  661. }
  662. return false;
  663. }
  664. EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
  665. {
  666. return VS_INTERLACEMETHOD_RENDER_BOB;
  667. }
  668. void CDecoder::FiniVDPAUOutput()
  669. {
  670. if (!m_vdpauConfigured)
  671. return;
  672. CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
  673. // uninit output
  674. m_vdpauOutput.Dispose();
  675. m_vdpauConfigured = false;
  676. VdpStatus vdp_st;
  677. vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
  678. if (CheckStatus(vdp_st, __LINE__))
  679. return;
  680. m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
  681. CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", m_videoSurfaces.Size());
  682. VdpVideoSurface surf;
  683. while((surf = m_videoSurfaces.RemoveNext()) != VDP_INVALID_HANDLE)
  684. {
  685. m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
  686. if (CheckStatus(vdp_st, __LINE__))
  687. return;
  688. }
  689. m_videoSurfaces.Reset();
  690. }
  691. void CDecoder::ReadFormatOf( AVCodecID codec
  692. , VdpDecoderProfile &vdp_decoder_profile
  693. , VdpChromaType &vdp_chroma_type)
  694. {
  695. switch (codec)
  696. {
  697. case AV_CODEC_ID_MPEG1VIDEO:
  698. vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
  699. vdp_chroma_type = VDP_CHROMA_TYPE_420;
  700. break;
  701. case AV_CODEC_ID_MPEG2VIDEO:
  702. vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
  703. vdp_chroma_type = VDP_CHROMA_TYPE_420;
  704. break;
  705. case AV_CODEC_ID_H264:
  706. vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
  707. vdp_chroma_type = VDP_CHROMA_TYPE_420;
  708. break;
  709. case AV_CODEC_ID_WMV3:
  710. vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
  711. vdp_chroma_type = VDP_CHROMA_TYPE_420;
  712. break;
  713. case AV_CODEC_ID_VC1:
  714. vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
  715. vdp_chroma_type = VDP_CHROMA_TYPE_420;
  716. break;
  717. case AV_CODEC_ID_MPEG4:
  718. vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
  719. vdp_chroma_type = VDP_CHROMA_TYPE_420;
  720. break;
  721. default:
  722. vdp_decoder_profile = 0;
  723. vdp_chroma_type = 0;
  724. break;
  725. }
  726. }
  727. bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
  728. {
  729. FiniVDPAUOutput();
  730. VdpStatus vdp_st;
  731. VdpDecoderProfile vdp_decoder_profile;
  732. m_vdpauConfig.vidWidth = avctx->width;
  733. m_vdpauConfig.vidHeight = avctx->height;
  734. m_vdpauConfig.surfaceWidth = avctx->coded_width;
  735. m_vdpauConfig.surfaceHeight = avctx->coded_height;
  736. SetWidthHeight(avctx->width,avctx->height);
  737. CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
  738. CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
  739. ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
  740. if(avctx->codec_id == AV_CODEC_ID_H264)
  741. {
  742. m_vdpauConfig.maxReferences = ref_frames;
  743. if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
  744. if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
  745. }
  746. else
  747. m_vdpauConfig.maxReferences = 2;
  748. vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(),
  749. vdp_decoder_profile,
  750. m_vdpauConfig.surfaceWidth,
  751. m_vdpauConfig.surfaceHeight,
  752. m_vdpauConfig.maxReferences,
  753. &m_vdpauConfig.vdpDecoder);
  754. if (CheckStatus(vdp_st, __LINE__))
  755. return false;
  756. // initialize output
  757. CSingleLock lock(g_graphicsContext);
  758. m_vdpauConfig.stats = &m_bufferStats;
  759. m_vdpauConfig.vdpau = this;
  760. m_bufferStats.Reset();
  761. m_vdpauOutput.Start();
  762. Message *reply;
  763. if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::INIT,
  764. &reply,
  765. 2000,
  766. &m_vdpauConfig,
  767. sizeof(m_vdpauConfig)))
  768. {
  769. bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
  770. reply->Release();
  771. if (!success)
  772. {
  773. CLog::Log(LOGERROR, "VDPAU::%s - vdpau output returned error", __FUNCTION__);
  774. m_vdpauOutput.Dispose();
  775. return false;
  776. }
  777. }
  778. else
  779. {
  780. CLog::Log(LOGERROR, "VDPAU::%s - failed to init output", __FUNCTION__);
  781. m_vdpauOutput.Dispose();
  782. return false;
  783. }
  784. m_inMsgEvent.Reset();
  785. m_vdpauConfigured = true;
  786. return true;
  787. }
  788. int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic)
  789. {
  790. //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
  791. CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
  792. CDecoder* vdp = (CDecoder*)ctx->GetHardware();
  793. // while we are waiting to recover we can't do anything
  794. CSingleLock lock(vdp->m_DecoderSection);
  795. if(vdp->m_DisplayState != VDPAU_OPEN)
  796. {
  797. CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
  798. return -1;
  799. }
  800. VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
  801. surf = vdp->m_videoSurfaces.GetFree(surf != 0 ? surf : VDP_INVALID_HANDLE);
  802. VdpStatus vdp_st = VDP_STATUS_ERROR;
  803. if (surf == VDP_INVALID_HANDLE)
  804. {
  805. // create a new surface
  806. VdpDecoderProfile profile;
  807. ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
  808. vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_video_surface_create(vdp->m_vdpauConfig.context->GetDevice(),
  809. vdp->m_vdpauConfig.vdpChromaType,
  810. avctx->coded_width,
  811. avctx->coded_height,
  812. &surf);
  813. vdp->CheckStatus(vdp_st, __LINE__);
  814. if (vdp_st != VDP_STATUS_OK)
  815. {
  816. CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
  817. return -1;
  818. }
  819. vdp->m_videoSurfaces.AddSurface(surf);
  820. }
  821. pic->data[1] = pic->data[2] = NULL;
  822. pic->data[0] = (uint8_t*)(uintptr_t)surf;
  823. pic->data[3] = (uint8_t*)(uintptr_t)surf;
  824. pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
  825. pic->type= FF_BUFFER_TYPE_USER;
  826. pic->reordered_opaque= avctx->reordered_opaque;
  827. return 0;
  828. }
  829. void CDecoder::FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic)
  830. {
  831. CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
  832. CDecoder* vdp = (CDecoder*)ctx->GetHardware();
  833. VdpVideoSurface surf;
  834. unsigned int i;
  835. CSingleLock lock(vdp->m_DecoderSection);
  836. surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
  837. vdp->m_videoSurfaces.ClearReference(surf);
  838. for(i=0; i<4; i++)
  839. pic->data[i]= NULL;
  840. }
  841. VdpStatus CDecoder::Render( VdpDecoder decoder, VdpVideoSurface target,
  842. VdpPictureInfo const *picture_info,
  843. uint32_t bitstream_buffer_count,
  844. VdpBitstreamBuffer const * bitstream_buffers)
  845. {
  846. return VDP_STATUS_OK;
  847. }
  848. void CDecoder::FFDrawSlice(struct AVCodecContext *s,
  849. const AVFrame *src, int offset[4],
  850. int y, int type, int height)
  851. {
  852. CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
  853. CDecoder* vdp = (CDecoder*)ctx->GetHardware();
  854. // while we are waiting to recover we can't do anything
  855. CSingleLock lock(vdp->m_DecoderSection);
  856. if(vdp->m_DisplayState != VDPAU_OPEN)
  857. return;
  858. if(src->linesize[0] || src->linesize[1] || src->linesize[2]
  859. || offset[0] || offset[1] || offset[2])
  860. {
  861. CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
  862. return;
  863. }
  864. VdpStatus vdp_st;
  865. VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
  866. // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
  867. if (!vdp->m_videoSurfaces.IsValid(surf))
  868. {
  869. CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
  870. return;
  871. }
  872. uint32_t max_refs = 0;
  873. if(s->codec_id == AV_CODEC_ID_H264)
  874. max_refs = vdp->m_hwContext.info.h264.num_ref_frames;
  875. if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
  876. || vdp->m_vdpauConfigured == false
  877. || vdp->m_vdpauConfig.maxReferences < max_refs)
  878. {
  879. if(!vdp->ConfigVDPAU(s, max_refs))
  880. return;
  881. }
  882. uint64_t startTime = CurrentHostCounter();
  883. uint16_t decoded, processed, rend;
  884. vdp->m_bufferStats.Get(decoded, processed, rend);
  885. vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
  886. surf,
  887. (VdpPictureInfo const *)&(vdp->m_hwContext.info),
  888. vdp->m_hwContext.bitstream_buffers_used,
  889. vdp->m_hwContext.bitstream_buffers);
  890. vdp->CheckStatus(vdp_st, __LINE__);
  891. uint64_t diff = CurrentHostCounter() - startTime;
  892. if (diff*1000/CurrentHostFrequency() > 30)
  893. CLog::Log(LOGDEBUG, "CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
  894. }
  895. int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
  896. {
  897. int result = Check(avctx);
  898. if (result)
  899. return result;
  900. CSingleLock lock(m_DecoderSection);
  901. if (!m_vdpauConfigured)
  902. return VC_ERROR;
  903. if(pFrame)
  904. { // we have a new frame from decoder
  905. VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pFrame->data[3];
  906. // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
  907. if (!m_videoSurfaces.IsValid(surf))
  908. {
  909. CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
  910. return VC_BUFFER;
  911. }
  912. m_videoSurfaces.MarkRender(surf);
  913. // send frame to output for processing
  914. CVdpauDecodedPicture pic;
  915. memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
  916. ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
  917. pic.videoSurface = surf;
  918. pic.DVDPic.color_matrix = avctx->colorspace;
  919. m_bufferStats.IncDecoded();
  920. m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
  921. //TODO
  922. // m_codecControl = pic.DVDPic.iFlags & (DVP_FLAG_DRAIN | DVP_FLAG_NO_POSTPROC);
  923. }
  924. int retval = 0;
  925. uint16_t decoded, processed, render;
  926. Message *msg;
  927. while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
  928. {
  929. if (msg->signal == COutputControlProtocol::ERROR)
  930. {
  931. m_DisplayState = VDPAU_ERROR;
  932. retval |= VC_ERROR;
  933. }
  934. msg->Release();
  935. }
  936. m_bufferStats.Get(decoded, processed, render);
  937. uint64_t startTime = CurrentHostCounter();
  938. while (!retval)
  939. {
  940. // first fill the buffers to keep vdpau busy
  941. // mixer will run with decoded >= 2. output is limited by number of output surfaces
  942. // In case mixer is bypassed we limit by looking at processed
  943. if (decoded < 3 && processed < 3)
  944. {
  945. retval |= VC_BUFFER;
  946. }
  947. else if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
  948. {
  949. if (msg->signal == COutputDataProtocol::PICTURE)
  950. {
  951. if (m_presentPicture)
  952. {
  953. m_presentPicture->ReturnUnused();
  954. m_presentPicture = 0;
  955. }
  956. m_presentPicture = *(CVdpauRenderPicture**)msg->data;
  957. m_presentPicture->vdpau = this;
  958. m_bufferStats.DecRender();
  959. m_bufferStats.Get(decoded, processed, render);
  960. retval |= VC_PICTURE;
  961. msg->Release();
  962. break;
  963. }
  964. msg->Release();
  965. }
  966. else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
  967. {
  968. if (msg->signal == COutputControlProtocol::STATS)
  969. {
  970. m_bufferStats.Get(decoded, processed, render);
  971. }
  972. else
  973. {
  974. m_DisplayState = VDPAU_ERROR;
  975. retval |= VC_ERROR;
  976. }
  977. msg->Release();
  978. }
  979. if (decoded < 3 && processed < 3)
  980. {
  981. retval |= VC_BUFFER;
  982. }
  983. if (!retval && !m_inMsgEvent.WaitMSec(2000))
  984. break;
  985. }
  986. uint64_t diff = CurrentHostCounter() - startTime;
  987. if (retval & VC_PICTURE)
  988. {
  989. m_bufferStats.SetParams(diff, m_codecControl);
  990. }
  991. if (diff*1000/CurrentHostFrequency() > 50)
  992. CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
  993. if (!retval)
  994. {
  995. CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
  996. m_DisplayState = VDPAU_ERROR;
  997. retval |= VC_ERROR;
  998. }
  999. return retval;
  1000. }
  1001. bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
  1002. {
  1003. CSingleLock lock(m_DecoderSection);
  1004. if (m_DisplayState != VDPAU_OPEN)
  1005. return false;
  1006. *picture = m_presentPicture->DVDPic;
  1007. picture->vdpau = m_presentPicture;
  1008. return true;
  1009. }
  1010. void CDecoder::Reset()
  1011. {
  1012. CSingleLock lock(m_DecoderSection);
  1013. if (!m_vdpauConfigured)
  1014. return;
  1015. Message *reply;
  1016. if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
  1017. &reply,
  1018. 2000))
  1019. {
  1020. bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
  1021. reply->Release();
  1022. if (!success)
  1023. {
  1024. CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
  1025. m_DisplayState = VDPAU_ERROR;
  1026. }
  1027. else
  1028. m_bufferStats.Reset();
  1029. }
  1030. else
  1031. {
  1032. CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
  1033. m_DisplayState = VDPAU_ERROR;
  1034. }
  1035. }
  1036. bool CDecoder::CanSkipDeint()
  1037. {
  1038. return m_bufferStats.CanSkipDeint();
  1039. }
  1040. void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
  1041. {
  1042. m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
  1043. }
  1044. bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
  1045. {
  1046. if (vdp_st != VDP_STATUS_OK)
  1047. {
  1048. CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) at %s:%d\n", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st, __FILE__, line);
  1049. if(m_DisplayState == VDPAU_OPEN)
  1050. {
  1051. if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
  1052. {
  1053. m_DisplayEvent.Reset();
  1054. m_DisplayState = VDPAU_LOST;
  1055. }
  1056. else
  1057. m_DisplayState = VDPAU_ERROR;
  1058. }
  1059. return true;
  1060. }
  1061. return false;
  1062. }
  1063. //-----------------------------------------------------------------------------
  1064. // RenderPicture
  1065. //-----------------------------------------------------------------------------
  1066. CVdpauRenderPicture* CVdpauRenderPicture::Acquire()
  1067. {
  1068. CSingleLock lock(renderPicSection);
  1069. if (refCount == 0)
  1070. vdpau->Acquire();
  1071. refCount++;
  1072. return this;
  1073. }
  1074. long CVdpauRenderPicture::Release()
  1075. {
  1076. CSingleLock lock(renderPicSection);
  1077. refCount--;
  1078. if (refCount > 0)
  1079. return refCount;
  1080. lock.Leave();
  1081. vdpau->ReturnRenderPicture(this);
  1082. vdpau->ReleasePicReference();
  1083. return refCount;
  1084. }
  1085. void CVdpauRenderPicture::ReturnUnused()
  1086. {
  1087. { CSingleLock lock(renderPicSection);
  1088. if (refCount > 0)
  1089. return;
  1090. }
  1091. if (vdpau)
  1092. vdpau->ReturnRenderPicture(this);
  1093. }
  1094. void CVdpauRenderPicture::Sync()
  1095. {
  1096. #ifdef GL_ARB_sync
  1097. CSingleLock lock(renderPicSection);
  1098. if (usefence)
  1099. {
  1100. if(glIsSync(fence))
  1101. {
  1102. glDeleteSync(fence);
  1103. fence = None;
  1104. }
  1105. fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  1106. }
  1107. #endif
  1108. }
  1109. //-----------------------------------------------------------------------------
  1110. // Mixer
  1111. //-----------------------------------------------------------------------------
  1112. CMixer::CMixer(CEvent *inMsgEvent) :
  1113. CThread("Vdpau Mixer"),
  1114. m_controlPort("ControlPort", inMsgEvent, &m_outMsgEvent),
  1115. m_dataPort("DataPort", inMsgEvent, &m_outMsgEvent)
  1116. {
  1117. m_inMsgEvent = inMsgEvent;
  1118. }
  1119. CMixer::~CMixer()
  1120. {
  1121. Dispose();
  1122. }
  1123. void CMixer::Start()
  1124. {
  1125. Create();
  1126. }
  1127. void CMixer::Dispose()
  1128. {
  1129. m_bStop = true;
  1130. m_outMsgEvent.Set();
  1131. StopThread();
  1132. m_controlPort.Purge();
  1133. m_dataPort.Purge();
  1134. }
  1135. bool CMixer::IsActive()
  1136. {
  1137. return IsRunning();
  1138. }
  1139. void CMixer::OnStartup()
  1140. {
  1141. CLog::Log(LOGNOTICE, "CMixer::OnStartup: Output Thread created");
  1142. }
  1143. void CMixer::OnExit()
  1144. {
  1145. CLog::Log(LOGNOTICE, "CMixer::OnExit: Output Thread terminated");
  1146. }
  1147. enum MIXER_STATES
  1148. {
  1149. M_TOP = 0, // 0
  1150. M_TOP_ERROR, // 1
  1151. M_TOP_UNCONFIGURED, // 2
  1152. M_TOP_CONFIGURED, // 3
  1153. M_TOP_CONFIGURED_WAIT1, // 4
  1154. M_TOP_CONFIGURED_STEP1, // 5
  1155. M_TOP_CONFIGURED_WAIT2, // 6
  1156. M_TOP_CONFIGURED_STEP2, // 7
  1157. };
  1158. int MIXER_parentStates[] = {
  1159. -1,
  1160. 0, //TOP_ERROR
  1161. 0, //TOP_UNCONFIGURED
  1162. 0, //TOP_CONFIGURED
  1163. 3, //TOP_CONFIGURED_WAIT1
  1164. 3, //TOP_CONFIGURED_STEP1
  1165. 3, //TOP_CONFIGURED_WAIT2
  1166. 3, //TOP_CONFIGURED_STEP2
  1167. };
  1168. void CMixer::StateMachine(int signal, Protocol *port, Message *msg)
  1169. {
  1170. for (int state = m_state; ; state = MIXER_parentStates[state])
  1171. {
  1172. switch (state)
  1173. {
  1174. case M_TOP: // TOP
  1175. if (port == &m_controlPort)
  1176. {
  1177. switch (signal)
  1178. {
  1179. case CMixerControlProtocol::FLUSH:
  1180. Flush();
  1181. msg->Reply(CMixerControlProtocol::ACC);
  1182. return;
  1183. default:
  1184. break;
  1185. }
  1186. }
  1187. {
  1188. std::string portName = port == NULL ? "timer" : port->portName;
  1189. CLog::Log(LOGWARNING, "CMixer::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
  1190. }
  1191. return;
  1192. case M_TOP_ERROR: // TOP
  1193. break;
  1194. case M_TOP_UNCONFIGURED:
  1195. if (port == &m_controlPort)
  1196. {
  1197. switch (signal)
  1198. {
  1199. case CMixerControlProtocol::INIT:
  1200. CVdpauConfig *data;
  1201. data = (CVdpauConfig*)msg->data;
  1202. if (data)
  1203. {
  1204. m_config = *data;
  1205. }
  1206. Init();
  1207. if (!m_vdpError)
  1208. {
  1209. m_state = M_TOP_CONFIGURED_WAIT1;
  1210. msg->Reply(CMixerControlProtocol::ACC);
  1211. }
  1212. else
  1213. {
  1214. msg->Reply(CMixerControlProtocol::ERROR);
  1215. }
  1216. return;
  1217. default:
  1218. break;
  1219. }
  1220. }
  1221. break;
  1222. case M_TOP_CONFIGURED:
  1223. if (port == &m_dataPort)
  1224. {
  1225. switch (signal)
  1226. {
  1227. case CMixerDataProtocol::FRAME:
  1228. CVdpauDecodedPicture *frame;
  1229. frame = (CVdpauDecodedPicture*)msg->data;
  1230. if (frame)
  1231. {
  1232. m_decodedPics.push(*frame);
  1233. }
  1234. m_extTimeout = 0;
  1235. return;
  1236. case CMixerDataProtocol::BUFFER:
  1237. VdpOutputSurface *surf;
  1238. surf = (VdpOutputSurface*)msg->data;
  1239. if (surf)
  1240. {
  1241. m_outputSurfaces.push(*surf);
  1242. }
  1243. m_extTimeout = 0;
  1244. return;
  1245. default:
  1246. break;
  1247. }
  1248. }
  1249. break;
  1250. case M_TOP_CONFIGURED_WAIT1:
  1251. if (port == NULL) // timeout
  1252. {
  1253. switch (signal)
  1254. {
  1255. case CMixerControlProtocol::TIMEOUT:
  1256. if (!m_decodedPics.empty() && !m_outputSurfaces.empty())
  1257. {
  1258. m_state = M_TOP_CONFIGURED_STEP1;
  1259. m_bStateMachineSelfTrigger = true;
  1260. }
  1261. else
  1262. {
  1263. m_extTimeout = 100;
  1264. }
  1265. return;
  1266. default:
  1267. break;
  1268. }
  1269. }
  1270. break;
  1271. case M_TOP_CONFIGURED_STEP1:
  1272. if (port == NULL) // timeout
  1273. {
  1274. switch (signal)
  1275. {
  1276. case CMixerControlProtocol::TIMEOUT:
  1277. m_mixerInput.push_front(m_decodedPics.front());
  1278. m_decodedPics.pop();
  1279. if (m_mixerInput.size() < 2)
  1280. {
  1281. m_state = M_TOP_CONFIGURED_WAIT1;
  1282. m_extTimeout = 0;
  1283. return;
  1284. }
  1285. InitCycle();
  1286. ProcessPicture();
  1287. if (m_vdpError)
  1288. {
  1289. m_state = M_TOP_CONFIGURED_WAIT1;
  1290. m_extTimeout = 1000;
  1291. return;
  1292. }
  1293. if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
  1294. m_outputSurfaces.pop();
  1295. m_config.stats->IncProcessed();
  1296. m_config.stats->DecDecoded();
  1297. m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
  1298. if (m_mixersteps > 1)
  1299. {
  1300. m_state = M_TOP_CONFIGURED_WAIT2;
  1301. m_extTimeout = 0;
  1302. }
  1303. else
  1304. {
  1305. FiniCycle();
  1306. m_state = M_TOP_CONFIGURED_WAIT1;
  1307. m_extTimeout = 0;
  1308. }
  1309. return;
  1310. default:
  1311. break;
  1312. }
  1313. }
  1314. break;
  1315. case M_TOP_CONFIGURED_WAIT2:
  1316. if (port == NULL) // timeout
  1317. {
  1318. switch (signal)
  1319. {
  1320. case CMixerControlProtocol::TIMEOUT:
  1321. if (!m_outputSurfaces.empty())
  1322. {
  1323. m_state = M_TOP_CONFIGURED_STEP2;
  1324. m_bStateMachineSelfTrigger = true;
  1325. }
  1326. else
  1327. {
  1328. m_extTimeout = 100;
  1329. }
  1330. return;
  1331. default:
  1332. break;
  1333. }
  1334. }
  1335. break;
  1336. case M_TOP_CONFIGURED_STEP2:
  1337. if (port == NULL) // timeout
  1338. {
  1339. switch (signal)
  1340. {
  1341. case CMixerControlProtocol::TIMEOUT:
  1342. m_processPicture.outputSurface = m_outputSurfaces.front();
  1343. m_mixerstep = 1;
  1344. ProcessPicture();
  1345. if (m_vdpError)
  1346. {
  1347. m_state = M_TOP_CONFIGURED_WAIT1;
  1348. m_extTimeout = 1000;
  1349. return;
  1350. }
  1351. if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
  1352. m_outputSurfaces.pop();
  1353. m_config.stats->IncProcessed();
  1354. m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
  1355. FiniCycle();
  1356. m_state = M_TOP_CONFIGURED_WAIT1;
  1357. m_extTimeout = 0;
  1358. return;
  1359. default:
  1360. break;
  1361. }
  1362. }
  1363. break;
  1364. default: // we are in no state, should not happen
  1365. CLog::Log(LOGERROR, "CMixer::%s - no valid state: %d", __FUNCTION__, m_state);
  1366. return;
  1367. }
  1368. } // for
  1369. }
  1370. void CMixer::Process()
  1371. {
  1372. Message *msg = NULL;
  1373. Protocol *port = NULL;
  1374. bool gotMsg;
  1375. m_state = M_TOP_UNCONFIGURED;
  1376. m_extTimeout = 1000;
  1377. m_bStateMachineSelfTrigger = false;
  1378. while (!m_bStop)
  1379. {
  1380. gotMsg = false;
  1381. if (m_bStateMachineSelfTrigger)
  1382. {
  1383. m_bStateMachineSelfTrigger = false;
  1384. // self trigger state machine
  1385. StateMachine(msg->signal, port, msg);
  1386. if (!m_bStateMachineSelfTrigger)
  1387. {
  1388. msg->Release();
  1389. msg = NULL;
  1390. }
  1391. continue;
  1392. }
  1393. // check control port
  1394. else if (m_controlPort.ReceiveOutMessage(&msg))
  1395. {
  1396. gotMsg = true;
  1397. port = &m_controlPort;
  1398. }
  1399. // check data port
  1400. else if (m_dataPort.ReceiveOutMessage(&msg))
  1401. {
  1402. gotMsg = true;
  1403. port = &m_dataPort;
  1404. }
  1405. if (gotMsg)
  1406. {
  1407. StateMachine(msg->signal, port, msg);
  1408. if (!m_bStateMachineSelfTrigger)
  1409. {
  1410. msg->Release();
  1411. msg = NULL;
  1412. }
  1413. continue;
  1414. }
  1415. // wait for message
  1416. else if (m_outMsgEvent.WaitMSec(m_extTimeout))
  1417. {
  1418. continue;
  1419. }
  1420. // time out
  1421. else
  1422. {
  1423. msg = m_controlPort.GetMessage();
  1424. msg->signal = CMixerControlProtocol::TIMEOUT;
  1425. port = 0;
  1426. // signal timeout to state machine
  1427. StateMachine(msg->signal, port, msg);
  1428. if (!m_bStateMachineSelfTrigger)
  1429. {
  1430. msg->Release();
  1431. msg = NULL;
  1432. }
  1433. }
  1434. }
  1435. Uninit();
  1436. }
  1437. void CMixer::CreateVdpauMixer()
  1438. {
  1439. CLog::Log(LOGNOTICE, " (VDPAU) Creating the video mixer");
  1440. InitCSCMatrix(m_config.vidWidth);
  1441. VdpVideoMixerParameter parameters[] = {
  1442. VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
  1443. VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
  1444. VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE};
  1445. void const * parameter_values[] = {
  1446. &m_config.surfaceWidth,
  1447. &m_config.surfaceHeight,
  1448. &m_config.vdpChromaType};
  1449. VdpStatus vdp_st = VDP_STATUS_ERROR;
  1450. vdp_st = m_config.context->GetProcs().vdp_video_mixer_create(m_config.context->GetDevice(),
  1451. m_config.context->GetFeatureCount(),
  1452. m_config.context->GetFeatures(),
  1453. ARSIZE(parameters),
  1454. parameters,
  1455. parameter_values,
  1456. &m_videoMixer);
  1457. CheckStatus(vdp_st, __LINE__);
  1458. // create 3 pitches of black lines needed for clipping top
  1459. // and bottom lines when de-interlacing
  1460. m_BlackBar = new uint32_t[3*m_config.outWidth];
  1461. memset(m_BlackBar, 0, 3*m_config.outWidth*sizeof(uint32_t));
  1462. }
  1463. void CMixer::InitCSCMatrix(int Width)
  1464. {
  1465. m_Procamp.struct_version = VDP_PROCAMP_VERSION;
  1466. m_Procamp.brightness = 0.0;
  1467. m_Procamp.contrast = 1.0;
  1468. m_Procamp.saturation = 1.0;
  1469. m_Procamp.hue = 0;
  1470. }
  1471. void CMixer::CheckFeatures()
  1472. {
  1473. if (m_Upscale != m_config.upscale)
  1474. {
  1475. SetHWUpscaling();
  1476. m_Upscale = m_config.upscale;
  1477. }
  1478. if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness ||
  1479. m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast ||
  1480. m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
  1481. {
  1482. SetColor();
  1483. m_Brightness = CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness;
  1484. m_Contrast = CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast;
  1485. m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
  1486. }
  1487. if (m_NoiseReduction != CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
  1488. {
  1489. m_NoiseReduction = CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction;
  1490. SetNoiseReduction();
  1491. }
  1492. if (m_Sharpness != CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
  1493. {
  1494. m_Sharpness = CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness;
  1495. SetSharpness();
  1496. }
  1497. if (m_DeintMode != CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode ||
  1498. m_Deint != CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod)
  1499. {
  1500. m_DeintMode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
  1501. m_Deint = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
  1502. SetDeinterlacing();
  1503. }
  1504. }
  1505. void CMixer::SetPostProcFeatures(bool postProcEnabled)
  1506. {
  1507. if (m_PostProc != postProcEnabled)
  1508. {
  1509. if (postProcEnabled)
  1510. {
  1511. SetNoiseReduction();
  1512. SetSharpness();
  1513. SetDeinterlacing();
  1514. SetHWUpscaling();
  1515. }
  1516. else
  1517. PostProcOff();
  1518. m_PostProc = postProcEnabled;
  1519. }
  1520. }
  1521. void CMixer::PostProcOff()
  1522. {
  1523. VdpStatus vdp_st;
  1524. if (m_videoMixer == VDP_INVALID_HANDLE)
  1525. return;
  1526. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
  1527. VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
  1528. VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE};
  1529. VdpBool enabled[]={0,0,0};
  1530. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1531. CheckStatus(vdp_st, __LINE__);
  1532. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
  1533. {
  1534. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION};
  1535. VdpBool enabled[]={0};
  1536. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1537. CheckStatus(vdp_st, __LINE__);
  1538. }
  1539. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
  1540. {
  1541. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS};
  1542. VdpBool enabled[]={0};
  1543. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1544. CheckStatus(vdp_st, __LINE__);
  1545. }
  1546. DisableHQScaling();
  1547. }
  1548. bool CMixer::GenerateStudioCSCMatrix(VdpColorStandard colorStandard, VdpCSCMatrix &studioCSCMatrix)
  1549. {
  1550. // instead use studioCSCKCoeffs601[3], studioCSCKCoeffs709[3] to generate float[3][4] matrix (float studioCSC[3][4])
  1551. // m00 = mRY = red: luma factor (contrast factor) (1.0)
  1552. // m10 = mGY = green: luma factor (contrast factor) (1.0)
  1553. // m20 = mBY = blue: luma factor (contrast factor) (1.0)
  1554. //
  1555. // m01 = mRB = red: blue color diff coeff (0.0)
  1556. // m11 = mGB = green: blue color diff coeff (-2Kb(1-Kb)/(Kg))
  1557. // m21 = mBB = blue: blue color diff coeff ((1-Kb)/0.5)
  1558. //
  1559. // m02 = mRR = red: red color diff coeff ((1-Kr)/0.5)
  1560. // m12 = mGR = green: red color diff coeff (-2Kr(1-Kr)/(Kg))
  1561. // m22 = mBR = blue: red color diff coeff (0.0)
  1562. //
  1563. // m03 = mRC = red: colour zero offset (brightness factor) (-(1-Kr)/0.5 * (128/255))
  1564. // m13 = mGC = green: colour zero offset (brightness factor) ((256/255) * (Kb(1-Kb) + Kr(1-Kr)) / Kg)
  1565. // m23 = mBC = blue: colour zero offset (brightness factor) (-(1-Kb)/0.5 * (128/255))
  1566. // columns
  1567. int Y = 0;
  1568. int Cb = 1;
  1569. int Cr = 2;
  1570. int C = 3;
  1571. // rows
  1572. int R = 0;
  1573. int G = 1;
  1574. int B = 2;
  1575. // colour standard coefficients for red, geen, blue
  1576. double Kr, Kg, Kb;
  1577. // colour diff zero position (use standard 8-bit coding precision)
  1578. double CDZ = 128; //256*0.5
  1579. // range excursion (use standard 8-bit coding precision)
  1580. double EXC = 255; //256-1
  1581. if (colorStandard == VDP_COLOR_STANDARD_ITUR_BT_601)
  1582. {
  1583. Kr = studioCSCKCoeffs601[0];
  1584. Kg = studioCSCKCoeffs601[1];
  1585. Kb = studioCSCKCoeffs601[2];
  1586. }
  1587. else // assume VDP_COLOR_STANDARD_ITUR_BT_709
  1588. {
  1589. Kr = studioCSCKCoeffs709[0];
  1590. Kg = studioCSCKCoeffs709[1];
  1591. Kb = studioCSCKCoeffs709[2];
  1592. }
  1593. // we keep luma unscaled to retain the levels present in source so that 16-235 luma is converted to RGB 16-235
  1594. studioCSCMatrix[R][Y] = 1.0;
  1595. studioCSCMatrix[G][Y] = 1.0;
  1596. studioCSCMatrix[B][Y] = 1.0;
  1597. studioCSCMatrix[R][Cb] = 0.0;
  1598. studioCSCMatrix[G][Cb] = (double)-2 * Kb * (1 - Kb) / Kg;
  1599. studioCSCMatrix[B][Cb] = (double)(1 - Kb) / 0.5;
  1600. studioCSCMatrix[R][Cr] = (double)(1 - Kr) / 0.5;
  1601. studioCSCMatrix[G][Cr] = (double)-2 * Kr * (1 - Kr) / Kg;
  1602. studioCSCMatrix[B][Cr] = 0.0;
  1603. studioCSCMatrix[R][C] = (double)-1 * studioCSCMatrix[R][Cr] * CDZ/EXC;
  1604. studioCSCMatrix[G][C] = (double)-1 * (studioCSCMatrix[G][Cb] + studioCSCMatrix[G][Cr]) * CDZ/EXC;
  1605. studioCSCMatrix[B][C] = (double)-1 * studioCSCMatrix[B][Cb] * CDZ/EXC;
  1606. return true;
  1607. }
  1608. void CMixer::SetColor()
  1609. {
  1610. VdpStatus vdp_st;
  1611. if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)
  1612. m_Procamp.brightness = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)-50) / 100;
  1613. if (m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)
  1614. m_Procamp.contrast = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)+50) / 100;
  1615. VdpColorStandard colorStandard;
  1616. switch(m_mixerInput[1].DVDPic.color_matrix)
  1617. {
  1618. case AVCOL_SPC_BT709:
  1619. colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
  1620. break;
  1621. case AVCOL_SPC_BT470BG:
  1622. case AVCOL_SPC_SMPTE170M:
  1623. colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
  1624. break;
  1625. case AVCOL_SPC_SMPTE240M:
  1626. colorStandard = VDP_COLOR_STANDARD_SMPTE_240M;
  1627. break;
  1628. case AVCOL_SPC_FCC:
  1629. case AVCOL_SPC_UNSPECIFIED:
  1630. case AVCOL_SPC_RGB:
  1631. default:
  1632. if(m_config.surfaceWidth > 1000)
  1633. colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
  1634. else
  1635. colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
  1636. }
  1637. VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX };
  1638. if (CSettings::Get().GetBool("videoscreen.limitedrange"))
  1639. {
  1640. float studioCSC[3][4];
  1641. GenerateStudioCSCMatrix(colorStandard, studioCSC);
  1642. void const * pm_CSCMatix[] = { &studioCSC };
  1643. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
  1644. }
  1645. else
  1646. {
  1647. vdp_st = m_config.context->GetProcs().vdp_generate_csc_matrix(&m_Procamp, colorStandard, &m_CSCMatrix);
  1648. void const * pm_CSCMatix[] = { &m_CSCMatrix };
  1649. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
  1650. }
  1651. CheckStatus(vdp_st, __LINE__);
  1652. }
  1653. void CMixer::SetNoiseReduction()
  1654. {
  1655. if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
  1656. return;
  1657. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION };
  1658. VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL };
  1659. VdpStatus vdp_st;
  1660. if (!CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
  1661. {
  1662. VdpBool enabled[]= {0};
  1663. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1664. CheckStatus(vdp_st, __LINE__);
  1665. return;
  1666. }
  1667. VdpBool enabled[]={1};
  1668. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1669. CheckStatus(vdp_st, __LINE__);
  1670. void* nr[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction };
  1671. CLog::Log(LOGNOTICE,"Setting Noise Reduction to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction);
  1672. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, nr);
  1673. CheckStatus(vdp_st, __LINE__);
  1674. }
  1675. void CMixer::SetSharpness()
  1676. {
  1677. if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
  1678. return;
  1679. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS };
  1680. VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL };
  1681. VdpStatus vdp_st;
  1682. if (!CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
  1683. {
  1684. VdpBool enabled[]={0};
  1685. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1686. CheckStatus(vdp_st, __LINE__);
  1687. return;
  1688. }
  1689. VdpBool enabled[]={1};
  1690. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1691. CheckStatus(vdp_st, __LINE__);
  1692. void* sh[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness };
  1693. CLog::Log(LOGNOTICE,"Setting Sharpness to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness);
  1694. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, sh);
  1695. CheckStatus(vdp_st, __LINE__);
  1696. }
  1697. EINTERLACEMETHOD CMixer::GetDeinterlacingMethod(bool log /* = false */)
  1698. {
  1699. EINTERLACEMETHOD method = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
  1700. if (method == VS_INTERLACEMETHOD_AUTO)
  1701. {
  1702. int deint = -1;
  1703. // if (m_config.outHeight >= 720)
  1704. // deint = g_advancedSettings.m_videoVDPAUdeintHD;
  1705. // else
  1706. // deint = g_advancedSettings.m_videoVDPAUdeintSD;
  1707. if (deint != -1)
  1708. {
  1709. if (m_config.vdpau->Supports(EINTERLACEMETHOD(deint)))
  1710. {
  1711. method = EINTERLACEMETHOD(deint);
  1712. if (log)
  1713. CLog::Log(LOGNOTICE, "CVDPAU::GetDeinterlacingMethod: set de-interlacing to %d", deint);
  1714. }
  1715. else
  1716. {
  1717. if (log)
  1718. CLog::Log(LOGWARNING, "CVDPAU::GetDeinterlacingMethod: method for de-interlacing (advanced settings) not supported");
  1719. }
  1720. }
  1721. }
  1722. return method;
  1723. }
  1724. void CMixer::SetDeinterlacing()
  1725. {
  1726. VdpStatus vdp_st;
  1727. if (m_videoMixer == VDP_INVALID_HANDLE)
  1728. return;
  1729. EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
  1730. EINTERLACEMETHOD method = GetDeinterlacingMethod(true);
  1731. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
  1732. VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
  1733. VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE };
  1734. if (mode == VS_DEINTERLACEMODE_OFF)
  1735. {
  1736. VdpBool enabled[] = {0,0,0};
  1737. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1738. }
  1739. else
  1740. {
  1741. if (method == VS_INTERLACEMETHOD_AUTO)
  1742. {
  1743. VdpBool enabled[] = {1,0,0};
  1744. if (g_advancedSettings.m_videoVDPAUtelecine)
  1745. enabled[2] = 1;
  1746. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1747. }
  1748. else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
  1749. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF)
  1750. {
  1751. VdpBool enabled[] = {1,0,0};
  1752. if (g_advancedSettings.m_videoVDPAUtelecine)
  1753. enabled[2] = 1;
  1754. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1755. }
  1756. else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
  1757. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF)
  1758. {
  1759. VdpBool enabled[] = {1,1,0};
  1760. if (g_advancedSettings.m_videoVDPAUtelecine)
  1761. enabled[2] = 1;
  1762. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1763. }
  1764. else
  1765. {
  1766. VdpBool enabled[]={0,0,0};
  1767. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1768. }
  1769. }
  1770. CheckStatus(vdp_st, __LINE__);
  1771. SetDeintSkipChroma();
  1772. m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
  1773. }
  1774. void CMixer::SetDeintSkipChroma()
  1775. {
  1776. VdpVideoMixerAttribute attribute[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
  1777. VdpStatus vdp_st;
  1778. uint8_t val;
  1779. if (g_advancedSettings.m_videoVDPAUdeintSkipChromaHD && m_config.outHeight >= 720)
  1780. val = 1;
  1781. else
  1782. val = 0;
  1783. void const *values[]={&val};
  1784. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attribute), attribute, values);
  1785. CheckStatus(vdp_st, __LINE__);
  1786. }
  1787. void CMixer::SetHWUpscaling()
  1788. {
  1789. #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
  1790. VdpStatus vdp_st;
  1791. VdpBool enabled[]={1};
  1792. switch (m_config.upscale)
  1793. {
  1794. case 9:
  1795. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
  1796. {
  1797. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
  1798. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1799. break;
  1800. }
  1801. case 8:
  1802. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
  1803. {
  1804. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
  1805. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1806. break;
  1807. }
  1808. case 7:
  1809. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
  1810. {
  1811. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
  1812. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1813. break;
  1814. }
  1815. case 6:
  1816. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
  1817. {
  1818. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
  1819. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1820. break;
  1821. }
  1822. case 5:
  1823. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
  1824. {
  1825. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
  1826. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1827. break;
  1828. }
  1829. case 4:
  1830. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
  1831. {
  1832. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
  1833. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1834. break;
  1835. }
  1836. case 3:
  1837. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
  1838. {
  1839. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
  1840. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1841. break;
  1842. }
  1843. case 2:
  1844. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
  1845. {
  1846. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
  1847. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1848. break;
  1849. }
  1850. case 1:
  1851. if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
  1852. {
  1853. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
  1854. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1855. break;
  1856. }
  1857. default:
  1858. DisableHQScaling();
  1859. return;
  1860. }
  1861. CheckStatus(vdp_st, __LINE__);
  1862. #endif
  1863. }
  1864. void CMixer::DisableHQScaling()
  1865. {
  1866. VdpStatus vdp_st;
  1867. if (m_videoMixer == VDP_INVALID_HANDLE)
  1868. return;
  1869. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
  1870. {
  1871. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
  1872. VdpBool enabled[]={0};
  1873. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1874. CheckStatus(vdp_st, __LINE__);
  1875. }
  1876. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
  1877. {
  1878. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
  1879. VdpBool enabled[]={0};
  1880. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1881. CheckStatus(vdp_st, __LINE__);
  1882. }
  1883. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
  1884. {
  1885. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
  1886. VdpBool enabled[]={0};
  1887. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1888. CheckStatus(vdp_st, __LINE__);
  1889. }
  1890. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
  1891. {
  1892. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
  1893. VdpBool enabled[]={0};
  1894. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1895. CheckStatus(vdp_st, __LINE__);
  1896. }
  1897. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
  1898. {
  1899. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
  1900. VdpBool enabled[]={0};
  1901. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1902. CheckStatus(vdp_st, __LINE__);
  1903. }
  1904. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
  1905. {
  1906. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
  1907. VdpBool enabled[]={0};
  1908. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1909. CheckStatus(vdp_st, __LINE__);
  1910. }
  1911. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
  1912. {
  1913. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
  1914. VdpBool enabled[]={0};
  1915. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1916. CheckStatus(vdp_st, __LINE__);
  1917. }
  1918. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
  1919. {
  1920. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
  1921. VdpBool enabled[]={0};
  1922. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1923. CheckStatus(vdp_st, __LINE__);
  1924. }
  1925. if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
  1926. {
  1927. VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
  1928. VdpBool enabled[]={0};
  1929. vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
  1930. CheckStatus(vdp_st, __LINE__);
  1931. }
  1932. }
  1933. void CMixer::Init()
  1934. {
  1935. m_Brightness = 0.0;
  1936. m_Contrast = 0.0;
  1937. m_NoiseReduction = 0.0;
  1938. m_Sharpness = 0.0;
  1939. m_DeintMode = 0;
  1940. m_Deint = 0;
  1941. m_ColorMatrix = 0;
  1942. m_PostProc = false;
  1943. m_vdpError = false;
  1944. m_config.upscale = g_advancedSettings.m_videoVDPAUScaling;
  1945. m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
  1946. CreateVdpauMixer();
  1947. }
  1948. void CMixer::Uninit()
  1949. {
  1950. Flush();
  1951. while (!m_outputSurfaces.empty())
  1952. {
  1953. m_outputSurfaces.pop();
  1954. }
  1955. m_config.context->GetProcs().vdp_video_mixer_destroy(m_videoMixer);
  1956. delete [] m_BlackBar;
  1957. }
  1958. void CMixer::Flush()
  1959. {
  1960. while (!m_mixerInput.empty())
  1961. {
  1962. CVdpauDecodedPicture pic = m_mixerInput.back();
  1963. m_mixerInput.pop_back();
  1964. m_config.videoSurfaces->ClearRender(pic.videoSurface);
  1965. }
  1966. while (!m_decodedPics.empty())
  1967. {
  1968. CVdpauDecodedPicture pic = m_decodedPics.front();
  1969. m_decodedPics.pop();
  1970. m_config.videoSurfaces->ClearRender(pic.videoSurface);
  1971. }
  1972. Message *msg;
  1973. while (m_dataPort.ReceiveOutMessage(&msg))
  1974. {
  1975. if (msg->signal == CMixerDataProtocol::FRAME)
  1976. {
  1977. CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
  1978. m_config.videoSurfaces->ClearRender(pic.videoSurface);
  1979. }
  1980. else if (msg->signal == CMixerDataProtocol::BUFFER)
  1981. {
  1982. VdpOutputSurface *surf;
  1983. surf = (VdpOutputSurface*)msg->data;
  1984. m_outputSurfaces.push(*surf);
  1985. }
  1986. msg->Release();
  1987. }
  1988. }
  1989. void CMixer::InitCycle()
  1990. {
  1991. CheckFeatures();
  1992. int flags;
  1993. uint64_t latency;
  1994. m_config.stats->GetParams(latency, flags);
  1995. // TODO
  1996. if (0) //flags & DVP_FLAG_NO_POSTPROC)
  1997. SetPostProcFeatures(false);
  1998. else
  1999. SetPostProcFeatures(true);
  2000. m_config.stats->SetCanSkipDeint(false);
  2001. EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
  2002. EINTERLACEMETHOD method = GetDeinterlacingMethod();
  2003. bool interlaced = m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_INTERLACED;
  2004. // TODO
  2005. if (//!(flags & DVP_FLAG_NO_POSTPROC) &&
  2006. (mode == VS_DEINTERLACEMODE_FORCE ||
  2007. (mode == VS_DEINTERLACEMODE_AUTO && interlaced)))
  2008. {
  2009. if((method == VS_INTERLACEMETHOD_AUTO && interlaced)
  2010. || method == VS_INTERLACEMETHOD_VDPAU_BOB
  2011. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
  2012. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
  2013. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
  2014. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
  2015. || method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE )
  2016. {
  2017. if(method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
  2018. || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
  2019. || !g_graphicsContext.IsFullScreenVideo())
  2020. m_mixersteps = 1;
  2021. else
  2022. {
  2023. m_mixersteps = 2;
  2024. m_config.stats->SetCanSkipDeint(true);
  2025. }
  2026. // TODO
  2027. if (0) //m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_DROPDEINT)
  2028. {
  2029. m_mixersteps = 1;
  2030. }
  2031. if(m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_TOP_FIELD_FIRST)
  2032. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
  2033. else
  2034. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
  2035. m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
  2036. m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
  2037. DVP_FLAG_REPEAT_TOP_FIELD |
  2038. DVP_FLAG_INTERLACED);
  2039. m_config.useInteropYuv = false;
  2040. }
  2041. else if (method == VS_INTERLACEMETHOD_RENDER_BOB)
  2042. {
  2043. m_mixersteps = 1;
  2044. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
  2045. m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
  2046. m_config.useInteropYuv = true;
  2047. }
  2048. else
  2049. {
  2050. CLog::Log(LOGERROR, "CMixer::%s - interlace method: %d not supported, setting to AUTO", __FUNCTION__, method);
  2051. m_mixersteps = 1;
  2052. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
  2053. m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
  2054. m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
  2055. DVP_FLAG_REPEAT_TOP_FIELD |
  2056. DVP_FLAG_INTERLACED);
  2057. CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod = VS_INTERLACEMETHOD_AUTO;
  2058. }
  2059. }
  2060. else
  2061. {
  2062. m_mixersteps = 1;
  2063. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
  2064. if (m_config.useInteropYuv)
  2065. m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
  2066. else
  2067. {
  2068. m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
  2069. m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
  2070. DVP_FLAG_REPEAT_TOP_FIELD |
  2071. DVP_FLAG_INTERLACED);
  2072. }
  2073. }
  2074. m_mixerstep = 0;
  2075. if (m_mixerInput[1].DVDPic.format == RENDER_FMT_VDPAU)
  2076. {
  2077. m_processPicture.outputSurface = m_outputSurfaces.front();
  2078. m_mixerInput[1].DVDPic.iWidth = m_config.outWidth;
  2079. m_mixerInput[1].DVDPic.iHeight = m_config.outHeight;
  2080. }
  2081. else
  2082. {
  2083. m_mixerInput[1].DVDPic.iWidth = m_config.vidWidth;
  2084. m_mixerInput[1].DVDPic.iHeight = m_config.vidHeight;
  2085. }
  2086. m_processPicture.DVDPic = m_mixerInput[1].DVDPic;
  2087. m_processPicture.videoSurface = m_mixerInput[1].videoSurface;
  2088. }
  2089. void CMixer::FiniCycle()
  2090. {
  2091. // Keep video surfaces for one 2 cycles longer than used
  2092. // by mixer. This avoids blocking in decoder.
  2093. // NVidia recommends num_ref + 5
  2094. while (m_mixerInput.size() > 5)
  2095. {
  2096. CVdpauDecodedPicture &tmp = m_mixerInput.back();
  2097. if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
  2098. {
  2099. m_config.videoSurfaces->ClearRender(tmp.videoSurface);
  2100. }
  2101. m_mixerInput.pop_back();
  2102. }
  2103. }
  2104. void CMixer::ProcessPicture()
  2105. {
  2106. if (m_processPicture.DVDPic.format == RENDER_FMT_VDPAU_420)
  2107. return;
  2108. VdpStatus vdp_st;
  2109. if (m_mixerstep == 1)
  2110. {
  2111. if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD)
  2112. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
  2113. else
  2114. m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
  2115. }
  2116. VdpVideoSurface past_surfaces[4] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
  2117. VdpVideoSurface futu_surfaces[2] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
  2118. uint32_t pastCount = 4;
  2119. uint32_t futuCount = 2;
  2120. if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
  2121. {
  2122. // use only 2 past 1 future for progressive/weave
  2123. // (only used for postproc anyway eg noise reduction)
  2124. if (m_mixerInput.size() > 3)
  2125. past_surfaces[1] = m_mixerInput[3].videoSurface;
  2126. if (m_mixerInput.size() > 2)
  2127. past_surfaces[0] = m_mixerInput[2].videoSurface;
  2128. futu_surfaces[0] = m_mixerInput[0].videoSurface;
  2129. pastCount = 2;
  2130. futuCount = 1;
  2131. }
  2132. else
  2133. {
  2134. if(m_mixerstep == 0)
  2135. { // first field
  2136. if (m_mixerInput.size() > 3)
  2137. {
  2138. past_surfaces[3] = m_mixerInput[3].videoSurface;
  2139. past_surfaces[2] = m_mixerInput[3].videoSurface;
  2140. }
  2141. if (m_mixerInput.size() > 2)
  2142. {
  2143. past_surfaces[1] = m_mixerInput[2].videoSurface;
  2144. past_surfaces[0] = m_mixerInput[2].videoSurface;
  2145. }
  2146. futu_surfaces[0] = m_mixerInput[1].videoSurface;
  2147. futu_surfaces[1] = m_mixerInput[0].videoSurface;
  2148. }
  2149. else
  2150. { // second field
  2151. if (m_mixerInput.size() > 3)
  2152. {
  2153. past_surfaces[3] = m_mixerInput[3].videoSurface;
  2154. }
  2155. if (m_mixerInput.size() > 2)
  2156. {
  2157. past_surfaces[2] = m_mixerInput[2].videoSurface;
  2158. past_surfaces[1] = m_mixerInput[2].videoSurface;
  2159. }
  2160. past_surfaces[0] = m_mixerInput[1].videoSurface;
  2161. futu_surfaces[0] = m_mixerInput[1].videoSurface;
  2162. futu_surfaces[1] = m_mixerInput[1].videoSurface;
  2163. if (m_mixerInput[0].DVDPic.pts != DVD_NOPTS_VALUE &&
  2164. m_mixerInput[1].DVDPic.pts != DVD_NOPTS_VALUE)
  2165. {
  2166. m_processPicture.DVDPic.pts = m_mixerInput[1].DVDPic.pts +
  2167. (m_mixerInput[0].DVDPic.pts -
  2168. m_mixerInput[1].DVDPic.pts) / 2;
  2169. }
  2170. else
  2171. m_processPicture.DVDPic.pts = DVD_NOPTS_VALUE;
  2172. m_processPicture.DVDPic.dts = DVD_NOPTS_VALUE;
  2173. }
  2174. m_processPicture.DVDPic.iRepeatPicture = 0.0;
  2175. } // interlaced
  2176. VdpRect sourceRect;
  2177. sourceRect.x0 = 0;
  2178. sourceRect.y0 = 0;
  2179. sourceRect.x1 = m_config.vidWidth;
  2180. sourceRect.y1 = m_config.vidHeight;
  2181. VdpRect destRect;
  2182. destRect.x0 = 0;
  2183. destRect.y0 = 0;
  2184. destRect.x1 = m_config.outWidth;
  2185. destRect.y1 = m_config.outHeight;
  2186. // start vdpau video mixer
  2187. vdp_st = m_config.context->GetProcs().vdp_video_mixer_render(m_videoMixer,
  2188. VDP_INVALID_HANDLE,
  2189. 0,
  2190. m_mixerfield,
  2191. pastCount,
  2192. past_surfaces,
  2193. m_mixerInput[1].videoSurface,
  2194. futuCount,
  2195. futu_surfaces,
  2196. &sourceRect,
  2197. m_processPicture.outputSurface,
  2198. &destRect,
  2199. &destRect,
  2200. 0,
  2201. NULL);
  2202. CheckStatus(vdp_st, __LINE__);
  2203. if (m_mixerfield != VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
  2204. {
  2205. // in order to clip top and bottom lines when de-interlacing
  2206. // we black those lines as a work around for not working
  2207. // background colour using the mixer
  2208. // pixel perfect is preferred over overscanning or zooming
  2209. VdpRect clipRect = destRect;
  2210. clipRect.y1 = clipRect.y0 + 2;
  2211. uint32_t *data[] = {m_BlackBar};
  2212. uint32_t pitches[] = {destRect.x1};
  2213. vdp_st = m_config.context->GetProcs().vdp_output_surface_put_bits_native(m_processPicture.outputSurface,
  2214. (void**)data,
  2215. pitches,
  2216. &clipRect);
  2217. CheckStatus(vdp_st, __LINE__);
  2218. clipRect = destRect;
  2219. clipRect.y0 = clipRect.y1 - 2;
  2220. vdp_st = m_config.context->GetProcs().vdp_output_surface_put_bits_native(m_processPicture.outputSurface,
  2221. (void**)data,
  2222. pitches,
  2223. &clipRect);
  2224. CheckStatus(vdp_st, __LINE__);
  2225. }
  2226. }
  2227. bool CMixer::CheckStatus(VdpStatus vdp_st, int line)
  2228. {
  2229. if (vdp_st != VDP_STATUS_OK)
  2230. {
  2231. CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) at %s:%d\n", m_config.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st, __FILE__, line);
  2232. m_vdpError = true;
  2233. return true;
  2234. }
  2235. return false;
  2236. }
  2237. //-----------------------------------------------------------------------------
  2238. // Buffer Pool
  2239. //-----------------------------------------------------------------------------
  2240. VdpauBufferPool::VdpauBufferPool()
  2241. {
  2242. CVdpauRenderPicture *pic;
  2243. for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
  2244. {
  2245. pic = new CVdpauRenderPicture(renderPicSec);
  2246. allRenderPics.push_back(pic);
  2247. }
  2248. }
  2249. VdpauBufferPool::~VdpauBufferPool()
  2250. {
  2251. CVdpauRenderPicture *pic;
  2252. for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
  2253. {
  2254. pic = allRenderPics[i];
  2255. delete pic;
  2256. }
  2257. allRenderPics.clear();
  2258. }
  2259. //-----------------------------------------------------------------------------
  2260. // Output
  2261. //-----------------------------------------------------------------------------
  2262. COutput::COutput(CEvent *inMsgEvent) :
  2263. CThread("Vdpau Output"),
  2264. m_controlPort("OutputControlPort", inMsgEvent, &m_outMsgEvent),
  2265. m_dataPort("OutputDataPort", inMsgEvent, &m_outMsgEvent),
  2266. m_mixer(&m_outMsgEvent)
  2267. {
  2268. m_inMsgEvent = inMsgEvent;
  2269. for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); ++i)
  2270. {
  2271. m_bufferPool.freeRenderPics.push_back(i);
  2272. }
  2273. }
  2274. void COutput::Start()
  2275. {
  2276. Create();
  2277. }
  2278. COutput::~COutput()
  2279. {
  2280. Dispose();
  2281. m_bufferPool.freeRenderPics.clear();
  2282. m_bufferPool.usedRenderPics.clear();
  2283. }
  2284. void COutput::Dispose()
  2285. {
  2286. CSingleLock lock(g_graphicsContext);
  2287. m_bStop = true;
  2288. m_outMsgEvent.Set();
  2289. StopThread();
  2290. m_controlPort.Purge();
  2291. m_dataPort.Purge();
  2292. }
  2293. void COutput::OnStartup()
  2294. {
  2295. CLog::Log(LOGNOTICE, "COutput::OnStartup: Output Thread created");
  2296. }
  2297. void COutput::OnExit()
  2298. {
  2299. CLog::Log(LOGNOTICE, "COutput::OnExit: Output Thread terminated");
  2300. }
  2301. enum OUTPUT_STATES
  2302. {
  2303. O_TOP = 0, // 0
  2304. O_TOP_ERROR, // 1
  2305. O_TOP_UNCONFIGURED, // 2
  2306. O_TOP_CONFIGURED, // 3
  2307. O_TOP_CONFIGURED_IDLE, // 4
  2308. O_TOP_CONFIGURED_WORK, // 5
  2309. };
  2310. int VDPAU_OUTPUT_parentStates[] = {
  2311. -1,
  2312. 0, //TOP_ERROR
  2313. 0, //TOP_UNCONFIGURED
  2314. 0, //TOP_CONFIGURED
  2315. 3, //TOP_CONFIGURED_IDLE
  2316. 3, //TOP_CONFIGURED_WORK
  2317. };
  2318. void COutput::StateMachine(int signal, Protocol *port, Message *msg)
  2319. {
  2320. for (int state = m_state; ; state = VDPAU_OUTPUT_parentStates[state])
  2321. {
  2322. switch (state)
  2323. {
  2324. case O_TOP: // TOP
  2325. if (port == &m_controlPort)
  2326. {
  2327. switch (signal)
  2328. {
  2329. case COutputControlProtocol::FLUSH:
  2330. msg->Reply(COutputControlProtocol::ACC);
  2331. return;
  2332. case COutputControlProtocol::PRECLEANUP:
  2333. msg->Reply(COutputControlProtocol::ACC);
  2334. return;
  2335. default:
  2336. break;
  2337. }
  2338. }
  2339. else if (port == &m_dataPort)
  2340. {
  2341. switch (signal)
  2342. {
  2343. case COutputDataProtocol::RETURNPIC:
  2344. CVdpauRenderPicture *pic;
  2345. pic = *((CVdpauRenderPicture**)msg->data);
  2346. QueueReturnPicture(pic);
  2347. return;
  2348. default:
  2349. break;
  2350. }
  2351. }
  2352. {
  2353. std::string portName = port == NULL ? "timer" : port->portName;
  2354. CLog::Log(LOGWARNING, "COutput::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
  2355. }
  2356. return;
  2357. case O_TOP_ERROR:
  2358. break;
  2359. case O_TOP_UNCONFIGURED:
  2360. if (port == &m_controlPort)
  2361. {
  2362. switch (signal)
  2363. {
  2364. case COutputControlProtocol::INIT:
  2365. CVdpauConfig *data;
  2366. data = (CVdpauConfig*)msg->data;
  2367. if (data)
  2368. {
  2369. m_config = *data;
  2370. }
  2371. Init();
  2372. Message *reply;
  2373. if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::INIT,
  2374. &reply, 1000, &m_config, sizeof(m_config)))
  2375. {
  2376. if (reply->signal != CMixerControlProtocol::ACC)
  2377. m_vdpError = true;
  2378. reply->Release();
  2379. }
  2380. // set initial number of
  2381. m_bufferPool.numOutputSurfaces = 4;
  2382. EnsureBufferPool();
  2383. if (!m_vdpError)
  2384. {
  2385. m_state = O_TOP_CONFIGURED_IDLE;
  2386. msg->Reply(COutputControlProtocol::ACC, &m_config, sizeof(m_config));
  2387. }
  2388. else
  2389. {
  2390. m_state = O_TOP_ERROR;
  2391. msg->Reply(COutputControlProtocol::ERROR);
  2392. }
  2393. return;
  2394. default:
  2395. break;
  2396. }
  2397. }
  2398. break;
  2399. case O_TOP_CONFIGURED:
  2400. if (port == &m_controlPort)
  2401. {
  2402. switch (signal)
  2403. {
  2404. case COutputControlProtocol::FLUSH:
  2405. Flush();
  2406. msg->Reply(COutputControlProtocol::ACC);
  2407. return;
  2408. case COutputControlProtocol::PRECLEANUP:
  2409. Flush();
  2410. PreCleanup();
  2411. msg->Reply(COutputControlProtocol::ACC);
  2412. return;
  2413. default:
  2414. break;
  2415. }
  2416. }
  2417. else if (port == &m_dataPort)
  2418. {
  2419. switch (signal)
  2420. {
  2421. case COutputDataProtocol::NEWFRAME:
  2422. CVdpauDecodedPicture *frame;
  2423. frame = (CVdpauDecodedPicture*)msg->data;
  2424. if (frame)
  2425. {
  2426. m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::FRAME,
  2427. frame,sizeof(CVdpauDecodedPicture));
  2428. }
  2429. return;
  2430. case COutputDataProtocol::RETURNPIC:
  2431. CVdpauRenderPicture *pic;
  2432. pic = *((CVdpauRenderPicture**)msg->data);
  2433. QueueReturnPicture(pic);
  2434. m_controlPort.SendInMessage(COutputControlProtocol::STATS);
  2435. m_state = O_TOP_CONFIGURED_WORK;
  2436. m_extTimeout = 0;
  2437. return;
  2438. default:
  2439. break;
  2440. }
  2441. }
  2442. else if (port == &m_mixer.m_dataPort)
  2443. {
  2444. switch (signal)
  2445. {
  2446. case CMixerDataProtocol::PICTURE:
  2447. CVdpauProcessedPicture *pic;
  2448. pic = (CVdpauProcessedPicture*)msg->data;
  2449. m_bufferPool.processedPics.push(*pic);
  2450. m_state = O_TOP_CONFIGURED_WORK;
  2451. m_extTimeout = 0;
  2452. return;
  2453. default:
  2454. break;
  2455. }
  2456. }
  2457. break;
  2458. case O_TOP_CONFIGURED_IDLE:
  2459. if (port == NULL) // timeout
  2460. {
  2461. switch (signal)
  2462. {
  2463. case COutputControlProtocol::TIMEOUT:
  2464. if (ProcessSyncPicture())
  2465. m_extTimeout = 10;
  2466. else
  2467. m_extTimeout = 100;
  2468. if (HasWork())
  2469. {
  2470. m_state = O_TOP_CONFIGURED_WORK;
  2471. m_extTimeout = 0;
  2472. }
  2473. return;
  2474. default:
  2475. break;
  2476. }
  2477. }
  2478. break;
  2479. case O_TOP_CONFIGURED_WORK:
  2480. if (port == NULL) // timeout
  2481. {
  2482. switch (signal)
  2483. {
  2484. case COutputControlProtocol::TIMEOUT:
  2485. if (HasWork())
  2486. {
  2487. CVdpauRenderPicture *pic;
  2488. pic = ProcessMixerPicture();
  2489. if (pic)
  2490. {
  2491. m_config.stats->DecProcessed();
  2492. m_config.stats->IncRender();
  2493. m_dataPort.SendInMessage(COutputDataProtocol::PICTURE, &pic, sizeof(pic));
  2494. }
  2495. m_extTimeout = 1;
  2496. }
  2497. else
  2498. {
  2499. m_state = O_TOP_CONFIGURED_IDLE;
  2500. m_extTimeout = 0;
  2501. }
  2502. return;
  2503. default:
  2504. break;
  2505. }
  2506. }
  2507. break;
  2508. default: // we are in no state, should not happen
  2509. CLog::Log(LOGERROR, "COutput::%s - no valid state: %d", __FUNCTION__, m_state);
  2510. return;
  2511. }
  2512. } // for
  2513. }
  2514. void COutput::Process()
  2515. {
  2516. Message *msg = NULL;
  2517. Protocol *port = NULL;
  2518. bool gotMsg;
  2519. m_state = O_TOP_UNCONFIGURED;
  2520. m_extTimeout = 1000;
  2521. m_bStateMachineSelfTrigger = false;
  2522. while (!m_bStop)
  2523. {
  2524. gotMsg = false;
  2525. if (m_bStateMachineSelfTrigger)
  2526. {
  2527. m_bStateMachineSelfTrigger = false;
  2528. // self trigger state machine
  2529. StateMachine(msg->signal, port, msg);
  2530. if (!m_bStateMachineSelfTrigger)
  2531. {
  2532. msg->Release();
  2533. msg = NULL;
  2534. }
  2535. continue;
  2536. }
  2537. // check control port
  2538. else if (m_controlPort.ReceiveOutMessage(&msg))
  2539. {
  2540. gotMsg = true;
  2541. port = &m_controlPort;
  2542. }
  2543. // check data port
  2544. else if (m_dataPort.ReceiveOutMessage(&msg))
  2545. {
  2546. gotMsg = true;
  2547. port = &m_dataPort;
  2548. }
  2549. // check mixer data port
  2550. else if (m_mixer.m_dataPort.ReceiveInMessage(&msg))
  2551. {
  2552. gotMsg = true;
  2553. port = &m_mixer.m_dataPort;
  2554. }
  2555. if (gotMsg)
  2556. {
  2557. StateMachine(msg->signal, port, msg);
  2558. if (!m_bStateMachineSelfTrigger)
  2559. {
  2560. msg->Release();
  2561. msg = NULL;
  2562. }
  2563. continue;
  2564. }
  2565. // wait for message
  2566. else if (m_outMsgEvent.WaitMSec(m_extTimeout))
  2567. {
  2568. continue;
  2569. }
  2570. // time out
  2571. else
  2572. {
  2573. msg = m_controlPort.GetMessage();
  2574. msg->signal = COutputControlProtocol::TIMEOUT;
  2575. port = 0;
  2576. // signal timeout to state machine
  2577. StateMachine(msg->signal, port, msg);
  2578. if (!m_bStateMachineSelfTrigger)
  2579. {
  2580. msg->Release();
  2581. msg = NULL;
  2582. }
  2583. }
  2584. }
  2585. Flush();
  2586. Uninit();
  2587. }
  2588. bool COutput::Init()
  2589. {
  2590. if (!CreateGlxContext())
  2591. return false;
  2592. if (!GLInit())
  2593. return false;
  2594. m_mixer.Start();
  2595. m_vdpError = false;
  2596. return true;
  2597. }
  2598. bool COutput::Uninit()
  2599. {
  2600. m_mixer.Dispose();
  2601. GLUnmapSurfaces();
  2602. ReleaseBufferPool();
  2603. DestroyGlxContext();
  2604. return true;
  2605. }
  2606. void COutput::Flush()
  2607. {
  2608. if (m_mixer.IsActive())
  2609. {
  2610. Message *reply;
  2611. if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::FLUSH,
  2612. &reply,
  2613. 2000))
  2614. {
  2615. reply->Release();
  2616. }
  2617. else
  2618. CLog::Log(LOGERROR, "Coutput::%s - failed to flush mixer", __FUNCTION__);
  2619. }
  2620. Message *msg;
  2621. while (m_mixer.m_dataPort.ReceiveInMessage(&msg))
  2622. {
  2623. if (msg->signal == CMixerDataProtocol::PICTURE)
  2624. {
  2625. CVdpauProcessedPicture pic = *(CVdpauProcessedPicture*)msg->data;
  2626. m_bufferPool.processedPics.push(pic);
  2627. }
  2628. msg->Release();
  2629. }
  2630. while (m_dataPort.ReceiveOutMessage(&msg))
  2631. {
  2632. if (msg->signal == COutputDataProtocol::NEWFRAME)
  2633. {
  2634. CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
  2635. m_config.videoSurfaces->ClearRender(pic.videoSurface);
  2636. }
  2637. else if (msg->signal == COutputDataProtocol::RETURNPIC)
  2638. {
  2639. CVdpauRenderPicture *pic;
  2640. pic = *((CVdpauRenderPicture**)msg->data);
  2641. QueueReturnPicture(pic);
  2642. }
  2643. msg->Release();
  2644. }
  2645. while (m_dataPort.ReceiveInMessage(&msg))
  2646. {
  2647. if (msg->signal == COutputDataProtocol::PICTURE)
  2648. {
  2649. CVdpauRenderPicture *pic;
  2650. pic = *((CVdpauRenderPicture**)msg->data);
  2651. QueueReturnPicture(pic);
  2652. }
  2653. }
  2654. // reset used render flag which was cleared on mixer flush
  2655. std::deque<int>::iterator it;
  2656. for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
  2657. {
  2658. CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[*it];
  2659. if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
  2660. {
  2661. std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it2;
  2662. it2 = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
  2663. if (it2 == m_bufferPool.glVideoSurfaceMap.end())
  2664. {
  2665. CLog::Log(LOGDEBUG, "COutput::Flush - gl surface not found");
  2666. continue;
  2667. }
  2668. m_config.videoSurfaces->MarkRender(it2->second.sourceVuv);
  2669. }
  2670. }
  2671. // clear processed pics
  2672. while(!m_bufferPool.processedPics.empty())
  2673. {
  2674. CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
  2675. if (procPic.DVDPic.format == RENDER_FMT_VDPAU)
  2676. {
  2677. m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &procPic.outputSurface, sizeof(procPic.outputSurface));
  2678. }
  2679. else if (procPic.DVDPic.format == RENDER_FMT_VDPAU_420)
  2680. {
  2681. m_config.videoSurfaces->ClearRender(procPic.videoSurface);
  2682. }
  2683. m_bufferPool.processedPics.pop();
  2684. }
  2685. }
  2686. bool COutput::HasWork()
  2687. {
  2688. if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
  2689. return true;
  2690. return false;
  2691. }
  2692. CVdpauRenderPicture* COutput::ProcessMixerPicture()
  2693. {
  2694. CVdpauRenderPicture *retPic = NULL;
  2695. if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
  2696. {
  2697. int idx = m_bufferPool.freeRenderPics.front();
  2698. retPic = m_bufferPool.allRenderPics[idx];
  2699. m_bufferPool.freeRenderPics.pop_front();
  2700. m_bufferPool.usedRenderPics.push_back(idx);
  2701. CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
  2702. m_bufferPool.processedPics.pop();
  2703. retPic->DVDPic = procPic.DVDPic;
  2704. retPic->valid = true;
  2705. if (retPic->DVDPic.format == RENDER_FMT_VDPAU)
  2706. {
  2707. m_config.useInteropYuv = false;
  2708. m_bufferPool.numOutputSurfaces = NUM_RENDER_PICS;
  2709. EnsureBufferPool();
  2710. GLMapSurfaces();
  2711. retPic->sourceIdx = procPic.outputSurface;
  2712. retPic->texture[0] = m_bufferPool.glOutputSurfaceMap[procPic.outputSurface].texture[0];
  2713. retPic->crop = CRect(0,0,0,0);
  2714. }
  2715. else
  2716. {
  2717. m_config.useInteropYuv = true;
  2718. GLMapSurfaces();
  2719. retPic->sourceIdx = procPic.videoSurface;
  2720. for (unsigned int i=0; i<4; ++i)
  2721. retPic->texture[i] = m_bufferPool.glVideoSurfaceMap[procPic.videoSurface].texture[i];
  2722. retPic->texWidth = m_config.surfaceWidth;
  2723. retPic->texHeight = m_config.surfaceHeight;
  2724. retPic->crop.x1 = 0;
  2725. retPic->crop.y1 = 0;
  2726. retPic->crop.x2 = m_config.surfaceWidth - m_config.vidWidth;
  2727. retPic->crop.y2 = m_config.surfaceHeight - m_config.vidHeight;
  2728. }
  2729. }
  2730. return retPic;
  2731. }
  2732. void COutput::QueueReturnPicture(CVdpauRenderPicture *pic)
  2733. {
  2734. std::deque<int>::iterator it;
  2735. for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
  2736. {
  2737. if (m_bufferPool.allRenderPics[*it] == pic)
  2738. {
  2739. break;
  2740. }
  2741. }
  2742. if (it == m_bufferPool.usedRenderPics.end())
  2743. {
  2744. CLog::Log(LOGWARNING, "COutput::QueueReturnPicture - pic not found");
  2745. return;
  2746. }
  2747. // check if already queued
  2748. std::deque<int>::iterator it2 = find(m_bufferPool.syncRenderPics.begin(),
  2749. m_bufferPool.syncRenderPics.end(),
  2750. *it);
  2751. if (it2 == m_bufferPool.syncRenderPics.end())
  2752. {
  2753. m_bufferPool.syncRenderPics.push_back(*it);
  2754. }
  2755. ProcessSyncPicture();
  2756. }
  2757. bool COutput::ProcessSyncPicture()
  2758. {
  2759. CVdpauRenderPicture *pic;
  2760. bool busy = false;
  2761. std::deque<int>::iterator it;
  2762. for (it = m_bufferPool.syncRenderPics.begin(); it != m_bufferPool.syncRenderPics.end(); )
  2763. {
  2764. pic = m_bufferPool.allRenderPics[*it];
  2765. #ifdef GL_ARB_sync
  2766. if (pic->usefence)
  2767. {
  2768. if (glIsSync(pic->fence))
  2769. {
  2770. GLint state;
  2771. GLsizei length;
  2772. glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
  2773. if(state == GL_SIGNALED)
  2774. {
  2775. glDeleteSync(pic->fence);
  2776. pic->fence = None;
  2777. }
  2778. else
  2779. {
  2780. busy = true;
  2781. ++it;
  2782. continue;
  2783. }
  2784. }
  2785. }
  2786. #endif
  2787. m_bufferPool.freeRenderPics.push_back(*it);
  2788. std::deque<int>::iterator it2 = find(m_bufferPool.usedRenderPics.begin(),
  2789. m_bufferPool.usedRenderPics.end(),
  2790. *it);
  2791. if (it2 == m_bufferPool.usedRenderPics.end())
  2792. {
  2793. CLog::Log(LOGERROR, "COutput::ProcessSyncPicture - pic not found in queue");
  2794. }
  2795. else
  2796. {
  2797. m_bufferPool.usedRenderPics.erase(it2);
  2798. }
  2799. it = m_bufferPool.syncRenderPics.erase(it);
  2800. if (pic->valid)
  2801. {
  2802. ProcessReturnPicture(pic);
  2803. }
  2804. else
  2805. {
  2806. CLog::Log(LOGDEBUG, "COutput::%s - return of invalid render pic", __FUNCTION__);
  2807. }
  2808. }
  2809. return busy;
  2810. }
  2811. void COutput::ProcessReturnPicture(CVdpauRenderPicture *pic)
  2812. {
  2813. if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
  2814. {
  2815. std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
  2816. it = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
  2817. if (it == m_bufferPool.glVideoSurfaceMap.end())
  2818. {
  2819. CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
  2820. return;
  2821. }
  2822. VdpVideoSurface surf = it->second.sourceVuv;
  2823. m_config.videoSurfaces->ClearRender(surf);
  2824. }
  2825. else if (pic->DVDPic.format == RENDER_FMT_VDPAU)
  2826. {
  2827. std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
  2828. it = m_bufferPool.glOutputSurfaceMap.find(pic->sourceIdx);
  2829. if (it == m_bufferPool.glOutputSurfaceMap.end())
  2830. {
  2831. CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
  2832. return;
  2833. }
  2834. VdpOutputSurface outSurf = it->second.sourceRgb;
  2835. m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &outSurf, sizeof(outSurf));
  2836. }
  2837. }
  2838. bool COutput::EnsureBufferPool()
  2839. {
  2840. VdpStatus vdp_st;
  2841. // Creation of outputSurfaces
  2842. VdpOutputSurface outputSurface;
  2843. for (int i = m_bufferPool.outputSurfaces.size(); i < m_bufferPool.numOutputSurfaces; i++)
  2844. {
  2845. vdp_st = m_config.context->GetProcs().vdp_output_surface_create(m_config.context->GetDevice(),
  2846. VDP_RGBA_FORMAT_B8G8R8A8,
  2847. m_config.outWidth,
  2848. m_config.outHeight,
  2849. &outputSurface);
  2850. if (CheckStatus(vdp_st, __LINE__))
  2851. return false;
  2852. m_bufferPool.outputSurfaces.push_back(outputSurface);
  2853. m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
  2854. &outputSurface,
  2855. sizeof(VdpOutputSurface));
  2856. CLog::Log(LOGNOTICE, "VDPAU::COutput::InitBufferPool - Output Surface created");
  2857. }
  2858. return true;
  2859. }
  2860. void COutput::ReleaseBufferPool()
  2861. {
  2862. VdpStatus vdp_st;
  2863. CSingleLock lock(m_bufferPool.renderPicSec);
  2864. // release all output surfaces
  2865. for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
  2866. {
  2867. if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
  2868. continue;
  2869. vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
  2870. CheckStatus(vdp_st, __LINE__);
  2871. }
  2872. m_bufferPool.outputSurfaces.clear();
  2873. // wait for all fences
  2874. XbmcThreads::EndTime timeout(1000);
  2875. for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
  2876. {
  2877. CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[i];
  2878. if (pic->usefence)
  2879. {
  2880. #ifdef GL_ARB_sync
  2881. while (glIsSync(pic->fence))
  2882. {
  2883. GLint state;
  2884. GLsizei length;
  2885. glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
  2886. if(state == GL_SIGNALED || timeout.IsTimePast())
  2887. {
  2888. glDeleteSync(pic->fence);
  2889. }
  2890. else
  2891. {
  2892. Sleep(5);
  2893. }
  2894. }
  2895. pic->fence = None;
  2896. #endif
  2897. }
  2898. }
  2899. if (timeout.IsTimePast())
  2900. {
  2901. CLog::Log(LOGERROR, "COutput::%s - timeout waiting for fence", __FUNCTION__);
  2902. }
  2903. ProcessSyncPicture();
  2904. // invalidate all used render pictures
  2905. for (unsigned int i = 0; i < m_bufferPool.usedRenderPics.size(); ++i)
  2906. {
  2907. CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[m_bufferPool.usedRenderPics[i]];
  2908. pic->valid = false;
  2909. }
  2910. }
  2911. void COutput::PreCleanup()
  2912. {
  2913. VdpStatus vdp_st;
  2914. m_mixer.Dispose();
  2915. ProcessSyncPicture();
  2916. CSingleLock lock(m_bufferPool.renderPicSec);
  2917. for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
  2918. {
  2919. if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
  2920. continue;
  2921. // check if output surface is in use
  2922. bool used = false;
  2923. std::deque<int>::iterator it;
  2924. CVdpauRenderPicture *pic;
  2925. for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
  2926. {
  2927. pic = m_bufferPool.allRenderPics[*it];
  2928. if ((pic->sourceIdx == m_bufferPool.outputSurfaces[i]) && pic->valid)
  2929. {
  2930. used = true;
  2931. break;
  2932. }
  2933. }
  2934. if (used)
  2935. continue;
  2936. #ifdef GL_NV_vdpau_interop
  2937. // unmap surface
  2938. std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it_map;
  2939. it_map = m_bufferPool.glOutputSurfaceMap.find(m_bufferPool.outputSurfaces[i]);
  2940. if (it_map == m_bufferPool.glOutputSurfaceMap.end())
  2941. {
  2942. CLog::Log(LOGERROR, "%s - could not find gl surface", __FUNCTION__);
  2943. continue;
  2944. }
  2945. glVDPAUUnregisterSurfaceNV(it_map->second.glVdpauSurface);
  2946. glDeleteTextures(1, it_map->second.texture);
  2947. m_bufferPool.glOutputSurfaceMap.erase(it_map);
  2948. #endif
  2949. vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
  2950. CheckStatus(vdp_st, __LINE__);
  2951. m_bufferPool.outputSurfaces[i] = VDP_INVALID_HANDLE;
  2952. CLog::Log(LOGDEBUG, "VDPAU::PreCleanup - released output surface");
  2953. }
  2954. }
  2955. void COutput::InitMixer()
  2956. {
  2957. for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
  2958. {
  2959. m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
  2960. &m_bufferPool.outputSurfaces[i],
  2961. sizeof(VdpOutputSurface));
  2962. }
  2963. }
  2964. bool COutput::GLInit()
  2965. {
  2966. #ifdef GL_NV_vdpau_interop
  2967. glVDPAUInitNV = NULL;
  2968. glVDPAUFiniNV = NULL;
  2969. glVDPAURegisterOutputSurfaceNV = NULL;
  2970. glVDPAURegisterVideoSurfaceNV = NULL;
  2971. glVDPAUIsSurfaceNV = NULL;
  2972. glVDPAUUnregisterSurfaceNV = NULL;
  2973. glVDPAUSurfaceAccessNV = NULL;
  2974. glVDPAUMapSurfacesNV = NULL;
  2975. glVDPAUUnmapSurfacesNV = NULL;
  2976. glVDPAUGetSurfaceivNV = NULL;
  2977. #endif
  2978. #ifdef GL_NV_vdpau_interop
  2979. if (glewIsSupported("GL_NV_vdpau_interop"))
  2980. {
  2981. if (!glVDPAUInitNV)
  2982. glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUInitNV");
  2983. if (!glVDPAUFiniNV)
  2984. glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((GLubyte *) "glVDPAUFiniNV");
  2985. if (!glVDPAURegisterOutputSurfaceNV)
  2986. glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterOutputSurfaceNV");
  2987. if (!glVDPAURegisterVideoSurfaceNV)
  2988. glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterVideoSurfaceNV");
  2989. if (!glVDPAUIsSurfaceNV)
  2990. glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUIsSurfaceNV");
  2991. if (!glVDPAUUnregisterSurfaceNV)
  2992. glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnregisterSurfaceNV");
  2993. if (!glVDPAUSurfaceAccessNV)
  2994. glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUSurfaceAccessNV");
  2995. if (!glVDPAUMapSurfacesNV)
  2996. glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUMapSurfacesNV");
  2997. if (!glVDPAUUnmapSurfacesNV)
  2998. glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnmapSurfacesNV");
  2999. if (!glVDPAUGetSurfaceivNV)
  3000. glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUGetSurfaceivNV");
  3001. CLog::Log(LOGNOTICE, "VDPAU::COutput GL interop supported");
  3002. }
  3003. else
  3004. #endif
  3005. {
  3006. // TODO should be detected before vdpau is opened, though very unlikely
  3007. // that this code is hit
  3008. CLog::Log(LOGERROR, "VDPAU::COutput driver does not support GL_NV_vdpau_interop");
  3009. }
  3010. #ifdef GL_NV_vdpau_interop
  3011. while (glGetError() != GL_NO_ERROR);
  3012. glVDPAUInitNV(reinterpret_cast<void*>(m_config.context->GetDevice()), reinterpret_cast<void*>(m_config.context->GetProcs().vdp_get_proc_address));
  3013. if (glGetError() != GL_NO_ERROR)
  3014. {
  3015. CLog::Log(LOGERROR, "VDPAU::COutput - GLInitInterop glVDPAUInitNV failed");
  3016. m_vdpError = true;
  3017. return false;
  3018. }
  3019. CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop initialized");
  3020. #endif
  3021. #ifdef GL_ARB_sync
  3022. bool hasfence = glewIsSupported("GL_ARB_sync");
  3023. for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
  3024. {
  3025. m_bufferPool.allRenderPics[i]->usefence = hasfence;
  3026. }
  3027. #endif
  3028. return true;
  3029. }
  3030. void COutput::GLMapSurfaces()
  3031. {
  3032. #ifdef GL_NV_vdpau_interop
  3033. if (m_config.useInteropYuv)
  3034. {
  3035. VdpauBufferPool::GLVideoSurface glSurface;
  3036. VdpVideoSurface surf;
  3037. if (m_config.videoSurfaces->Size() != m_bufferPool.glVideoSurfaceMap.size())
  3038. {
  3039. for (unsigned int i = 0; i < m_config.videoSurfaces->Size(); i++)
  3040. {
  3041. surf = m_config.videoSurfaces->GetAtIndex(i);
  3042. if (surf == VDP_INVALID_HANDLE)
  3043. continue;
  3044. if (m_bufferPool.glVideoSurfaceMap.find(surf) == m_bufferPool.glVideoSurfaceMap.end())
  3045. {
  3046. glSurface.sourceVuv = surf;
  3047. while (glGetError() != GL_NO_ERROR) ;
  3048. glGenTextures(4, glSurface.texture);
  3049. if (glGetError() != GL_NO_ERROR)
  3050. {
  3051. CLog::Log(LOGERROR, "VDPAU::COutput error creating texture");
  3052. m_vdpError = true;
  3053. }
  3054. glSurface.glVdpauSurface = glVDPAURegisterVideoSurfaceNV(reinterpret_cast<void*>(surf),
  3055. GL_TEXTURE_2D, 4, glSurface.texture);
  3056. if (glGetError() != GL_NO_ERROR)
  3057. {
  3058. CLog::Log(LOGERROR, "VDPAU::COutput error register video surface");
  3059. m_vdpError = true;
  3060. }
  3061. glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
  3062. if (glGetError() != GL_NO_ERROR)
  3063. {
  3064. CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
  3065. m_vdpError = true;
  3066. }
  3067. glVDPAUMapSurfacesNV(1, &glSurface.glVdpauSurface);
  3068. if (glGetError() != GL_NO_ERROR)
  3069. {
  3070. CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
  3071. m_vdpError = true;
  3072. }
  3073. m_bufferPool.glVideoSurfaceMap[surf] = glSurface;
  3074. if (m_vdpError)
  3075. return;
  3076. CLog::Log(LOGNOTICE, "VDPAU::COutput registered surface");
  3077. }
  3078. }
  3079. }
  3080. }
  3081. else
  3082. {
  3083. if (m_bufferPool.glOutputSurfaceMap.size() != m_bufferPool.numOutputSurfaces)
  3084. {
  3085. VdpauBufferPool::GLVideoSurface glSurface;
  3086. for (unsigned int i = m_bufferPool.glOutputSurfaceMap.size(); i<m_bufferPool.outputSurfaces.size(); i++)
  3087. {
  3088. glSurface.sourceRgb = m_bufferPool.outputSurfaces[i];
  3089. glGenTextures(1, glSurface.texture);
  3090. glSurface.glVdpauSurface = glVDPAURegisterOutputSurfaceNV(reinterpret_cast<void*>(m_bufferPool.outputSurfaces[i]),
  3091. GL_TEXTURE_2D, 1, glSurface.texture);
  3092. if (glGetError() != GL_NO_ERROR)
  3093. {
  3094. CLog::Log(LOGERROR, "VDPAU::COutput error register output surface");
  3095. m_vdpError = true;
  3096. }
  3097. glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
  3098. if (glGetError() != GL_NO_ERROR)
  3099. {
  3100. CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
  3101. m_vdpError = true;
  3102. }
  3103. glVDPAUMapSurfacesNV(1, &glSurface.glVdpauSurface);
  3104. if (glGetError() != GL_NO_ERROR)
  3105. {
  3106. CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
  3107. m_vdpError = true;
  3108. }
  3109. m_bufferPool.glOutputSurfaceMap[m_bufferPool.outputSurfaces[i]] = glSurface;
  3110. if (m_vdpError)
  3111. return;
  3112. }
  3113. CLog::Log(LOGNOTICE, "VDPAU::COutput registered output surfaces");
  3114. }
  3115. }
  3116. #endif
  3117. }
  3118. void COutput::GLUnmapSurfaces()
  3119. {
  3120. #ifdef GL_NV_vdpau_interop
  3121. {
  3122. std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
  3123. for (it = m_bufferPool.glVideoSurfaceMap.begin(); it != m_bufferPool.glVideoSurfaceMap.end(); ++it)
  3124. {
  3125. glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
  3126. glDeleteTextures(4, it->second.texture);
  3127. }
  3128. m_bufferPool.glVideoSurfaceMap.clear();
  3129. }
  3130. std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
  3131. for (it = m_bufferPool.glOutputSurfaceMap.begin(); it != m_bufferPool.glOutputSurfaceMap.end(); ++it)
  3132. {
  3133. glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
  3134. glDeleteTextures(1, it->second.texture);
  3135. }
  3136. m_bufferPool.glOutputSurfaceMap.clear();
  3137. glVDPAUFiniNV();
  3138. CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop finished");
  3139. #endif
  3140. }
  3141. bool COutput::CheckStatus(VdpStatus vdp_st, int line)
  3142. {
  3143. if (vdp_st != VDP_STATUS_OK)
  3144. {
  3145. CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) at %s:%d\n", m_config.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st, __FILE__, line);
  3146. m_vdpError = true;
  3147. return true;
  3148. }
  3149. return false;
  3150. }
  3151. bool COutput::CreateGlxContext()
  3152. {
  3153. GLXContext glContext;
  3154. m_Display = g_Windowing.GetDisplay();
  3155. glContext = g_Windowing.GetGlxContext();
  3156. m_Window = g_Windowing.GetWindow();
  3157. // Get our window attribs.
  3158. XWindowAttributes wndattribs;
  3159. XGetWindowAttributes(m_Display, m_Window, &wndattribs);
  3160. // Get visual Info
  3161. XVisualInfo visInfo;
  3162. visInfo.visualid = wndattribs.visual->visualid;
  3163. int nvisuals = 0;
  3164. XVisualInfo* visuals = XGetVisualInfo(m_Display, VisualIDMask, &visInfo, &nvisuals);
  3165. if (nvisuals != 1)
  3166. {
  3167. CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - could not find visual");
  3168. return false;
  3169. }
  3170. visInfo = visuals[0];
  3171. XFree(visuals);
  3172. m_pixmap = XCreatePixmap(m_Display,
  3173. m_Window,
  3174. 192,
  3175. 108,
  3176. visInfo.depth);
  3177. if (!m_pixmap)
  3178. {
  3179. CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - Unable to create XPixmap");
  3180. return false;
  3181. }
  3182. // create gl pixmap
  3183. m_glPixmap = glXCreateGLXPixmap(m_Display, &visInfo, m_pixmap);
  3184. if (!m_glPixmap)
  3185. {
  3186. CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not create glPixmap");
  3187. return false;
  3188. }
  3189. m_glContext = glXCreateContext(m_Display, &visInfo, glContext, True);
  3190. if (!glXMakeCurrent(m_Display, m_glPixmap, m_glContext))
  3191. {
  3192. CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not make Pixmap current");
  3193. return false;
  3194. }
  3195. CLog::Log(LOGNOTICE, "VDPAU::COutput::CreateGlxContext - created context");
  3196. return true;
  3197. }
  3198. bool COutput::DestroyGlxContext()
  3199. {
  3200. if (m_glContext)
  3201. {
  3202. glXMakeCurrent(m_Display, None, NULL);
  3203. glXDestroyContext(m_Display, m_glContext);
  3204. }
  3205. m_glContext = 0;
  3206. if (m_glPixmap)
  3207. glXDestroyPixmap(m_Display, m_glPixmap);
  3208. m_glPixmap = 0;
  3209. if (m_pixmap)
  3210. XFreePixmap(m_Display, m_pixmap);
  3211. m_pixmap = 0;
  3212. return true;
  3213. }
  3214. #endif