2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
24 #include "windowing/WindowingFactory.h"
26 #include "guilib/GraphicContext.h"
27 #include "guilib/TextureManager.h"
28 #include "cores/VideoRenderers/RenderManager.h"
29 #include "DVDVideoCodecFFmpeg.h"
31 #include "settings/Settings.h"
32 #include "settings/AdvancedSettings.h"
33 #include "settings/MediaSettings.h"
34 #include "Application.h"
35 #include "utils/MathUtils.h"
36 #include "utils/TimeUtils.h"
37 #include "DVDCodecs/DVDCodecUtils.h"
38 #include "cores/VideoRenderers/RenderFlags.h"
40 using namespace VDPAU;
41 #define NUM_RENDER_PICS 7
43 #define ARSIZE(x) (sizeof(x) / sizeof((x)[0]))
45 // settings codecs mapping
46 DVDCodecAvailableType g_vdpau_available[] = {
47 { AV_CODEC_ID_H263, "videoplayer.usevdpaumpeg4" },
48 { AV_CODEC_ID_MPEG4, "videoplayer.usevdpaumpeg4" },
49 { AV_CODEC_ID_WMV3, "videoplayer.usevdpauvc1" },
50 { AV_CODEC_ID_VC1, "videoplayer.usevdpauvc1" },
51 { AV_CODEC_ID_MPEG2VIDEO, "videoplayer.usevdpaumpeg2" },
53 const size_t settings_count = sizeof(g_vdpau_available) / sizeof(DVDCodecAvailableType);
55 CDecoder::Desc decoder_profiles[] = {
56 {"MPEG1", VDP_DECODER_PROFILE_MPEG1},
57 {"MPEG2_SIMPLE", VDP_DECODER_PROFILE_MPEG2_SIMPLE},
58 {"MPEG2_MAIN", VDP_DECODER_PROFILE_MPEG2_MAIN},
59 {"H264_BASELINE",VDP_DECODER_PROFILE_H264_BASELINE},
60 {"H264_MAIN", VDP_DECODER_PROFILE_H264_MAIN},
61 {"H264_HIGH", VDP_DECODER_PROFILE_H264_HIGH},
62 {"VC1_SIMPLE", VDP_DECODER_PROFILE_VC1_SIMPLE},
63 {"VC1_MAIN", VDP_DECODER_PROFILE_VC1_MAIN},
64 {"VC1_ADVANCED", VDP_DECODER_PROFILE_VC1_ADVANCED},
65 {"MPEG4_PART2_ASP", VDP_DECODER_PROFILE_MPEG4_PART2_ASP},
67 const size_t decoder_profile_count = sizeof(decoder_profiles)/sizeof(CDecoder::Desc);
69 static struct SInterlaceMapping
71 const EINTERLACEMETHOD method;
72 const VdpVideoMixerFeature feature;
73 } g_interlace_mapping[] =
74 { {VS_INTERLACEMETHOD_VDPAU_TEMPORAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
75 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
76 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
77 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF, VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
78 , {VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE , VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE}
79 , {VS_INTERLACEMETHOD_NONE , (VdpVideoMixerFeature)-1}
82 static float studioCSCKCoeffs601[3] = {0.299, 0.587, 0.114}; //BT601 {Kr, Kg, Kb}
83 static float studioCSCKCoeffs709[3] = {0.2126, 0.7152, 0.0722}; //BT709 {Kr, Kg, Kb}
85 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 CVDPAUContext *CVDPAUContext::m_context = 0;
89 CCriticalSection CVDPAUContext::m_section;
90 Display *CVDPAUContext::m_display = 0;
91 void *CVDPAUContext::m_dlHandle = 0;
93 CVDPAUContext::CVDPAUContext()
99 void CVDPAUContext::Release()
101 CSingleLock lock(m_section);
112 void CVDPAUContext::Close()
114 CLog::Log(LOGNOTICE, "VDPAU::Close - closing decoder context");
118 bool CVDPAUContext::EnsureContext(CVDPAUContext **ctx)
120 CSingleLock lock(m_section);
124 m_context->m_refCount++;
129 m_context = new CVDPAUContext();
132 CSingleLock gLock(g_graphicsContext);
133 if (!m_context->LoadSymbols() || !m_context->CreateContext())
141 m_context->m_refCount++;
147 VDPAU_procs& CVDPAUContext::GetProcs()
152 VdpVideoMixerFeature* CVDPAUContext::GetFeatures()
154 return m_vdpFeatures;
157 int CVDPAUContext::GetFeatureCount()
159 return m_featureCount;
162 bool CVDPAUContext::LoadSymbols()
166 m_dlHandle = dlopen("libvdpau.so.1", RTLD_LAZY);
169 const char* error = dlerror();
171 error = "dlerror() returned NULL";
173 CLog::Log(LOGERROR,"VDPAU::LoadSymbols: Unable to get handle to lib: %s", error);
180 dl_vdp_device_create_x11 = (VdpStatus (*)(Display*, int, VdpDevice*, VdpStatus (**)(VdpDevice, VdpFuncId, void**)))dlsym(m_dlHandle, (const char*)"vdp_device_create_x11");
184 CLog::Log(LOGERROR,"(VDPAU) - %s in %s",error,__FUNCTION__);
185 m_vdpDevice = VDP_INVALID_HANDLE;
191 bool CVDPAUContext::CreateContext()
193 CLog::Log(LOGNOTICE,"VDPAU::CreateContext - creating decoder context");
196 { CSingleLock lock(g_graphicsContext);
198 m_display = XOpenDisplay(NULL);
199 mScreen = g_Windowing.GetCurrentScreen();
204 vdp_st = dl_vdp_device_create_x11(m_display,
207 &m_vdpProcs.vdp_get_proc_address);
209 CLog::Log(LOGNOTICE,"vdp_device = 0x%08x vdp_st = 0x%08x",m_vdpDevice,vdp_st);
210 if (vdp_st != VDP_STATUS_OK)
212 CLog::Log(LOGERROR,"(VDPAU) unable to init VDPAU - vdp_st = 0x%x. Falling back.",vdp_st);
213 m_vdpDevice = VDP_INVALID_HANDLE;
218 SpewHardwareAvailable();
222 void CVDPAUContext::QueryProcs()
226 #define VDP_PROC(id, proc) \
228 vdp_st = m_vdpProcs.vdp_get_proc_address(m_vdpDevice, id, (void**)&proc); \
229 if (vdp_st != VDP_STATUS_OK) \
231 CLog::Log(LOGERROR, "CVDPAUContext::GetProcs - failed to get proc id"); \
235 VDP_PROC(VDP_FUNC_ID_GET_ERROR_STRING , m_vdpProcs.vdp_get_error_string);
236 VDP_PROC(VDP_FUNC_ID_DEVICE_DESTROY , m_vdpProcs.vdp_device_destroy);
237 VDP_PROC(VDP_FUNC_ID_GENERATE_CSC_MATRIX , m_vdpProcs.vdp_generate_csc_matrix);
238 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_CREATE , m_vdpProcs.vdp_video_surface_create);
239 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY , m_vdpProcs.vdp_video_surface_destroy);
240 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_put_bits_y_cb_cr);
241 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_get_bits_y_cb_cr);
242 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_output_surface_put_bits_y_cb_cr);
243 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE , m_vdpProcs.vdp_output_surface_put_bits_native);
244 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_CREATE , m_vdpProcs.vdp_output_surface_create);
245 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY , m_vdpProcs.vdp_output_surface_destroy);
246 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE , m_vdpProcs.vdp_output_surface_get_bits_native);
247 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, m_vdpProcs.vdp_output_surface_render_output_surface);
248 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED , m_vdpProcs.vdp_output_surface_put_bits_indexed);
249 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_CREATE , m_vdpProcs.vdp_video_mixer_create);
250 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES , m_vdpProcs.vdp_video_mixer_set_feature_enables);
251 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_DESTROY , m_vdpProcs.vdp_video_mixer_destroy);
252 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_RENDER , m_vdpProcs.vdp_video_mixer_render);
253 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES , m_vdpProcs.vdp_video_mixer_set_attribute_values);
254 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_PARAMETER_SUPPORT , m_vdpProcs.vdp_video_mixer_query_parameter_support);
255 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_FEATURE_SUPPORT , m_vdpProcs.vdp_video_mixer_query_feature_support);
256 VDP_PROC(VDP_FUNC_ID_DECODER_CREATE , m_vdpProcs.vdp_decoder_create);
257 VDP_PROC(VDP_FUNC_ID_DECODER_DESTROY , m_vdpProcs.vdp_decoder_destroy);
258 VDP_PROC(VDP_FUNC_ID_DECODER_RENDER , m_vdpProcs.vdp_decoder_render);
259 VDP_PROC(VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES , m_vdpProcs.vdp_decoder_query_caps);
263 VdpDevice CVDPAUContext::GetDevice()
268 void CVDPAUContext::DestroyContext()
270 if (!m_vdpProcs.vdp_device_destroy)
273 m_vdpProcs.vdp_device_destroy(m_vdpDevice);
274 m_vdpDevice = VDP_INVALID_HANDLE;
277 void CVDPAUContext::SpewHardwareAvailable() //CopyrighVDPAUt (c) 2008 Wladimir J. van der Laan -- VDPInfo
280 CLog::Log(LOGNOTICE,"VDPAU Decoder capabilities:");
281 CLog::Log(LOGNOTICE,"name level macbs width height");
282 CLog::Log(LOGNOTICE,"------------------------------------");
283 for(unsigned int x=0; x<decoder_profile_count; ++x)
285 VdpBool is_supported = false;
286 uint32_t max_level, max_macroblocks, max_width, max_height;
287 rv = m_vdpProcs.vdp_decoder_query_caps(m_vdpDevice, decoder_profiles[x].id,
288 &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
289 if(rv == VDP_STATUS_OK && is_supported)
291 CLog::Log(LOGNOTICE,"%-16s %2i %5i %5i %5i\n", decoder_profiles[x].name,
292 max_level, max_macroblocks, max_width, max_height);
295 CLog::Log(LOGNOTICE,"------------------------------------");
297 #define CHECK_SUPPORT(feature) \
300 if(m_vdpProcs.vdp_video_mixer_query_feature_support(m_vdpDevice, feature, &supported) == VDP_STATUS_OK && supported) { \
301 CLog::Log(LOGNOTICE, "Mixer feature: "#feature); \
302 m_vdpFeatures[m_featureCount++] = feature; \
306 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION);
307 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_SHARPNESS);
308 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL);
309 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL);
310 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE);
311 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
312 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1);
313 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2);
314 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3);
315 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4);
316 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5);
317 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6);
318 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7);
319 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8);
320 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9);
325 bool CVDPAUContext::Supports(VdpVideoMixerFeature feature)
327 for(int i = 0; i < m_featureCount; i++)
329 if(m_vdpFeatures[i] == feature)
335 //-----------------------------------------------------------------------------
336 // VDPAU Video Surface states
337 //-----------------------------------------------------------------------------
339 #define SURFACE_USED_FOR_REFERENCE 0x01
340 #define SURFACE_USED_FOR_RENDER 0x02
342 void CVideoSurfaces::AddSurface(VdpVideoSurface surf)
344 CSingleLock lock(m_section);
345 m_state[surf] = SURFACE_USED_FOR_REFERENCE;
348 void CVideoSurfaces::ClearReference(VdpVideoSurface surf)
350 CSingleLock lock(m_section);
351 if (m_state.find(surf) == m_state.end())
353 CLog::Log(LOGWARNING, "CVideoSurfaces::ClearReference - surface invalid");
356 m_state[surf] &= ~SURFACE_USED_FOR_REFERENCE;
357 if (m_state[surf] == 0)
359 m_freeSurfaces.push_back(surf);
363 bool CVideoSurfaces::MarkRender(VdpVideoSurface surf)
365 CSingleLock lock(m_section);
366 if (m_state.find(surf) == m_state.end())
368 CLog::Log(LOGWARNING, "CVideoSurfaces::MarkRender - surface invalid");
371 std::list<VdpVideoSurface>::iterator it;
372 it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
373 if (it != m_freeSurfaces.end())
375 m_freeSurfaces.erase(it);
377 m_state[surf] |= SURFACE_USED_FOR_RENDER;
381 void CVideoSurfaces::ClearRender(VdpVideoSurface surf)
383 CSingleLock lock(m_section);
384 if (m_state.find(surf) == m_state.end())
386 CLog::Log(LOGWARNING, "CVideoSurfaces::ClearRender - surface invalid");
389 m_state[surf] &= ~SURFACE_USED_FOR_RENDER;
390 if (m_state[surf] == 0)
392 m_freeSurfaces.push_back(surf);
396 bool CVideoSurfaces::IsValid(VdpVideoSurface surf)
398 CSingleLock lock(m_section);
399 if (m_state.find(surf) != m_state.end())
405 VdpVideoSurface CVideoSurfaces::GetFree(VdpVideoSurface surf)
407 CSingleLock lock(m_section);
408 if (m_state.find(surf) != m_state.end())
410 std::list<VdpVideoSurface>::iterator it;
411 it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
412 if (it == m_freeSurfaces.end())
414 CLog::Log(LOGWARNING, "CVideoSurfaces::GetFree - surface not free");
418 m_freeSurfaces.erase(it);
419 m_state[surf] = SURFACE_USED_FOR_REFERENCE;
424 if (!m_freeSurfaces.empty())
426 VdpVideoSurface freeSurf = m_freeSurfaces.front();
427 m_freeSurfaces.pop_front();
428 m_state[freeSurf] = SURFACE_USED_FOR_REFERENCE;
432 return VDP_INVALID_HANDLE;
435 VdpVideoSurface CVideoSurfaces::GetAtIndex(int idx)
437 if (idx >= m_state.size())
438 return VDP_INVALID_HANDLE;
440 std::map<VdpVideoSurface, int>::iterator it = m_state.begin();
441 for(int i = 0; i < idx; i++)
446 VdpVideoSurface CVideoSurfaces::RemoveNext(bool skiprender)
448 CSingleLock lock(m_section);
449 VdpVideoSurface surf;
450 std::map<VdpVideoSurface, int>::iterator it;
451 for(it = m_state.begin(); it != m_state.end(); ++it)
453 if (skiprender && it->second & SURFACE_USED_FOR_RENDER)
458 std::list<VdpVideoSurface>::iterator it2;
459 it2 = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
460 if (it2 != m_freeSurfaces.end())
461 m_freeSurfaces.erase(it2);
464 return VDP_INVALID_HANDLE;
467 void CVideoSurfaces::Reset()
469 CSingleLock lock(m_section);
470 m_freeSurfaces.clear();
474 int CVideoSurfaces::Size()
476 CSingleLock lock(m_section);
477 return m_state.size();
480 //-----------------------------------------------------------------------------
482 //-----------------------------------------------------------------------------
484 CDecoder::CDecoder() : m_vdpauOutput(&m_inMsgEvent)
486 m_vdpauConfig.videoSurfaces = &m_videoSurfaces;
488 m_vdpauConfigured = false;
489 m_hwContext.bitstream_buffers_allocated = 0;
490 m_DisplayState = VDPAU_OPEN;
491 m_vdpauConfig.context = 0;
494 bool CDecoder::Open(AVCodecContext* avctx, const enum PixelFormat fmt, unsigned int surfaces)
496 // check if user wants to decode this format with VDPAU
497 std::string gpuvendor = g_Windowing.GetRenderVendor();
498 std::transform(gpuvendor.begin(), gpuvendor.end(), gpuvendor.begin(), ::tolower);
499 // nvidia is whitelisted despite for mpeg-4 we need to query user settings
500 if ((gpuvendor.compare(0, 6, "nvidia") != 0) || (avctx->codec_id == AV_CODEC_ID_MPEG4) || (avctx->codec_id == AV_CODEC_ID_H263))
502 if (CDVDVideoCodec::IsCodecDisabled(g_vdpau_available, settings_count, avctx->codec_id))
506 #ifndef GL_NV_vdpau_interop
507 CLog::Log(LOGNOTICE, "VDPAU: compilation without required extension GL_NV_vdpau_interop");
510 if (!g_Windowing.IsExtSupported("GL_NV_vdpau_interop"))
512 CLog::Log(LOGNOTICE, "VDPAU: required extension GL_NV_vdpau_interop not found");
516 if(avctx->coded_width == 0
517 || avctx->coded_height == 0)
519 CLog::Log(LOGWARNING,"(VDPAU) no width/height available, can't init");
522 m_vdpauConfig.numRenderBuffers = surfaces;
523 m_decoderThread = CThread::GetCurrentThreadId();
525 if (!CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
528 m_DisplayState = VDPAU_OPEN;
529 m_vdpauConfigured = false;
531 if (!m_dllAvUtil.Load())
534 m_presentPicture = 0;
537 VdpDecoderProfile profile = 0;
538 /* Change FFMPEG Codec ID to VDPAU Profile. */
539 ReadFormatOf(avctx->codec_id, profile, m_vdpauConfig.vdpChromaType);
543 VdpBool is_supported = false;
544 uint32_t max_level, max_macroblocks, max_width, max_height;
545 /* Query Device Capabilities to Ensure that VDPAU Can handle the Requested Codec */
546 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_query_caps(m_vdpauConfig.context->GetDevice(),
547 profile, &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
548 /* Test to make Sure there is a Possibility the Codec will Work */
549 if(CheckStatus(vdp_st, __LINE__))
551 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) checking for decoder support\n", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
554 if(max_width < avctx->coded_width || max_height < avctx->coded_height)
556 CLog::Log(LOGWARNING,"(VDPAU) Requested Picture Dimensions (%i, %i) exceed hardware capabilities ( %i, %i).",
557 avctx->coded_width, avctx->coded_height, max_width, max_height);
560 if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
561 CLog::Log(LOGWARNING,"(VDPAU) width %i might not be supported because of hardware bug", avctx->width);
563 /* attempt to create a decoder with this width/height, some sizes are not supported by hw */
564 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(), profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
566 if(CheckStatus(vdp_st, __LINE__))
568 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) checking for decoder support\n", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
572 m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
573 CheckStatus(vdp_st, __LINE__);
575 /* finally setup ffmpeg */
576 memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
577 m_hwContext.render = CDecoder::Render;
578 m_hwContext.bitstream_buffers_allocated = 0;
579 avctx->get_buffer = CDecoder::FFGetBuffer;
580 avctx->reget_buffer = CDecoder::FFGetBuffer;
581 avctx->release_buffer = CDecoder::FFReleaseBuffer;
582 avctx->draw_horiz_band = CDecoder::FFDrawSlice;
583 avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
584 avctx->hwaccel_context = &m_hwContext;
585 avctx->thread_count = 1;
587 g_Windowing.Register(this);
594 CDecoder::~CDecoder()
599 void CDecoder::Close()
601 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
603 g_Windowing.Unregister(this);
605 CSingleLock lock(m_DecoderSection);
608 m_vdpauOutput.Dispose();
610 if (m_hwContext.bitstream_buffers_allocated)
612 m_dllAvUtil.av_freep(&m_hwContext.bitstream_buffers);
615 m_dllAvUtil.Unload();
617 if (m_vdpauConfig.context)
618 m_vdpauConfig.context->Release();
619 m_vdpauConfig.context = 0;
622 long CDecoder::Release()
624 // check if we should do some pre-cleanup here
625 // a second decoder might need resources
626 if (m_vdpauConfigured == true)
628 CSingleLock lock(m_DecoderSection);
629 CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
632 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
636 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
640 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
641 m_DisplayState = VDPAU_ERROR;
646 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
647 m_DisplayState = VDPAU_ERROR;
650 VdpVideoSurface surf;
651 while((surf = m_videoSurfaces.RemoveNext(true)) != VDP_INVALID_HANDLE)
653 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
656 return IHardwareDecoder::Release();
659 long CDecoder::ReleasePicReference()
661 return IHardwareDecoder::Release();
664 void CDecoder::SetWidthHeight(int width, int height)
666 m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
668 //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
669 //this requires the least amount of gpu memory bandwidth
670 if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
672 //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
673 if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
675 m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
676 m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
678 else //scale height to the desktop size if the aspect ratio is smaller than the desktop
680 m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
681 m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
686 m_vdpauConfig.outWidth = width;
687 m_vdpauConfig.outHeight = height;
689 CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
692 void CDecoder::OnLostDevice()
694 CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
696 int count = g_graphicsContext.exit();
698 CSingleLock lock(m_DecoderSection);
700 if (m_vdpauConfig.context)
701 m_vdpauConfig.context->Release();
702 m_vdpauConfig.context = 0;
704 m_DisplayState = VDPAU_LOST;
706 m_DisplayEvent.Reset();
708 g_graphicsContext.restore(count);
711 void CDecoder::OnResetDevice()
713 CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
715 int count = g_graphicsContext.exit();
717 CSingleLock lock(m_DecoderSection);
718 if (m_DisplayState == VDPAU_LOST)
720 m_DisplayState = VDPAU_RESET;
722 m_DisplayEvent.Set();
725 g_graphicsContext.restore(count);
728 int CDecoder::Check(AVCodecContext* avctx)
732 { CSingleLock lock(m_DecoderSection);
733 state = m_DisplayState;
736 if (state == VDPAU_LOST)
738 CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
739 if (!m_DisplayEvent.WaitMSec(4000))
741 CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
746 CSingleLock lock(m_DecoderSection);
747 state = m_DisplayState;
750 if (state == VDPAU_RESET || state == VDPAU_ERROR)
752 CSingleLock lock(m_DecoderSection);
755 if (m_vdpauConfig.context)
756 m_vdpauConfig.context->Release();
757 m_vdpauConfig.context = 0;
759 if (CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
761 m_DisplayState = VDPAU_OPEN;
762 m_vdpauConfigured = false;
765 if (state == VDPAU_RESET)
773 bool CDecoder::IsVDPAUFormat(PixelFormat format)
775 if (format == AV_PIX_FMT_VDPAU)
781 bool CDecoder::Supports(VdpVideoMixerFeature feature)
783 return m_vdpauConfig.context->Supports(feature);
786 bool CDecoder::Supports(EINTERLACEMETHOD method)
788 if(method == VS_INTERLACEMETHOD_VDPAU_BOB
789 || method == VS_INTERLACEMETHOD_AUTO)
792 if (method == VS_INTERLACEMETHOD_RENDER_BOB)
795 if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
798 for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
800 if(p->method == method)
801 return Supports(p->feature);
806 EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
808 return VS_INTERLACEMETHOD_RENDER_BOB;
811 void CDecoder::FiniVDPAUOutput()
813 if (!m_vdpauConfigured)
816 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
819 m_vdpauOutput.Dispose();
820 m_vdpauConfigured = false;
824 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
825 if (CheckStatus(vdp_st, __LINE__))
827 m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
829 CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", m_videoSurfaces.Size());
831 VdpVideoSurface surf;
832 while((surf = m_videoSurfaces.RemoveNext()) != VDP_INVALID_HANDLE)
834 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
835 if (CheckStatus(vdp_st, __LINE__))
838 m_videoSurfaces.Reset();
841 void CDecoder::ReadFormatOf( AVCodecID codec
842 , VdpDecoderProfile &vdp_decoder_profile
843 , VdpChromaType &vdp_chroma_type)
847 case AV_CODEC_ID_MPEG1VIDEO:
848 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
849 vdp_chroma_type = VDP_CHROMA_TYPE_420;
851 case AV_CODEC_ID_MPEG2VIDEO:
852 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
853 vdp_chroma_type = VDP_CHROMA_TYPE_420;
855 case AV_CODEC_ID_H264:
856 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
857 vdp_chroma_type = VDP_CHROMA_TYPE_420;
859 case AV_CODEC_ID_WMV3:
860 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
861 vdp_chroma_type = VDP_CHROMA_TYPE_420;
863 case AV_CODEC_ID_VC1:
864 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
865 vdp_chroma_type = VDP_CHROMA_TYPE_420;
867 case AV_CODEC_ID_MPEG4:
868 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
869 vdp_chroma_type = VDP_CHROMA_TYPE_420;
872 vdp_decoder_profile = 0;
878 bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
883 VdpDecoderProfile vdp_decoder_profile;
885 m_vdpauConfig.vidWidth = avctx->width;
886 m_vdpauConfig.vidHeight = avctx->height;
887 m_vdpauConfig.surfaceWidth = avctx->coded_width;
888 m_vdpauConfig.surfaceHeight = avctx->coded_height;
890 SetWidthHeight(avctx->width,avctx->height);
892 CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
893 CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
895 ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
897 if(avctx->codec_id == AV_CODEC_ID_H264)
899 m_vdpauConfig.maxReferences = ref_frames;
900 if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
901 if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
904 m_vdpauConfig.maxReferences = 2;
906 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(),
908 m_vdpauConfig.surfaceWidth,
909 m_vdpauConfig.surfaceHeight,
910 m_vdpauConfig.maxReferences,
911 &m_vdpauConfig.vdpDecoder);
912 if (CheckStatus(vdp_st, __LINE__))
916 CSingleLock lock(g_graphicsContext);
917 m_vdpauConfig.stats = &m_bufferStats;
918 m_vdpauConfig.vdpau = this;
919 m_bufferStats.Reset();
920 m_vdpauOutput.Start();
922 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::INIT,
926 sizeof(m_vdpauConfig)))
928 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
932 CLog::Log(LOGERROR, "VDPAU::%s - vdpau output returned error", __FUNCTION__);
933 m_vdpauOutput.Dispose();
939 CLog::Log(LOGERROR, "VDPAU::%s - failed to init output", __FUNCTION__);
940 m_vdpauOutput.Dispose();
944 m_inMsgEvent.Reset();
945 m_vdpauConfigured = true;
949 int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic)
951 //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
952 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
953 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
955 // while we are waiting to recover we can't do anything
956 CSingleLock lock(vdp->m_DecoderSection);
958 if(vdp->m_DisplayState != VDPAU_OPEN)
960 CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
964 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
965 surf = vdp->m_videoSurfaces.GetFree(surf != 0 ? surf : VDP_INVALID_HANDLE);
967 VdpStatus vdp_st = VDP_STATUS_ERROR;
968 if (surf == VDP_INVALID_HANDLE)
970 // create a new surface
971 VdpDecoderProfile profile;
972 ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
974 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_video_surface_create(vdp->m_vdpauConfig.context->GetDevice(),
975 vdp->m_vdpauConfig.vdpChromaType,
979 vdp->CheckStatus(vdp_st, __LINE__);
980 if (vdp_st != VDP_STATUS_OK)
982 CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
985 vdp->m_videoSurfaces.AddSurface(surf);
988 pic->data[1] = pic->data[2] = NULL;
989 pic->data[0] = (uint8_t*)(uintptr_t)surf;
990 pic->data[3] = (uint8_t*)(uintptr_t)surf;
992 pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
994 pic->type= FF_BUFFER_TYPE_USER;
996 pic->reordered_opaque= avctx->reordered_opaque;
1000 void CDecoder::FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic)
1002 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
1003 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
1005 VdpVideoSurface surf;
1008 CSingleLock lock(vdp->m_DecoderSection);
1010 surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
1012 vdp->m_videoSurfaces.ClearReference(surf);
1018 VdpStatus CDecoder::Render( VdpDecoder decoder, VdpVideoSurface target,
1019 VdpPictureInfo const *picture_info,
1020 uint32_t bitstream_buffer_count,
1021 VdpBitstreamBuffer const * bitstream_buffers)
1023 return VDP_STATUS_OK;
1026 void CDecoder::FFDrawSlice(struct AVCodecContext *s,
1027 const AVFrame *src, int offset[4],
1028 int y, int type, int height)
1030 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
1031 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
1033 // while we are waiting to recover we can't do anything
1034 CSingleLock lock(vdp->m_DecoderSection);
1036 if(vdp->m_DisplayState != VDPAU_OPEN)
1039 if(src->linesize[0] || src->linesize[1] || src->linesize[2]
1040 || offset[0] || offset[1] || offset[2])
1042 CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
1047 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
1049 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1050 if (!vdp->m_videoSurfaces.IsValid(surf))
1052 CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
1056 uint32_t max_refs = 0;
1057 if(s->codec_id == AV_CODEC_ID_H264)
1058 max_refs = vdp->m_hwContext.info.h264.num_ref_frames;
1060 if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
1061 || vdp->m_vdpauConfigured == false
1062 || vdp->m_vdpauConfig.maxReferences < max_refs)
1064 if(!vdp->ConfigVDPAU(s, max_refs))
1068 uint64_t startTime = CurrentHostCounter();
1069 uint16_t decoded, processed, rend;
1070 vdp->m_bufferStats.Get(decoded, processed, rend);
1071 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
1073 (VdpPictureInfo const *)&(vdp->m_hwContext.info),
1074 vdp->m_hwContext.bitstream_buffers_used,
1075 vdp->m_hwContext.bitstream_buffers);
1076 vdp->CheckStatus(vdp_st, __LINE__);
1077 uint64_t diff = CurrentHostCounter() - startTime;
1078 if (diff*1000/CurrentHostFrequency() > 30)
1079 CLog::Log(LOGDEBUG, "CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
1083 int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
1085 int result = Check(avctx);
1089 CSingleLock lock(m_DecoderSection);
1091 if (!m_vdpauConfigured)
1095 { // we have a new frame from decoder
1097 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pFrame->data[3];
1098 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1099 if (!m_videoSurfaces.IsValid(surf))
1101 CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
1104 m_videoSurfaces.MarkRender(surf);
1106 // send frame to output for processing
1107 CVdpauDecodedPicture pic;
1108 memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
1109 ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
1110 pic.videoSurface = surf;
1111 pic.DVDPic.color_matrix = avctx->colorspace;
1112 m_bufferStats.IncDecoded();
1113 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
1116 // m_codecControl = pic.DVDPic.iFlags & (DVP_FLAG_DRAIN | DVP_FLAG_NO_POSTPROC);
1120 uint16_t decoded, processed, render;
1122 while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1124 if (msg->signal == COutputControlProtocol::ERROR)
1126 m_DisplayState = VDPAU_ERROR;
1132 m_bufferStats.Get(decoded, processed, render);
1134 uint64_t startTime = CurrentHostCounter();
1137 // first fill the buffers to keep vdpau busy
1138 // mixer will run with decoded >= 2. output is limited by number of output surfaces
1139 // In case mixer is bypassed we limit by looking at processed
1140 if (decoded < 3 && processed < 3)
1142 retval |= VC_BUFFER;
1144 else if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
1146 if (msg->signal == COutputDataProtocol::PICTURE)
1148 if (m_presentPicture)
1150 m_presentPicture->ReturnUnused();
1151 m_presentPicture = 0;
1154 m_presentPicture = *(CVdpauRenderPicture**)msg->data;
1155 m_presentPicture->vdpau = this;
1156 m_bufferStats.DecRender();
1157 m_bufferStats.Get(decoded, processed, render);
1158 retval |= VC_PICTURE;
1164 else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1166 if (msg->signal == COutputControlProtocol::STATS)
1168 m_bufferStats.Get(decoded, processed, render);
1172 m_DisplayState = VDPAU_ERROR;
1178 if (decoded < 3 && processed < 3)
1180 retval |= VC_BUFFER;
1183 if (!retval && !m_inMsgEvent.WaitMSec(2000))
1186 uint64_t diff = CurrentHostCounter() - startTime;
1187 if (retval & VC_PICTURE)
1189 m_bufferStats.SetParams(diff, m_codecControl);
1191 if (diff*1000/CurrentHostFrequency() > 50)
1192 CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
1196 CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
1197 m_DisplayState = VDPAU_ERROR;
1204 bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
1206 CSingleLock lock(m_DecoderSection);
1208 if (m_DisplayState != VDPAU_OPEN)
1211 *picture = m_presentPicture->DVDPic;
1212 picture->vdpau = m_presentPicture;
1217 void CDecoder::Reset()
1219 CSingleLock lock(m_DecoderSection);
1221 if (!m_vdpauConfigured)
1225 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
1229 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
1233 CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
1234 m_DisplayState = VDPAU_ERROR;
1237 m_bufferStats.Reset();
1241 CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
1242 m_DisplayState = VDPAU_ERROR;
1246 bool CDecoder::CanSkipDeint()
1248 return m_bufferStats.CanSkipDeint();
1251 void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
1253 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
1256 bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
1258 if (vdp_st != VDP_STATUS_OK)
1260 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);
1262 if(m_DisplayState == VDPAU_OPEN)
1264 if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
1266 m_DisplayEvent.Reset();
1267 m_DisplayState = VDPAU_LOST;
1270 m_DisplayState = VDPAU_ERROR;
1278 //-----------------------------------------------------------------------------
1280 //-----------------------------------------------------------------------------
1282 CVdpauRenderPicture* CVdpauRenderPicture::Acquire()
1284 CSingleLock lock(renderPicSection);
1293 long CVdpauRenderPicture::Release()
1295 CSingleLock lock(renderPicSection);
1302 vdpau->ReturnRenderPicture(this);
1303 vdpau->ReleasePicReference();
1308 void CVdpauRenderPicture::ReturnUnused()
1310 { CSingleLock lock(renderPicSection);
1315 vdpau->ReturnRenderPicture(this);
1318 void CVdpauRenderPicture::Sync()
1321 CSingleLock lock(renderPicSection);
1326 glDeleteSync(fence);
1329 fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1334 //-----------------------------------------------------------------------------
1336 //-----------------------------------------------------------------------------
1337 CMixer::CMixer(CEvent *inMsgEvent) :
1338 CThread("Vdpau Mixer"),
1339 m_controlPort("ControlPort", inMsgEvent, &m_outMsgEvent),
1340 m_dataPort("DataPort", inMsgEvent, &m_outMsgEvent)
1342 m_inMsgEvent = inMsgEvent;
1350 void CMixer::Start()
1355 void CMixer::Dispose()
1358 m_outMsgEvent.Set();
1361 m_controlPort.Purge();
1365 bool CMixer::IsActive()
1370 void CMixer::OnStartup()
1372 CLog::Log(LOGNOTICE, "CMixer::OnStartup: Output Thread created");
1375 void CMixer::OnExit()
1377 CLog::Log(LOGNOTICE, "CMixer::OnExit: Output Thread terminated");
1384 M_TOP_UNCONFIGURED, // 2
1385 M_TOP_CONFIGURED, // 3
1386 M_TOP_CONFIGURED_WAIT1, // 4
1387 M_TOP_CONFIGURED_STEP1, // 5
1388 M_TOP_CONFIGURED_WAIT2, // 6
1389 M_TOP_CONFIGURED_STEP2, // 7
1392 int MIXER_parentStates[] = {
1395 0, //TOP_UNCONFIGURED
1397 3, //TOP_CONFIGURED_WAIT1
1398 3, //TOP_CONFIGURED_STEP1
1399 3, //TOP_CONFIGURED_WAIT2
1400 3, //TOP_CONFIGURED_STEP2
1403 void CMixer::StateMachine(int signal, Protocol *port, Message *msg)
1405 for (int state = m_state; ; state = MIXER_parentStates[state])
1410 if (port == &m_controlPort)
1414 case CMixerControlProtocol::FLUSH:
1416 msg->Reply(CMixerControlProtocol::ACC);
1423 std::string portName = port == NULL ? "timer" : port->portName;
1424 CLog::Log(LOGWARNING, "CMixer::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
1428 case M_TOP_ERROR: // TOP
1431 case M_TOP_UNCONFIGURED:
1432 if (port == &m_controlPort)
1436 case CMixerControlProtocol::INIT:
1438 data = (CVdpauConfig*)msg->data;
1446 m_state = M_TOP_CONFIGURED_WAIT1;
1447 msg->Reply(CMixerControlProtocol::ACC);
1451 msg->Reply(CMixerControlProtocol::ERROR);
1460 case M_TOP_CONFIGURED:
1461 if (port == &m_dataPort)
1465 case CMixerDataProtocol::FRAME:
1466 CVdpauDecodedPicture *frame;
1467 frame = (CVdpauDecodedPicture*)msg->data;
1470 m_decodedPics.push(*frame);
1474 case CMixerDataProtocol::BUFFER:
1475 VdpOutputSurface *surf;
1476 surf = (VdpOutputSurface*)msg->data;
1479 m_outputSurfaces.push(*surf);
1489 case M_TOP_CONFIGURED_WAIT1:
1490 if (port == NULL) // timeout
1494 case CMixerControlProtocol::TIMEOUT:
1495 if (!m_decodedPics.empty() && !m_outputSurfaces.empty())
1497 m_state = M_TOP_CONFIGURED_STEP1;
1498 m_bStateMachineSelfTrigger = true;
1511 case M_TOP_CONFIGURED_STEP1:
1512 if (port == NULL) // timeout
1516 case CMixerControlProtocol::TIMEOUT:
1517 m_mixerInput.push_front(m_decodedPics.front());
1518 m_decodedPics.pop();
1519 if (m_mixerInput.size() < 2)
1521 m_state = M_TOP_CONFIGURED_WAIT1;
1529 m_state = M_TOP_CONFIGURED_WAIT1;
1530 m_extTimeout = 1000;
1533 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1534 m_outputSurfaces.pop();
1535 m_config.stats->IncProcessed();
1536 m_config.stats->DecDecoded();
1537 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1538 if (m_mixersteps > 1)
1540 m_state = M_TOP_CONFIGURED_WAIT2;
1546 m_state = M_TOP_CONFIGURED_WAIT1;
1556 case M_TOP_CONFIGURED_WAIT2:
1557 if (port == NULL) // timeout
1561 case CMixerControlProtocol::TIMEOUT:
1562 if (!m_outputSurfaces.empty())
1564 m_state = M_TOP_CONFIGURED_STEP2;
1565 m_bStateMachineSelfTrigger = true;
1578 case M_TOP_CONFIGURED_STEP2:
1579 if (port == NULL) // timeout
1583 case CMixerControlProtocol::TIMEOUT:
1584 m_processPicture.outputSurface = m_outputSurfaces.front();
1589 m_state = M_TOP_CONFIGURED_WAIT1;
1590 m_extTimeout = 1000;
1593 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1594 m_outputSurfaces.pop();
1595 m_config.stats->IncProcessed();
1596 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1598 m_state = M_TOP_CONFIGURED_WAIT1;
1607 default: // we are in no state, should not happen
1608 CLog::Log(LOGERROR, "CMixer::%s - no valid state: %d", __FUNCTION__, m_state);
1614 void CMixer::Process()
1616 Message *msg = NULL;
1617 Protocol *port = NULL;
1620 m_state = M_TOP_UNCONFIGURED;
1621 m_extTimeout = 1000;
1622 m_bStateMachineSelfTrigger = false;
1628 if (m_bStateMachineSelfTrigger)
1630 m_bStateMachineSelfTrigger = false;
1631 // self trigger state machine
1632 StateMachine(msg->signal, port, msg);
1633 if (!m_bStateMachineSelfTrigger)
1640 // check control port
1641 else if (m_controlPort.ReceiveOutMessage(&msg))
1644 port = &m_controlPort;
1647 else if (m_dataPort.ReceiveOutMessage(&msg))
1655 StateMachine(msg->signal, port, msg);
1656 if (!m_bStateMachineSelfTrigger)
1665 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
1672 msg = m_controlPort.GetMessage();
1673 msg->signal = CMixerControlProtocol::TIMEOUT;
1675 // signal timeout to state machine
1676 StateMachine(msg->signal, port, msg);
1677 if (!m_bStateMachineSelfTrigger)
1687 void CMixer::CreateVdpauMixer()
1689 CLog::Log(LOGNOTICE, " (VDPAU) Creating the video mixer");
1691 InitCSCMatrix(m_config.vidWidth);
1693 VdpVideoMixerParameter parameters[] = {
1694 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
1695 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
1696 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE};
1698 void const * parameter_values[] = {
1699 &m_config.surfaceWidth,
1700 &m_config.surfaceHeight,
1701 &m_config.vdpChromaType};
1703 VdpStatus vdp_st = VDP_STATUS_ERROR;
1704 vdp_st = m_config.context->GetProcs().vdp_video_mixer_create(m_config.context->GetDevice(),
1705 m_config.context->GetFeatureCount(),
1706 m_config.context->GetFeatures(),
1711 CheckStatus(vdp_st, __LINE__);
1715 void CMixer::InitCSCMatrix(int Width)
1717 m_Procamp.struct_version = VDP_PROCAMP_VERSION;
1718 m_Procamp.brightness = 0.0;
1719 m_Procamp.contrast = 1.0;
1720 m_Procamp.saturation = 1.0;
1724 void CMixer::CheckFeatures()
1726 if (m_Upscale != m_config.upscale)
1729 m_Upscale = m_config.upscale;
1731 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness ||
1732 m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast ||
1733 m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
1736 m_Brightness = CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness;
1737 m_Contrast = CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast;
1738 m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
1740 if (m_NoiseReduction != CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1742 m_NoiseReduction = CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction;
1743 SetNoiseReduction();
1745 if (m_Sharpness != CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1747 m_Sharpness = CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness;
1750 if (m_DeintMode != CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode ||
1751 m_Deint != CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod)
1753 m_DeintMode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
1754 m_Deint = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1759 void CMixer::SetPostProcFeatures(bool postProcEnabled)
1761 if (m_PostProc != postProcEnabled)
1763 if (postProcEnabled)
1765 SetNoiseReduction();
1772 m_PostProc = postProcEnabled;
1776 void CMixer::PostProcOff()
1780 if (m_videoMixer == VDP_INVALID_HANDLE)
1783 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1784 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1785 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE};
1787 VdpBool enabled[]={0,0,0};
1788 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1789 CheckStatus(vdp_st, __LINE__);
1791 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1793 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION};
1795 VdpBool enabled[]={0};
1796 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1797 CheckStatus(vdp_st, __LINE__);
1800 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1802 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS};
1804 VdpBool enabled[]={0};
1805 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1806 CheckStatus(vdp_st, __LINE__);
1812 bool CMixer::GenerateStudioCSCMatrix(VdpColorStandard colorStandard, VdpCSCMatrix &studioCSCMatrix)
1814 // instead use studioCSCKCoeffs601[3], studioCSCKCoeffs709[3] to generate float[3][4] matrix (float studioCSC[3][4])
1815 // m00 = mRY = red: luma factor (contrast factor) (1.0)
1816 // m10 = mGY = green: luma factor (contrast factor) (1.0)
1817 // m20 = mBY = blue: luma factor (contrast factor) (1.0)
1819 // m01 = mRB = red: blue color diff coeff (0.0)
1820 // m11 = mGB = green: blue color diff coeff (-2Kb(1-Kb)/(Kg))
1821 // m21 = mBB = blue: blue color diff coeff ((1-Kb)/0.5)
1823 // m02 = mRR = red: red color diff coeff ((1-Kr)/0.5)
1824 // m12 = mGR = green: red color diff coeff (-2Kr(1-Kr)/(Kg))
1825 // m22 = mBR = blue: red color diff coeff (0.0)
1827 // m03 = mRC = red: colour zero offset (brightness factor) (-(1-Kr)/0.5 * (128/255))
1828 // m13 = mGC = green: colour zero offset (brightness factor) ((256/255) * (Kb(1-Kb) + Kr(1-Kr)) / Kg)
1829 // m23 = mBC = blue: colour zero offset (brightness factor) (-(1-Kb)/0.5 * (128/255))
1840 // colour standard coefficients for red, geen, blue
1842 // colour diff zero position (use standard 8-bit coding precision)
1843 double CDZ = 128; //256*0.5
1844 // range excursion (use standard 8-bit coding precision)
1845 double EXC = 255; //256-1
1847 if (colorStandard == VDP_COLOR_STANDARD_ITUR_BT_601)
1849 Kr = studioCSCKCoeffs601[0];
1850 Kg = studioCSCKCoeffs601[1];
1851 Kb = studioCSCKCoeffs601[2];
1853 else // assume VDP_COLOR_STANDARD_ITUR_BT_709
1855 Kr = studioCSCKCoeffs709[0];
1856 Kg = studioCSCKCoeffs709[1];
1857 Kb = studioCSCKCoeffs709[2];
1859 // we keep luma unscaled to retain the levels present in source so that 16-235 luma is converted to RGB 16-235
1860 studioCSCMatrix[R][Y] = 1.0;
1861 studioCSCMatrix[G][Y] = 1.0;
1862 studioCSCMatrix[B][Y] = 1.0;
1864 studioCSCMatrix[R][Cb] = 0.0;
1865 studioCSCMatrix[G][Cb] = (double)-2 * Kb * (1 - Kb) / Kg;
1866 studioCSCMatrix[B][Cb] = (double)(1 - Kb) / 0.5;
1868 studioCSCMatrix[R][Cr] = (double)(1 - Kr) / 0.5;
1869 studioCSCMatrix[G][Cr] = (double)-2 * Kr * (1 - Kr) / Kg;
1870 studioCSCMatrix[B][Cr] = 0.0;
1872 studioCSCMatrix[R][C] = (double)-1 * studioCSCMatrix[R][Cr] * CDZ/EXC;
1873 studioCSCMatrix[G][C] = (double)-1 * (studioCSCMatrix[G][Cb] + studioCSCMatrix[G][Cr]) * CDZ/EXC;
1874 studioCSCMatrix[B][C] = (double)-1 * studioCSCMatrix[B][Cb] * CDZ/EXC;
1879 void CMixer::SetColor()
1883 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)
1884 m_Procamp.brightness = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)-50) / 100;
1885 if (m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)
1886 m_Procamp.contrast = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)+50) / 100;
1888 VdpColorStandard colorStandard;
1889 switch(m_mixerInput[1].DVDPic.color_matrix)
1891 case AVCOL_SPC_BT709:
1892 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1894 case AVCOL_SPC_BT470BG:
1895 case AVCOL_SPC_SMPTE170M:
1896 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1898 case AVCOL_SPC_SMPTE240M:
1899 colorStandard = VDP_COLOR_STANDARD_SMPTE_240M;
1902 case AVCOL_SPC_UNSPECIFIED:
1905 if(m_config.surfaceWidth > 1000)
1906 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1908 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1911 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX };
1912 if (CSettings::Get().GetBool("videoscreen.limitedrange"))
1914 float studioCSC[3][4];
1915 GenerateStudioCSCMatrix(colorStandard, studioCSC);
1916 void const * pm_CSCMatix[] = { &studioCSC };
1917 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1921 vdp_st = m_config.context->GetProcs().vdp_generate_csc_matrix(&m_Procamp, colorStandard, &m_CSCMatrix);
1922 void const * pm_CSCMatix[] = { &m_CSCMatrix };
1923 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1926 CheckStatus(vdp_st, __LINE__);
1929 void CMixer::SetNoiseReduction()
1931 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1934 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION };
1935 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL };
1938 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1940 VdpBool enabled[]= {0};
1941 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1942 CheckStatus(vdp_st, __LINE__);
1945 VdpBool enabled[]={1};
1946 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1947 CheckStatus(vdp_st, __LINE__);
1948 void* nr[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction };
1949 CLog::Log(LOGNOTICE,"Setting Noise Reduction to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction);
1950 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, nr);
1951 CheckStatus(vdp_st, __LINE__);
1954 void CMixer::SetSharpness()
1956 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1959 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS };
1960 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL };
1963 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1965 VdpBool enabled[]={0};
1966 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1967 CheckStatus(vdp_st, __LINE__);
1970 VdpBool enabled[]={1};
1971 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1972 CheckStatus(vdp_st, __LINE__);
1973 void* sh[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness };
1974 CLog::Log(LOGNOTICE,"Setting Sharpness to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness);
1975 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, sh);
1976 CheckStatus(vdp_st, __LINE__);
1979 EINTERLACEMETHOD CMixer::GetDeinterlacingMethod(bool log /* = false */)
1981 EINTERLACEMETHOD method = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1982 if (method == VS_INTERLACEMETHOD_AUTO)
1985 // if (m_config.outHeight >= 720)
1986 // deint = g_advancedSettings.m_videoVDPAUdeintHD;
1988 // deint = g_advancedSettings.m_videoVDPAUdeintSD;
1992 if (m_config.vdpau->Supports(EINTERLACEMETHOD(deint)))
1994 method = EINTERLACEMETHOD(deint);
1996 CLog::Log(LOGNOTICE, "CVDPAU::GetDeinterlacingMethod: set de-interlacing to %d", deint);
2001 CLog::Log(LOGWARNING, "CVDPAU::GetDeinterlacingMethod: method for de-interlacing (advanced settings) not supported");
2008 void CMixer::SetDeinterlacing()
2012 if (m_videoMixer == VDP_INVALID_HANDLE)
2015 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
2016 EINTERLACEMETHOD method = GetDeinterlacingMethod(true);
2018 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
2019 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
2020 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE };
2022 if (mode == VS_DEINTERLACEMODE_OFF)
2024 VdpBool enabled[] = {0,0,0};
2025 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2029 if (method == VS_INTERLACEMETHOD_AUTO)
2031 VdpBool enabled[] = {1,0,0};
2032 if (g_advancedSettings.m_videoVDPAUtelecine)
2034 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2036 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2037 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF)
2039 VdpBool enabled[] = {1,0,0};
2040 if (g_advancedSettings.m_videoVDPAUtelecine)
2042 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2044 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2045 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF)
2047 VdpBool enabled[] = {1,1,0};
2048 if (g_advancedSettings.m_videoVDPAUtelecine)
2050 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2054 VdpBool enabled[]={0,0,0};
2055 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2058 CheckStatus(vdp_st, __LINE__);
2060 SetDeintSkipChroma();
2062 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2065 void CMixer::SetDeintSkipChroma()
2067 VdpVideoMixerAttribute attribute[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
2071 if (g_advancedSettings.m_videoVDPAUdeintSkipChromaHD && m_config.outHeight >= 720)
2076 void const *values[]={&val};
2077 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attribute), attribute, values);
2079 CheckStatus(vdp_st, __LINE__);
2082 void CMixer::SetHWUpscaling()
2084 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
2087 VdpBool enabled[]={1};
2088 switch (m_config.upscale)
2091 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2093 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2094 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2098 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2100 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2101 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2105 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2107 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2108 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2112 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2114 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2115 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2119 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2121 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2122 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2126 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2128 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2129 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2133 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2135 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2136 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2140 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2142 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2143 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2147 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2149 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2150 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2157 CheckStatus(vdp_st, __LINE__);
2161 void CMixer::DisableHQScaling()
2165 if (m_videoMixer == VDP_INVALID_HANDLE)
2168 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2170 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2171 VdpBool enabled[]={0};
2172 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2173 CheckStatus(vdp_st, __LINE__);
2176 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2178 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2179 VdpBool enabled[]={0};
2180 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2181 CheckStatus(vdp_st, __LINE__);
2184 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2186 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2187 VdpBool enabled[]={0};
2188 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2189 CheckStatus(vdp_st, __LINE__);
2192 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2194 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2195 VdpBool enabled[]={0};
2196 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2197 CheckStatus(vdp_st, __LINE__);
2200 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2202 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2203 VdpBool enabled[]={0};
2204 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2205 CheckStatus(vdp_st, __LINE__);
2208 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2210 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2211 VdpBool enabled[]={0};
2212 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2213 CheckStatus(vdp_st, __LINE__);
2216 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2218 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2219 VdpBool enabled[]={0};
2220 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2221 CheckStatus(vdp_st, __LINE__);
2224 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2226 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2227 VdpBool enabled[]={0};
2228 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2229 CheckStatus(vdp_st, __LINE__);
2232 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2234 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2235 VdpBool enabled[]={0};
2236 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2237 CheckStatus(vdp_st, __LINE__);
2245 m_NoiseReduction = 0.0;
2250 m_SeenInterlaceFlag = false;
2255 m_config.upscale = g_advancedSettings.m_videoVDPAUScaling;
2256 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2261 void CMixer::Uninit()
2264 while (!m_outputSurfaces.empty())
2266 m_outputSurfaces.pop();
2268 m_config.context->GetProcs().vdp_video_mixer_destroy(m_videoMixer);
2271 void CMixer::Flush()
2273 while (!m_mixerInput.empty())
2275 CVdpauDecodedPicture pic = m_mixerInput.back();
2276 m_mixerInput.pop_back();
2277 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2279 while (!m_decodedPics.empty())
2281 CVdpauDecodedPicture pic = m_decodedPics.front();
2282 m_decodedPics.pop();
2283 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2286 while (m_dataPort.ReceiveOutMessage(&msg))
2288 if (msg->signal == CMixerDataProtocol::FRAME)
2290 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2291 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2293 else if (msg->signal == CMixerDataProtocol::BUFFER)
2295 VdpOutputSurface *surf;
2296 surf = (VdpOutputSurface*)msg->data;
2297 m_outputSurfaces.push(*surf);
2303 void CMixer::InitCycle()
2308 m_config.stats->GetParams(latency, flags);
2310 if (0) //flags & DVP_FLAG_NO_POSTPROC)
2311 SetPostProcFeatures(false);
2313 SetPostProcFeatures(true);
2315 m_config.stats->SetCanSkipDeint(false);
2317 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
2318 EINTERLACEMETHOD method = GetDeinterlacingMethod();
2319 bool interlaced = m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_INTERLACED;
2320 m_SeenInterlaceFlag |= interlaced;
2323 if (//!(flags & DVP_FLAG_NO_POSTPROC) &&
2324 (mode == VS_DEINTERLACEMODE_FORCE ||
2325 (mode == VS_DEINTERLACEMODE_AUTO && interlaced)))
2327 if((method == VS_INTERLACEMETHOD_AUTO && interlaced)
2328 || method == VS_INTERLACEMETHOD_VDPAU_BOB
2329 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2330 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2331 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2332 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2333 || method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE )
2335 if(method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2336 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2337 || !g_graphicsContext.IsFullScreenVideo())
2342 m_config.stats->SetCanSkipDeint(true);
2346 if (0) //m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_DROPDEINT)
2351 if(m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_TOP_FIELD_FIRST)
2352 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2354 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2356 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2357 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2358 DVP_FLAG_REPEAT_TOP_FIELD |
2359 DVP_FLAG_INTERLACED);
2360 m_config.useInteropYuv = false;
2362 else if (method == VS_INTERLACEMETHOD_RENDER_BOB)
2365 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2366 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2367 m_config.useInteropYuv = true;
2371 CLog::Log(LOGERROR, "CMixer::%s - interlace method: %d not supported, setting to AUTO", __FUNCTION__, method);
2373 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2374 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2375 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2376 DVP_FLAG_REPEAT_TOP_FIELD |
2377 DVP_FLAG_INTERLACED);
2379 CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod = VS_INTERLACEMETHOD_AUTO;
2385 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2387 if (m_config.useInteropYuv)
2388 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2391 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2392 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2393 DVP_FLAG_REPEAT_TOP_FIELD |
2394 DVP_FLAG_INTERLACED);
2399 if (m_mixerInput[1].DVDPic.format == RENDER_FMT_VDPAU)
2401 m_processPicture.outputSurface = m_outputSurfaces.front();
2402 m_mixerInput[1].DVDPic.iWidth = m_config.outWidth;
2403 m_mixerInput[1].DVDPic.iHeight = m_config.outHeight;
2404 if (m_SeenInterlaceFlag)
2406 double ratio = (double)m_mixerInput[1].DVDPic.iDisplayHeight / m_mixerInput[1].DVDPic.iHeight;
2407 m_mixerInput[1].DVDPic.iHeight -= 6;
2408 m_mixerInput[1].DVDPic.iDisplayHeight = lrint(ratio*m_mixerInput[1].DVDPic.iHeight);
2413 m_mixerInput[1].DVDPic.iWidth = m_config.vidWidth;
2414 m_mixerInput[1].DVDPic.iHeight = m_config.vidHeight;
2417 m_processPicture.DVDPic = m_mixerInput[1].DVDPic;
2418 m_processPicture.videoSurface = m_mixerInput[1].videoSurface;
2421 void CMixer::FiniCycle()
2423 // Keep video surfaces for one 2 cycles longer than used
2424 // by mixer. This avoids blocking in decoder.
2425 // NVidia recommends num_ref + 5
2426 while (m_mixerInput.size() > 5)
2428 CVdpauDecodedPicture &tmp = m_mixerInput.back();
2429 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
2431 m_config.videoSurfaces->ClearRender(tmp.videoSurface);
2433 m_mixerInput.pop_back();
2437 void CMixer::ProcessPicture()
2439 if (m_processPicture.DVDPic.format == RENDER_FMT_VDPAU_420)
2444 if (m_mixerstep == 1)
2446 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD)
2447 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2449 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2452 VdpVideoSurface past_surfaces[4] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2453 VdpVideoSurface futu_surfaces[2] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2454 uint32_t pastCount = 4;
2455 uint32_t futuCount = 2;
2457 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
2459 // use only 2 past 1 future for progressive/weave
2460 // (only used for postproc anyway eg noise reduction)
2461 if (m_mixerInput.size() > 3)
2462 past_surfaces[1] = m_mixerInput[3].videoSurface;
2463 if (m_mixerInput.size() > 2)
2464 past_surfaces[0] = m_mixerInput[2].videoSurface;
2465 futu_surfaces[0] = m_mixerInput[0].videoSurface;
2471 if(m_mixerstep == 0)
2473 if (m_mixerInput.size() > 3)
2475 past_surfaces[3] = m_mixerInput[3].videoSurface;
2476 past_surfaces[2] = m_mixerInput[3].videoSurface;
2478 if (m_mixerInput.size() > 2)
2480 past_surfaces[1] = m_mixerInput[2].videoSurface;
2481 past_surfaces[0] = m_mixerInput[2].videoSurface;
2483 futu_surfaces[0] = m_mixerInput[1].videoSurface;
2484 futu_surfaces[1] = m_mixerInput[0].videoSurface;
2488 if (m_mixerInput.size() > 3)
2490 past_surfaces[3] = m_mixerInput[3].videoSurface;
2492 if (m_mixerInput.size() > 2)
2494 past_surfaces[2] = m_mixerInput[2].videoSurface;
2495 past_surfaces[1] = m_mixerInput[2].videoSurface;
2497 past_surfaces[0] = m_mixerInput[1].videoSurface;
2498 futu_surfaces[0] = m_mixerInput[0].videoSurface;
2499 futu_surfaces[1] = m_mixerInput[0].videoSurface;
2501 if (m_mixerInput[0].DVDPic.pts != DVD_NOPTS_VALUE &&
2502 m_mixerInput[1].DVDPic.pts != DVD_NOPTS_VALUE)
2504 m_processPicture.DVDPic.pts = m_mixerInput[1].DVDPic.pts +
2505 (m_mixerInput[0].DVDPic.pts -
2506 m_mixerInput[1].DVDPic.pts) / 2;
2509 m_processPicture.DVDPic.pts = DVD_NOPTS_VALUE;
2510 m_processPicture.DVDPic.dts = DVD_NOPTS_VALUE;
2512 m_processPicture.DVDPic.iRepeatPicture = 0.0;
2518 sourceRect.x1 = m_config.vidWidth;
2519 sourceRect.y1 = m_config.vidHeight;
2524 destRect.x1 = m_config.outWidth;
2525 destRect.y1 = m_config.outHeight;
2527 // start vdpau video mixer
2528 vdp_st = m_config.context->GetProcs().vdp_video_mixer_render(m_videoMixer,
2534 m_mixerInput[1].videoSurface,
2538 m_processPicture.outputSurface,
2543 CheckStatus(vdp_st, __LINE__);
2547 bool CMixer::CheckStatus(VdpStatus vdp_st, int line)
2549 if (vdp_st != VDP_STATUS_OK)
2551 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);
2558 //-----------------------------------------------------------------------------
2560 //-----------------------------------------------------------------------------
2562 VdpauBufferPool::VdpauBufferPool()
2564 CVdpauRenderPicture *pic;
2565 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2567 pic = new CVdpauRenderPicture(renderPicSec);
2568 allRenderPics.push_back(pic);
2572 VdpauBufferPool::~VdpauBufferPool()
2574 CVdpauRenderPicture *pic;
2575 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2577 pic = allRenderPics[i];
2580 allRenderPics.clear();
2583 //-----------------------------------------------------------------------------
2585 //-----------------------------------------------------------------------------
2586 COutput::COutput(CEvent *inMsgEvent) :
2587 CThread("Vdpau Output"),
2588 m_controlPort("OutputControlPort", inMsgEvent, &m_outMsgEvent),
2589 m_dataPort("OutputDataPort", inMsgEvent, &m_outMsgEvent),
2590 m_mixer(&m_outMsgEvent)
2592 m_inMsgEvent = inMsgEvent;
2594 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); ++i)
2596 m_bufferPool.freeRenderPics.push_back(i);
2600 void COutput::Start()
2609 m_bufferPool.freeRenderPics.clear();
2610 m_bufferPool.usedRenderPics.clear();
2613 void COutput::Dispose()
2615 CSingleLock lock(g_graphicsContext);
2617 m_outMsgEvent.Set();
2619 m_controlPort.Purge();
2623 void COutput::OnStartup()
2625 CLog::Log(LOGNOTICE, "COutput::OnStartup: Output Thread created");
2628 void COutput::OnExit()
2630 CLog::Log(LOGNOTICE, "COutput::OnExit: Output Thread terminated");
2637 O_TOP_UNCONFIGURED, // 2
2638 O_TOP_CONFIGURED, // 3
2639 O_TOP_CONFIGURED_IDLE, // 4
2640 O_TOP_CONFIGURED_WORK, // 5
2643 int VDPAU_OUTPUT_parentStates[] = {
2646 0, //TOP_UNCONFIGURED
2648 3, //TOP_CONFIGURED_IDLE
2649 3, //TOP_CONFIGURED_WORK
2652 void COutput::StateMachine(int signal, Protocol *port, Message *msg)
2654 for (int state = m_state; ; state = VDPAU_OUTPUT_parentStates[state])
2659 if (port == &m_controlPort)
2663 case COutputControlProtocol::FLUSH:
2664 msg->Reply(COutputControlProtocol::ACC);
2666 case COutputControlProtocol::PRECLEANUP:
2667 msg->Reply(COutputControlProtocol::ACC);
2673 else if (port == &m_dataPort)
2677 case COutputDataProtocol::RETURNPIC:
2678 CVdpauRenderPicture *pic;
2679 pic = *((CVdpauRenderPicture**)msg->data);
2680 QueueReturnPicture(pic);
2687 std::string portName = port == NULL ? "timer" : port->portName;
2688 CLog::Log(LOGWARNING, "COutput::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
2695 case O_TOP_UNCONFIGURED:
2696 if (port == &m_controlPort)
2700 case COutputControlProtocol::INIT:
2702 data = (CVdpauConfig*)msg->data;
2709 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::INIT,
2710 &reply, 1000, &m_config, sizeof(m_config)))
2712 if (reply->signal != CMixerControlProtocol::ACC)
2717 // set initial number of
2718 m_bufferPool.numOutputSurfaces = 4;
2722 m_state = O_TOP_CONFIGURED_IDLE;
2723 msg->Reply(COutputControlProtocol::ACC, &m_config, sizeof(m_config));
2727 m_state = O_TOP_ERROR;
2728 msg->Reply(COutputControlProtocol::ERROR);
2737 case O_TOP_CONFIGURED:
2738 if (port == &m_controlPort)
2742 case COutputControlProtocol::FLUSH:
2744 msg->Reply(COutputControlProtocol::ACC);
2746 case COutputControlProtocol::PRECLEANUP:
2749 msg->Reply(COutputControlProtocol::ACC);
2755 else if (port == &m_dataPort)
2759 case COutputDataProtocol::NEWFRAME:
2760 CVdpauDecodedPicture *frame;
2761 frame = (CVdpauDecodedPicture*)msg->data;
2764 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::FRAME,
2765 frame,sizeof(CVdpauDecodedPicture));
2768 case COutputDataProtocol::RETURNPIC:
2769 CVdpauRenderPicture *pic;
2770 pic = *((CVdpauRenderPicture**)msg->data);
2771 QueueReturnPicture(pic);
2772 m_controlPort.SendInMessage(COutputControlProtocol::STATS);
2773 m_state = O_TOP_CONFIGURED_WORK;
2780 else if (port == &m_mixer.m_dataPort)
2784 case CMixerDataProtocol::PICTURE:
2785 CVdpauProcessedPicture *pic;
2786 pic = (CVdpauProcessedPicture*)msg->data;
2787 m_bufferPool.processedPics.push(*pic);
2788 m_state = O_TOP_CONFIGURED_WORK;
2797 case O_TOP_CONFIGURED_IDLE:
2798 if (port == NULL) // timeout
2802 case COutputControlProtocol::TIMEOUT:
2803 if (ProcessSyncPicture())
2809 m_state = O_TOP_CONFIGURED_WORK;
2819 case O_TOP_CONFIGURED_WORK:
2820 if (port == NULL) // timeout
2824 case COutputControlProtocol::TIMEOUT:
2827 CVdpauRenderPicture *pic;
2828 pic = ProcessMixerPicture();
2831 m_config.stats->DecProcessed();
2832 m_config.stats->IncRender();
2833 m_dataPort.SendInMessage(COutputDataProtocol::PICTURE, &pic, sizeof(pic));
2839 m_state = O_TOP_CONFIGURED_IDLE;
2849 default: // we are in no state, should not happen
2850 CLog::Log(LOGERROR, "COutput::%s - no valid state: %d", __FUNCTION__, m_state);
2856 void COutput::Process()
2858 Message *msg = NULL;
2859 Protocol *port = NULL;
2862 m_state = O_TOP_UNCONFIGURED;
2863 m_extTimeout = 1000;
2864 m_bStateMachineSelfTrigger = false;
2870 if (m_bStateMachineSelfTrigger)
2872 m_bStateMachineSelfTrigger = false;
2873 // self trigger state machine
2874 StateMachine(msg->signal, port, msg);
2875 if (!m_bStateMachineSelfTrigger)
2882 // check control port
2883 else if (m_controlPort.ReceiveOutMessage(&msg))
2886 port = &m_controlPort;
2889 else if (m_dataPort.ReceiveOutMessage(&msg))
2894 // check mixer data port
2895 else if (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2898 port = &m_mixer.m_dataPort;
2902 StateMachine(msg->signal, port, msg);
2903 if (!m_bStateMachineSelfTrigger)
2912 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
2919 msg = m_controlPort.GetMessage();
2920 msg->signal = COutputControlProtocol::TIMEOUT;
2922 // signal timeout to state machine
2923 StateMachine(msg->signal, port, msg);
2924 if (!m_bStateMachineSelfTrigger)
2935 bool COutput::Init()
2937 if (!CreateGlxContext())
2949 bool COutput::Uninit()
2953 ReleaseBufferPool();
2954 DestroyGlxContext();
2958 void COutput::Flush()
2960 if (m_mixer.IsActive())
2963 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::FLUSH,
2970 CLog::Log(LOGERROR, "Coutput::%s - failed to flush mixer", __FUNCTION__);
2974 while (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2976 if (msg->signal == CMixerDataProtocol::PICTURE)
2978 CVdpauProcessedPicture pic = *(CVdpauProcessedPicture*)msg->data;
2979 m_bufferPool.processedPics.push(pic);
2984 while (m_dataPort.ReceiveOutMessage(&msg))
2986 if (msg->signal == COutputDataProtocol::NEWFRAME)
2988 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2989 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2991 else if (msg->signal == COutputDataProtocol::RETURNPIC)
2993 CVdpauRenderPicture *pic;
2994 pic = *((CVdpauRenderPicture**)msg->data);
2995 QueueReturnPicture(pic);
3000 while (m_dataPort.ReceiveInMessage(&msg))
3002 if (msg->signal == COutputDataProtocol::PICTURE)
3004 CVdpauRenderPicture *pic;
3005 pic = *((CVdpauRenderPicture**)msg->data);
3006 QueueReturnPicture(pic);
3010 // reset used render flag which was cleared on mixer flush
3011 std::deque<int>::iterator it;
3012 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3014 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[*it];
3015 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3017 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it2;
3018 it2 = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3019 if (it2 == m_bufferPool.glVideoSurfaceMap.end())
3021 CLog::Log(LOGDEBUG, "COutput::Flush - gl surface not found");
3024 m_config.videoSurfaces->MarkRender(it2->second.sourceVuv);
3028 // clear processed pics
3029 while(!m_bufferPool.processedPics.empty())
3031 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3032 if (procPic.DVDPic.format == RENDER_FMT_VDPAU)
3034 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &procPic.outputSurface, sizeof(procPic.outputSurface));
3036 else if (procPic.DVDPic.format == RENDER_FMT_VDPAU_420)
3038 m_config.videoSurfaces->ClearRender(procPic.videoSurface);
3040 m_bufferPool.processedPics.pop();
3044 bool COutput::HasWork()
3046 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3051 CVdpauRenderPicture* COutput::ProcessMixerPicture()
3053 CVdpauRenderPicture *retPic = NULL;
3055 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3057 int idx = m_bufferPool.freeRenderPics.front();
3058 retPic = m_bufferPool.allRenderPics[idx];
3059 m_bufferPool.freeRenderPics.pop_front();
3060 m_bufferPool.usedRenderPics.push_back(idx);
3061 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3062 m_bufferPool.processedPics.pop();
3064 retPic->DVDPic = procPic.DVDPic;
3065 retPic->valid = true;
3066 if (retPic->DVDPic.format == RENDER_FMT_VDPAU)
3068 m_config.useInteropYuv = false;
3069 m_bufferPool.numOutputSurfaces = NUM_RENDER_PICS;
3071 GLMapSurface(false, procPic.outputSurface);
3072 retPic->sourceIdx = procPic.outputSurface;
3073 retPic->texture[0] = m_bufferPool.glOutputSurfaceMap[procPic.outputSurface].texture[0];
3074 retPic->texWidth = m_config.outWidth;
3075 retPic->texHeight = m_config.outHeight;
3076 retPic->crop.x1 = 0;
3077 retPic->crop.y1 = (m_config.outHeight - retPic->DVDPic.iHeight) / 2;
3078 retPic->crop.x2 = m_config.outWidth;
3079 retPic->crop.y2 = m_config.outHeight - retPic->crop.y1;
3083 m_config.useInteropYuv = true;
3084 GLMapSurface(true, procPic.videoSurface);
3085 retPic->sourceIdx = procPic.videoSurface;
3086 for (unsigned int i=0; i<4; ++i)
3087 retPic->texture[i] = m_bufferPool.glVideoSurfaceMap[procPic.videoSurface].texture[i];
3088 retPic->texWidth = m_config.surfaceWidth;
3089 retPic->texHeight = m_config.surfaceHeight;
3090 retPic->crop.x1 = 0;
3091 retPic->crop.y1 = 0;
3092 retPic->crop.x2 = m_config.surfaceWidth - m_config.vidWidth;
3093 retPic->crop.y2 = m_config.surfaceHeight - m_config.vidHeight;
3099 void COutput::QueueReturnPicture(CVdpauRenderPicture *pic)
3101 std::deque<int>::iterator it;
3102 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3104 if (m_bufferPool.allRenderPics[*it] == pic)
3110 if (it == m_bufferPool.usedRenderPics.end())
3112 CLog::Log(LOGWARNING, "COutput::QueueReturnPicture - pic not found");
3116 // check if already queued
3117 std::deque<int>::iterator it2 = find(m_bufferPool.syncRenderPics.begin(),
3118 m_bufferPool.syncRenderPics.end(),
3120 if (it2 == m_bufferPool.syncRenderPics.end())
3122 m_bufferPool.syncRenderPics.push_back(*it);
3125 ProcessSyncPicture();
3128 bool COutput::ProcessSyncPicture()
3130 CVdpauRenderPicture *pic;
3133 std::deque<int>::iterator it;
3134 for (it = m_bufferPool.syncRenderPics.begin(); it != m_bufferPool.syncRenderPics.end(); )
3136 pic = m_bufferPool.allRenderPics[*it];
3141 if (glIsSync(pic->fence))
3145 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3146 if(state == GL_SIGNALED)
3148 glDeleteSync(pic->fence);
3161 m_bufferPool.freeRenderPics.push_back(*it);
3163 std::deque<int>::iterator it2 = find(m_bufferPool.usedRenderPics.begin(),
3164 m_bufferPool.usedRenderPics.end(),
3166 if (it2 == m_bufferPool.usedRenderPics.end())
3168 CLog::Log(LOGERROR, "COutput::ProcessSyncPicture - pic not found in queue");
3172 m_bufferPool.usedRenderPics.erase(it2);
3174 it = m_bufferPool.syncRenderPics.erase(it);
3178 ProcessReturnPicture(pic);
3182 CLog::Log(LOGDEBUG, "COutput::%s - return of invalid render pic", __FUNCTION__);
3188 void COutput::ProcessReturnPicture(CVdpauRenderPicture *pic)
3190 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3192 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3193 it = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3194 if (it == m_bufferPool.glVideoSurfaceMap.end())
3196 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3199 #ifdef GL_NV_vdpau_interop
3200 glVDPAUUnmapSurfacesNV(1, &(it->second.glVdpauSurface));
3202 VdpVideoSurface surf = it->second.sourceVuv;
3203 m_config.videoSurfaces->ClearRender(surf);
3205 else if (pic->DVDPic.format == RENDER_FMT_VDPAU)
3207 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3208 it = m_bufferPool.glOutputSurfaceMap.find(pic->sourceIdx);
3209 if (it == m_bufferPool.glOutputSurfaceMap.end())
3211 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3214 #ifdef GL_NV_vdpau_interop
3215 glVDPAUUnmapSurfacesNV(1, &(it->second.glVdpauSurface));
3217 VdpOutputSurface outSurf = it->second.sourceRgb;
3218 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &outSurf, sizeof(outSurf));
3222 bool COutput::EnsureBufferPool()
3226 // Creation of outputSurfaces
3227 VdpOutputSurface outputSurface;
3228 for (int i = m_bufferPool.outputSurfaces.size(); i < m_bufferPool.numOutputSurfaces; i++)
3230 vdp_st = m_config.context->GetProcs().vdp_output_surface_create(m_config.context->GetDevice(),
3231 VDP_RGBA_FORMAT_B8G8R8A8,
3235 if (CheckStatus(vdp_st, __LINE__))
3237 m_bufferPool.outputSurfaces.push_back(outputSurface);
3239 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3241 sizeof(VdpOutputSurface));
3242 CLog::Log(LOGNOTICE, "VDPAU::COutput::InitBufferPool - Output Surface created");
3247 void COutput::ReleaseBufferPool()
3251 CSingleLock lock(m_bufferPool.renderPicSec);
3253 // release all output surfaces
3254 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3256 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3258 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3259 CheckStatus(vdp_st, __LINE__);
3261 m_bufferPool.outputSurfaces.clear();
3263 // wait for all fences
3264 XbmcThreads::EndTime timeout(1000);
3265 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3267 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[i];
3271 while (glIsSync(pic->fence))
3275 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3276 if(state == GL_SIGNALED || timeout.IsTimePast())
3278 glDeleteSync(pic->fence);
3289 if (timeout.IsTimePast())
3291 CLog::Log(LOGERROR, "COutput::%s - timeout waiting for fence", __FUNCTION__);
3293 ProcessSyncPicture();
3295 // invalidate all used render pictures
3296 for (unsigned int i = 0; i < m_bufferPool.usedRenderPics.size(); ++i)
3298 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[m_bufferPool.usedRenderPics[i]];
3303 void COutput::PreCleanup()
3309 ProcessSyncPicture();
3311 CSingleLock lock(m_bufferPool.renderPicSec);
3312 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3314 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3317 // check if output surface is in use
3319 std::deque<int>::iterator it;
3320 CVdpauRenderPicture *pic;
3321 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3323 pic = m_bufferPool.allRenderPics[*it];
3324 if ((pic->sourceIdx == m_bufferPool.outputSurfaces[i]) && pic->valid)
3333 #ifdef GL_NV_vdpau_interop
3335 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it_map;
3336 it_map = m_bufferPool.glOutputSurfaceMap.find(m_bufferPool.outputSurfaces[i]);
3337 if (it_map == m_bufferPool.glOutputSurfaceMap.end())
3339 CLog::Log(LOGERROR, "%s - could not find gl surface", __FUNCTION__);
3342 glVDPAUUnregisterSurfaceNV(it_map->second.glVdpauSurface);
3343 glDeleteTextures(1, it_map->second.texture);
3344 m_bufferPool.glOutputSurfaceMap.erase(it_map);
3347 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3348 CheckStatus(vdp_st, __LINE__);
3350 m_bufferPool.outputSurfaces[i] = VDP_INVALID_HANDLE;
3352 CLog::Log(LOGDEBUG, "VDPAU::PreCleanup - released output surface");
3357 void COutput::InitMixer()
3359 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3361 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3362 &m_bufferPool.outputSurfaces[i],
3363 sizeof(VdpOutputSurface));
3367 bool COutput::GLInit()
3369 #ifdef GL_NV_vdpau_interop
3370 glVDPAUInitNV = NULL;
3371 glVDPAUFiniNV = NULL;
3372 glVDPAURegisterOutputSurfaceNV = NULL;
3373 glVDPAURegisterVideoSurfaceNV = NULL;
3374 glVDPAUIsSurfaceNV = NULL;
3375 glVDPAUUnregisterSurfaceNV = NULL;
3376 glVDPAUSurfaceAccessNV = NULL;
3377 glVDPAUMapSurfacesNV = NULL;
3378 glVDPAUUnmapSurfacesNV = NULL;
3379 glVDPAUGetSurfaceivNV = NULL;
3382 #ifdef GL_NV_vdpau_interop
3384 glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUInitNV");
3386 glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((GLubyte *) "glVDPAUFiniNV");
3387 if (!glVDPAURegisterOutputSurfaceNV)
3388 glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterOutputSurfaceNV");
3389 if (!glVDPAURegisterVideoSurfaceNV)
3390 glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterVideoSurfaceNV");
3391 if (!glVDPAUIsSurfaceNV)
3392 glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUIsSurfaceNV");
3393 if (!glVDPAUUnregisterSurfaceNV)
3394 glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnregisterSurfaceNV");
3395 if (!glVDPAUSurfaceAccessNV)
3396 glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUSurfaceAccessNV");
3397 if (!glVDPAUMapSurfacesNV)
3398 glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUMapSurfacesNV");
3399 if (!glVDPAUUnmapSurfacesNV)
3400 glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnmapSurfacesNV");
3401 if (!glVDPAUGetSurfaceivNV)
3402 glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUGetSurfaceivNV");
3404 while (glGetError() != GL_NO_ERROR);
3405 glVDPAUInitNV(reinterpret_cast<void*>(m_config.context->GetDevice()), reinterpret_cast<void*>(m_config.context->GetProcs().vdp_get_proc_address));
3406 if (glGetError() != GL_NO_ERROR)
3408 CLog::Log(LOGERROR, "VDPAU::COutput - GLInitInterop glVDPAUInitNV failed");
3412 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop initialized");
3416 bool hasfence = glewIsSupported("GL_ARB_sync");
3417 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3419 m_bufferPool.allRenderPics[i]->usefence = hasfence;
3426 void COutput::GLMapSurface(bool yuv, uint32_t source)
3428 #ifdef GL_NV_vdpau_interop
3432 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3433 it = m_bufferPool.glVideoSurfaceMap.find(source);
3434 if (it == m_bufferPool.glVideoSurfaceMap.end())
3436 VdpauBufferPool::GLVideoSurface glSurface;
3437 VdpVideoSurface surf = source;
3439 if (surf == VDP_INVALID_HANDLE)
3442 glSurface.sourceVuv = surf;
3443 while (glGetError() != GL_NO_ERROR) ;
3444 glGenTextures(4, glSurface.texture);
3445 if (glGetError() != GL_NO_ERROR)
3447 CLog::Log(LOGERROR, "VDPAU::COutput error creating texture");
3450 glSurface.glVdpauSurface = glVDPAURegisterVideoSurfaceNV(reinterpret_cast<void*>(surf),
3451 GL_TEXTURE_2D, 4, glSurface.texture);
3453 if (glGetError() != GL_NO_ERROR)
3455 CLog::Log(LOGERROR, "VDPAU::COutput error register video surface");
3458 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3459 if (glGetError() != GL_NO_ERROR)
3461 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3464 m_bufferPool.glVideoSurfaceMap[surf] = glSurface;
3466 CLog::Log(LOGNOTICE, "VDPAU::COutput registered surface");
3469 while (glGetError() != GL_NO_ERROR) ;
3470 glVDPAUMapSurfacesNV(1, &m_bufferPool.glVideoSurfaceMap[source].glVdpauSurface);
3471 if (glGetError() != GL_NO_ERROR)
3473 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3482 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3483 it = m_bufferPool.glOutputSurfaceMap.find(source);
3484 if (it == m_bufferPool.glOutputSurfaceMap.end())
3486 unsigned int idx = 0;
3487 for (idx = 0; idx<m_bufferPool.outputSurfaces.size(); idx++)
3489 if (m_bufferPool.outputSurfaces[idx] == source)
3493 VdpauBufferPool::GLVideoSurface glSurface;
3494 glSurface.sourceRgb = m_bufferPool.outputSurfaces[idx];
3495 glGenTextures(1, glSurface.texture);
3496 glSurface.glVdpauSurface = glVDPAURegisterOutputSurfaceNV(reinterpret_cast<void*>(m_bufferPool.outputSurfaces[idx]),
3497 GL_TEXTURE_2D, 1, glSurface.texture);
3498 if (glGetError() != GL_NO_ERROR)
3500 CLog::Log(LOGERROR, "VDPAU::COutput error register output surface");
3503 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3504 if (glGetError() != GL_NO_ERROR)
3506 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3509 m_bufferPool.glOutputSurfaceMap[source] = glSurface;
3510 CLog::Log(LOGNOTICE, "VDPAU::COutput registered output surfaces");
3513 while (glGetError() != GL_NO_ERROR) ;
3514 glVDPAUMapSurfacesNV(1, &m_bufferPool.glOutputSurfaceMap[source].glVdpauSurface);
3515 if (glGetError() != GL_NO_ERROR)
3517 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3527 void COutput::GLUnmapSurfaces()
3529 #ifdef GL_NV_vdpau_interop
3532 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3533 for (it = m_bufferPool.glVideoSurfaceMap.begin(); it != m_bufferPool.glVideoSurfaceMap.end(); ++it)
3535 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3536 glDeleteTextures(4, it->second.texture);
3538 m_bufferPool.glVideoSurfaceMap.clear();
3541 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3542 for (it = m_bufferPool.glOutputSurfaceMap.begin(); it != m_bufferPool.glOutputSurfaceMap.end(); ++it)
3544 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3545 glDeleteTextures(1, it->second.texture);
3547 m_bufferPool.glOutputSurfaceMap.clear();
3551 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop finished");
3556 bool COutput::CheckStatus(VdpStatus vdp_st, int line)
3558 if (vdp_st != VDP_STATUS_OK)
3560 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);
3567 bool COutput::CreateGlxContext()
3569 GLXContext glContext;
3571 m_Display = g_Windowing.GetDisplay();
3572 glContext = g_Windowing.GetGlxContext();
3573 m_Window = g_Windowing.GetWindow();
3575 // Get our window attribs.
3576 XWindowAttributes wndattribs;
3577 XGetWindowAttributes(m_Display, m_Window, &wndattribs);
3580 XVisualInfo visInfo;
3581 visInfo.visualid = wndattribs.visual->visualid;
3583 XVisualInfo* visuals = XGetVisualInfo(m_Display, VisualIDMask, &visInfo, &nvisuals);
3586 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - could not find visual");
3589 visInfo = visuals[0];
3592 m_pixmap = XCreatePixmap(m_Display,
3599 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - Unable to create XPixmap");
3604 m_glPixmap = glXCreateGLXPixmap(m_Display, &visInfo, m_pixmap);
3608 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not create glPixmap");
3612 m_glContext = glXCreateContext(m_Display, &visInfo, glContext, True);
3614 if (!glXMakeCurrent(m_Display, m_glPixmap, m_glContext))
3616 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not make Pixmap current");
3620 CLog::Log(LOGNOTICE, "VDPAU::COutput::CreateGlxContext - created context");
3624 bool COutput::DestroyGlxContext()
3628 glXMakeCurrent(m_Display, None, NULL);
3629 glXDestroyContext(m_Display, m_glContext);
3634 glXDestroyPixmap(m_Display, m_glPixmap);
3638 XFreePixmap(m_Display, m_pixmap);