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::Open: required extension GL_NV_vdpau_interop not found");
516 if(avctx->coded_width == 0
517 || avctx->coded_height == 0)
519 CLog::Log(LOGWARNING,"VDPAU::Open: 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;
539 // convert FFMPEG codec ID to VDPAU profile.
540 ReadFormatOf(avctx->codec_id, profile, m_vdpauConfig.vdpChromaType);
544 VdpBool is_supported = false;
545 uint32_t max_level, max_macroblocks, max_width, max_height;
547 // query device capabilities to ensure that VDPAU can handle the requested codec
548 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_query_caps(m_vdpauConfig.context->GetDevice(),
549 profile, &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
551 // test to make sure there is a possibility the codec will work
552 if (CheckStatus(vdp_st, __LINE__))
554 CLog::Log(LOGERROR, "VDPAU::Open: error %s(%d) checking for decoder support", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
558 if (max_width < avctx->coded_width || max_height < avctx->coded_height)
560 CLog::Log(LOGWARNING,"VDPAU::Open: requested picture dimensions (%i, %i) exceed hardware capabilities ( %i, %i).",
561 avctx->coded_width, avctx->coded_height, max_width, max_height);
565 if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
566 CLog::Log(LOGWARNING,"VDPAU::Open width %i might not be supported because of hardware bug", avctx->width);
568 // attempt to create a decoder with this width/height, some sizes are not supported by hw
569 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(), profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
571 if (CheckStatus(vdp_st, __LINE__))
573 CLog::Log(LOGERROR, "VDPAU::Open: error: %s(%d) checking for decoder support", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
577 m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
578 CheckStatus(vdp_st, __LINE__);
580 // finally setup ffmpeg
581 memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
582 m_hwContext.render = CDecoder::Render;
583 m_hwContext.bitstream_buffers_allocated = 0;
584 avctx->get_buffer = CDecoder::FFGetBuffer;
585 avctx->reget_buffer = CDecoder::FFGetBuffer;
586 avctx->release_buffer = CDecoder::FFReleaseBuffer;
587 avctx->draw_horiz_band = CDecoder::FFDrawSlice;
588 avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
589 avctx->hwaccel_context = &m_hwContext;
590 avctx->thread_count = 1;
592 g_Windowing.Register(this);
599 CDecoder::~CDecoder()
604 void CDecoder::Close()
606 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
608 g_Windowing.Unregister(this);
610 CSingleLock lock(m_DecoderSection);
613 m_vdpauOutput.Dispose();
615 if (m_hwContext.bitstream_buffers_allocated)
617 m_dllAvUtil.av_freep(&m_hwContext.bitstream_buffers);
620 m_dllAvUtil.Unload();
622 if (m_vdpauConfig.context)
623 m_vdpauConfig.context->Release();
624 m_vdpauConfig.context = 0;
627 long CDecoder::Release()
629 // check if we should do some pre-cleanup here
630 // a second decoder might need resources
631 if (m_vdpauConfigured == true)
633 CSingleLock lock(m_DecoderSection);
634 CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
637 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
641 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
645 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
646 m_DisplayState = VDPAU_ERROR;
651 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
652 m_DisplayState = VDPAU_ERROR;
655 VdpVideoSurface surf;
656 while((surf = m_videoSurfaces.RemoveNext(true)) != VDP_INVALID_HANDLE)
658 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
661 return IHardwareDecoder::Release();
664 long CDecoder::ReleasePicReference()
666 return IHardwareDecoder::Release();
669 void CDecoder::SetWidthHeight(int width, int height)
671 m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
673 //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
674 //this requires the least amount of gpu memory bandwidth
675 if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
677 //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
678 if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
680 m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
681 m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
683 else //scale height to the desktop size if the aspect ratio is smaller than the desktop
685 m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
686 m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
691 m_vdpauConfig.outWidth = width;
692 m_vdpauConfig.outHeight = height;
694 CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
697 void CDecoder::OnLostDevice()
699 CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
701 int count = g_graphicsContext.exit();
703 CSingleLock lock(m_DecoderSection);
705 if (m_vdpauConfig.context)
706 m_vdpauConfig.context->Release();
707 m_vdpauConfig.context = 0;
709 m_DisplayState = VDPAU_LOST;
711 m_DisplayEvent.Reset();
713 g_graphicsContext.restore(count);
716 void CDecoder::OnResetDevice()
718 CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
720 int count = g_graphicsContext.exit();
722 CSingleLock lock(m_DecoderSection);
723 if (m_DisplayState == VDPAU_LOST)
725 m_DisplayState = VDPAU_RESET;
727 m_DisplayEvent.Set();
730 g_graphicsContext.restore(count);
733 int CDecoder::Check(AVCodecContext* avctx)
737 { CSingleLock lock(m_DecoderSection);
738 state = m_DisplayState;
741 if (state == VDPAU_LOST)
743 CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
744 if (!m_DisplayEvent.WaitMSec(4000))
746 CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
751 CSingleLock lock(m_DecoderSection);
752 state = m_DisplayState;
755 if (state == VDPAU_RESET || state == VDPAU_ERROR)
757 CSingleLock lock(m_DecoderSection);
760 if (m_vdpauConfig.context)
761 m_vdpauConfig.context->Release();
762 m_vdpauConfig.context = 0;
764 if (CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
766 m_DisplayState = VDPAU_OPEN;
767 m_vdpauConfigured = false;
770 if (state == VDPAU_RESET)
778 bool CDecoder::IsVDPAUFormat(PixelFormat format)
780 if (format == AV_PIX_FMT_VDPAU)
786 bool CDecoder::Supports(VdpVideoMixerFeature feature)
788 return m_vdpauConfig.context->Supports(feature);
791 bool CDecoder::Supports(EINTERLACEMETHOD method)
793 if(method == VS_INTERLACEMETHOD_VDPAU_BOB
794 || method == VS_INTERLACEMETHOD_AUTO)
797 if (method == VS_INTERLACEMETHOD_RENDER_BOB)
800 if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
803 for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
805 if(p->method == method)
806 return Supports(p->feature);
811 EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
813 return VS_INTERLACEMETHOD_RENDER_BOB;
816 void CDecoder::FiniVDPAUOutput()
818 if (!m_vdpauConfigured)
821 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
824 m_vdpauOutput.Dispose();
825 m_vdpauConfigured = false;
829 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
830 if (CheckStatus(vdp_st, __LINE__))
832 m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
834 CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", m_videoSurfaces.Size());
836 VdpVideoSurface surf;
837 while((surf = m_videoSurfaces.RemoveNext()) != VDP_INVALID_HANDLE)
839 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
840 if (CheckStatus(vdp_st, __LINE__))
843 m_videoSurfaces.Reset();
846 void CDecoder::ReadFormatOf( AVCodecID codec
847 , VdpDecoderProfile &vdp_decoder_profile
848 , VdpChromaType &vdp_chroma_type)
852 case AV_CODEC_ID_MPEG1VIDEO:
853 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
854 vdp_chroma_type = VDP_CHROMA_TYPE_420;
856 case AV_CODEC_ID_MPEG2VIDEO:
857 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
858 vdp_chroma_type = VDP_CHROMA_TYPE_420;
860 case AV_CODEC_ID_H264:
861 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
862 vdp_chroma_type = VDP_CHROMA_TYPE_420;
864 case AV_CODEC_ID_WMV3:
865 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
866 vdp_chroma_type = VDP_CHROMA_TYPE_420;
868 case AV_CODEC_ID_VC1:
869 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
870 vdp_chroma_type = VDP_CHROMA_TYPE_420;
872 case AV_CODEC_ID_MPEG4:
873 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
874 vdp_chroma_type = VDP_CHROMA_TYPE_420;
877 vdp_decoder_profile = 0;
883 bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
888 VdpDecoderProfile vdp_decoder_profile;
890 m_vdpauConfig.vidWidth = avctx->width;
891 m_vdpauConfig.vidHeight = avctx->height;
892 m_vdpauConfig.surfaceWidth = avctx->coded_width;
893 m_vdpauConfig.surfaceHeight = avctx->coded_height;
895 SetWidthHeight(avctx->width,avctx->height);
897 CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
898 CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
900 ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
902 if(avctx->codec_id == AV_CODEC_ID_H264)
904 m_vdpauConfig.maxReferences = ref_frames;
905 if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
906 if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
909 m_vdpauConfig.maxReferences = 2;
911 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(),
913 m_vdpauConfig.surfaceWidth,
914 m_vdpauConfig.surfaceHeight,
915 m_vdpauConfig.maxReferences,
916 &m_vdpauConfig.vdpDecoder);
917 if (CheckStatus(vdp_st, __LINE__))
921 CSingleLock lock(g_graphicsContext);
922 m_vdpauConfig.stats = &m_bufferStats;
923 m_vdpauConfig.vdpau = this;
924 m_bufferStats.Reset();
925 m_vdpauOutput.Start();
927 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::INIT,
931 sizeof(m_vdpauConfig)))
933 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
937 CLog::Log(LOGERROR, "VDPAU::%s - vdpau output returned error", __FUNCTION__);
938 m_vdpauOutput.Dispose();
944 CLog::Log(LOGERROR, "VDPAU::%s - failed to init output", __FUNCTION__);
945 m_vdpauOutput.Dispose();
949 m_inMsgEvent.Reset();
950 m_vdpauConfigured = true;
955 int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic)
957 //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
958 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
959 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
961 // while we are waiting to recover we can't do anything
962 CSingleLock lock(vdp->m_DecoderSection);
964 if(vdp->m_DisplayState != VDPAU_OPEN)
966 CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
970 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
971 surf = vdp->m_videoSurfaces.GetFree(surf != 0 ? surf : VDP_INVALID_HANDLE);
973 VdpStatus vdp_st = VDP_STATUS_ERROR;
974 if (surf == VDP_INVALID_HANDLE)
976 // create a new surface
977 VdpDecoderProfile profile;
978 ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
980 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_video_surface_create(vdp->m_vdpauConfig.context->GetDevice(),
981 vdp->m_vdpauConfig.vdpChromaType,
985 vdp->CheckStatus(vdp_st, __LINE__);
986 if (vdp_st != VDP_STATUS_OK)
988 CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
991 vdp->m_videoSurfaces.AddSurface(surf);
994 pic->data[1] = pic->data[2] = NULL;
995 pic->data[0] = (uint8_t*)(uintptr_t)surf;
996 pic->data[3] = (uint8_t*)(uintptr_t)surf;
998 pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
1000 pic->type= FF_BUFFER_TYPE_USER;
1002 pic->reordered_opaque= avctx->reordered_opaque;
1006 void CDecoder::FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic)
1008 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
1009 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
1011 VdpVideoSurface surf;
1014 CSingleLock lock(vdp->m_DecoderSection);
1016 surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
1018 vdp->m_videoSurfaces.ClearReference(surf);
1024 VdpStatus CDecoder::Render( VdpDecoder decoder, VdpVideoSurface target,
1025 VdpPictureInfo const *picture_info,
1026 uint32_t bitstream_buffer_count,
1027 VdpBitstreamBuffer const * bitstream_buffers)
1029 return VDP_STATUS_OK;
1032 void CDecoder::FFDrawSlice(struct AVCodecContext *s,
1033 const AVFrame *src, int offset[4],
1034 int y, int type, int height)
1036 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
1037 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
1039 // while we are waiting to recover we can't do anything
1040 CSingleLock lock(vdp->m_DecoderSection);
1042 if(vdp->m_DisplayState != VDPAU_OPEN)
1045 if(src->linesize[0] || src->linesize[1] || src->linesize[2]
1046 || offset[0] || offset[1] || offset[2])
1048 CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
1053 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
1055 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1056 if (!vdp->m_videoSurfaces.IsValid(surf))
1058 CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
1062 uint32_t max_refs = 0;
1063 if(s->codec_id == AV_CODEC_ID_H264)
1064 max_refs = vdp->m_hwContext.info.h264.num_ref_frames;
1066 if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
1067 || vdp->m_vdpauConfigured == false
1068 || vdp->m_vdpauConfig.maxReferences < max_refs)
1070 if(!vdp->ConfigVDPAU(s, max_refs))
1074 uint64_t startTime = CurrentHostCounter();
1075 uint16_t decoded, processed, rend;
1076 vdp->m_bufferStats.Get(decoded, processed, rend);
1077 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
1079 (VdpPictureInfo const *)&(vdp->m_hwContext.info),
1080 vdp->m_hwContext.bitstream_buffers_used,
1081 vdp->m_hwContext.bitstream_buffers);
1082 if (vdp->CheckStatus(vdp_st, __LINE__))
1083 vdp->m_DecoderError = true;
1085 vdp->m_DecoderError = false;
1087 uint64_t diff = CurrentHostCounter() - startTime;
1088 if (diff*1000/CurrentHostFrequency() > 30)
1089 CLog::Log(LOGDEBUG, "CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
1093 int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
1095 int result = Check(avctx);
1099 CSingleLock lock(m_DecoderSection);
1101 if (m_DecoderError && pFrame)
1104 if (!m_vdpauConfigured)
1108 { // we have a new frame from decoder
1110 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pFrame->data[3];
1111 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1112 if (!m_videoSurfaces.IsValid(surf))
1114 CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
1117 m_videoSurfaces.MarkRender(surf);
1119 // send frame to output for processing
1120 CVdpauDecodedPicture pic;
1121 memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
1122 ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
1123 pic.videoSurface = surf;
1124 pic.DVDPic.color_matrix = avctx->colorspace;
1125 m_bufferStats.IncDecoded();
1126 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
1129 // m_codecControl = pic.DVDPic.iFlags & (DVP_FLAG_DRAIN | DVP_FLAG_NO_POSTPROC);
1133 uint16_t decoded, processed, render;
1135 while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1137 if (msg->signal == COutputControlProtocol::ERROR)
1139 m_DisplayState = VDPAU_ERROR;
1145 m_bufferStats.Get(decoded, processed, render);
1147 uint64_t startTime = CurrentHostCounter();
1150 // first fill the buffers to keep vdpau busy
1151 // mixer will run with decoded >= 2. output is limited by number of output surfaces
1152 // In case mixer is bypassed we limit by looking at processed
1153 if (decoded < 3 && processed < 3)
1155 retval |= VC_BUFFER;
1157 else if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
1159 if (msg->signal == COutputDataProtocol::PICTURE)
1161 if (m_presentPicture)
1163 m_presentPicture->ReturnUnused();
1164 m_presentPicture = 0;
1167 m_presentPicture = *(CVdpauRenderPicture**)msg->data;
1168 m_presentPicture->vdpau = this;
1169 m_bufferStats.DecRender();
1170 m_bufferStats.Get(decoded, processed, render);
1171 retval |= VC_PICTURE;
1177 else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1179 if (msg->signal == COutputControlProtocol::STATS)
1181 m_bufferStats.Get(decoded, processed, render);
1185 m_DisplayState = VDPAU_ERROR;
1191 if (decoded < 3 && processed < 3)
1193 retval |= VC_BUFFER;
1196 if (!retval && !m_inMsgEvent.WaitMSec(2000))
1199 uint64_t diff = CurrentHostCounter() - startTime;
1200 if (retval & VC_PICTURE)
1202 m_bufferStats.SetParams(diff, m_codecControl);
1204 if (diff*1000/CurrentHostFrequency() > 50)
1205 CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
1209 CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
1210 m_DisplayState = VDPAU_ERROR;
1217 bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
1219 CSingleLock lock(m_DecoderSection);
1221 if (m_DisplayState != VDPAU_OPEN)
1224 *picture = m_presentPicture->DVDPic;
1225 picture->vdpau = m_presentPicture;
1230 void CDecoder::Reset()
1232 CSingleLock lock(m_DecoderSection);
1234 if (!m_vdpauConfigured)
1238 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
1242 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
1246 CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
1247 m_DisplayState = VDPAU_ERROR;
1250 m_bufferStats.Reset();
1254 CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
1255 m_DisplayState = VDPAU_ERROR;
1259 bool CDecoder::CanSkipDeint()
1261 return m_bufferStats.CanSkipDeint();
1264 void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
1266 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
1269 bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
1271 if (vdp_st != VDP_STATUS_OK)
1273 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);
1277 if(m_DisplayState == VDPAU_OPEN)
1279 if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
1281 m_DisplayEvent.Reset();
1282 m_DisplayState = VDPAU_LOST;
1284 else if (m_ErrorCount > 2)
1285 m_DisplayState = VDPAU_ERROR;
1294 //-----------------------------------------------------------------------------
1296 //-----------------------------------------------------------------------------
1298 CVdpauRenderPicture* CVdpauRenderPicture::Acquire()
1300 CSingleLock lock(renderPicSection);
1309 long CVdpauRenderPicture::Release()
1311 CSingleLock lock(renderPicSection);
1318 vdpau->ReturnRenderPicture(this);
1319 vdpau->ReleasePicReference();
1324 void CVdpauRenderPicture::ReturnUnused()
1326 { CSingleLock lock(renderPicSection);
1331 vdpau->ReturnRenderPicture(this);
1334 void CVdpauRenderPicture::Sync()
1337 CSingleLock lock(renderPicSection);
1342 glDeleteSync(fence);
1345 fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1350 //-----------------------------------------------------------------------------
1352 //-----------------------------------------------------------------------------
1353 CMixer::CMixer(CEvent *inMsgEvent) :
1354 CThread("Vdpau Mixer"),
1355 m_controlPort("ControlPort", inMsgEvent, &m_outMsgEvent),
1356 m_dataPort("DataPort", inMsgEvent, &m_outMsgEvent)
1358 m_inMsgEvent = inMsgEvent;
1366 void CMixer::Start()
1371 void CMixer::Dispose()
1374 m_outMsgEvent.Set();
1377 m_controlPort.Purge();
1381 bool CMixer::IsActive()
1386 void CMixer::OnStartup()
1388 CLog::Log(LOGNOTICE, "CMixer::OnStartup: Output Thread created");
1391 void CMixer::OnExit()
1393 CLog::Log(LOGNOTICE, "CMixer::OnExit: Output Thread terminated");
1400 M_TOP_UNCONFIGURED, // 2
1401 M_TOP_CONFIGURED, // 3
1402 M_TOP_CONFIGURED_WAIT1, // 4
1403 M_TOP_CONFIGURED_STEP1, // 5
1404 M_TOP_CONFIGURED_WAIT2, // 6
1405 M_TOP_CONFIGURED_STEP2, // 7
1408 int MIXER_parentStates[] = {
1411 0, //TOP_UNCONFIGURED
1413 3, //TOP_CONFIGURED_WAIT1
1414 3, //TOP_CONFIGURED_STEP1
1415 3, //TOP_CONFIGURED_WAIT2
1416 3, //TOP_CONFIGURED_STEP2
1419 void CMixer::StateMachine(int signal, Protocol *port, Message *msg)
1421 for (int state = m_state; ; state = MIXER_parentStates[state])
1426 if (port == &m_controlPort)
1430 case CMixerControlProtocol::FLUSH:
1432 msg->Reply(CMixerControlProtocol::ACC);
1439 std::string portName = port == NULL ? "timer" : port->portName;
1440 CLog::Log(LOGWARNING, "CMixer::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
1444 case M_TOP_ERROR: // TOP
1447 case M_TOP_UNCONFIGURED:
1448 if (port == &m_controlPort)
1452 case CMixerControlProtocol::INIT:
1454 data = (CVdpauConfig*)msg->data;
1462 m_state = M_TOP_CONFIGURED_WAIT1;
1463 msg->Reply(CMixerControlProtocol::ACC);
1467 msg->Reply(CMixerControlProtocol::ERROR);
1476 case M_TOP_CONFIGURED:
1477 if (port == &m_dataPort)
1481 case CMixerDataProtocol::FRAME:
1482 CVdpauDecodedPicture *frame;
1483 frame = (CVdpauDecodedPicture*)msg->data;
1486 m_decodedPics.push(*frame);
1490 case CMixerDataProtocol::BUFFER:
1491 VdpOutputSurface *surf;
1492 surf = (VdpOutputSurface*)msg->data;
1495 m_outputSurfaces.push(*surf);
1505 case M_TOP_CONFIGURED_WAIT1:
1506 if (port == NULL) // timeout
1510 case CMixerControlProtocol::TIMEOUT:
1511 if (!m_decodedPics.empty() && !m_outputSurfaces.empty())
1513 m_state = M_TOP_CONFIGURED_STEP1;
1514 m_bStateMachineSelfTrigger = true;
1527 case M_TOP_CONFIGURED_STEP1:
1528 if (port == NULL) // timeout
1532 case CMixerControlProtocol::TIMEOUT:
1533 m_mixerInput.push_front(m_decodedPics.front());
1534 m_decodedPics.pop();
1535 if (m_mixerInput.size() < 2)
1537 m_state = M_TOP_CONFIGURED_WAIT1;
1545 m_state = M_TOP_CONFIGURED_WAIT1;
1546 m_extTimeout = 1000;
1549 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1550 m_outputSurfaces.pop();
1551 m_config.stats->IncProcessed();
1552 m_config.stats->DecDecoded();
1553 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1554 if (m_mixersteps > 1)
1556 m_state = M_TOP_CONFIGURED_WAIT2;
1562 m_state = M_TOP_CONFIGURED_WAIT1;
1572 case M_TOP_CONFIGURED_WAIT2:
1573 if (port == NULL) // timeout
1577 case CMixerControlProtocol::TIMEOUT:
1578 if (!m_outputSurfaces.empty())
1580 m_state = M_TOP_CONFIGURED_STEP2;
1581 m_bStateMachineSelfTrigger = true;
1594 case M_TOP_CONFIGURED_STEP2:
1595 if (port == NULL) // timeout
1599 case CMixerControlProtocol::TIMEOUT:
1600 m_processPicture.outputSurface = m_outputSurfaces.front();
1605 m_state = M_TOP_CONFIGURED_WAIT1;
1606 m_extTimeout = 1000;
1609 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1610 m_outputSurfaces.pop();
1611 m_config.stats->IncProcessed();
1612 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1614 m_state = M_TOP_CONFIGURED_WAIT1;
1623 default: // we are in no state, should not happen
1624 CLog::Log(LOGERROR, "CMixer::%s - no valid state: %d", __FUNCTION__, m_state);
1630 void CMixer::Process()
1632 Message *msg = NULL;
1633 Protocol *port = NULL;
1636 m_state = M_TOP_UNCONFIGURED;
1637 m_extTimeout = 1000;
1638 m_bStateMachineSelfTrigger = false;
1644 if (m_bStateMachineSelfTrigger)
1646 m_bStateMachineSelfTrigger = false;
1647 // self trigger state machine
1648 StateMachine(msg->signal, port, msg);
1649 if (!m_bStateMachineSelfTrigger)
1656 // check control port
1657 else if (m_controlPort.ReceiveOutMessage(&msg))
1660 port = &m_controlPort;
1663 else if (m_dataPort.ReceiveOutMessage(&msg))
1671 StateMachine(msg->signal, port, msg);
1672 if (!m_bStateMachineSelfTrigger)
1681 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
1688 msg = m_controlPort.GetMessage();
1689 msg->signal = CMixerControlProtocol::TIMEOUT;
1691 // signal timeout to state machine
1692 StateMachine(msg->signal, port, msg);
1693 if (!m_bStateMachineSelfTrigger)
1703 void CMixer::CreateVdpauMixer()
1705 CLog::Log(LOGNOTICE, " (VDPAU) Creating the video mixer");
1707 InitCSCMatrix(m_config.vidWidth);
1709 VdpVideoMixerParameter parameters[] = {
1710 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
1711 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
1712 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE};
1714 void const * parameter_values[] = {
1715 &m_config.surfaceWidth,
1716 &m_config.surfaceHeight,
1717 &m_config.vdpChromaType};
1719 VdpStatus vdp_st = VDP_STATUS_ERROR;
1720 vdp_st = m_config.context->GetProcs().vdp_video_mixer_create(m_config.context->GetDevice(),
1721 m_config.context->GetFeatureCount(),
1722 m_config.context->GetFeatures(),
1727 CheckStatus(vdp_st, __LINE__);
1731 void CMixer::InitCSCMatrix(int Width)
1733 m_Procamp.struct_version = VDP_PROCAMP_VERSION;
1734 m_Procamp.brightness = 0.0;
1735 m_Procamp.contrast = 1.0;
1736 m_Procamp.saturation = 1.0;
1740 void CMixer::CheckFeatures()
1742 if (m_Upscale != m_config.upscale)
1745 m_Upscale = m_config.upscale;
1747 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness ||
1748 m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast ||
1749 m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
1752 m_Brightness = CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness;
1753 m_Contrast = CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast;
1754 m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
1756 if (m_NoiseReduction != CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1758 m_NoiseReduction = CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction;
1759 SetNoiseReduction();
1761 if (m_Sharpness != CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1763 m_Sharpness = CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness;
1766 if (m_DeintMode != CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode ||
1767 m_Deint != CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod)
1769 m_DeintMode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
1770 m_Deint = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1775 void CMixer::SetPostProcFeatures(bool postProcEnabled)
1777 if (m_PostProc != postProcEnabled)
1779 if (postProcEnabled)
1781 SetNoiseReduction();
1788 m_PostProc = postProcEnabled;
1792 void CMixer::PostProcOff()
1796 if (m_videoMixer == VDP_INVALID_HANDLE)
1799 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1800 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1801 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE};
1803 VdpBool enabled[]={0,0,0};
1804 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1805 CheckStatus(vdp_st, __LINE__);
1807 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1809 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION};
1811 VdpBool enabled[]={0};
1812 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1813 CheckStatus(vdp_st, __LINE__);
1816 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1818 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS};
1820 VdpBool enabled[]={0};
1821 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1822 CheckStatus(vdp_st, __LINE__);
1828 bool CMixer::GenerateStudioCSCMatrix(VdpColorStandard colorStandard, VdpCSCMatrix &studioCSCMatrix)
1830 // instead use studioCSCKCoeffs601[3], studioCSCKCoeffs709[3] to generate float[3][4] matrix (float studioCSC[3][4])
1831 // m00 = mRY = red: luma factor (contrast factor) (1.0)
1832 // m10 = mGY = green: luma factor (contrast factor) (1.0)
1833 // m20 = mBY = blue: luma factor (contrast factor) (1.0)
1835 // m01 = mRB = red: blue color diff coeff (0.0)
1836 // m11 = mGB = green: blue color diff coeff (-2Kb(1-Kb)/(Kg))
1837 // m21 = mBB = blue: blue color diff coeff ((1-Kb)/0.5)
1839 // m02 = mRR = red: red color diff coeff ((1-Kr)/0.5)
1840 // m12 = mGR = green: red color diff coeff (-2Kr(1-Kr)/(Kg))
1841 // m22 = mBR = blue: red color diff coeff (0.0)
1843 // m03 = mRC = red: colour zero offset (brightness factor) (-(1-Kr)/0.5 * (128/255))
1844 // m13 = mGC = green: colour zero offset (brightness factor) ((256/255) * (Kb(1-Kb) + Kr(1-Kr)) / Kg)
1845 // m23 = mBC = blue: colour zero offset (brightness factor) (-(1-Kb)/0.5 * (128/255))
1856 // colour standard coefficients for red, geen, blue
1858 // colour diff zero position (use standard 8-bit coding precision)
1859 double CDZ = 128; //256*0.5
1860 // range excursion (use standard 8-bit coding precision)
1861 double EXC = 255; //256-1
1863 if (colorStandard == VDP_COLOR_STANDARD_ITUR_BT_601)
1865 Kr = studioCSCKCoeffs601[0];
1866 Kg = studioCSCKCoeffs601[1];
1867 Kb = studioCSCKCoeffs601[2];
1869 else // assume VDP_COLOR_STANDARD_ITUR_BT_709
1871 Kr = studioCSCKCoeffs709[0];
1872 Kg = studioCSCKCoeffs709[1];
1873 Kb = studioCSCKCoeffs709[2];
1875 // we keep luma unscaled to retain the levels present in source so that 16-235 luma is converted to RGB 16-235
1876 studioCSCMatrix[R][Y] = 1.0;
1877 studioCSCMatrix[G][Y] = 1.0;
1878 studioCSCMatrix[B][Y] = 1.0;
1880 studioCSCMatrix[R][Cb] = 0.0;
1881 studioCSCMatrix[G][Cb] = (double)-2 * Kb * (1 - Kb) / Kg;
1882 studioCSCMatrix[B][Cb] = (double)(1 - Kb) / 0.5;
1884 studioCSCMatrix[R][Cr] = (double)(1 - Kr) / 0.5;
1885 studioCSCMatrix[G][Cr] = (double)-2 * Kr * (1 - Kr) / Kg;
1886 studioCSCMatrix[B][Cr] = 0.0;
1888 studioCSCMatrix[R][C] = (double)-1 * studioCSCMatrix[R][Cr] * CDZ/EXC;
1889 studioCSCMatrix[G][C] = (double)-1 * (studioCSCMatrix[G][Cb] + studioCSCMatrix[G][Cr]) * CDZ/EXC;
1890 studioCSCMatrix[B][C] = (double)-1 * studioCSCMatrix[B][Cb] * CDZ/EXC;
1895 void CMixer::SetColor()
1899 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)
1900 m_Procamp.brightness = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)-50) / 100;
1901 if (m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)
1902 m_Procamp.contrast = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)+50) / 100;
1904 VdpColorStandard colorStandard;
1905 switch(m_mixerInput[1].DVDPic.color_matrix)
1907 case AVCOL_SPC_BT709:
1908 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1910 case AVCOL_SPC_BT470BG:
1911 case AVCOL_SPC_SMPTE170M:
1912 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1914 case AVCOL_SPC_SMPTE240M:
1915 colorStandard = VDP_COLOR_STANDARD_SMPTE_240M;
1918 case AVCOL_SPC_UNSPECIFIED:
1921 if(m_config.surfaceWidth > 1000)
1922 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1924 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1927 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX };
1928 if (CSettings::Get().GetBool("videoscreen.limitedrange"))
1930 float studioCSC[3][4];
1931 GenerateStudioCSCMatrix(colorStandard, studioCSC);
1932 void const * pm_CSCMatix[] = { &studioCSC };
1933 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1937 vdp_st = m_config.context->GetProcs().vdp_generate_csc_matrix(&m_Procamp, colorStandard, &m_CSCMatrix);
1938 void const * pm_CSCMatix[] = { &m_CSCMatrix };
1939 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1942 CheckStatus(vdp_st, __LINE__);
1945 void CMixer::SetNoiseReduction()
1947 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1950 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION };
1951 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL };
1954 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1956 VdpBool enabled[]= {0};
1957 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1958 CheckStatus(vdp_st, __LINE__);
1961 VdpBool enabled[]={1};
1962 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1963 CheckStatus(vdp_st, __LINE__);
1964 void* nr[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction };
1965 CLog::Log(LOGNOTICE,"Setting Noise Reduction to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction);
1966 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, nr);
1967 CheckStatus(vdp_st, __LINE__);
1970 void CMixer::SetSharpness()
1972 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1975 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS };
1976 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL };
1979 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1981 VdpBool enabled[]={0};
1982 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1983 CheckStatus(vdp_st, __LINE__);
1986 VdpBool enabled[]={1};
1987 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1988 CheckStatus(vdp_st, __LINE__);
1989 void* sh[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness };
1990 CLog::Log(LOGNOTICE,"Setting Sharpness to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness);
1991 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, sh);
1992 CheckStatus(vdp_st, __LINE__);
1995 EINTERLACEMETHOD CMixer::GetDeinterlacingMethod(bool log /* = false */)
1997 EINTERLACEMETHOD method = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1998 if (method == VS_INTERLACEMETHOD_AUTO)
2001 // if (m_config.outHeight >= 720)
2002 // deint = g_advancedSettings.m_videoVDPAUdeintHD;
2004 // deint = g_advancedSettings.m_videoVDPAUdeintSD;
2008 if (m_config.vdpau->Supports(EINTERLACEMETHOD(deint)))
2010 method = EINTERLACEMETHOD(deint);
2012 CLog::Log(LOGNOTICE, "CVDPAU::GetDeinterlacingMethod: set de-interlacing to %d", deint);
2017 CLog::Log(LOGWARNING, "CVDPAU::GetDeinterlacingMethod: method for de-interlacing (advanced settings) not supported");
2024 void CMixer::SetDeinterlacing()
2028 if (m_videoMixer == VDP_INVALID_HANDLE)
2031 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
2032 EINTERLACEMETHOD method = GetDeinterlacingMethod(true);
2034 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
2035 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
2036 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE };
2038 if (mode == VS_DEINTERLACEMODE_OFF)
2040 VdpBool enabled[] = {0,0,0};
2041 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2045 if (method == VS_INTERLACEMETHOD_AUTO)
2047 VdpBool enabled[] = {1,0,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);
2052 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2053 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF)
2055 VdpBool enabled[] = {1,0,0};
2056 if (g_advancedSettings.m_videoVDPAUtelecine)
2058 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2060 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2061 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF)
2063 VdpBool enabled[] = {1,1,0};
2064 if (g_advancedSettings.m_videoVDPAUtelecine)
2066 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2070 VdpBool enabled[]={0,0,0};
2071 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2074 CheckStatus(vdp_st, __LINE__);
2076 SetDeintSkipChroma();
2078 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2081 void CMixer::SetDeintSkipChroma()
2083 VdpVideoMixerAttribute attribute[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
2087 if (g_advancedSettings.m_videoVDPAUdeintSkipChromaHD && m_config.outHeight >= 720)
2092 void const *values[]={&val};
2093 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attribute), attribute, values);
2095 CheckStatus(vdp_st, __LINE__);
2098 void CMixer::SetHWUpscaling()
2100 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
2103 VdpBool enabled[]={1};
2104 switch (m_config.upscale)
2107 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2109 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2110 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2114 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2116 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2117 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2121 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2123 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2124 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2128 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2130 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2131 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2135 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2137 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2138 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2142 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2144 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2145 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2149 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2151 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2152 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2156 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2158 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2159 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2163 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2165 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2166 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2173 CheckStatus(vdp_st, __LINE__);
2177 void CMixer::DisableHQScaling()
2181 if (m_videoMixer == VDP_INVALID_HANDLE)
2184 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2186 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
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_L2))
2194 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
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_L3))
2202 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
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_L4))
2210 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
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_L5))
2218 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
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_L6))
2226 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
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_L7))
2234 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
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__);
2240 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2242 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2243 VdpBool enabled[]={0};
2244 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2245 CheckStatus(vdp_st, __LINE__);
2248 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2250 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2251 VdpBool enabled[]={0};
2252 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2253 CheckStatus(vdp_st, __LINE__);
2261 m_NoiseReduction = 0.0;
2266 m_SeenInterlaceFlag = false;
2271 m_config.upscale = g_advancedSettings.m_videoVDPAUScaling;
2272 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2277 void CMixer::Uninit()
2280 while (!m_outputSurfaces.empty())
2282 m_outputSurfaces.pop();
2284 m_config.context->GetProcs().vdp_video_mixer_destroy(m_videoMixer);
2287 void CMixer::Flush()
2289 while (!m_mixerInput.empty())
2291 CVdpauDecodedPicture pic = m_mixerInput.back();
2292 m_mixerInput.pop_back();
2293 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2295 while (!m_decodedPics.empty())
2297 CVdpauDecodedPicture pic = m_decodedPics.front();
2298 m_decodedPics.pop();
2299 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2302 while (m_dataPort.ReceiveOutMessage(&msg))
2304 if (msg->signal == CMixerDataProtocol::FRAME)
2306 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2307 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2309 else if (msg->signal == CMixerDataProtocol::BUFFER)
2311 VdpOutputSurface *surf;
2312 surf = (VdpOutputSurface*)msg->data;
2313 m_outputSurfaces.push(*surf);
2319 void CMixer::InitCycle()
2324 m_config.stats->GetParams(latency, flags);
2326 if (0) //flags & DVP_FLAG_NO_POSTPROC)
2327 SetPostProcFeatures(false);
2329 SetPostProcFeatures(true);
2331 m_config.stats->SetCanSkipDeint(false);
2333 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
2334 EINTERLACEMETHOD method = GetDeinterlacingMethod();
2335 bool interlaced = m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_INTERLACED;
2336 m_SeenInterlaceFlag |= interlaced;
2339 if (//!(flags & DVP_FLAG_NO_POSTPROC) &&
2340 (mode == VS_DEINTERLACEMODE_FORCE ||
2341 (mode == VS_DEINTERLACEMODE_AUTO && interlaced)))
2343 if((method == VS_INTERLACEMETHOD_AUTO && interlaced)
2344 || method == VS_INTERLACEMETHOD_VDPAU_BOB
2345 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2346 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2347 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2348 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2349 || method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE )
2351 if(method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2352 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2353 || !g_graphicsContext.IsFullScreenVideo())
2358 m_config.stats->SetCanSkipDeint(true);
2362 if (0) //m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_DROPDEINT)
2367 if(m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_TOP_FIELD_FIRST)
2368 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2370 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2372 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2373 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2374 DVP_FLAG_REPEAT_TOP_FIELD |
2375 DVP_FLAG_INTERLACED);
2376 m_config.useInteropYuv = false;
2378 else if (method == VS_INTERLACEMETHOD_RENDER_BOB)
2381 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2382 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2383 m_config.useInteropYuv = true;
2387 CLog::Log(LOGERROR, "CMixer::%s - interlace method: %d not supported, setting to AUTO", __FUNCTION__, method);
2389 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2390 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2391 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2392 DVP_FLAG_REPEAT_TOP_FIELD |
2393 DVP_FLAG_INTERLACED);
2395 CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod = VS_INTERLACEMETHOD_AUTO;
2401 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2403 if (m_config.useInteropYuv)
2404 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2407 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2408 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2409 DVP_FLAG_REPEAT_TOP_FIELD |
2410 DVP_FLAG_INTERLACED);
2415 if (m_mixerInput[1].DVDPic.format == RENDER_FMT_VDPAU)
2417 m_processPicture.outputSurface = m_outputSurfaces.front();
2418 m_mixerInput[1].DVDPic.iWidth = m_config.outWidth;
2419 m_mixerInput[1].DVDPic.iHeight = m_config.outHeight;
2420 if (m_SeenInterlaceFlag)
2422 double ratio = (double)m_mixerInput[1].DVDPic.iDisplayHeight / m_mixerInput[1].DVDPic.iHeight;
2423 m_mixerInput[1].DVDPic.iHeight -= 6;
2424 m_mixerInput[1].DVDPic.iDisplayHeight = lrint(ratio*m_mixerInput[1].DVDPic.iHeight);
2429 m_mixerInput[1].DVDPic.iWidth = m_config.vidWidth;
2430 m_mixerInput[1].DVDPic.iHeight = m_config.vidHeight;
2433 m_processPicture.DVDPic = m_mixerInput[1].DVDPic;
2434 m_processPicture.videoSurface = m_mixerInput[1].videoSurface;
2437 void CMixer::FiniCycle()
2439 // Keep video surfaces for one 2 cycles longer than used
2440 // by mixer. This avoids blocking in decoder.
2441 // NVidia recommends num_ref + 5
2442 while (m_mixerInput.size() > 5)
2444 CVdpauDecodedPicture &tmp = m_mixerInput.back();
2445 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
2447 m_config.videoSurfaces->ClearRender(tmp.videoSurface);
2449 m_mixerInput.pop_back();
2453 void CMixer::ProcessPicture()
2455 if (m_processPicture.DVDPic.format == RENDER_FMT_VDPAU_420)
2460 if (m_mixerstep == 1)
2462 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD)
2463 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2465 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2468 VdpVideoSurface past_surfaces[4] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2469 VdpVideoSurface futu_surfaces[2] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2470 uint32_t pastCount = 4;
2471 uint32_t futuCount = 2;
2473 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
2475 // use only 2 past 1 future for progressive/weave
2476 // (only used for postproc anyway eg noise reduction)
2477 if (m_mixerInput.size() > 3)
2478 past_surfaces[1] = m_mixerInput[3].videoSurface;
2479 if (m_mixerInput.size() > 2)
2480 past_surfaces[0] = m_mixerInput[2].videoSurface;
2481 futu_surfaces[0] = m_mixerInput[0].videoSurface;
2487 if(m_mixerstep == 0)
2489 if (m_mixerInput.size() > 3)
2491 past_surfaces[3] = m_mixerInput[3].videoSurface;
2492 past_surfaces[2] = m_mixerInput[3].videoSurface;
2494 if (m_mixerInput.size() > 2)
2496 past_surfaces[1] = m_mixerInput[2].videoSurface;
2497 past_surfaces[0] = m_mixerInput[2].videoSurface;
2499 futu_surfaces[0] = m_mixerInput[1].videoSurface;
2500 futu_surfaces[1] = m_mixerInput[0].videoSurface;
2504 if (m_mixerInput.size() > 3)
2506 past_surfaces[3] = m_mixerInput[3].videoSurface;
2508 if (m_mixerInput.size() > 2)
2510 past_surfaces[2] = m_mixerInput[2].videoSurface;
2511 past_surfaces[1] = m_mixerInput[2].videoSurface;
2513 past_surfaces[0] = m_mixerInput[1].videoSurface;
2514 futu_surfaces[0] = m_mixerInput[0].videoSurface;
2515 futu_surfaces[1] = m_mixerInput[0].videoSurface;
2517 if (m_mixerInput[0].DVDPic.pts != DVD_NOPTS_VALUE &&
2518 m_mixerInput[1].DVDPic.pts != DVD_NOPTS_VALUE)
2520 m_processPicture.DVDPic.pts = m_mixerInput[1].DVDPic.pts +
2521 (m_mixerInput[0].DVDPic.pts -
2522 m_mixerInput[1].DVDPic.pts) / 2;
2525 m_processPicture.DVDPic.pts = DVD_NOPTS_VALUE;
2526 m_processPicture.DVDPic.dts = DVD_NOPTS_VALUE;
2528 m_processPicture.DVDPic.iRepeatPicture = 0.0;
2534 sourceRect.x1 = m_config.vidWidth;
2535 sourceRect.y1 = m_config.vidHeight;
2540 destRect.x1 = m_config.outWidth;
2541 destRect.y1 = m_config.outHeight;
2543 // start vdpau video mixer
2544 vdp_st = m_config.context->GetProcs().vdp_video_mixer_render(m_videoMixer,
2550 m_mixerInput[1].videoSurface,
2554 m_processPicture.outputSurface,
2559 CheckStatus(vdp_st, __LINE__);
2563 bool CMixer::CheckStatus(VdpStatus vdp_st, int line)
2565 if (vdp_st != VDP_STATUS_OK)
2567 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);
2574 //-----------------------------------------------------------------------------
2576 //-----------------------------------------------------------------------------
2578 VdpauBufferPool::VdpauBufferPool()
2580 CVdpauRenderPicture *pic;
2581 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2583 pic = new CVdpauRenderPicture(renderPicSec);
2584 allRenderPics.push_back(pic);
2588 VdpauBufferPool::~VdpauBufferPool()
2590 CVdpauRenderPicture *pic;
2591 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2593 pic = allRenderPics[i];
2596 allRenderPics.clear();
2599 //-----------------------------------------------------------------------------
2601 //-----------------------------------------------------------------------------
2602 COutput::COutput(CEvent *inMsgEvent) :
2603 CThread("Vdpau Output"),
2604 m_controlPort("OutputControlPort", inMsgEvent, &m_outMsgEvent),
2605 m_dataPort("OutputDataPort", inMsgEvent, &m_outMsgEvent),
2606 m_mixer(&m_outMsgEvent)
2608 m_inMsgEvent = inMsgEvent;
2610 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); ++i)
2612 m_bufferPool.freeRenderPics.push_back(i);
2616 void COutput::Start()
2625 m_bufferPool.freeRenderPics.clear();
2626 m_bufferPool.usedRenderPics.clear();
2629 void COutput::Dispose()
2631 CSingleLock lock(g_graphicsContext);
2633 m_outMsgEvent.Set();
2635 m_controlPort.Purge();
2639 void COutput::OnStartup()
2641 CLog::Log(LOGNOTICE, "COutput::OnStartup: Output Thread created");
2644 void COutput::OnExit()
2646 CLog::Log(LOGNOTICE, "COutput::OnExit: Output Thread terminated");
2653 O_TOP_UNCONFIGURED, // 2
2654 O_TOP_CONFIGURED, // 3
2655 O_TOP_CONFIGURED_IDLE, // 4
2656 O_TOP_CONFIGURED_WORK, // 5
2659 int VDPAU_OUTPUT_parentStates[] = {
2662 0, //TOP_UNCONFIGURED
2664 3, //TOP_CONFIGURED_IDLE
2665 3, //TOP_CONFIGURED_WORK
2668 void COutput::StateMachine(int signal, Protocol *port, Message *msg)
2670 for (int state = m_state; ; state = VDPAU_OUTPUT_parentStates[state])
2675 if (port == &m_controlPort)
2679 case COutputControlProtocol::FLUSH:
2680 msg->Reply(COutputControlProtocol::ACC);
2682 case COutputControlProtocol::PRECLEANUP:
2683 msg->Reply(COutputControlProtocol::ACC);
2689 else if (port == &m_dataPort)
2693 case COutputDataProtocol::RETURNPIC:
2694 CVdpauRenderPicture *pic;
2695 pic = *((CVdpauRenderPicture**)msg->data);
2696 QueueReturnPicture(pic);
2703 std::string portName = port == NULL ? "timer" : port->portName;
2704 CLog::Log(LOGWARNING, "COutput::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
2711 case O_TOP_UNCONFIGURED:
2712 if (port == &m_controlPort)
2716 case COutputControlProtocol::INIT:
2718 data = (CVdpauConfig*)msg->data;
2725 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::INIT,
2726 &reply, 1000, &m_config, sizeof(m_config)))
2728 if (reply->signal != CMixerControlProtocol::ACC)
2733 // set initial number of
2734 m_bufferPool.numOutputSurfaces = 4;
2738 m_state = O_TOP_CONFIGURED_IDLE;
2739 msg->Reply(COutputControlProtocol::ACC, &m_config, sizeof(m_config));
2743 m_state = O_TOP_ERROR;
2744 msg->Reply(COutputControlProtocol::ERROR);
2753 case O_TOP_CONFIGURED:
2754 if (port == &m_controlPort)
2758 case COutputControlProtocol::FLUSH:
2760 msg->Reply(COutputControlProtocol::ACC);
2762 case COutputControlProtocol::PRECLEANUP:
2765 msg->Reply(COutputControlProtocol::ACC);
2771 else if (port == &m_dataPort)
2775 case COutputDataProtocol::NEWFRAME:
2776 CVdpauDecodedPicture *frame;
2777 frame = (CVdpauDecodedPicture*)msg->data;
2780 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::FRAME,
2781 frame,sizeof(CVdpauDecodedPicture));
2784 case COutputDataProtocol::RETURNPIC:
2785 CVdpauRenderPicture *pic;
2786 pic = *((CVdpauRenderPicture**)msg->data);
2787 QueueReturnPicture(pic);
2788 m_controlPort.SendInMessage(COutputControlProtocol::STATS);
2789 m_state = O_TOP_CONFIGURED_WORK;
2796 else if (port == &m_mixer.m_dataPort)
2800 case CMixerDataProtocol::PICTURE:
2801 CVdpauProcessedPicture *pic;
2802 pic = (CVdpauProcessedPicture*)msg->data;
2803 m_bufferPool.processedPics.push(*pic);
2804 m_state = O_TOP_CONFIGURED_WORK;
2813 case O_TOP_CONFIGURED_IDLE:
2814 if (port == NULL) // timeout
2818 case COutputControlProtocol::TIMEOUT:
2819 if (ProcessSyncPicture())
2825 m_state = O_TOP_CONFIGURED_WORK;
2835 case O_TOP_CONFIGURED_WORK:
2836 if (port == NULL) // timeout
2840 case COutputControlProtocol::TIMEOUT:
2843 CVdpauRenderPicture *pic;
2844 pic = ProcessMixerPicture();
2847 m_config.stats->DecProcessed();
2848 m_config.stats->IncRender();
2849 m_dataPort.SendInMessage(COutputDataProtocol::PICTURE, &pic, sizeof(pic));
2855 m_state = O_TOP_CONFIGURED_IDLE;
2865 default: // we are in no state, should not happen
2866 CLog::Log(LOGERROR, "COutput::%s - no valid state: %d", __FUNCTION__, m_state);
2872 void COutput::Process()
2874 Message *msg = NULL;
2875 Protocol *port = NULL;
2878 m_state = O_TOP_UNCONFIGURED;
2879 m_extTimeout = 1000;
2880 m_bStateMachineSelfTrigger = false;
2886 if (m_bStateMachineSelfTrigger)
2888 m_bStateMachineSelfTrigger = false;
2889 // self trigger state machine
2890 StateMachine(msg->signal, port, msg);
2891 if (!m_bStateMachineSelfTrigger)
2898 // check control port
2899 else if (m_controlPort.ReceiveOutMessage(&msg))
2902 port = &m_controlPort;
2905 else if (m_dataPort.ReceiveOutMessage(&msg))
2910 // check mixer data port
2911 else if (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2914 port = &m_mixer.m_dataPort;
2918 StateMachine(msg->signal, port, msg);
2919 if (!m_bStateMachineSelfTrigger)
2928 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
2935 msg = m_controlPort.GetMessage();
2936 msg->signal = COutputControlProtocol::TIMEOUT;
2938 // signal timeout to state machine
2939 StateMachine(msg->signal, port, msg);
2940 if (!m_bStateMachineSelfTrigger)
2951 bool COutput::Init()
2953 if (!CreateGlxContext())
2965 bool COutput::Uninit()
2969 while(ProcessSyncPicture())
2974 ReleaseBufferPool();
2975 DestroyGlxContext();
2979 void COutput::Flush()
2981 if (m_mixer.IsActive())
2984 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::FLUSH,
2991 CLog::Log(LOGERROR, "Coutput::%s - failed to flush mixer", __FUNCTION__);
2995 while (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2997 if (msg->signal == CMixerDataProtocol::PICTURE)
2999 CVdpauProcessedPicture pic = *(CVdpauProcessedPicture*)msg->data;
3000 m_bufferPool.processedPics.push(pic);
3005 while (m_dataPort.ReceiveOutMessage(&msg))
3007 if (msg->signal == COutputDataProtocol::NEWFRAME)
3009 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
3010 m_config.videoSurfaces->ClearRender(pic.videoSurface);
3012 else if (msg->signal == COutputDataProtocol::RETURNPIC)
3014 CVdpauRenderPicture *pic;
3015 pic = *((CVdpauRenderPicture**)msg->data);
3016 QueueReturnPicture(pic);
3021 while (m_dataPort.ReceiveInMessage(&msg))
3023 if (msg->signal == COutputDataProtocol::PICTURE)
3025 CVdpauRenderPicture *pic;
3026 pic = *((CVdpauRenderPicture**)msg->data);
3027 QueueReturnPicture(pic);
3031 // reset used render flag which was cleared on mixer flush
3032 std::deque<int>::iterator it;
3033 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3035 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[*it];
3036 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3038 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it2;
3039 it2 = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3040 if (it2 == m_bufferPool.glVideoSurfaceMap.end())
3042 CLog::Log(LOGDEBUG, "COutput::Flush - gl surface not found");
3045 m_config.videoSurfaces->MarkRender(it2->second.sourceVuv);
3049 // clear processed pics
3050 while(!m_bufferPool.processedPics.empty())
3052 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3053 if (procPic.DVDPic.format == RENDER_FMT_VDPAU)
3055 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &procPic.outputSurface, sizeof(procPic.outputSurface));
3057 else if (procPic.DVDPic.format == RENDER_FMT_VDPAU_420)
3059 m_config.videoSurfaces->ClearRender(procPic.videoSurface);
3061 m_bufferPool.processedPics.pop();
3065 bool COutput::HasWork()
3067 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3072 CVdpauRenderPicture* COutput::ProcessMixerPicture()
3074 CVdpauRenderPicture *retPic = NULL;
3076 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3078 int idx = m_bufferPool.freeRenderPics.front();
3079 retPic = m_bufferPool.allRenderPics[idx];
3080 m_bufferPool.freeRenderPics.pop_front();
3081 m_bufferPool.usedRenderPics.push_back(idx);
3082 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3083 m_bufferPool.processedPics.pop();
3085 retPic->DVDPic = procPic.DVDPic;
3086 retPic->valid = true;
3087 if (retPic->DVDPic.format == RENDER_FMT_VDPAU)
3089 m_config.useInteropYuv = false;
3090 m_bufferPool.numOutputSurfaces = NUM_RENDER_PICS;
3092 GLMapSurface(false, procPic.outputSurface);
3093 retPic->sourceIdx = procPic.outputSurface;
3094 retPic->texture[0] = m_bufferPool.glOutputSurfaceMap[procPic.outputSurface].texture[0];
3095 retPic->texWidth = m_config.outWidth;
3096 retPic->texHeight = m_config.outHeight;
3097 retPic->crop.x1 = 0;
3098 retPic->crop.y1 = (m_config.outHeight - retPic->DVDPic.iHeight) / 2;
3099 retPic->crop.x2 = m_config.outWidth;
3100 retPic->crop.y2 = m_config.outHeight - retPic->crop.y1;
3104 m_config.useInteropYuv = true;
3105 GLMapSurface(true, procPic.videoSurface);
3106 retPic->sourceIdx = procPic.videoSurface;
3107 for (unsigned int i=0; i<4; ++i)
3108 retPic->texture[i] = m_bufferPool.glVideoSurfaceMap[procPic.videoSurface].texture[i];
3109 retPic->texWidth = m_config.surfaceWidth;
3110 retPic->texHeight = m_config.surfaceHeight;
3111 retPic->crop.x1 = 0;
3112 retPic->crop.y1 = 0;
3113 retPic->crop.x2 = m_config.surfaceWidth - m_config.vidWidth;
3114 retPic->crop.y2 = m_config.surfaceHeight - m_config.vidHeight;
3120 void COutput::QueueReturnPicture(CVdpauRenderPicture *pic)
3122 std::deque<int>::iterator it;
3123 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3125 if (m_bufferPool.allRenderPics[*it] == pic)
3131 if (it == m_bufferPool.usedRenderPics.end())
3133 CLog::Log(LOGWARNING, "COutput::QueueReturnPicture - pic not found");
3137 // check if already queued
3138 std::deque<int>::iterator it2 = find(m_bufferPool.syncRenderPics.begin(),
3139 m_bufferPool.syncRenderPics.end(),
3141 if (it2 == m_bufferPool.syncRenderPics.end())
3143 m_bufferPool.syncRenderPics.push_back(*it);
3146 ProcessSyncPicture();
3149 bool COutput::ProcessSyncPicture()
3151 CVdpauRenderPicture *pic;
3154 std::deque<int>::iterator it;
3155 for (it = m_bufferPool.syncRenderPics.begin(); it != m_bufferPool.syncRenderPics.end(); )
3157 pic = m_bufferPool.allRenderPics[*it];
3162 if (glIsSync(pic->fence))
3166 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3167 if(state == GL_SIGNALED)
3169 glDeleteSync(pic->fence);
3182 m_bufferPool.freeRenderPics.push_back(*it);
3184 std::deque<int>::iterator it2 = find(m_bufferPool.usedRenderPics.begin(),
3185 m_bufferPool.usedRenderPics.end(),
3187 if (it2 == m_bufferPool.usedRenderPics.end())
3189 CLog::Log(LOGERROR, "COutput::ProcessSyncPicture - pic not found in queue");
3193 m_bufferPool.usedRenderPics.erase(it2);
3195 it = m_bufferPool.syncRenderPics.erase(it);
3199 ProcessReturnPicture(pic);
3203 CLog::Log(LOGDEBUG, "COutput::%s - return of invalid render pic", __FUNCTION__);
3209 void COutput::ProcessReturnPicture(CVdpauRenderPicture *pic)
3211 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3213 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3214 it = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3215 if (it == m_bufferPool.glVideoSurfaceMap.end())
3217 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3220 #ifdef GL_NV_vdpau_interop
3221 glVDPAUUnmapSurfacesNV(1, &(it->second.glVdpauSurface));
3223 VdpVideoSurface surf = it->second.sourceVuv;
3224 m_config.videoSurfaces->ClearRender(surf);
3226 else if (pic->DVDPic.format == RENDER_FMT_VDPAU)
3228 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3229 it = m_bufferPool.glOutputSurfaceMap.find(pic->sourceIdx);
3230 if (it == m_bufferPool.glOutputSurfaceMap.end())
3232 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3235 #ifdef GL_NV_vdpau_interop
3236 glVDPAUUnmapSurfacesNV(1, &(it->second.glVdpauSurface));
3238 VdpOutputSurface outSurf = it->second.sourceRgb;
3239 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &outSurf, sizeof(outSurf));
3243 bool COutput::EnsureBufferPool()
3247 // Creation of outputSurfaces
3248 VdpOutputSurface outputSurface;
3249 for (int i = m_bufferPool.outputSurfaces.size(); i < m_bufferPool.numOutputSurfaces; i++)
3251 vdp_st = m_config.context->GetProcs().vdp_output_surface_create(m_config.context->GetDevice(),
3252 VDP_RGBA_FORMAT_B8G8R8A8,
3256 if (CheckStatus(vdp_st, __LINE__))
3258 m_bufferPool.outputSurfaces.push_back(outputSurface);
3260 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3262 sizeof(VdpOutputSurface));
3263 CLog::Log(LOGNOTICE, "VDPAU::COutput::InitBufferPool - Output Surface created");
3268 void COutput::ReleaseBufferPool()
3272 CSingleLock lock(m_bufferPool.renderPicSec);
3274 // release all output surfaces
3275 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3277 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3279 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3280 CheckStatus(vdp_st, __LINE__);
3282 m_bufferPool.outputSurfaces.clear();
3284 // wait for all fences
3285 XbmcThreads::EndTime timeout(1000);
3286 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3288 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[i];
3292 while (glIsSync(pic->fence))
3296 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3297 if(state == GL_SIGNALED || timeout.IsTimePast())
3299 glDeleteSync(pic->fence);
3310 if (timeout.IsTimePast())
3312 CLog::Log(LOGERROR, "COutput::%s - timeout waiting for fence", __FUNCTION__);
3314 ProcessSyncPicture();
3316 // invalidate all used render pictures
3317 for (unsigned int i = 0; i < m_bufferPool.usedRenderPics.size(); ++i)
3319 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[m_bufferPool.usedRenderPics[i]];
3324 void COutput::PreCleanup()
3330 ProcessSyncPicture();
3332 CSingleLock lock(m_bufferPool.renderPicSec);
3333 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3335 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3338 // check if output surface is in use
3340 std::deque<int>::iterator it;
3341 CVdpauRenderPicture *pic;
3342 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3344 pic = m_bufferPool.allRenderPics[*it];
3345 if ((pic->sourceIdx == m_bufferPool.outputSurfaces[i]) && pic->valid)
3354 #ifdef GL_NV_vdpau_interop
3356 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it_map;
3357 it_map = m_bufferPool.glOutputSurfaceMap.find(m_bufferPool.outputSurfaces[i]);
3358 if (it_map == m_bufferPool.glOutputSurfaceMap.end())
3360 CLog::Log(LOGERROR, "%s - could not find gl surface", __FUNCTION__);
3363 glVDPAUUnregisterSurfaceNV(it_map->second.glVdpauSurface);
3364 glDeleteTextures(1, it_map->second.texture);
3365 m_bufferPool.glOutputSurfaceMap.erase(it_map);
3368 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3369 CheckStatus(vdp_st, __LINE__);
3371 m_bufferPool.outputSurfaces[i] = VDP_INVALID_HANDLE;
3373 CLog::Log(LOGDEBUG, "VDPAU::PreCleanup - released output surface");
3378 void COutput::InitMixer()
3380 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3382 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3383 &m_bufferPool.outputSurfaces[i],
3384 sizeof(VdpOutputSurface));
3388 bool COutput::GLInit()
3390 #ifdef GL_NV_vdpau_interop
3391 glVDPAUInitNV = NULL;
3392 glVDPAUFiniNV = NULL;
3393 glVDPAURegisterOutputSurfaceNV = NULL;
3394 glVDPAURegisterVideoSurfaceNV = NULL;
3395 glVDPAUIsSurfaceNV = NULL;
3396 glVDPAUUnregisterSurfaceNV = NULL;
3397 glVDPAUSurfaceAccessNV = NULL;
3398 glVDPAUMapSurfacesNV = NULL;
3399 glVDPAUUnmapSurfacesNV = NULL;
3400 glVDPAUGetSurfaceivNV = NULL;
3403 #ifdef GL_NV_vdpau_interop
3405 glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUInitNV");
3407 glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((GLubyte *) "glVDPAUFiniNV");
3408 if (!glVDPAURegisterOutputSurfaceNV)
3409 glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterOutputSurfaceNV");
3410 if (!glVDPAURegisterVideoSurfaceNV)
3411 glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterVideoSurfaceNV");
3412 if (!glVDPAUIsSurfaceNV)
3413 glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUIsSurfaceNV");
3414 if (!glVDPAUUnregisterSurfaceNV)
3415 glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnregisterSurfaceNV");
3416 if (!glVDPAUSurfaceAccessNV)
3417 glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUSurfaceAccessNV");
3418 if (!glVDPAUMapSurfacesNV)
3419 glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUMapSurfacesNV");
3420 if (!glVDPAUUnmapSurfacesNV)
3421 glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnmapSurfacesNV");
3422 if (!glVDPAUGetSurfaceivNV)
3423 glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUGetSurfaceivNV");
3425 while (glGetError() != GL_NO_ERROR);
3426 glVDPAUInitNV(reinterpret_cast<void*>(m_config.context->GetDevice()), reinterpret_cast<void*>(m_config.context->GetProcs().vdp_get_proc_address));
3427 if (glGetError() != GL_NO_ERROR)
3429 CLog::Log(LOGERROR, "VDPAU::COutput - GLInitInterop glVDPAUInitNV failed");
3433 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop initialized");
3437 bool hasfence = glewIsSupported("GL_ARB_sync");
3438 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3440 m_bufferPool.allRenderPics[i]->usefence = hasfence;
3447 void COutput::GLMapSurface(bool yuv, uint32_t source)
3449 #ifdef GL_NV_vdpau_interop
3453 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3454 it = m_bufferPool.glVideoSurfaceMap.find(source);
3455 if (it == m_bufferPool.glVideoSurfaceMap.end())
3457 VdpauBufferPool::GLVideoSurface glSurface;
3458 VdpVideoSurface surf = source;
3460 if (surf == VDP_INVALID_HANDLE)
3463 glSurface.sourceVuv = surf;
3464 while (glGetError() != GL_NO_ERROR) ;
3465 glGenTextures(4, glSurface.texture);
3466 if (glGetError() != GL_NO_ERROR)
3468 CLog::Log(LOGERROR, "VDPAU::COutput error creating texture");
3471 glSurface.glVdpauSurface = glVDPAURegisterVideoSurfaceNV(reinterpret_cast<void*>(surf),
3472 GL_TEXTURE_2D, 4, glSurface.texture);
3474 if (glGetError() != GL_NO_ERROR)
3476 CLog::Log(LOGERROR, "VDPAU::COutput error register video surface");
3479 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3480 if (glGetError() != GL_NO_ERROR)
3482 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3485 m_bufferPool.glVideoSurfaceMap[surf] = glSurface;
3487 CLog::Log(LOGNOTICE, "VDPAU::COutput registered surface");
3490 while (glGetError() != GL_NO_ERROR) ;
3491 glVDPAUMapSurfacesNV(1, &m_bufferPool.glVideoSurfaceMap[source].glVdpauSurface);
3492 if (glGetError() != GL_NO_ERROR)
3494 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3503 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3504 it = m_bufferPool.glOutputSurfaceMap.find(source);
3505 if (it == m_bufferPool.glOutputSurfaceMap.end())
3507 unsigned int idx = 0;
3508 for (idx = 0; idx<m_bufferPool.outputSurfaces.size(); idx++)
3510 if (m_bufferPool.outputSurfaces[idx] == source)
3514 VdpauBufferPool::GLVideoSurface glSurface;
3515 glSurface.sourceRgb = m_bufferPool.outputSurfaces[idx];
3516 glGenTextures(1, glSurface.texture);
3517 glSurface.glVdpauSurface = glVDPAURegisterOutputSurfaceNV(reinterpret_cast<void*>(m_bufferPool.outputSurfaces[idx]),
3518 GL_TEXTURE_2D, 1, glSurface.texture);
3519 if (glGetError() != GL_NO_ERROR)
3521 CLog::Log(LOGERROR, "VDPAU::COutput error register output surface");
3524 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3525 if (glGetError() != GL_NO_ERROR)
3527 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3530 m_bufferPool.glOutputSurfaceMap[source] = glSurface;
3531 CLog::Log(LOGNOTICE, "VDPAU::COutput registered output surfaces");
3534 while (glGetError() != GL_NO_ERROR) ;
3535 glVDPAUMapSurfacesNV(1, &m_bufferPool.glOutputSurfaceMap[source].glVdpauSurface);
3536 if (glGetError() != GL_NO_ERROR)
3538 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3548 void COutput::GLUnmapSurfaces()
3550 #ifdef GL_NV_vdpau_interop
3553 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3554 for (it = m_bufferPool.glVideoSurfaceMap.begin(); it != m_bufferPool.glVideoSurfaceMap.end(); ++it)
3556 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3557 glDeleteTextures(4, it->second.texture);
3559 m_bufferPool.glVideoSurfaceMap.clear();
3562 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3563 for (it = m_bufferPool.glOutputSurfaceMap.begin(); it != m_bufferPool.glOutputSurfaceMap.end(); ++it)
3565 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3566 glDeleteTextures(1, it->second.texture);
3568 m_bufferPool.glOutputSurfaceMap.clear();
3572 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop finished");
3577 bool COutput::CheckStatus(VdpStatus vdp_st, int line)
3579 if (vdp_st != VDP_STATUS_OK)
3581 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);
3588 bool COutput::CreateGlxContext()
3590 GLXContext glContext;
3592 m_Display = g_Windowing.GetDisplay();
3593 glContext = g_Windowing.GetGlxContext();
3594 m_Window = g_Windowing.GetWindow();
3596 // Get our window attribs.
3597 XWindowAttributes wndattribs;
3598 XGetWindowAttributes(m_Display, m_Window, &wndattribs);
3601 XVisualInfo visInfo;
3602 visInfo.visualid = wndattribs.visual->visualid;
3604 XVisualInfo* visuals = XGetVisualInfo(m_Display, VisualIDMask, &visInfo, &nvisuals);
3607 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - could not find visual");
3610 visInfo = visuals[0];
3613 m_pixmap = XCreatePixmap(m_Display,
3620 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - Unable to create XPixmap");
3625 m_glPixmap = glXCreateGLXPixmap(m_Display, &visInfo, m_pixmap);
3629 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not create glPixmap");
3633 m_glContext = glXCreateContext(m_Display, &visInfo, glContext, True);
3635 if (!glXMakeCurrent(m_Display, m_glPixmap, m_glContext))
3637 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not make Pixmap current");
3641 CLog::Log(LOGNOTICE, "VDPAU::COutput::CreateGlxContext - created context");
3645 bool COutput::DestroyGlxContext()
3649 glXMakeCurrent(m_Display, None, NULL);
3650 glXDestroyContext(m_Display, m_glContext);
3655 glXDestroyPixmap(m_Display, m_glPixmap);
3659 XFreePixmap(m_Display, m_pixmap);