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"
39 #include "utils/log.h"
41 using namespace Actor;
42 using namespace VDPAU;
43 #define NUM_RENDER_PICS 7
44 #define NUM_CROP_PIX 3
46 #define ARSIZE(x) (sizeof(x) / sizeof((x)[0]))
48 CDecoder::Desc decoder_profiles[] = {
49 {"MPEG1", VDP_DECODER_PROFILE_MPEG1},
50 {"MPEG2_SIMPLE", VDP_DECODER_PROFILE_MPEG2_SIMPLE},
51 {"MPEG2_MAIN", VDP_DECODER_PROFILE_MPEG2_MAIN},
52 {"H264_BASELINE",VDP_DECODER_PROFILE_H264_BASELINE},
53 {"H264_MAIN", VDP_DECODER_PROFILE_H264_MAIN},
54 {"H264_HIGH", VDP_DECODER_PROFILE_H264_HIGH},
55 {"VC1_SIMPLE", VDP_DECODER_PROFILE_VC1_SIMPLE},
56 {"VC1_MAIN", VDP_DECODER_PROFILE_VC1_MAIN},
57 {"VC1_ADVANCED", VDP_DECODER_PROFILE_VC1_ADVANCED},
58 {"MPEG4_PART2_ASP", VDP_DECODER_PROFILE_MPEG4_PART2_ASP},
59 #ifdef VDP_DECODER_PROFILE_HEVC_MAIN
60 {"HEVC_MAIN", VDP_DECODER_PROFILE_HEVC_MAIN},
63 const size_t decoder_profile_count = sizeof(decoder_profiles)/sizeof(CDecoder::Desc);
65 static struct SInterlaceMapping
67 const EINTERLACEMETHOD method;
68 const VdpVideoMixerFeature feature;
69 } g_interlace_mapping[] =
70 { {VS_INTERLACEMETHOD_VDPAU_TEMPORAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
71 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
72 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
73 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF, VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
74 , {VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE , VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE}
75 , {VS_INTERLACEMETHOD_NONE , (VdpVideoMixerFeature)-1}
78 static float studioCSCKCoeffs601[3] = {0.299, 0.587, 0.114}; //BT601 {Kr, Kg, Kb}
79 static float studioCSCKCoeffs709[3] = {0.2126, 0.7152, 0.0722}; //BT709 {Kr, Kg, Kb}
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 CVDPAUContext *CVDPAUContext::m_context = 0;
85 CCriticalSection CVDPAUContext::m_section;
86 Display *CVDPAUContext::m_display = 0;
87 void *CVDPAUContext::m_dlHandle = 0;
89 CVDPAUContext::CVDPAUContext()
95 void CVDPAUContext::Release()
97 CSingleLock lock(m_section);
108 void CVDPAUContext::Close()
110 CLog::Log(LOGNOTICE, "VDPAU::Close - closing decoder context");
114 bool CVDPAUContext::EnsureContext(CVDPAUContext **ctx)
116 CSingleLock lock(m_section);
120 m_context->m_refCount++;
125 m_context = new CVDPAUContext();
128 CSingleLock gLock(g_graphicsContext);
129 if (!m_context->LoadSymbols() || !m_context->CreateContext())
138 m_context->m_refCount++;
144 VDPAU_procs& CVDPAUContext::GetProcs()
149 VdpVideoMixerFeature* CVDPAUContext::GetFeatures()
151 return m_vdpFeatures;
154 int CVDPAUContext::GetFeatureCount()
156 return m_featureCount;
159 bool CVDPAUContext::LoadSymbols()
163 m_dlHandle = dlopen("libvdpau.so.1", RTLD_LAZY);
166 const char* error = dlerror();
168 error = "dlerror() returned NULL";
170 CLog::Log(LOGERROR,"VDPAU::LoadSymbols: Unable to get handle to lib: %s", error);
177 dl_vdp_device_create_x11 = (VdpStatus (*)(Display*, int, VdpDevice*, VdpStatus (**)(VdpDevice, VdpFuncId, void**)))dlsym(m_dlHandle, (const char*)"vdp_device_create_x11");
181 CLog::Log(LOGERROR,"(VDPAU) - %s in %s",error,__FUNCTION__);
182 m_vdpDevice = VDP_INVALID_HANDLE;
188 bool CVDPAUContext::CreateContext()
190 CLog::Log(LOGNOTICE,"VDPAU::CreateContext - creating decoder context");
193 { CSingleLock lock(g_graphicsContext);
195 m_display = XOpenDisplay(NULL);
196 mScreen = g_Windowing.GetCurrentScreen();
201 vdp_st = dl_vdp_device_create_x11(m_display,
204 &m_vdpProcs.vdp_get_proc_address);
206 CLog::Log(LOGNOTICE,"vdp_device = 0x%08x vdp_st = 0x%08x",m_vdpDevice,vdp_st);
207 if (vdp_st != VDP_STATUS_OK)
209 CLog::Log(LOGERROR,"(VDPAU) unable to init VDPAU - vdp_st = 0x%x. Falling back.",vdp_st);
210 m_vdpDevice = VDP_INVALID_HANDLE;
215 SpewHardwareAvailable();
219 void CVDPAUContext::QueryProcs()
223 #define VDP_PROC(id, proc) \
225 vdp_st = m_vdpProcs.vdp_get_proc_address(m_vdpDevice, id, (void**)&proc); \
226 if (vdp_st != VDP_STATUS_OK) \
228 CLog::Log(LOGERROR, "CVDPAUContext::GetProcs - failed to get proc id"); \
232 VDP_PROC(VDP_FUNC_ID_GET_ERROR_STRING , m_vdpProcs.vdp_get_error_string);
233 VDP_PROC(VDP_FUNC_ID_DEVICE_DESTROY , m_vdpProcs.vdp_device_destroy);
234 VDP_PROC(VDP_FUNC_ID_GENERATE_CSC_MATRIX , m_vdpProcs.vdp_generate_csc_matrix);
235 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_CREATE , m_vdpProcs.vdp_video_surface_create);
236 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY , m_vdpProcs.vdp_video_surface_destroy);
237 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_put_bits_y_cb_cr);
238 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_get_bits_y_cb_cr);
239 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_output_surface_put_bits_y_cb_cr);
240 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE , m_vdpProcs.vdp_output_surface_put_bits_native);
241 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_CREATE , m_vdpProcs.vdp_output_surface_create);
242 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY , m_vdpProcs.vdp_output_surface_destroy);
243 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE , m_vdpProcs.vdp_output_surface_get_bits_native);
244 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, m_vdpProcs.vdp_output_surface_render_output_surface);
245 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED , m_vdpProcs.vdp_output_surface_put_bits_indexed);
246 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_CREATE , m_vdpProcs.vdp_video_mixer_create);
247 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES , m_vdpProcs.vdp_video_mixer_set_feature_enables);
248 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_DESTROY , m_vdpProcs.vdp_video_mixer_destroy);
249 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_RENDER , m_vdpProcs.vdp_video_mixer_render);
250 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES , m_vdpProcs.vdp_video_mixer_set_attribute_values);
251 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_PARAMETER_SUPPORT , m_vdpProcs.vdp_video_mixer_query_parameter_support);
252 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_FEATURE_SUPPORT , m_vdpProcs.vdp_video_mixer_query_feature_support);
253 VDP_PROC(VDP_FUNC_ID_DECODER_CREATE , m_vdpProcs.vdp_decoder_create);
254 VDP_PROC(VDP_FUNC_ID_DECODER_DESTROY , m_vdpProcs.vdp_decoder_destroy);
255 VDP_PROC(VDP_FUNC_ID_DECODER_RENDER , m_vdpProcs.vdp_decoder_render);
256 VDP_PROC(VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES , m_vdpProcs.vdp_decoder_query_caps);
260 VdpDevice CVDPAUContext::GetDevice()
265 void CVDPAUContext::DestroyContext()
267 if (!m_vdpProcs.vdp_device_destroy)
270 m_vdpProcs.vdp_device_destroy(m_vdpDevice);
271 m_vdpDevice = VDP_INVALID_HANDLE;
274 void CVDPAUContext::SpewHardwareAvailable() //CopyrighVDPAUt (c) 2008 Wladimir J. van der Laan -- VDPInfo
277 CLog::Log(LOGNOTICE,"VDPAU Decoder capabilities:");
278 CLog::Log(LOGNOTICE,"name level macbs width height");
279 CLog::Log(LOGNOTICE,"------------------------------------");
280 for(unsigned int x=0; x<decoder_profile_count; ++x)
282 VdpBool is_supported = false;
283 uint32_t max_level, max_macroblocks, max_width, max_height;
284 rv = m_vdpProcs.vdp_decoder_query_caps(m_vdpDevice, decoder_profiles[x].id,
285 &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
286 if(rv == VDP_STATUS_OK && is_supported)
288 CLog::Log(LOGNOTICE,"%-16s %2i %5i %5i %5i\n", decoder_profiles[x].name,
289 max_level, max_macroblocks, max_width, max_height);
292 CLog::Log(LOGNOTICE,"------------------------------------");
294 #define CHECK_SUPPORT(feature) \
297 if(m_vdpProcs.vdp_video_mixer_query_feature_support(m_vdpDevice, feature, &supported) == VDP_STATUS_OK && supported) { \
298 CLog::Log(LOGNOTICE, "Mixer feature: "#feature); \
299 m_vdpFeatures[m_featureCount++] = feature; \
303 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION);
304 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_SHARPNESS);
305 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL);
306 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL);
307 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE);
308 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
309 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1);
310 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2);
311 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3);
312 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4);
313 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5);
314 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6);
315 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7);
316 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8);
317 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9);
322 bool CVDPAUContext::Supports(VdpVideoMixerFeature feature)
324 for(int i = 0; i < m_featureCount; i++)
326 if(m_vdpFeatures[i] == feature)
332 //-----------------------------------------------------------------------------
333 // VDPAU Video Surface states
334 //-----------------------------------------------------------------------------
336 #define SURFACE_USED_FOR_REFERENCE 0x01
337 #define SURFACE_USED_FOR_RENDER 0x02
339 void CVideoSurfaces::AddSurface(VdpVideoSurface surf)
341 CSingleLock lock(m_section);
342 m_state[surf] = SURFACE_USED_FOR_REFERENCE;
345 void CVideoSurfaces::ClearReference(VdpVideoSurface surf)
347 CSingleLock lock(m_section);
348 if (m_state.find(surf) == m_state.end())
350 CLog::Log(LOGWARNING, "CVideoSurfaces::ClearReference - surface invalid");
353 m_state[surf] &= ~SURFACE_USED_FOR_REFERENCE;
354 if (m_state[surf] == 0)
356 m_freeSurfaces.push_back(surf);
360 bool CVideoSurfaces::MarkRender(VdpVideoSurface surf)
362 CSingleLock lock(m_section);
363 if (m_state.find(surf) == m_state.end())
365 CLog::Log(LOGWARNING, "CVideoSurfaces::MarkRender - surface invalid");
368 std::list<VdpVideoSurface>::iterator it;
369 it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
370 if (it != m_freeSurfaces.end())
372 m_freeSurfaces.erase(it);
374 m_state[surf] |= SURFACE_USED_FOR_RENDER;
378 void CVideoSurfaces::ClearRender(VdpVideoSurface surf)
380 CSingleLock lock(m_section);
381 if (m_state.find(surf) == m_state.end())
383 CLog::Log(LOGWARNING, "CVideoSurfaces::ClearRender - surface invalid");
386 m_state[surf] &= ~SURFACE_USED_FOR_RENDER;
387 if (m_state[surf] == 0)
389 m_freeSurfaces.push_back(surf);
393 bool CVideoSurfaces::IsValid(VdpVideoSurface surf)
395 CSingleLock lock(m_section);
396 if (m_state.find(surf) != m_state.end())
402 VdpVideoSurface CVideoSurfaces::GetFree(VdpVideoSurface surf)
404 CSingleLock lock(m_section);
405 if (m_state.find(surf) != m_state.end())
407 std::list<VdpVideoSurface>::iterator it;
408 it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
409 if (it == m_freeSurfaces.end())
411 CLog::Log(LOGWARNING, "CVideoSurfaces::GetFree - surface not free");
415 m_freeSurfaces.erase(it);
416 m_state[surf] = SURFACE_USED_FOR_REFERENCE;
421 if (!m_freeSurfaces.empty())
423 VdpVideoSurface freeSurf = m_freeSurfaces.front();
424 m_freeSurfaces.pop_front();
425 m_state[freeSurf] = SURFACE_USED_FOR_REFERENCE;
429 return VDP_INVALID_HANDLE;
432 VdpVideoSurface CVideoSurfaces::RemoveNext(bool skiprender)
434 CSingleLock lock(m_section);
435 VdpVideoSurface surf;
436 std::map<VdpVideoSurface, int>::iterator it;
437 for(it = m_state.begin(); it != m_state.end(); ++it)
439 if (skiprender && it->second & SURFACE_USED_FOR_RENDER)
444 std::list<VdpVideoSurface>::iterator it2;
445 it2 = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
446 if (it2 != m_freeSurfaces.end())
447 m_freeSurfaces.erase(it2);
450 return VDP_INVALID_HANDLE;
453 void CVideoSurfaces::Reset()
455 CSingleLock lock(m_section);
456 m_freeSurfaces.clear();
460 int CVideoSurfaces::Size()
462 CSingleLock lock(m_section);
463 return m_state.size();
466 //-----------------------------------------------------------------------------
468 //-----------------------------------------------------------------------------
470 CDecoder::CDecoder() : m_vdpauOutput(&m_inMsgEvent)
472 m_vdpauConfig.videoSurfaces = &m_videoSurfaces;
474 m_vdpauConfigured = false;
475 m_DisplayState = VDPAU_OPEN;
476 m_vdpauConfig.context = 0;
479 bool CDecoder::Open(AVCodecContext* avctx, AVCodecContext* mainctx, const enum PixelFormat fmt, unsigned int surfaces)
481 // check if user wants to decode this format with VDPAU
482 std::string gpuvendor = g_Windowing.GetRenderVendor();
483 std::transform(gpuvendor.begin(), gpuvendor.end(), gpuvendor.begin(), ::tolower);
484 // nvidia is whitelisted despite for mpeg-4 we need to query user settings
485 if ((gpuvendor.compare(0, 6, "nvidia") != 0) || (avctx->codec_id == AV_CODEC_ID_MPEG4) || (avctx->codec_id == AV_CODEC_ID_H263))
487 std::map<AVCodecID, std::string> settings_map = {
488 { AV_CODEC_ID_H263, CSettings::SETTING_VIDEOPLAYER_USEVDPAUMPEG4 },
489 { AV_CODEC_ID_MPEG4, CSettings::SETTING_VIDEOPLAYER_USEVDPAUMPEG4 },
490 { AV_CODEC_ID_WMV3, CSettings::SETTING_VIDEOPLAYER_USEVDPAUVC1 },
491 { AV_CODEC_ID_VC1, CSettings::SETTING_VIDEOPLAYER_USEVDPAUVC1 },
492 { AV_CODEC_ID_MPEG2VIDEO, CSettings::SETTING_VIDEOPLAYER_USEVDPAUMPEG2 },
494 if (CDVDVideoCodec::IsCodecDisabled(settings_map, avctx->codec_id))
498 #ifndef GL_NV_vdpau_interop
499 CLog::Log(LOGNOTICE, "VDPAU: compilation without required extension GL_NV_vdpau_interop");
502 if (!g_Windowing.IsExtSupported("GL_NV_vdpau_interop"))
504 CLog::Log(LOGNOTICE, "VDPAU::Open: required extension GL_NV_vdpau_interop not found");
508 if(avctx->coded_width == 0
509 || avctx->coded_height == 0)
511 CLog::Log(LOGWARNING,"VDPAU::Open: no width/height available, can't init");
514 m_vdpauConfig.numRenderBuffers = surfaces;
515 m_decoderThread = CThread::GetCurrentThreadId();
517 if (!CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
520 m_DisplayState = VDPAU_OPEN;
521 m_vdpauConfigured = false;
523 m_presentPicture = 0;
526 VdpDecoderProfile profile = 0;
528 // convert FFMPEG codec ID to VDPAU profile.
529 ReadFormatOf(avctx->codec_id, profile, m_vdpauConfig.vdpChromaType);
533 VdpBool is_supported = false;
534 uint32_t max_level, max_macroblocks, max_width, max_height;
536 // query device capabilities to ensure that VDPAU can handle the requested codec
537 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_query_caps(m_vdpauConfig.context->GetDevice(),
538 profile, &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
540 // test to make sure there is a possibility the codec will work
541 if (CheckStatus(vdp_st, __LINE__))
543 CLog::Log(LOGERROR, "VDPAU::Open: error %s(%d) checking for decoder support", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
547 if (max_width < (uint32_t) avctx->coded_width || max_height < (uint32_t) avctx->coded_height)
549 CLog::Log(LOGWARNING,"VDPAU::Open: requested picture dimensions (%i, %i) exceed hardware capabilities ( %i, %i).",
550 avctx->coded_width, avctx->coded_height, max_width, max_height);
554 if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
555 CLog::Log(LOGWARNING,"VDPAU::Open width %i might not be supported because of hardware bug", avctx->width);
557 // attempt to create a decoder with this width/height, some sizes are not supported by hw
558 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(), profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
560 if (CheckStatus(vdp_st, __LINE__))
562 CLog::Log(LOGERROR, "VDPAU::Open: error: %s(%d) checking for decoder support", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
566 m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
567 CheckStatus(vdp_st, __LINE__);
569 // finally setup ffmpeg
570 memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
571 m_hwContext.render2 = CDecoder::Render;
572 avctx->get_buffer2 = CDecoder::FFGetBuffer;
573 avctx->slice_flags = SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
574 avctx->hwaccel_context = &m_hwContext;
576 mainctx->get_buffer2 = CDecoder::FFGetBuffer;
577 mainctx->slice_flags = SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
578 mainctx->hwaccel_context = &m_hwContext;
580 g_Windowing.Register(this);
587 CDecoder::~CDecoder()
592 void CDecoder::Close()
594 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
596 g_Windowing.Unregister(this);
598 CSingleLock lock(m_DecoderSection);
601 m_vdpauOutput.Dispose();
603 if (m_vdpauConfig.context)
604 m_vdpauConfig.context->Release();
605 m_vdpauConfig.context = 0;
608 long CDecoder::Release()
610 // check if we should do some pre-cleanup here
611 // a second decoder might need resources
612 if (m_vdpauConfigured == true)
614 CSingleLock lock(m_DecoderSection);
615 CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
618 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
622 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
626 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
627 m_DisplayState = VDPAU_ERROR;
632 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
633 m_DisplayState = VDPAU_ERROR;
636 VdpVideoSurface surf;
637 while((surf = m_videoSurfaces.RemoveNext(true)) != VDP_INVALID_HANDLE)
639 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
642 return IHardwareDecoder::Release();
645 long CDecoder::ReleasePicReference()
647 return IHardwareDecoder::Release();
650 void CDecoder::SetWidthHeight(int width, int height)
652 m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
654 //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
655 //this requires the least amount of gpu memory bandwidth
656 if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
658 //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
659 if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
661 m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
662 m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
664 else //scale height to the desktop size if the aspect ratio is smaller than the desktop
666 m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
667 m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
672 m_vdpauConfig.outWidth = width;
673 m_vdpauConfig.outHeight = height;
675 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
676 CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
679 void CDecoder::OnLostDevice()
681 CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
683 int count = g_graphicsContext.exit();
685 CSingleLock lock(m_DecoderSection);
687 if (m_vdpauConfig.context)
688 m_vdpauConfig.context->Release();
689 m_vdpauConfig.context = 0;
691 m_DisplayState = VDPAU_LOST;
693 m_DisplayEvent.Reset();
695 g_graphicsContext.restore(count);
698 void CDecoder::OnResetDevice()
700 CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
702 int count = g_graphicsContext.exit();
704 CSingleLock lock(m_DecoderSection);
705 if (m_DisplayState == VDPAU_LOST)
707 m_DisplayState = VDPAU_RESET;
709 m_DisplayEvent.Set();
712 g_graphicsContext.restore(count);
715 int CDecoder::Check(AVCodecContext* avctx)
719 { CSingleLock lock(m_DecoderSection);
720 state = m_DisplayState;
723 if (state == VDPAU_LOST)
725 CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
726 if (!m_DisplayEvent.WaitMSec(4000))
728 CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
733 CSingleLock lock(m_DecoderSection);
734 state = m_DisplayState;
737 if (state == VDPAU_RESET || state == VDPAU_ERROR)
739 CSingleLock lock(m_DecoderSection);
742 if (m_vdpauConfig.context)
743 m_vdpauConfig.context->Release();
744 m_vdpauConfig.context = 0;
746 if (CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
748 m_DisplayState = VDPAU_OPEN;
749 m_vdpauConfigured = false;
752 if (state == VDPAU_RESET)
760 bool CDecoder::IsVDPAUFormat(PixelFormat format)
762 if (format == AV_PIX_FMT_VDPAU)
768 bool CDecoder::Supports(VdpVideoMixerFeature feature)
770 return m_vdpauConfig.context->Supports(feature);
773 bool CDecoder::Supports(EINTERLACEMETHOD method)
775 if(method == VS_INTERLACEMETHOD_VDPAU_BOB
776 || method == VS_INTERLACEMETHOD_AUTO)
779 if (method == VS_INTERLACEMETHOD_RENDER_BOB)
782 if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
785 for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
787 if(p->method == method)
788 return Supports(p->feature);
793 EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
795 return VS_INTERLACEMETHOD_RENDER_BOB;
798 void CDecoder::FiniVDPAUOutput()
800 if (!m_vdpauConfigured)
803 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
806 m_vdpauOutput.Dispose();
807 m_vdpauConfigured = false;
811 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
812 if (CheckStatus(vdp_st, __LINE__))
814 m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
816 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
817 CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", m_videoSurfaces.Size());
819 VdpVideoSurface surf;
820 while((surf = m_videoSurfaces.RemoveNext()) != VDP_INVALID_HANDLE)
822 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
823 if (CheckStatus(vdp_st, __LINE__))
826 m_videoSurfaces.Reset();
829 void CDecoder::ReadFormatOf( AVCodecID codec
830 , VdpDecoderProfile &vdp_decoder_profile
831 , VdpChromaType &vdp_chroma_type)
835 case AV_CODEC_ID_MPEG1VIDEO:
836 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
837 vdp_chroma_type = VDP_CHROMA_TYPE_420;
839 case AV_CODEC_ID_MPEG2VIDEO:
840 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
841 vdp_chroma_type = VDP_CHROMA_TYPE_420;
843 case AV_CODEC_ID_H264:
844 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
845 vdp_chroma_type = VDP_CHROMA_TYPE_420;
847 #ifdef VDP_DECODER_PROFILE_HEVC_MAIN
848 case AV_CODEC_ID_HEVC:
849 vdp_decoder_profile = VDP_DECODER_PROFILE_HEVC_MAIN;
850 vdp_chroma_type = VDP_CHROMA_TYPE_420;
853 case AV_CODEC_ID_WMV3:
854 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
855 vdp_chroma_type = VDP_CHROMA_TYPE_420;
857 case AV_CODEC_ID_VC1:
858 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
859 vdp_chroma_type = VDP_CHROMA_TYPE_420;
861 case AV_CODEC_ID_MPEG4:
862 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
863 vdp_chroma_type = VDP_CHROMA_TYPE_420;
866 vdp_decoder_profile = 0;
872 bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
877 VdpDecoderProfile vdp_decoder_profile;
879 m_vdpauConfig.vidWidth = avctx->width;
880 m_vdpauConfig.vidHeight = avctx->height;
881 m_vdpauConfig.surfaceWidth = avctx->coded_width;
882 m_vdpauConfig.surfaceHeight = avctx->coded_height;
884 SetWidthHeight(avctx->width,avctx->height);
886 CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
887 CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
889 ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
891 if (avctx->codec_id == AV_CODEC_ID_H264)
893 m_vdpauConfig.maxReferences = ref_frames;
894 if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
895 if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
897 else if (avctx->codec_id == AV_CODEC_ID_HEVC)
899 // The DPB works quite differently in hevc and there isn't a per-file max
900 // reference number, so we force the maximum number (source: upstream ffmpeg)
901 m_vdpauConfig.maxReferences = 16;
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;
950 int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic, int flags)
952 //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
953 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
954 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
956 // while we are waiting to recover we can't do anything
957 CSingleLock lock(vdp->m_DecoderSection);
959 if(vdp->m_DisplayState != VDPAU_OPEN)
961 CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
965 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
966 surf = vdp->m_videoSurfaces.GetFree(surf != 0 ? surf : VDP_INVALID_HANDLE);
968 VdpStatus vdp_st = VDP_STATUS_ERROR;
969 if (surf == VDP_INVALID_HANDLE)
971 // create a new surface
972 VdpDecoderProfile profile;
973 ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
975 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_video_surface_create(vdp->m_vdpauConfig.context->GetDevice(),
976 vdp->m_vdpauConfig.vdpChromaType,
980 vdp->CheckStatus(vdp_st, __LINE__);
981 if (vdp_st != VDP_STATUS_OK)
983 CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
986 vdp->m_videoSurfaces.AddSurface(surf);
989 pic->data[1] = pic->data[2] = NULL;
990 pic->data[0] = (uint8_t*)(uintptr_t)surf;
991 pic->data[3] = (uint8_t*)(uintptr_t)surf;
992 pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
993 AVBufferRef *buffer = av_buffer_create(pic->data[3], 0, FFReleaseBuffer, ctx, 0);
996 CLog::Log(LOGERROR, "CVDPAU::%s - error creating buffer", __FUNCTION__);
999 pic->buf[0] = buffer;
1001 pic->reordered_opaque= avctx->reordered_opaque;
1005 void CDecoder::FFReleaseBuffer(void *opaque, uint8_t *data)
1007 CDecoder *vdp = (CDecoder*)((CDVDVideoCodecFFmpeg*)opaque)->GetHardware();
1009 VdpVideoSurface surf;
1011 CSingleLock lock(vdp->m_DecoderSection);
1013 surf = (VdpVideoSurface)(uintptr_t)data;
1015 vdp->m_videoSurfaces.ClearReference(surf);
1018 int CDecoder::Render(struct AVCodecContext *s, struct AVFrame *src,
1019 const VdpPictureInfo *info, uint32_t buffers_used,
1020 const VdpBitstreamBuffer *buffers)
1022 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
1023 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
1025 // while we are waiting to recover we can't do anything
1026 CSingleLock lock(vdp->m_DecoderSection);
1028 if(vdp->m_DisplayState != VDPAU_OPEN)
1031 if(src->linesize[0] || src->linesize[1] || src->linesize[2])
1033 CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
1038 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
1040 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1041 if (!vdp->m_videoSurfaces.IsValid(surf))
1043 CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
1047 uint32_t max_refs = 0;
1048 if(s->codec_id == AV_CODEC_ID_H264)
1051 if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
1052 || vdp->m_vdpauConfigured == false
1053 || vdp->m_vdpauConfig.maxReferences < max_refs)
1055 if(!vdp->ConfigVDPAU(s, max_refs))
1059 uint64_t startTime = CurrentHostCounter();
1060 uint16_t decoded, processed, rend;
1061 vdp->m_bufferStats.Get(decoded, processed, rend);
1062 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
1063 surf, info, buffers_used, buffers);
1064 if (vdp->CheckStatus(vdp_st, __LINE__))
1067 uint64_t diff = CurrentHostCounter() - startTime;
1068 if (diff*1000/CurrentHostFrequency() > 30)
1070 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
1071 CLog::Log(LOGDEBUG, "CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
1078 int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
1080 int result = Check(avctx);
1084 CSingleLock lock(m_DecoderSection);
1086 if (!m_vdpauConfigured)
1090 { // we have a new frame from decoder
1092 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pFrame->data[3];
1093 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1094 if (!m_videoSurfaces.IsValid(surf))
1096 CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
1099 m_videoSurfaces.MarkRender(surf);
1101 // send frame to output for processing
1102 CVdpauDecodedPicture pic;
1103 memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
1104 ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
1105 pic.videoSurface = surf;
1106 pic.DVDPic.color_matrix = avctx->colorspace;
1107 m_bufferStats.IncDecoded();
1108 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
1110 m_codecControl = pic.DVDPic.iFlags & (DVD_CODEC_CTRL_DRAIN | DVD_CODEC_CTRL_NO_POSTPROC);
1114 uint16_t decoded, processed, render;
1116 while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1118 if (msg->signal == COutputControlProtocol::ERROR)
1120 m_DisplayState = VDPAU_ERROR;
1126 m_bufferStats.Get(decoded, processed, render);
1128 uint64_t startTime = CurrentHostCounter();
1131 // first fill the buffers to keep vdpau busy
1132 // mixer will run with decoded >= 2. output is limited by number of output surfaces
1133 // In case mixer is bypassed we limit by looking at processed
1134 if (decoded < 3 && processed < 3)
1136 retval |= VC_BUFFER;
1138 else if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
1140 if (msg->signal == COutputDataProtocol::PICTURE)
1142 if (m_presentPicture)
1144 m_presentPicture->ReturnUnused();
1145 m_presentPicture = 0;
1148 m_presentPicture = *(CVdpauRenderPicture**)msg->data;
1149 m_presentPicture->vdpau = this;
1150 m_bufferStats.DecRender();
1151 m_bufferStats.Get(decoded, processed, render);
1152 retval |= VC_PICTURE;
1158 else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1160 if (msg->signal == COutputControlProtocol::STATS)
1162 m_bufferStats.Get(decoded, processed, render);
1166 m_DisplayState = VDPAU_ERROR;
1172 if (decoded < 3 && processed < 3)
1174 retval |= VC_BUFFER;
1177 if (!retval && !m_inMsgEvent.WaitMSec(2000))
1180 uint64_t diff = CurrentHostCounter() - startTime;
1181 if (retval & VC_PICTURE)
1183 m_bufferStats.SetParams(diff, m_codecControl);
1185 if (diff*1000/CurrentHostFrequency() > 50)
1187 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
1188 CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
1193 CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
1194 m_DisplayState = VDPAU_ERROR;
1201 bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
1203 CSingleLock lock(m_DecoderSection);
1205 if (m_DisplayState != VDPAU_OPEN)
1208 *picture = m_presentPicture->DVDPic;
1209 picture->vdpau = m_presentPicture;
1214 void CDecoder::Reset()
1216 CSingleLock lock(m_DecoderSection);
1218 if (!m_vdpauConfigured)
1222 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
1226 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
1230 CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
1231 m_DisplayState = VDPAU_ERROR;
1234 m_bufferStats.Reset();
1238 CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
1239 m_DisplayState = VDPAU_ERROR;
1243 bool CDecoder::CanSkipDeint()
1245 return m_bufferStats.CanSkipDeint();
1248 void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
1250 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
1253 bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
1255 if (vdp_st != VDP_STATUS_OK)
1257 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);
1261 if(m_DisplayState == VDPAU_OPEN)
1263 if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
1265 m_DisplayEvent.Reset();
1266 m_DisplayState = VDPAU_LOST;
1268 else if (m_ErrorCount > 2)
1269 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 = m_config.context->GetProcs().vdp_video_mixer_create(m_config.context->GetDevice(),
1704 m_config.context->GetFeatureCount(),
1705 m_config.context->GetFeatures(),
1710 CheckStatus(vdp_st, __LINE__);
1714 void CMixer::InitCSCMatrix(int Width)
1716 m_Procamp.struct_version = VDP_PROCAMP_VERSION;
1717 m_Procamp.brightness = 0.0;
1718 m_Procamp.contrast = 1.0;
1719 m_Procamp.saturation = 1.0;
1723 void CMixer::CheckFeatures()
1725 if (m_Upscale != m_config.upscale)
1728 m_Upscale = m_config.upscale;
1730 if (m_Brightness != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Brightness ||
1731 m_Contrast != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Contrast ||
1732 m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
1735 m_Brightness = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Brightness;
1736 m_Contrast = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Contrast;
1737 m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
1739 if (m_NoiseReduction != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_NoiseReduction)
1741 m_NoiseReduction = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_NoiseReduction;
1742 SetNoiseReduction();
1744 if (m_Sharpness != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Sharpness)
1746 m_Sharpness = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Sharpness;
1749 if (m_DeintMode != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_DeinterlaceMode ||
1750 m_Deint != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_InterlaceMethod)
1752 m_DeintMode = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_DeinterlaceMode;
1753 m_Deint = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_InterlaceMethod;
1758 void CMixer::SetPostProcFeatures(bool postProcEnabled)
1760 if (m_PostProc != postProcEnabled)
1762 if (postProcEnabled)
1764 SetNoiseReduction();
1771 m_PostProc = postProcEnabled;
1775 void CMixer::PostProcOff()
1779 if (m_videoMixer == VDP_INVALID_HANDLE)
1782 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1783 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1784 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE};
1786 VdpBool enabled[]={0,0,0};
1787 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1788 CheckStatus(vdp_st, __LINE__);
1790 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1792 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION};
1794 VdpBool enabled[]={0};
1795 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1796 CheckStatus(vdp_st, __LINE__);
1799 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1801 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS};
1803 VdpBool enabled[]={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__);
1811 bool CMixer::GenerateStudioCSCMatrix(VdpColorStandard colorStandard, VdpCSCMatrix &studioCSCMatrix)
1813 // instead use studioCSCKCoeffs601[3], studioCSCKCoeffs709[3] to generate float[3][4] matrix (float studioCSC[3][4])
1814 // m00 = mRY = red: luma factor (contrast factor) (1.0)
1815 // m10 = mGY = green: luma factor (contrast factor) (1.0)
1816 // m20 = mBY = blue: luma factor (contrast factor) (1.0)
1818 // m01 = mRB = red: blue color diff coeff (0.0)
1819 // m11 = mGB = green: blue color diff coeff (-2Kb(1-Kb)/(Kg))
1820 // m21 = mBB = blue: blue color diff coeff ((1-Kb)/0.5)
1822 // m02 = mRR = red: red color diff coeff ((1-Kr)/0.5)
1823 // m12 = mGR = green: red color diff coeff (-2Kr(1-Kr)/(Kg))
1824 // m22 = mBR = blue: red color diff coeff (0.0)
1826 // m03 = mRC = red: colour zero offset (brightness factor) (-(1-Kr)/0.5 * (128/255))
1827 // m13 = mGC = green: colour zero offset (brightness factor) ((256/255) * (Kb(1-Kb) + Kr(1-Kr)) / Kg)
1828 // m23 = mBC = blue: colour zero offset (brightness factor) (-(1-Kb)/0.5 * (128/255))
1839 // colour standard coefficients for red, geen, blue
1841 // colour diff zero position (use standard 8-bit coding precision)
1842 double CDZ = 128; //256*0.5
1843 // range excursion (use standard 8-bit coding precision)
1844 double EXC = 255; //256-1
1846 if (colorStandard == VDP_COLOR_STANDARD_ITUR_BT_601)
1848 Kr = studioCSCKCoeffs601[0];
1849 Kg = studioCSCKCoeffs601[1];
1850 Kb = studioCSCKCoeffs601[2];
1852 else // assume VDP_COLOR_STANDARD_ITUR_BT_709
1854 Kr = studioCSCKCoeffs709[0];
1855 Kg = studioCSCKCoeffs709[1];
1856 Kb = studioCSCKCoeffs709[2];
1858 // we keep luma unscaled to retain the levels present in source so that 16-235 luma is converted to RGB 16-235
1859 studioCSCMatrix[R][Y] = 1.0;
1860 studioCSCMatrix[G][Y] = 1.0;
1861 studioCSCMatrix[B][Y] = 1.0;
1863 studioCSCMatrix[R][Cb] = 0.0;
1864 studioCSCMatrix[G][Cb] = (double)-2 * Kb * (1 - Kb) / Kg;
1865 studioCSCMatrix[B][Cb] = (double)(1 - Kb) / 0.5;
1867 studioCSCMatrix[R][Cr] = (double)(1 - Kr) / 0.5;
1868 studioCSCMatrix[G][Cr] = (double)-2 * Kr * (1 - Kr) / Kg;
1869 studioCSCMatrix[B][Cr] = 0.0;
1871 studioCSCMatrix[R][C] = (double)-1 * studioCSCMatrix[R][Cr] * CDZ/EXC;
1872 studioCSCMatrix[G][C] = (double)-1 * (studioCSCMatrix[G][Cb] + studioCSCMatrix[G][Cr]) * CDZ/EXC;
1873 studioCSCMatrix[B][C] = (double)-1 * studioCSCMatrix[B][Cb] * CDZ/EXC;
1878 void CMixer::SetColor()
1882 if (m_Brightness != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Brightness)
1883 m_Procamp.brightness = (float)((CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Brightness)-50) / 100;
1884 if (m_Contrast != CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Contrast)
1885 m_Procamp.contrast = (float)((CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Contrast)+50) / 100;
1887 VdpColorStandard colorStandard;
1888 switch(m_mixerInput[1].DVDPic.color_matrix)
1890 case AVCOL_SPC_BT709:
1891 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1893 case AVCOL_SPC_BT470BG:
1894 case AVCOL_SPC_SMPTE170M:
1895 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1897 case AVCOL_SPC_SMPTE240M:
1898 colorStandard = VDP_COLOR_STANDARD_SMPTE_240M;
1901 case AVCOL_SPC_UNSPECIFIED:
1904 if(m_config.surfaceWidth > 1000)
1905 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1907 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1910 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX };
1911 if (CSettings::GetInstance().GetBool(CSettings::SETTING_VIDEOSCREEN_LIMITEDRANGE))
1913 float studioCSC[3][4];
1914 GenerateStudioCSCMatrix(colorStandard, studioCSC);
1915 void const * pm_CSCMatix[] = { &studioCSC };
1916 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1920 vdp_st = m_config.context->GetProcs().vdp_generate_csc_matrix(&m_Procamp, colorStandard, &m_CSCMatrix);
1921 if(vdp_st != VDP_STATUS_ERROR)
1923 void const * pm_CSCMatix[] = { &m_CSCMatrix };
1924 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1928 CheckStatus(vdp_st, __LINE__);
1931 void CMixer::SetNoiseReduction()
1933 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1936 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION };
1937 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL };
1940 if (!CMediaSettings::GetInstance().GetCurrentVideoSettings().m_NoiseReduction)
1942 VdpBool enabled[]= {0};
1943 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1944 CheckStatus(vdp_st, __LINE__);
1947 VdpBool enabled[]={1};
1948 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1949 CheckStatus(vdp_st, __LINE__);
1950 void* nr[] = { &CMediaSettings::GetInstance().GetCurrentVideoSettings().m_NoiseReduction };
1951 CLog::Log(LOGNOTICE,"Setting Noise Reduction to %f",CMediaSettings::GetInstance().GetCurrentVideoSettings().m_NoiseReduction);
1952 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, nr);
1953 CheckStatus(vdp_st, __LINE__);
1956 void CMixer::SetSharpness()
1958 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1961 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS };
1962 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL };
1965 if (!CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Sharpness)
1967 VdpBool enabled[]={0};
1968 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1969 CheckStatus(vdp_st, __LINE__);
1972 VdpBool enabled[]={1};
1973 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1974 CheckStatus(vdp_st, __LINE__);
1975 void* sh[] = { &CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Sharpness };
1976 CLog::Log(LOGNOTICE,"Setting Sharpness to %f",CMediaSettings::GetInstance().GetCurrentVideoSettings().m_Sharpness);
1977 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, sh);
1978 CheckStatus(vdp_st, __LINE__);
1981 EINTERLACEMETHOD CMixer::GetDeinterlacingMethod(bool log /* = false */)
1983 EINTERLACEMETHOD method = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_InterlaceMethod;
1984 if (method == VS_INTERLACEMETHOD_AUTO)
1987 // if (m_config.outHeight >= 720)
1988 // deint = g_advancedSettings.m_videoVDPAUdeintHD;
1990 // deint = g_advancedSettings.m_videoVDPAUdeintSD;
1994 if (m_config.vdpau->Supports(EINTERLACEMETHOD(deint)))
1996 method = EINTERLACEMETHOD(deint);
1998 CLog::Log(LOGNOTICE, "CVDPAU::GetDeinterlacingMethod: set de-interlacing to %d", deint);
2003 CLog::Log(LOGWARNING, "CVDPAU::GetDeinterlacingMethod: method for de-interlacing (advanced settings) not supported");
2010 void CMixer::SetDeinterlacing()
2014 if (m_videoMixer == VDP_INVALID_HANDLE)
2017 EDEINTERLACEMODE mode = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_DeinterlaceMode;
2018 EINTERLACEMETHOD method = GetDeinterlacingMethod(true);
2020 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
2021 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
2022 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE };
2024 if (mode == VS_DEINTERLACEMODE_OFF)
2026 VdpBool enabled[] = {0,0,0};
2027 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2031 if (method == VS_INTERLACEMETHOD_AUTO)
2033 VdpBool enabled[] = {1,0,0};
2034 if (g_advancedSettings.m_videoVDPAUtelecine)
2036 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2038 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2039 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF)
2041 VdpBool enabled[] = {1,0,0};
2042 if (g_advancedSettings.m_videoVDPAUtelecine)
2044 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2046 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2047 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF)
2049 VdpBool enabled[] = {1,1,0};
2050 if (g_advancedSettings.m_videoVDPAUtelecine)
2052 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2056 VdpBool enabled[]={0,0,0};
2057 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2060 CheckStatus(vdp_st, __LINE__);
2062 SetDeintSkipChroma();
2064 m_config.useInteropYuv = !CSettings::GetInstance().GetBool(CSettings::SETTING_VIDEOPLAYER_USEVDPAUMIXER);
2067 void CMixer::SetDeintSkipChroma()
2069 VdpVideoMixerAttribute attribute[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
2073 if (g_advancedSettings.m_videoVDPAUdeintSkipChromaHD && m_config.outHeight >= 720)
2078 void const *values[]={&val};
2079 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attribute), attribute, values);
2081 CheckStatus(vdp_st, __LINE__);
2084 void CMixer::SetHWUpscaling()
2086 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
2089 VdpBool enabled[]={1};
2090 switch (m_config.upscale)
2093 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2095 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2096 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2100 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2102 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2103 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2107 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2109 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
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_L6))
2116 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
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_L5))
2123 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
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_L4))
2130 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
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_L3))
2137 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
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_L2))
2144 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
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_L1))
2151 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2152 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2159 CheckStatus(vdp_st, __LINE__);
2163 void CMixer::DisableHQScaling()
2167 if (m_videoMixer == VDP_INVALID_HANDLE)
2170 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2172 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2173 VdpBool enabled[]={0};
2174 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2175 CheckStatus(vdp_st, __LINE__);
2178 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2180 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2181 VdpBool enabled[]={0};
2182 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2183 CheckStatus(vdp_st, __LINE__);
2186 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2188 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2189 VdpBool enabled[]={0};
2190 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2191 CheckStatus(vdp_st, __LINE__);
2194 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2196 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2197 VdpBool enabled[]={0};
2198 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2199 CheckStatus(vdp_st, __LINE__);
2202 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2204 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2205 VdpBool enabled[]={0};
2206 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2207 CheckStatus(vdp_st, __LINE__);
2210 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2212 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2213 VdpBool enabled[]={0};
2214 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2215 CheckStatus(vdp_st, __LINE__);
2218 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2220 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2221 VdpBool enabled[]={0};
2222 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2223 CheckStatus(vdp_st, __LINE__);
2226 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2228 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2229 VdpBool enabled[]={0};
2230 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2231 CheckStatus(vdp_st, __LINE__);
2234 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2236 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2237 VdpBool enabled[]={0};
2238 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2239 CheckStatus(vdp_st, __LINE__);
2247 m_NoiseReduction = 0.0;
2252 m_SeenInterlaceFlag = false;
2257 m_config.upscale = g_advancedSettings.m_videoVDPAUScaling;
2258 m_config.useInteropYuv = !CSettings::GetInstance().GetBool(CSettings::SETTING_VIDEOPLAYER_USEVDPAUMIXER);
2263 void CMixer::Uninit()
2266 while (!m_outputSurfaces.empty())
2268 m_outputSurfaces.pop();
2270 m_config.context->GetProcs().vdp_video_mixer_destroy(m_videoMixer);
2273 void CMixer::Flush()
2275 while (!m_mixerInput.empty())
2277 CVdpauDecodedPicture pic = m_mixerInput.back();
2278 m_mixerInput.pop_back();
2279 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2281 while (!m_decodedPics.empty())
2283 CVdpauDecodedPicture pic = m_decodedPics.front();
2284 m_decodedPics.pop();
2285 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2288 while (m_dataPort.ReceiveOutMessage(&msg))
2290 if (msg->signal == CMixerDataProtocol::FRAME)
2292 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2293 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2295 else if (msg->signal == CMixerDataProtocol::BUFFER)
2297 VdpOutputSurface *surf;
2298 surf = (VdpOutputSurface*)msg->data;
2299 m_outputSurfaces.push(*surf);
2305 void CMixer::InitCycle()
2310 m_config.stats->GetParams(latency, flags);
2311 if (flags & DVD_CODEC_CTRL_NO_POSTPROC)
2312 SetPostProcFeatures(false);
2314 SetPostProcFeatures(true);
2316 m_config.stats->SetCanSkipDeint(false);
2318 EDEINTERLACEMODE mode = CMediaSettings::GetInstance().GetCurrentVideoSettings().m_DeinterlaceMode;
2319 EINTERLACEMETHOD method = GetDeinterlacingMethod();
2320 bool interlaced = m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_INTERLACED;
2321 m_SeenInterlaceFlag |= interlaced;
2323 if (!(flags & DVD_CODEC_CTRL_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);
2345 if (m_mixerInput[1].DVDPic.iFlags & DVD_CODEC_CTRL_SKIPDEINT)
2350 if(m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_TOP_FIELD_FIRST)
2351 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2353 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2355 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2356 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2357 DVP_FLAG_REPEAT_TOP_FIELD |
2358 DVP_FLAG_INTERLACED);
2359 m_config.useInteropYuv = false;
2361 else if (method == VS_INTERLACEMETHOD_RENDER_BOB)
2364 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2365 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2366 m_config.useInteropYuv = true;
2370 CLog::Log(LOGERROR, "CMixer::%s - interlace method: %d not supported, setting to AUTO", __FUNCTION__, method);
2372 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2373 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2374 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2375 DVP_FLAG_REPEAT_TOP_FIELD |
2376 DVP_FLAG_INTERLACED);
2378 CMediaSettings::GetInstance().GetCurrentVideoSettings().m_InterlaceMethod = VS_INTERLACEMETHOD_AUTO;
2384 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2386 if (m_config.useInteropYuv)
2387 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
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);
2398 m_processPicture.crop = false;
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.iDisplayHeight = lrint(ratio*(m_mixerInput[1].DVDPic.iHeight-NUM_CROP_PIX*2));
2408 m_processPicture.crop = true;
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 while(ProcessSyncPicture())
2958 ReleaseBufferPool();
2959 DestroyGlxContext();
2963 void COutput::Flush()
2965 if (m_mixer.IsActive())
2968 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::FLUSH,
2975 CLog::Log(LOGERROR, "Coutput::%s - failed to flush mixer", __FUNCTION__);
2979 while (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2981 if (msg->signal == CMixerDataProtocol::PICTURE)
2983 CVdpauProcessedPicture pic = *(CVdpauProcessedPicture*)msg->data;
2984 m_bufferPool.processedPics.push(pic);
2989 while (m_dataPort.ReceiveOutMessage(&msg))
2991 if (msg->signal == COutputDataProtocol::NEWFRAME)
2993 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2994 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2996 else if (msg->signal == COutputDataProtocol::RETURNPIC)
2998 CVdpauRenderPicture *pic;
2999 pic = *((CVdpauRenderPicture**)msg->data);
3000 QueueReturnPicture(pic);
3005 while (m_dataPort.ReceiveInMessage(&msg))
3007 if (msg->signal == COutputDataProtocol::PICTURE)
3009 CVdpauRenderPicture *pic;
3010 pic = *((CVdpauRenderPicture**)msg->data);
3011 QueueReturnPicture(pic);
3015 // reset used render flag which was cleared on mixer flush
3016 std::deque<int>::iterator it;
3017 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3019 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[*it];
3020 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3022 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it2;
3023 it2 = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3024 if (it2 == m_bufferPool.glVideoSurfaceMap.end())
3026 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
3027 CLog::Log(LOGDEBUG, "COutput::Flush - gl surface not found");
3031 m_config.videoSurfaces->MarkRender(it2->second.sourceVuv);
3035 // clear processed pics
3036 while(!m_bufferPool.processedPics.empty())
3038 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3039 if (procPic.DVDPic.format == RENDER_FMT_VDPAU)
3041 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &procPic.outputSurface, sizeof(procPic.outputSurface));
3043 else if (procPic.DVDPic.format == RENDER_FMT_VDPAU_420)
3045 m_config.videoSurfaces->ClearRender(procPic.videoSurface);
3047 m_bufferPool.processedPics.pop();
3051 bool COutput::HasWork()
3053 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3058 CVdpauRenderPicture* COutput::ProcessMixerPicture()
3060 CVdpauRenderPicture *retPic = NULL;
3062 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3064 int idx = m_bufferPool.freeRenderPics.front();
3065 retPic = m_bufferPool.allRenderPics[idx];
3066 m_bufferPool.freeRenderPics.pop_front();
3067 m_bufferPool.usedRenderPics.push_back(idx);
3068 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3069 m_bufferPool.processedPics.pop();
3071 retPic->DVDPic = procPic.DVDPic;
3072 retPic->valid = true;
3073 if (retPic->DVDPic.format == RENDER_FMT_VDPAU)
3075 m_config.useInteropYuv = false;
3076 m_bufferPool.numOutputSurfaces = NUM_RENDER_PICS;
3078 GLMapSurface(false, procPic.outputSurface);
3079 retPic->sourceIdx = procPic.outputSurface;
3080 retPic->texture[0] = m_bufferPool.glOutputSurfaceMap[procPic.outputSurface].texture[0];
3081 retPic->texWidth = m_config.outWidth;
3082 retPic->texHeight = m_config.outHeight;
3083 retPic->crop.x1 = 0;
3084 retPic->crop.y1 = procPic.crop ? NUM_CROP_PIX : 0;
3085 retPic->crop.x2 = m_config.outWidth;
3086 retPic->crop.y2 = m_config.outHeight - retPic->crop.y1;
3090 m_config.useInteropYuv = true;
3091 GLMapSurface(true, procPic.videoSurface);
3092 retPic->sourceIdx = procPic.videoSurface;
3093 for (unsigned int i=0; i<4; ++i)
3094 retPic->texture[i] = m_bufferPool.glVideoSurfaceMap[procPic.videoSurface].texture[i];
3095 retPic->texWidth = m_config.surfaceWidth;
3096 retPic->texHeight = m_config.surfaceHeight;
3097 retPic->crop.x1 = 0;
3098 retPic->crop.y1 = 0;
3099 retPic->crop.x2 = m_config.surfaceWidth - m_config.vidWidth;
3100 retPic->crop.y2 = m_config.surfaceHeight - m_config.vidHeight;
3106 void COutput::QueueReturnPicture(CVdpauRenderPicture *pic)
3108 std::deque<int>::iterator it;
3109 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3111 if (m_bufferPool.allRenderPics[*it] == pic)
3117 if (it == m_bufferPool.usedRenderPics.end())
3119 CLog::Log(LOGWARNING, "COutput::QueueReturnPicture - pic not found");
3123 // check if already queued
3124 std::deque<int>::iterator it2 = find(m_bufferPool.syncRenderPics.begin(),
3125 m_bufferPool.syncRenderPics.end(),
3127 if (it2 == m_bufferPool.syncRenderPics.end())
3129 m_bufferPool.syncRenderPics.push_back(*it);
3132 ProcessSyncPicture();
3135 bool COutput::ProcessSyncPicture()
3137 CVdpauRenderPicture *pic;
3140 std::deque<int>::iterator it;
3141 for (it = m_bufferPool.syncRenderPics.begin(); it != m_bufferPool.syncRenderPics.end(); )
3143 pic = m_bufferPool.allRenderPics[*it];
3148 if (glIsSync(pic->fence))
3152 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3153 if(state == GL_SIGNALED)
3155 glDeleteSync(pic->fence);
3168 m_bufferPool.freeRenderPics.push_back(*it);
3170 std::deque<int>::iterator it2 = find(m_bufferPool.usedRenderPics.begin(),
3171 m_bufferPool.usedRenderPics.end(),
3173 if (it2 == m_bufferPool.usedRenderPics.end())
3175 CLog::Log(LOGERROR, "COutput::ProcessSyncPicture - pic not found in queue");
3179 m_bufferPool.usedRenderPics.erase(it2);
3181 it = m_bufferPool.syncRenderPics.erase(it);
3185 ProcessReturnPicture(pic);
3189 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
3190 CLog::Log(LOGDEBUG, "COutput::%s - return of invalid render pic", __FUNCTION__);
3196 void COutput::ProcessReturnPicture(CVdpauRenderPicture *pic)
3198 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3200 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3201 it = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3202 if (it == m_bufferPool.glVideoSurfaceMap.end())
3204 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
3205 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3209 #ifdef GL_NV_vdpau_interop
3210 glVDPAUUnmapSurfacesNV(1, &(it->second.glVdpauSurface));
3212 VdpVideoSurface surf = it->second.sourceVuv;
3213 m_config.videoSurfaces->ClearRender(surf);
3215 else if (pic->DVDPic.format == RENDER_FMT_VDPAU)
3217 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3218 it = m_bufferPool.glOutputSurfaceMap.find(pic->sourceIdx);
3219 if (it == m_bufferPool.glOutputSurfaceMap.end())
3221 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
3222 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3226 #ifdef GL_NV_vdpau_interop
3227 glVDPAUUnmapSurfacesNV(1, &(it->second.glVdpauSurface));
3229 VdpOutputSurface outSurf = it->second.sourceRgb;
3230 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &outSurf, sizeof(outSurf));
3234 bool COutput::EnsureBufferPool()
3238 // Creation of outputSurfaces
3239 VdpOutputSurface outputSurface;
3240 for (int i = m_bufferPool.outputSurfaces.size(); i < m_bufferPool.numOutputSurfaces; i++)
3242 vdp_st = m_config.context->GetProcs().vdp_output_surface_create(m_config.context->GetDevice(),
3243 VDP_RGBA_FORMAT_B8G8R8A8,
3247 if (CheckStatus(vdp_st, __LINE__))
3249 m_bufferPool.outputSurfaces.push_back(outputSurface);
3251 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3253 sizeof(VdpOutputSurface));
3254 CLog::Log(LOGNOTICE, "VDPAU::COutput::InitBufferPool - Output Surface created");
3259 void COutput::ReleaseBufferPool()
3263 CSingleLock lock(m_bufferPool.renderPicSec);
3265 // release all output surfaces
3266 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3268 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3270 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3271 CheckStatus(vdp_st, __LINE__);
3273 m_bufferPool.outputSurfaces.clear();
3275 // wait for all fences
3276 XbmcThreads::EndTime timeout(1000);
3277 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3279 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[i];
3283 while (glIsSync(pic->fence))
3287 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3288 if(state == GL_SIGNALED || timeout.IsTimePast())
3290 glDeleteSync(pic->fence);
3301 if (timeout.IsTimePast())
3303 CLog::Log(LOGERROR, "COutput::%s - timeout waiting for fence", __FUNCTION__);
3305 ProcessSyncPicture();
3307 // invalidate all used render pictures
3308 for (unsigned int i = 0; i < m_bufferPool.usedRenderPics.size(); ++i)
3310 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[m_bufferPool.usedRenderPics[i]];
3315 void COutput::PreCleanup()
3321 ProcessSyncPicture();
3323 CSingleLock lock(m_bufferPool.renderPicSec);
3324 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3326 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3329 // check if output surface is in use
3331 std::deque<int>::iterator it;
3332 CVdpauRenderPicture *pic;
3333 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3335 pic = m_bufferPool.allRenderPics[*it];
3336 if ((pic->sourceIdx == m_bufferPool.outputSurfaces[i]) && pic->valid)
3345 #ifdef GL_NV_vdpau_interop
3347 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it_map;
3348 it_map = m_bufferPool.glOutputSurfaceMap.find(m_bufferPool.outputSurfaces[i]);
3349 if (it_map == m_bufferPool.glOutputSurfaceMap.end())
3351 CLog::Log(LOGERROR, "%s - could not find gl surface", __FUNCTION__);
3354 glVDPAUUnregisterSurfaceNV(it_map->second.glVdpauSurface);
3355 glDeleteTextures(1, it_map->second.texture);
3356 m_bufferPool.glOutputSurfaceMap.erase(it_map);
3359 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3360 CheckStatus(vdp_st, __LINE__);
3362 m_bufferPool.outputSurfaces[i] = VDP_INVALID_HANDLE;
3363 if (g_advancedSettings.CanLogComponent(LOGVIDEO))
3364 CLog::Log(LOGDEBUG, "VDPAU::PreCleanup - released output surface");
3369 void COutput::InitMixer()
3371 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3373 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3374 &m_bufferPool.outputSurfaces[i],
3375 sizeof(VdpOutputSurface));
3379 bool COutput::GLInit()
3381 #ifdef GL_NV_vdpau_interop
3382 glVDPAUInitNV = NULL;
3383 glVDPAUFiniNV = NULL;
3384 glVDPAURegisterOutputSurfaceNV = NULL;
3385 glVDPAURegisterVideoSurfaceNV = NULL;
3386 glVDPAUIsSurfaceNV = NULL;
3387 glVDPAUUnregisterSurfaceNV = NULL;
3388 glVDPAUSurfaceAccessNV = NULL;
3389 glVDPAUMapSurfacesNV = NULL;
3390 glVDPAUUnmapSurfacesNV = NULL;
3391 glVDPAUGetSurfaceivNV = NULL;
3394 #ifdef GL_NV_vdpau_interop
3396 glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUInitNV");
3398 glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((GLubyte *) "glVDPAUFiniNV");
3399 if (!glVDPAURegisterOutputSurfaceNV)
3400 glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterOutputSurfaceNV");
3401 if (!glVDPAURegisterVideoSurfaceNV)
3402 glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterVideoSurfaceNV");
3403 if (!glVDPAUIsSurfaceNV)
3404 glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUIsSurfaceNV");
3405 if (!glVDPAUUnregisterSurfaceNV)
3406 glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnregisterSurfaceNV");
3407 if (!glVDPAUSurfaceAccessNV)
3408 glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUSurfaceAccessNV");
3409 if (!glVDPAUMapSurfacesNV)
3410 glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUMapSurfacesNV");
3411 if (!glVDPAUUnmapSurfacesNV)
3412 glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnmapSurfacesNV");
3413 if (!glVDPAUGetSurfaceivNV)
3414 glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUGetSurfaceivNV");
3416 while (glGetError() != GL_NO_ERROR);
3417 glVDPAUInitNV(reinterpret_cast<void*>(m_config.context->GetDevice()), reinterpret_cast<void*>(m_config.context->GetProcs().vdp_get_proc_address));
3418 if (glGetError() != GL_NO_ERROR)
3420 CLog::Log(LOGERROR, "VDPAU::COutput - GLInitInterop glVDPAUInitNV failed");
3424 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop initialized");
3428 bool hasfence = glewIsSupported("GL_ARB_sync");
3429 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3431 m_bufferPool.allRenderPics[i]->usefence = hasfence;
3438 void COutput::GLMapSurface(bool yuv, uint32_t source)
3440 #ifdef GL_NV_vdpau_interop
3444 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3445 it = m_bufferPool.glVideoSurfaceMap.find(source);
3446 if (it == m_bufferPool.glVideoSurfaceMap.end())
3448 VdpauBufferPool::GLVideoSurface glSurface;
3449 VdpVideoSurface surf = source;
3451 if (surf == VDP_INVALID_HANDLE)
3454 glSurface.sourceVuv = surf;
3455 while (glGetError() != GL_NO_ERROR) ;
3456 glGenTextures(4, glSurface.texture);
3457 if (glGetError() != GL_NO_ERROR)
3459 CLog::Log(LOGERROR, "VDPAU::COutput error creating texture");
3462 glSurface.glVdpauSurface = glVDPAURegisterVideoSurfaceNV(reinterpret_cast<void*>(surf),
3463 GL_TEXTURE_2D, 4, glSurface.texture);
3465 if (glGetError() != GL_NO_ERROR)
3467 CLog::Log(LOGERROR, "VDPAU::COutput error register video surface");
3470 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3471 if (glGetError() != GL_NO_ERROR)
3473 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3476 m_bufferPool.glVideoSurfaceMap[surf] = glSurface;
3478 CLog::Log(LOGNOTICE, "VDPAU::COutput registered surface");
3481 while (glGetError() != GL_NO_ERROR) ;
3482 glVDPAUMapSurfacesNV(1, &m_bufferPool.glVideoSurfaceMap[source].glVdpauSurface);
3483 if (glGetError() != GL_NO_ERROR)
3485 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3494 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3495 it = m_bufferPool.glOutputSurfaceMap.find(source);
3496 if (it == m_bufferPool.glOutputSurfaceMap.end())
3498 unsigned int idx = 0;
3499 for (idx = 0; idx<m_bufferPool.outputSurfaces.size(); idx++)
3501 if (m_bufferPool.outputSurfaces[idx] == source)
3505 VdpauBufferPool::GLVideoSurface glSurface;
3506 glSurface.sourceRgb = m_bufferPool.outputSurfaces[idx];
3507 glGenTextures(1, glSurface.texture);
3508 glSurface.glVdpauSurface = glVDPAURegisterOutputSurfaceNV(reinterpret_cast<void*>(m_bufferPool.outputSurfaces[idx]),
3509 GL_TEXTURE_2D, 1, glSurface.texture);
3510 if (glGetError() != GL_NO_ERROR)
3512 CLog::Log(LOGERROR, "VDPAU::COutput error register output surface");
3515 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3516 if (glGetError() != GL_NO_ERROR)
3518 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3521 m_bufferPool.glOutputSurfaceMap[source] = glSurface;
3522 CLog::Log(LOGNOTICE, "VDPAU::COutput registered output surfaces");
3525 while (glGetError() != GL_NO_ERROR) ;
3526 glVDPAUMapSurfacesNV(1, &m_bufferPool.glOutputSurfaceMap[source].glVdpauSurface);
3527 if (glGetError() != GL_NO_ERROR)
3529 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3539 void COutput::GLUnmapSurfaces()
3541 #ifdef GL_NV_vdpau_interop
3544 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3545 for (it = m_bufferPool.glVideoSurfaceMap.begin(); it != m_bufferPool.glVideoSurfaceMap.end(); ++it)
3547 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3548 glDeleteTextures(4, it->second.texture);
3550 m_bufferPool.glVideoSurfaceMap.clear();
3553 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3554 for (it = m_bufferPool.glOutputSurfaceMap.begin(); it != m_bufferPool.glOutputSurfaceMap.end(); ++it)
3556 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3557 glDeleteTextures(1, it->second.texture);
3559 m_bufferPool.glOutputSurfaceMap.clear();
3563 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop finished");
3568 bool COutput::CheckStatus(VdpStatus vdp_st, int line)
3570 if (vdp_st != VDP_STATUS_OK)
3572 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);
3579 bool COutput::CreateGlxContext()
3581 GLXContext glContext;
3583 m_Display = g_Windowing.GetDisplay();
3584 glContext = g_Windowing.GetGlxContext();
3585 m_Window = g_Windowing.GetWindow();
3587 // Get our window attribs.
3588 XWindowAttributes wndattribs;
3589 XGetWindowAttributes(m_Display, m_Window, &wndattribs);
3592 XVisualInfo visInfo;
3593 visInfo.visualid = wndattribs.visual->visualid;
3595 XVisualInfo* visuals = XGetVisualInfo(m_Display, VisualIDMask, &visInfo, &nvisuals);
3598 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - could not find visual");
3601 visInfo = visuals[0];
3604 m_pixmap = XCreatePixmap(m_Display,
3611 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - Unable to create XPixmap");
3616 m_glPixmap = glXCreateGLXPixmap(m_Display, &visInfo, m_pixmap);
3620 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not create glPixmap");
3624 m_glContext = glXCreateContext(m_Display, &visInfo, glContext, True);
3626 if (!glXMakeCurrent(m_Display, m_glPixmap, m_glContext))
3628 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not make Pixmap current");
3632 CLog::Log(LOGNOTICE, "VDPAU::COutput::CreateGlxContext - created context");
3636 bool COutput::DestroyGlxContext()
3640 glXMakeCurrent(m_Display, None, NULL);
3641 glXDestroyContext(m_Display, m_glContext);
3646 glXDestroyPixmap(m_Display, m_glPixmap);
3650 XFreePixmap(m_Display, m_pixmap);