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 CDecoder::Desc decoder_profiles[] = {
46 {"MPEG1", VDP_DECODER_PROFILE_MPEG1},
47 {"MPEG2_SIMPLE", VDP_DECODER_PROFILE_MPEG2_SIMPLE},
48 {"MPEG2_MAIN", VDP_DECODER_PROFILE_MPEG2_MAIN},
49 {"H264_BASELINE",VDP_DECODER_PROFILE_H264_BASELINE},
50 {"H264_MAIN", VDP_DECODER_PROFILE_H264_MAIN},
51 {"H264_HIGH", VDP_DECODER_PROFILE_H264_HIGH},
52 {"VC1_SIMPLE", VDP_DECODER_PROFILE_VC1_SIMPLE},
53 {"VC1_MAIN", VDP_DECODER_PROFILE_VC1_MAIN},
54 {"VC1_ADVANCED", VDP_DECODER_PROFILE_VC1_ADVANCED},
55 #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
56 {"MPEG4_PART2_ASP", VDP_DECODER_PROFILE_MPEG4_PART2_ASP},
59 const size_t decoder_profile_count = sizeof(decoder_profiles)/sizeof(CDecoder::Desc);
61 static struct SInterlaceMapping
63 const EINTERLACEMETHOD method;
64 const VdpVideoMixerFeature feature;
65 } g_interlace_mapping[] =
66 { {VS_INTERLACEMETHOD_VDPAU_TEMPORAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
67 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
68 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
69 , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF, VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
70 , {VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE , VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE}
71 , {VS_INTERLACEMETHOD_NONE , (VdpVideoMixerFeature)-1}
74 static float studioCSCKCoeffs601[3] = {0.299, 0.587, 0.114}; //BT601 {Kr, Kg, Kb}
75 static float studioCSCKCoeffs709[3] = {0.2126, 0.7152, 0.0722}; //BT709 {Kr, Kg, Kb}
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 CVDPAUContext *CVDPAUContext::m_context = 0;
81 CCriticalSection CVDPAUContext::m_section;
82 Display *CVDPAUContext::m_display = 0;
83 void *CVDPAUContext::m_dlHandle = 0;
85 CVDPAUContext::CVDPAUContext()
91 void CVDPAUContext::Release()
93 CSingleLock lock(m_section);
104 void CVDPAUContext::Close()
106 CLog::Log(LOGNOTICE, "VDPAU::Close - closing decoder context");
110 bool CVDPAUContext::EnsureContext(CVDPAUContext **ctx)
112 CSingleLock lock(m_section);
116 m_context->m_refCount++;
121 m_context = new CVDPAUContext();
124 CSingleLock gLock(g_graphicsContext);
125 if (!m_context->LoadSymbols() || !m_context->CreateContext())
133 m_context->m_refCount++;
139 VDPAU_procs& CVDPAUContext::GetProcs()
144 VdpVideoMixerFeature* CVDPAUContext::GetFeatures()
146 return m_vdpFeatures;
149 int CVDPAUContext::GetFeatureCount()
151 return m_featureCount;
154 bool CVDPAUContext::LoadSymbols()
158 m_dlHandle = dlopen("libvdpau.so.1", RTLD_LAZY);
161 const char* error = dlerror();
163 error = "dlerror() returned NULL";
165 CLog::Log(LOGERROR,"VDPAU::LoadSymbols: Unable to get handle to lib: %s", error);
172 dl_vdp_device_create_x11 = (VdpStatus (*)(Display*, int, VdpDevice*, VdpStatus (**)(VdpDevice, VdpFuncId, void**)))dlsym(m_dlHandle, (const char*)"vdp_device_create_x11");
176 CLog::Log(LOGERROR,"(VDPAU) - %s in %s",error,__FUNCTION__);
177 m_vdpDevice = VDP_INVALID_HANDLE;
183 bool CVDPAUContext::CreateContext()
185 CLog::Log(LOGNOTICE,"VDPAU::CreateContext - creating decoder context");
188 { CSingleLock lock(g_graphicsContext);
190 m_display = XOpenDisplay(NULL);
191 mScreen = g_Windowing.GetCurrentScreen();
196 vdp_st = dl_vdp_device_create_x11(m_display,
199 &m_vdpProcs.vdp_get_proc_address);
201 CLog::Log(LOGNOTICE,"vdp_device = 0x%08x vdp_st = 0x%08x",m_vdpDevice,vdp_st);
202 if (vdp_st != VDP_STATUS_OK)
204 CLog::Log(LOGERROR,"(VDPAU) unable to init VDPAU - vdp_st = 0x%x. Falling back.",vdp_st);
205 m_vdpDevice = VDP_INVALID_HANDLE;
210 SpewHardwareAvailable();
214 void CVDPAUContext::QueryProcs()
218 #define VDP_PROC(id, proc) \
220 vdp_st = m_vdpProcs.vdp_get_proc_address(m_vdpDevice, id, (void**)&proc); \
221 if (vdp_st != VDP_STATUS_OK) \
223 CLog::Log(LOGERROR, "CVDPAUContext::GetProcs - failed to get proc id"); \
227 VDP_PROC(VDP_FUNC_ID_GET_ERROR_STRING , m_vdpProcs.vdp_get_error_string);
228 VDP_PROC(VDP_FUNC_ID_DEVICE_DESTROY , m_vdpProcs.vdp_device_destroy);
229 VDP_PROC(VDP_FUNC_ID_GENERATE_CSC_MATRIX , m_vdpProcs.vdp_generate_csc_matrix);
230 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_CREATE , m_vdpProcs.vdp_video_surface_create);
231 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY , m_vdpProcs.vdp_video_surface_destroy);
232 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_put_bits_y_cb_cr);
233 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_get_bits_y_cb_cr);
234 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_output_surface_put_bits_y_cb_cr);
235 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE , m_vdpProcs.vdp_output_surface_put_bits_native);
236 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_CREATE , m_vdpProcs.vdp_output_surface_create);
237 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY , m_vdpProcs.vdp_output_surface_destroy);
238 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE , m_vdpProcs.vdp_output_surface_get_bits_native);
239 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, m_vdpProcs.vdp_output_surface_render_output_surface);
240 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED , m_vdpProcs.vdp_output_surface_put_bits_indexed);
241 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_CREATE , m_vdpProcs.vdp_video_mixer_create);
242 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES , m_vdpProcs.vdp_video_mixer_set_feature_enables);
243 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_DESTROY , m_vdpProcs.vdp_video_mixer_destroy);
244 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_RENDER , m_vdpProcs.vdp_video_mixer_render);
245 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES , m_vdpProcs.vdp_video_mixer_set_attribute_values);
246 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_PARAMETER_SUPPORT , m_vdpProcs.vdp_video_mixer_query_parameter_support);
247 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_FEATURE_SUPPORT , m_vdpProcs.vdp_video_mixer_query_feature_support);
248 VDP_PROC(VDP_FUNC_ID_DECODER_CREATE , m_vdpProcs.vdp_decoder_create);
249 VDP_PROC(VDP_FUNC_ID_DECODER_DESTROY , m_vdpProcs.vdp_decoder_destroy);
250 VDP_PROC(VDP_FUNC_ID_DECODER_RENDER , m_vdpProcs.vdp_decoder_render);
251 VDP_PROC(VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES , m_vdpProcs.vdp_decoder_query_caps);
255 VdpDevice CVDPAUContext::GetDevice()
260 void CVDPAUContext::DestroyContext()
262 if (!m_vdpProcs.vdp_device_destroy)
265 m_vdpProcs.vdp_device_destroy(m_vdpDevice);
266 m_vdpDevice = VDP_INVALID_HANDLE;
269 void CVDPAUContext::SpewHardwareAvailable() //CopyrighVDPAUt (c) 2008 Wladimir J. van der Laan -- VDPInfo
272 CLog::Log(LOGNOTICE,"VDPAU Decoder capabilities:");
273 CLog::Log(LOGNOTICE,"name level macbs width height");
274 CLog::Log(LOGNOTICE,"------------------------------------");
275 for(unsigned int x=0; x<decoder_profile_count; ++x)
277 VdpBool is_supported = false;
278 uint32_t max_level, max_macroblocks, max_width, max_height;
279 rv = m_vdpProcs.vdp_decoder_query_caps(m_vdpDevice, decoder_profiles[x].id,
280 &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
281 if(rv == VDP_STATUS_OK && is_supported)
283 CLog::Log(LOGNOTICE,"%-16s %2i %5i %5i %5i\n", decoder_profiles[x].name,
284 max_level, max_macroblocks, max_width, max_height);
287 CLog::Log(LOGNOTICE,"------------------------------------");
289 #define CHECK_SUPPORT(feature) \
292 if(m_vdpProcs.vdp_video_mixer_query_feature_support(m_vdpDevice, feature, &supported) == VDP_STATUS_OK && supported) { \
293 CLog::Log(LOGNOTICE, "Mixer feature: "#feature); \
294 m_vdpFeatures[m_featureCount++] = feature; \
298 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION);
299 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_SHARPNESS);
300 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL);
301 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL);
302 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE);
303 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
304 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1);
305 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2);
306 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3);
307 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4);
308 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5);
309 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6);
310 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7);
311 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8);
312 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9);
317 bool CVDPAUContext::Supports(VdpVideoMixerFeature feature)
319 for(int i = 0; i < m_featureCount; i++)
321 if(m_vdpFeatures[i] == feature)
327 //-----------------------------------------------------------------------------
328 // VDPAU Video Surface states
329 //-----------------------------------------------------------------------------
331 #define SURFACE_USED_FOR_REFERENCE 0x01
332 #define SURFACE_USED_FOR_RENDER 0x02
334 void CVideoSurfaces::AddSurface(VdpVideoSurface surf)
336 CSingleLock lock(m_section);
337 m_state[surf] = SURFACE_USED_FOR_REFERENCE;
340 void CVideoSurfaces::ClearReference(VdpVideoSurface surf)
342 CSingleLock lock(m_section);
343 if (m_state.find(surf) == m_state.end())
345 CLog::Log(LOGWARNING, "CVideoSurfaces::ClearReference - surface invalid");
348 m_state[surf] &= ~SURFACE_USED_FOR_REFERENCE;
349 if (m_state[surf] == 0)
351 m_freeSurfaces.push_back(surf);
355 bool CVideoSurfaces::MarkRender(VdpVideoSurface surf)
357 CSingleLock lock(m_section);
358 if (m_state.find(surf) == m_state.end())
360 CLog::Log(LOGWARNING, "CVideoSurfaces::MarkRender - surface invalid");
363 std::list<VdpVideoSurface>::iterator it;
364 it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
365 if (it != m_freeSurfaces.end())
367 m_freeSurfaces.erase(it);
369 m_state[surf] |= SURFACE_USED_FOR_RENDER;
373 void CVideoSurfaces::ClearRender(VdpVideoSurface surf)
375 CSingleLock lock(m_section);
376 if (m_state.find(surf) == m_state.end())
378 CLog::Log(LOGWARNING, "CVideoSurfaces::ClearRender - surface invalid");
381 m_state[surf] &= ~SURFACE_USED_FOR_RENDER;
382 if (m_state[surf] == 0)
384 m_freeSurfaces.push_back(surf);
388 bool CVideoSurfaces::IsValid(VdpVideoSurface surf)
390 CSingleLock lock(m_section);
391 if (m_state.find(surf) != m_state.end())
397 VdpVideoSurface CVideoSurfaces::GetFree(VdpVideoSurface surf)
399 CSingleLock lock(m_section);
400 if (m_state.find(surf) != m_state.end())
402 std::list<VdpVideoSurface>::iterator it;
403 it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
404 if (it == m_freeSurfaces.end())
406 CLog::Log(LOGWARNING, "CVideoSurfaces::GetFree - surface not free");
410 m_freeSurfaces.erase(it);
411 m_state[surf] = SURFACE_USED_FOR_REFERENCE;
416 if (!m_freeSurfaces.empty())
418 VdpVideoSurface freeSurf = m_freeSurfaces.front();
419 m_freeSurfaces.pop_front();
420 m_state[freeSurf] = SURFACE_USED_FOR_REFERENCE;
424 return VDP_INVALID_HANDLE;
427 VdpVideoSurface CVideoSurfaces::GetAtIndex(int idx)
429 if (idx >= m_state.size())
430 return VDP_INVALID_HANDLE;
432 std::map<VdpVideoSurface, int>::iterator it = m_state.begin();
433 for(int i = 0; i < idx; i++)
438 VdpVideoSurface CVideoSurfaces::RemoveNext(bool skiprender)
440 CSingleLock lock(m_section);
441 VdpVideoSurface surf;
442 std::map<VdpVideoSurface, int>::iterator it;
443 for(it = m_state.begin(); it != m_state.end(); ++it)
445 if (skiprender && it->second & SURFACE_USED_FOR_RENDER)
450 std::list<VdpVideoSurface>::iterator it2;
451 it2 = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
452 if (it2 != m_freeSurfaces.end())
453 m_freeSurfaces.erase(it2);
456 return VDP_INVALID_HANDLE;
459 void CVideoSurfaces::Reset()
461 CSingleLock lock(m_section);
462 m_freeSurfaces.clear();
466 int CVideoSurfaces::Size()
468 CSingleLock lock(m_section);
469 return m_state.size();
472 //-----------------------------------------------------------------------------
474 //-----------------------------------------------------------------------------
476 CDecoder::CDecoder() : m_vdpauOutput(&m_inMsgEvent)
478 m_vdpauConfig.videoSurfaces = &m_videoSurfaces;
480 m_vdpauConfigured = false;
481 m_hwContext.bitstream_buffers_allocated = 0;
482 m_DisplayState = VDPAU_OPEN;
483 m_vdpauConfig.context = 0;
486 bool CDecoder::Open(AVCodecContext* avctx, const enum PixelFormat, unsigned int surfaces)
488 #ifndef GL_NV_vdpau_interop
489 CLog::Log(LOGNOTICE, "VDPAU: compilation without required extension GL_NV_vdpau_interop");
492 if (!g_Windowing.IsExtSupported("GL_NV_vdpau_interop"))
494 CLog::Log(LOGNOTICE, "VDPAU: required extension GL_NV_vdpau_interop not found");
498 if(avctx->coded_width == 0
499 || avctx->coded_height == 0)
501 CLog::Log(LOGWARNING,"(VDPAU) no width/height available, can't init");
504 m_vdpauConfig.numRenderBuffers = surfaces;
505 m_decoderThread = CThread::GetCurrentThreadId();
507 if (!CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
510 m_DisplayState = VDPAU_OPEN;
511 m_vdpauConfigured = false;
513 if (!m_dllAvUtil.Load())
516 m_presentPicture = 0;
519 VdpDecoderProfile profile = 0;
520 if(avctx->codec_id == AV_CODEC_ID_H264)
521 profile = VDP_DECODER_PROFILE_H264_HIGH;
522 #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
523 else if(avctx->codec_id == AV_CODEC_ID_MPEG4)
524 profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
528 if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
529 CLog::Log(LOGWARNING,"(VDPAU) width %i might not be supported because of hardware bug", avctx->width);
531 /* attempt to create a decoder with this width/height, some sizes are not supported by hw */
533 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(), profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
535 if(vdp_st != VDP_STATUS_OK)
537 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) checking for decoder support\n", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
541 m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
542 CheckStatus(vdp_st, __LINE__);
545 /* finally setup ffmpeg */
546 memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
547 m_hwContext.render = CDecoder::Render;
548 m_hwContext.bitstream_buffers_allocated = 0;
549 avctx->get_buffer = CDecoder::FFGetBuffer;
550 avctx->reget_buffer = CDecoder::FFGetBuffer;
551 avctx->release_buffer = CDecoder::FFReleaseBuffer;
552 avctx->draw_horiz_band = CDecoder::FFDrawSlice;
553 avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
554 avctx->hwaccel_context = &m_hwContext;
555 avctx->thread_count = 1;
557 g_Windowing.Register(this);
563 CDecoder::~CDecoder()
568 void CDecoder::Close()
570 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
572 g_Windowing.Unregister(this);
574 CSingleLock lock(m_DecoderSection);
577 m_vdpauOutput.Dispose();
579 if (m_hwContext.bitstream_buffers_allocated)
581 m_dllAvUtil.av_freep(&m_hwContext.bitstream_buffers);
584 m_dllAvUtil.Unload();
586 if (m_vdpauConfig.context)
587 m_vdpauConfig.context->Release();
588 m_vdpauConfig.context = 0;
591 long CDecoder::Release()
593 // check if we should do some pre-cleanup here
594 // a second decoder might need resources
595 if (m_vdpauConfigured == true)
597 CSingleLock lock(m_DecoderSection);
598 CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
601 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
605 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
609 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
610 m_DisplayState = VDPAU_ERROR;
615 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
616 m_DisplayState = VDPAU_ERROR;
619 VdpVideoSurface surf;
620 while((surf = m_videoSurfaces.RemoveNext(true)) != VDP_INVALID_HANDLE)
622 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
625 return IHardwareDecoder::Release();
628 long CDecoder::ReleasePicReference()
630 return IHardwareDecoder::Release();
633 void CDecoder::SetWidthHeight(int width, int height)
635 m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
637 //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
638 //this requires the least amount of gpu memory bandwidth
639 if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
641 //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
642 if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
644 m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
645 m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
647 else //scale height to the desktop size if the aspect ratio is smaller than the desktop
649 m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
650 m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
655 m_vdpauConfig.outWidth = width;
656 m_vdpauConfig.outHeight = height;
658 CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
661 void CDecoder::OnLostDevice()
663 CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
665 int count = g_graphicsContext.exit();
667 CSingleLock lock(m_DecoderSection);
669 if (m_vdpauConfig.context)
670 m_vdpauConfig.context->Release();
671 m_vdpauConfig.context = 0;
673 m_DisplayState = VDPAU_LOST;
675 m_DisplayEvent.Reset();
677 g_graphicsContext.restore(count);
680 void CDecoder::OnResetDevice()
682 CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
684 int count = g_graphicsContext.exit();
686 CSingleLock lock(m_DecoderSection);
687 if (m_DisplayState == VDPAU_LOST)
689 m_DisplayState = VDPAU_RESET;
691 m_DisplayEvent.Set();
694 g_graphicsContext.restore(count);
697 int CDecoder::Check(AVCodecContext* avctx)
701 { CSingleLock lock(m_DecoderSection);
702 state = m_DisplayState;
705 if (state == VDPAU_LOST)
707 CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
708 if (!m_DisplayEvent.WaitMSec(4000))
710 CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
715 CSingleLock lock(m_DecoderSection);
716 state = m_DisplayState;
719 if (state == VDPAU_RESET || state == VDPAU_ERROR)
721 CSingleLock lock(m_DecoderSection);
724 if (m_vdpauConfig.context)
725 m_vdpauConfig.context->Release();
726 m_vdpauConfig.context = 0;
728 if (CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
730 m_DisplayState = VDPAU_OPEN;
731 m_vdpauConfigured = false;
734 if (state == VDPAU_RESET)
742 bool CDecoder::IsVDPAUFormat(PixelFormat format)
744 if (format == AV_PIX_FMT_VDPAU)
750 bool CDecoder::Supports(VdpVideoMixerFeature feature)
752 return m_vdpauConfig.context->Supports(feature);
755 bool CDecoder::Supports(EINTERLACEMETHOD method)
757 if(method == VS_INTERLACEMETHOD_VDPAU_BOB
758 || method == VS_INTERLACEMETHOD_AUTO)
761 if (method == VS_INTERLACEMETHOD_RENDER_BOB)
764 if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
767 for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
769 if(p->method == method)
770 return Supports(p->feature);
775 EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
777 return VS_INTERLACEMETHOD_RENDER_BOB;
780 void CDecoder::FiniVDPAUOutput()
782 if (!m_vdpauConfigured)
785 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
788 m_vdpauOutput.Dispose();
789 m_vdpauConfigured = false;
793 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
794 if (CheckStatus(vdp_st, __LINE__))
796 m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
798 CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", m_videoSurfaces.Size());
800 VdpVideoSurface surf;
801 while((surf = m_videoSurfaces.RemoveNext()) != VDP_INVALID_HANDLE)
803 m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
804 if (CheckStatus(vdp_st, __LINE__))
807 m_videoSurfaces.Reset();
810 void CDecoder::ReadFormatOf( AVCodecID codec
811 , VdpDecoderProfile &vdp_decoder_profile
812 , VdpChromaType &vdp_chroma_type)
816 case AV_CODEC_ID_MPEG1VIDEO:
817 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
818 vdp_chroma_type = VDP_CHROMA_TYPE_420;
820 case AV_CODEC_ID_MPEG2VIDEO:
821 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
822 vdp_chroma_type = VDP_CHROMA_TYPE_420;
824 case AV_CODEC_ID_H264:
825 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
826 vdp_chroma_type = VDP_CHROMA_TYPE_420;
828 case AV_CODEC_ID_WMV3:
829 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
830 vdp_chroma_type = VDP_CHROMA_TYPE_420;
832 case AV_CODEC_ID_VC1:
833 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
834 vdp_chroma_type = VDP_CHROMA_TYPE_420;
836 case AV_CODEC_ID_MPEG4:
837 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
838 vdp_chroma_type = VDP_CHROMA_TYPE_420;
841 vdp_decoder_profile = 0;
847 bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
852 VdpDecoderProfile vdp_decoder_profile;
854 m_vdpauConfig.vidWidth = avctx->width;
855 m_vdpauConfig.vidHeight = avctx->height;
856 m_vdpauConfig.surfaceWidth = avctx->coded_width;
857 m_vdpauConfig.surfaceHeight = avctx->coded_height;
859 SetWidthHeight(avctx->width,avctx->height);
861 CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
862 CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
864 ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
866 if(avctx->codec_id == AV_CODEC_ID_H264)
868 m_vdpauConfig.maxReferences = ref_frames;
869 if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
870 if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
873 m_vdpauConfig.maxReferences = 2;
875 vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(),
877 m_vdpauConfig.surfaceWidth,
878 m_vdpauConfig.surfaceHeight,
879 m_vdpauConfig.maxReferences,
880 &m_vdpauConfig.vdpDecoder);
881 if (CheckStatus(vdp_st, __LINE__))
885 CSingleLock lock(g_graphicsContext);
886 m_vdpauConfig.stats = &m_bufferStats;
887 m_vdpauConfig.vdpau = this;
888 m_bufferStats.Reset();
889 m_vdpauOutput.Start();
891 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::INIT,
895 sizeof(m_vdpauConfig)))
897 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
901 CLog::Log(LOGERROR, "VDPAU::%s - vdpau output returned error", __FUNCTION__);
902 m_vdpauOutput.Dispose();
908 CLog::Log(LOGERROR, "VDPAU::%s - failed to init output", __FUNCTION__);
909 m_vdpauOutput.Dispose();
913 m_inMsgEvent.Reset();
914 m_vdpauConfigured = true;
918 int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic)
920 //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
921 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
922 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
924 // while we are waiting to recover we can't do anything
925 CSingleLock lock(vdp->m_DecoderSection);
927 if(vdp->m_DisplayState != VDPAU_OPEN)
929 CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
933 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
934 surf = vdp->m_videoSurfaces.GetFree(surf != 0 ? surf : VDP_INVALID_HANDLE);
936 VdpStatus vdp_st = VDP_STATUS_ERROR;
937 if (surf == VDP_INVALID_HANDLE)
939 // create a new surface
940 VdpDecoderProfile profile;
941 ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
943 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_video_surface_create(vdp->m_vdpauConfig.context->GetDevice(),
944 vdp->m_vdpauConfig.vdpChromaType,
948 vdp->CheckStatus(vdp_st, __LINE__);
949 if (vdp_st != VDP_STATUS_OK)
951 CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
954 vdp->m_videoSurfaces.AddSurface(surf);
957 pic->data[1] = pic->data[2] = NULL;
958 pic->data[0] = (uint8_t*)(uintptr_t)surf;
959 pic->data[3] = (uint8_t*)(uintptr_t)surf;
961 pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
963 pic->type= FF_BUFFER_TYPE_USER;
965 pic->reordered_opaque= avctx->reordered_opaque;
969 void CDecoder::FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic)
971 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
972 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
974 VdpVideoSurface surf;
977 CSingleLock lock(vdp->m_DecoderSection);
979 surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
981 vdp->m_videoSurfaces.ClearReference(surf);
987 VdpStatus CDecoder::Render( VdpDecoder decoder, VdpVideoSurface target,
988 VdpPictureInfo const *picture_info,
989 uint32_t bitstream_buffer_count,
990 VdpBitstreamBuffer const * bitstream_buffers)
992 return VDP_STATUS_OK;
995 void CDecoder::FFDrawSlice(struct AVCodecContext *s,
996 const AVFrame *src, int offset[4],
997 int y, int type, int height)
999 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
1000 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
1002 // while we are waiting to recover we can't do anything
1003 CSingleLock lock(vdp->m_DecoderSection);
1005 if(vdp->m_DisplayState != VDPAU_OPEN)
1008 if(src->linesize[0] || src->linesize[1] || src->linesize[2]
1009 || offset[0] || offset[1] || offset[2])
1011 CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
1016 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
1018 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1019 if (!vdp->m_videoSurfaces.IsValid(surf))
1021 CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
1025 uint32_t max_refs = 0;
1026 if(s->codec_id == AV_CODEC_ID_H264)
1027 max_refs = vdp->m_hwContext.info.h264.num_ref_frames;
1029 if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
1030 || vdp->m_vdpauConfigured == false
1031 || vdp->m_vdpauConfig.maxReferences < max_refs)
1033 if(!vdp->ConfigVDPAU(s, max_refs))
1037 uint64_t startTime = CurrentHostCounter();
1038 uint16_t decoded, processed, rend;
1039 vdp->m_bufferStats.Get(decoded, processed, rend);
1040 vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
1042 (VdpPictureInfo const *)&(vdp->m_hwContext.info),
1043 vdp->m_hwContext.bitstream_buffers_used,
1044 vdp->m_hwContext.bitstream_buffers);
1045 vdp->CheckStatus(vdp_st, __LINE__);
1046 uint64_t diff = CurrentHostCounter() - startTime;
1047 if (diff*1000/CurrentHostFrequency() > 30)
1048 CLog::Log(LOGDEBUG, "CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
1052 int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
1054 int result = Check(avctx);
1058 CSingleLock lock(m_DecoderSection);
1060 if (!m_vdpauConfigured)
1064 { // we have a new frame from decoder
1066 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pFrame->data[3];
1067 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
1068 if (!m_videoSurfaces.IsValid(surf))
1070 CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
1073 m_videoSurfaces.MarkRender(surf);
1075 // send frame to output for processing
1076 CVdpauDecodedPicture pic;
1077 memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
1078 ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
1079 pic.videoSurface = surf;
1080 pic.DVDPic.color_matrix = avctx->colorspace;
1081 m_bufferStats.IncDecoded();
1082 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
1085 // m_codecControl = pic.DVDPic.iFlags & (DVP_FLAG_DRAIN | DVP_FLAG_NO_POSTPROC);
1089 uint16_t decoded, processed, render;
1091 while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1093 if (msg->signal == COutputControlProtocol::ERROR)
1095 m_DisplayState = VDPAU_ERROR;
1101 m_bufferStats.Get(decoded, processed, render);
1103 uint64_t startTime = CurrentHostCounter();
1106 // first fill the buffers to keep vdpau busy
1107 // mixer will run with decoded >= 2. output is limited by number of output surfaces
1108 // In case mixer is bypassed we limit by looking at processed
1109 if (decoded < 3 && processed < 3)
1111 retval |= VC_BUFFER;
1113 else if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
1115 if (msg->signal == COutputDataProtocol::PICTURE)
1117 if (m_presentPicture)
1119 m_presentPicture->ReturnUnused();
1120 m_presentPicture = 0;
1123 m_presentPicture = *(CVdpauRenderPicture**)msg->data;
1124 m_presentPicture->vdpau = this;
1125 m_bufferStats.DecRender();
1126 m_bufferStats.Get(decoded, processed, render);
1127 retval |= VC_PICTURE;
1133 else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
1135 if (msg->signal == COutputControlProtocol::STATS)
1137 m_bufferStats.Get(decoded, processed, render);
1141 m_DisplayState = VDPAU_ERROR;
1147 if (decoded < 3 && processed < 3)
1149 retval |= VC_BUFFER;
1152 if (!retval && !m_inMsgEvent.WaitMSec(2000))
1155 uint64_t diff = CurrentHostCounter() - startTime;
1156 if (retval & VC_PICTURE)
1158 m_bufferStats.SetParams(diff, m_codecControl);
1160 if (diff*1000/CurrentHostFrequency() > 50)
1161 CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
1165 CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
1166 m_DisplayState = VDPAU_ERROR;
1173 bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
1175 CSingleLock lock(m_DecoderSection);
1177 if (m_DisplayState != VDPAU_OPEN)
1180 *picture = m_presentPicture->DVDPic;
1181 picture->vdpau = m_presentPicture;
1186 void CDecoder::Reset()
1188 CSingleLock lock(m_DecoderSection);
1190 if (!m_vdpauConfigured)
1194 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
1198 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
1202 CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
1203 m_DisplayState = VDPAU_ERROR;
1206 m_bufferStats.Reset();
1210 CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
1211 m_DisplayState = VDPAU_ERROR;
1215 bool CDecoder::CanSkipDeint()
1217 return m_bufferStats.CanSkipDeint();
1220 void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
1222 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
1225 bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
1227 if (vdp_st != VDP_STATUS_OK)
1229 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);
1231 if(m_DisplayState == VDPAU_OPEN)
1233 if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
1235 m_DisplayEvent.Reset();
1236 m_DisplayState = VDPAU_LOST;
1239 m_DisplayState = VDPAU_ERROR;
1247 //-----------------------------------------------------------------------------
1249 //-----------------------------------------------------------------------------
1251 CVdpauRenderPicture* CVdpauRenderPicture::Acquire()
1253 CSingleLock lock(renderPicSection);
1262 long CVdpauRenderPicture::Release()
1264 CSingleLock lock(renderPicSection);
1271 vdpau->ReturnRenderPicture(this);
1272 vdpau->ReleasePicReference();
1277 void CVdpauRenderPicture::ReturnUnused()
1279 { CSingleLock lock(renderPicSection);
1284 vdpau->ReturnRenderPicture(this);
1287 void CVdpauRenderPicture::Sync()
1290 CSingleLock lock(renderPicSection);
1295 glDeleteSync(fence);
1298 fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1303 //-----------------------------------------------------------------------------
1305 //-----------------------------------------------------------------------------
1306 CMixer::CMixer(CEvent *inMsgEvent) :
1307 CThread("Vdpau Mixer"),
1308 m_controlPort("ControlPort", inMsgEvent, &m_outMsgEvent),
1309 m_dataPort("DataPort", inMsgEvent, &m_outMsgEvent)
1311 m_inMsgEvent = inMsgEvent;
1319 void CMixer::Start()
1324 void CMixer::Dispose()
1327 m_outMsgEvent.Set();
1330 m_controlPort.Purge();
1334 bool CMixer::IsActive()
1339 void CMixer::OnStartup()
1341 CLog::Log(LOGNOTICE, "CMixer::OnStartup: Output Thread created");
1344 void CMixer::OnExit()
1346 CLog::Log(LOGNOTICE, "CMixer::OnExit: Output Thread terminated");
1353 M_TOP_UNCONFIGURED, // 2
1354 M_TOP_CONFIGURED, // 3
1355 M_TOP_CONFIGURED_WAIT1, // 4
1356 M_TOP_CONFIGURED_STEP1, // 5
1357 M_TOP_CONFIGURED_WAIT2, // 6
1358 M_TOP_CONFIGURED_STEP2, // 7
1361 int MIXER_parentStates[] = {
1364 0, //TOP_UNCONFIGURED
1366 3, //TOP_CONFIGURED_WAIT1
1367 3, //TOP_CONFIGURED_STEP1
1368 3, //TOP_CONFIGURED_WAIT2
1369 3, //TOP_CONFIGURED_STEP2
1372 void CMixer::StateMachine(int signal, Protocol *port, Message *msg)
1374 for (int state = m_state; ; state = MIXER_parentStates[state])
1379 if (port == &m_controlPort)
1383 case CMixerControlProtocol::FLUSH:
1385 msg->Reply(CMixerControlProtocol::ACC);
1392 std::string portName = port == NULL ? "timer" : port->portName;
1393 CLog::Log(LOGWARNING, "CMixer::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
1397 case M_TOP_ERROR: // TOP
1400 case M_TOP_UNCONFIGURED:
1401 if (port == &m_controlPort)
1405 case CMixerControlProtocol::INIT:
1407 data = (CVdpauConfig*)msg->data;
1415 m_state = M_TOP_CONFIGURED_WAIT1;
1416 msg->Reply(CMixerControlProtocol::ACC);
1420 msg->Reply(CMixerControlProtocol::ERROR);
1429 case M_TOP_CONFIGURED:
1430 if (port == &m_dataPort)
1434 case CMixerDataProtocol::FRAME:
1435 CVdpauDecodedPicture *frame;
1436 frame = (CVdpauDecodedPicture*)msg->data;
1439 m_decodedPics.push(*frame);
1443 case CMixerDataProtocol::BUFFER:
1444 VdpOutputSurface *surf;
1445 surf = (VdpOutputSurface*)msg->data;
1448 m_outputSurfaces.push(*surf);
1458 case M_TOP_CONFIGURED_WAIT1:
1459 if (port == NULL) // timeout
1463 case CMixerControlProtocol::TIMEOUT:
1464 if (!m_decodedPics.empty() && !m_outputSurfaces.empty())
1466 m_state = M_TOP_CONFIGURED_STEP1;
1467 m_bStateMachineSelfTrigger = true;
1480 case M_TOP_CONFIGURED_STEP1:
1481 if (port == NULL) // timeout
1485 case CMixerControlProtocol::TIMEOUT:
1486 m_mixerInput.push_front(m_decodedPics.front());
1487 m_decodedPics.pop();
1488 if (m_mixerInput.size() < 2)
1490 m_state = M_TOP_CONFIGURED_WAIT1;
1498 m_state = M_TOP_CONFIGURED_WAIT1;
1499 m_extTimeout = 1000;
1502 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1503 m_outputSurfaces.pop();
1504 m_config.stats->IncProcessed();
1505 m_config.stats->DecDecoded();
1506 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1507 if (m_mixersteps > 1)
1509 m_state = M_TOP_CONFIGURED_WAIT2;
1515 m_state = M_TOP_CONFIGURED_WAIT1;
1525 case M_TOP_CONFIGURED_WAIT2:
1526 if (port == NULL) // timeout
1530 case CMixerControlProtocol::TIMEOUT:
1531 if (!m_outputSurfaces.empty())
1533 m_state = M_TOP_CONFIGURED_STEP2;
1534 m_bStateMachineSelfTrigger = true;
1547 case M_TOP_CONFIGURED_STEP2:
1548 if (port == NULL) // timeout
1552 case CMixerControlProtocol::TIMEOUT:
1553 m_processPicture.outputSurface = m_outputSurfaces.front();
1558 m_state = M_TOP_CONFIGURED_WAIT1;
1559 m_extTimeout = 1000;
1562 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1563 m_outputSurfaces.pop();
1564 m_config.stats->IncProcessed();
1565 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1567 m_state = M_TOP_CONFIGURED_WAIT1;
1576 default: // we are in no state, should not happen
1577 CLog::Log(LOGERROR, "CMixer::%s - no valid state: %d", __FUNCTION__, m_state);
1583 void CMixer::Process()
1585 Message *msg = NULL;
1586 Protocol *port = NULL;
1589 m_state = M_TOP_UNCONFIGURED;
1590 m_extTimeout = 1000;
1591 m_bStateMachineSelfTrigger = false;
1597 if (m_bStateMachineSelfTrigger)
1599 m_bStateMachineSelfTrigger = false;
1600 // self trigger state machine
1601 StateMachine(msg->signal, port, msg);
1602 if (!m_bStateMachineSelfTrigger)
1609 // check control port
1610 else if (m_controlPort.ReceiveOutMessage(&msg))
1613 port = &m_controlPort;
1616 else if (m_dataPort.ReceiveOutMessage(&msg))
1624 StateMachine(msg->signal, port, msg);
1625 if (!m_bStateMachineSelfTrigger)
1634 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
1641 msg = m_controlPort.GetMessage();
1642 msg->signal = CMixerControlProtocol::TIMEOUT;
1644 // signal timeout to state machine
1645 StateMachine(msg->signal, port, msg);
1646 if (!m_bStateMachineSelfTrigger)
1656 void CMixer::CreateVdpauMixer()
1658 CLog::Log(LOGNOTICE, " (VDPAU) Creating the video mixer");
1660 InitCSCMatrix(m_config.vidWidth);
1662 VdpVideoMixerParameter parameters[] = {
1663 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
1664 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
1665 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE};
1667 void const * parameter_values[] = {
1668 &m_config.surfaceWidth,
1669 &m_config.surfaceHeight,
1670 &m_config.vdpChromaType};
1672 VdpStatus vdp_st = VDP_STATUS_ERROR;
1673 vdp_st = m_config.context->GetProcs().vdp_video_mixer_create(m_config.context->GetDevice(),
1674 m_config.context->GetFeatureCount(),
1675 m_config.context->GetFeatures(),
1680 CheckStatus(vdp_st, __LINE__);
1684 void CMixer::InitCSCMatrix(int Width)
1686 m_Procamp.struct_version = VDP_PROCAMP_VERSION;
1687 m_Procamp.brightness = 0.0;
1688 m_Procamp.contrast = 1.0;
1689 m_Procamp.saturation = 1.0;
1693 void CMixer::CheckFeatures()
1695 if (m_Upscale != m_config.upscale)
1698 m_Upscale = m_config.upscale;
1700 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness ||
1701 m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast ||
1702 m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
1705 m_Brightness = CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness;
1706 m_Contrast = CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast;
1707 m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
1709 if (m_NoiseReduction != CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1711 m_NoiseReduction = CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction;
1712 SetNoiseReduction();
1714 if (m_Sharpness != CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1716 m_Sharpness = CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness;
1719 if (m_DeintMode != CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode ||
1720 m_Deint != CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod)
1722 m_DeintMode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
1723 m_Deint = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1728 void CMixer::SetPostProcFeatures(bool postProcEnabled)
1730 if (m_PostProc != postProcEnabled)
1732 if (postProcEnabled)
1734 SetNoiseReduction();
1741 m_PostProc = postProcEnabled;
1745 void CMixer::PostProcOff()
1749 if (m_videoMixer == VDP_INVALID_HANDLE)
1752 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1753 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1754 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE};
1756 VdpBool enabled[]={0,0,0};
1757 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1758 CheckStatus(vdp_st, __LINE__);
1760 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1762 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION};
1764 VdpBool enabled[]={0};
1765 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1766 CheckStatus(vdp_st, __LINE__);
1769 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1771 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS};
1773 VdpBool enabled[]={0};
1774 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1775 CheckStatus(vdp_st, __LINE__);
1781 bool CMixer::GenerateStudioCSCMatrix(VdpColorStandard colorStandard, VdpCSCMatrix &studioCSCMatrix)
1783 // instead use studioCSCKCoeffs601[3], studioCSCKCoeffs709[3] to generate float[3][4] matrix (float studioCSC[3][4])
1784 // m00 = mRY = red: luma factor (contrast factor) (1.0)
1785 // m10 = mGY = green: luma factor (contrast factor) (1.0)
1786 // m20 = mBY = blue: luma factor (contrast factor) (1.0)
1788 // m01 = mRB = red: blue color diff coeff (0.0)
1789 // m11 = mGB = green: blue color diff coeff (-2Kb(1-Kb)/(Kg))
1790 // m21 = mBB = blue: blue color diff coeff ((1-Kb)/0.5)
1792 // m02 = mRR = red: red color diff coeff ((1-Kr)/0.5)
1793 // m12 = mGR = green: red color diff coeff (-2Kr(1-Kr)/(Kg))
1794 // m22 = mBR = blue: red color diff coeff (0.0)
1796 // m03 = mRC = red: colour zero offset (brightness factor) (-(1-Kr)/0.5 * (128/255))
1797 // m13 = mGC = green: colour zero offset (brightness factor) ((256/255) * (Kb(1-Kb) + Kr(1-Kr)) / Kg)
1798 // m23 = mBC = blue: colour zero offset (brightness factor) (-(1-Kb)/0.5 * (128/255))
1809 // colour standard coefficients for red, geen, blue
1811 // colour diff zero position (use standard 8-bit coding precision)
1812 double CDZ = 128; //256*0.5
1813 // range excursion (use standard 8-bit coding precision)
1814 double EXC = 255; //256-1
1816 if (colorStandard == VDP_COLOR_STANDARD_ITUR_BT_601)
1818 Kr = studioCSCKCoeffs601[0];
1819 Kg = studioCSCKCoeffs601[1];
1820 Kb = studioCSCKCoeffs601[2];
1822 else // assume VDP_COLOR_STANDARD_ITUR_BT_709
1824 Kr = studioCSCKCoeffs709[0];
1825 Kg = studioCSCKCoeffs709[1];
1826 Kb = studioCSCKCoeffs709[2];
1828 // we keep luma unscaled to retain the levels present in source so that 16-235 luma is converted to RGB 16-235
1829 studioCSCMatrix[R][Y] = 1.0;
1830 studioCSCMatrix[G][Y] = 1.0;
1831 studioCSCMatrix[B][Y] = 1.0;
1833 studioCSCMatrix[R][Cb] = 0.0;
1834 studioCSCMatrix[G][Cb] = (double)-2 * Kb * (1 - Kb) / Kg;
1835 studioCSCMatrix[B][Cb] = (double)(1 - Kb) / 0.5;
1837 studioCSCMatrix[R][Cr] = (double)(1 - Kr) / 0.5;
1838 studioCSCMatrix[G][Cr] = (double)-2 * Kr * (1 - Kr) / Kg;
1839 studioCSCMatrix[B][Cr] = 0.0;
1841 studioCSCMatrix[R][C] = (double)-1 * studioCSCMatrix[R][Cr] * CDZ/EXC;
1842 studioCSCMatrix[G][C] = (double)-1 * (studioCSCMatrix[G][Cb] + studioCSCMatrix[G][Cr]) * CDZ/EXC;
1843 studioCSCMatrix[B][C] = (double)-1 * studioCSCMatrix[B][Cb] * CDZ/EXC;
1848 void CMixer::SetColor()
1852 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)
1853 m_Procamp.brightness = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)-50) / 100;
1854 if (m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)
1855 m_Procamp.contrast = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)+50) / 100;
1857 VdpColorStandard colorStandard;
1858 switch(m_mixerInput[1].DVDPic.color_matrix)
1860 case AVCOL_SPC_BT709:
1861 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1863 case AVCOL_SPC_BT470BG:
1864 case AVCOL_SPC_SMPTE170M:
1865 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1867 case AVCOL_SPC_SMPTE240M:
1868 colorStandard = VDP_COLOR_STANDARD_SMPTE_240M;
1871 case AVCOL_SPC_UNSPECIFIED:
1874 if(m_config.surfaceWidth > 1000)
1875 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1877 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1880 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX };
1881 if (CSettings::Get().GetBool("videoscreen.limitedrange"))
1883 float studioCSC[3][4];
1884 GenerateStudioCSCMatrix(colorStandard, studioCSC);
1885 void const * pm_CSCMatix[] = { &studioCSC };
1886 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1890 vdp_st = m_config.context->GetProcs().vdp_generate_csc_matrix(&m_Procamp, colorStandard, &m_CSCMatrix);
1891 void const * pm_CSCMatix[] = { &m_CSCMatrix };
1892 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1895 CheckStatus(vdp_st, __LINE__);
1898 void CMixer::SetNoiseReduction()
1900 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1903 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION };
1904 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL };
1907 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1909 VdpBool enabled[]= {0};
1910 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1911 CheckStatus(vdp_st, __LINE__);
1914 VdpBool enabled[]={1};
1915 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1916 CheckStatus(vdp_st, __LINE__);
1917 void* nr[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction };
1918 CLog::Log(LOGNOTICE,"Setting Noise Reduction to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction);
1919 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, nr);
1920 CheckStatus(vdp_st, __LINE__);
1923 void CMixer::SetSharpness()
1925 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1928 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS };
1929 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL };
1932 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1934 VdpBool enabled[]={0};
1935 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1936 CheckStatus(vdp_st, __LINE__);
1939 VdpBool enabled[]={1};
1940 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1941 CheckStatus(vdp_st, __LINE__);
1942 void* sh[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness };
1943 CLog::Log(LOGNOTICE,"Setting Sharpness to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness);
1944 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, sh);
1945 CheckStatus(vdp_st, __LINE__);
1948 EINTERLACEMETHOD CMixer::GetDeinterlacingMethod(bool log /* = false */)
1950 EINTERLACEMETHOD method = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1951 if (method == VS_INTERLACEMETHOD_AUTO)
1954 // if (m_config.outHeight >= 720)
1955 // deint = g_advancedSettings.m_videoVDPAUdeintHD;
1957 // deint = g_advancedSettings.m_videoVDPAUdeintSD;
1961 if (m_config.vdpau->Supports(EINTERLACEMETHOD(deint)))
1963 method = EINTERLACEMETHOD(deint);
1965 CLog::Log(LOGNOTICE, "CVDPAU::GetDeinterlacingMethod: set de-interlacing to %d", deint);
1970 CLog::Log(LOGWARNING, "CVDPAU::GetDeinterlacingMethod: method for de-interlacing (advanced settings) not supported");
1977 void CMixer::SetDeinterlacing()
1981 if (m_videoMixer == VDP_INVALID_HANDLE)
1984 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
1985 EINTERLACEMETHOD method = GetDeinterlacingMethod(true);
1987 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1988 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1989 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE };
1991 if (mode == VS_DEINTERLACEMODE_OFF)
1993 VdpBool enabled[] = {0,0,0};
1994 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1998 if (method == VS_INTERLACEMETHOD_AUTO)
2000 VdpBool enabled[] = {1,0,0};
2001 if (g_advancedSettings.m_videoVDPAUtelecine)
2003 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2005 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2006 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF)
2008 VdpBool enabled[] = {1,0,0};
2009 if (g_advancedSettings.m_videoVDPAUtelecine)
2011 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2013 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2014 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF)
2016 VdpBool enabled[] = {1,1,0};
2017 if (g_advancedSettings.m_videoVDPAUtelecine)
2019 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2023 VdpBool enabled[]={0,0,0};
2024 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2027 CheckStatus(vdp_st, __LINE__);
2029 SetDeintSkipChroma();
2031 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2034 void CMixer::SetDeintSkipChroma()
2036 VdpVideoMixerAttribute attribute[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
2040 if (g_advancedSettings.m_videoVDPAUdeintSkipChromaHD && m_config.outHeight >= 720)
2045 void const *values[]={&val};
2046 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attribute), attribute, values);
2048 CheckStatus(vdp_st, __LINE__);
2051 void CMixer::SetHWUpscaling()
2053 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
2056 VdpBool enabled[]={1};
2057 switch (m_config.upscale)
2060 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2062 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2063 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2067 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2069 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2070 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2074 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2076 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2077 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2081 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2083 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2084 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2088 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2090 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2091 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2095 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2097 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2098 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2102 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2104 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2105 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2109 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2111 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2112 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2116 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2118 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2119 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2126 CheckStatus(vdp_st, __LINE__);
2130 void CMixer::DisableHQScaling()
2134 if (m_videoMixer == VDP_INVALID_HANDLE)
2137 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
2139 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
2140 VdpBool enabled[]={0};
2141 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2142 CheckStatus(vdp_st, __LINE__);
2145 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2147 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2148 VdpBool enabled[]={0};
2149 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2150 CheckStatus(vdp_st, __LINE__);
2153 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2155 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2156 VdpBool enabled[]={0};
2157 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2158 CheckStatus(vdp_st, __LINE__);
2161 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2163 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2164 VdpBool enabled[]={0};
2165 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2166 CheckStatus(vdp_st, __LINE__);
2169 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2171 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2172 VdpBool enabled[]={0};
2173 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2174 CheckStatus(vdp_st, __LINE__);
2177 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2179 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2180 VdpBool enabled[]={0};
2181 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2182 CheckStatus(vdp_st, __LINE__);
2185 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2187 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2188 VdpBool enabled[]={0};
2189 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2190 CheckStatus(vdp_st, __LINE__);
2193 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2195 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2196 VdpBool enabled[]={0};
2197 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2198 CheckStatus(vdp_st, __LINE__);
2201 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2203 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2204 VdpBool enabled[]={0};
2205 vdp_st = m_config.context->GetProcs().vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2206 CheckStatus(vdp_st, __LINE__);
2214 m_NoiseReduction = 0.0;
2219 m_SeenInterlaceFlag = false;
2224 m_config.upscale = g_advancedSettings.m_videoVDPAUScaling;
2225 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2230 void CMixer::Uninit()
2233 while (!m_outputSurfaces.empty())
2235 m_outputSurfaces.pop();
2237 m_config.context->GetProcs().vdp_video_mixer_destroy(m_videoMixer);
2240 void CMixer::Flush()
2242 while (!m_mixerInput.empty())
2244 CVdpauDecodedPicture pic = m_mixerInput.back();
2245 m_mixerInput.pop_back();
2246 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2248 while (!m_decodedPics.empty())
2250 CVdpauDecodedPicture pic = m_decodedPics.front();
2251 m_decodedPics.pop();
2252 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2255 while (m_dataPort.ReceiveOutMessage(&msg))
2257 if (msg->signal == CMixerDataProtocol::FRAME)
2259 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2260 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2262 else if (msg->signal == CMixerDataProtocol::BUFFER)
2264 VdpOutputSurface *surf;
2265 surf = (VdpOutputSurface*)msg->data;
2266 m_outputSurfaces.push(*surf);
2272 void CMixer::InitCycle()
2277 m_config.stats->GetParams(latency, flags);
2279 if (0) //flags & DVP_FLAG_NO_POSTPROC)
2280 SetPostProcFeatures(false);
2282 SetPostProcFeatures(true);
2284 m_config.stats->SetCanSkipDeint(false);
2286 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
2287 EINTERLACEMETHOD method = GetDeinterlacingMethod();
2288 bool interlaced = m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_INTERLACED;
2289 m_SeenInterlaceFlag |= interlaced;
2292 if (//!(flags & DVP_FLAG_NO_POSTPROC) &&
2293 (mode == VS_DEINTERLACEMODE_FORCE ||
2294 (mode == VS_DEINTERLACEMODE_AUTO && interlaced)))
2296 if((method == VS_INTERLACEMETHOD_AUTO && interlaced)
2297 || method == VS_INTERLACEMETHOD_VDPAU_BOB
2298 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2299 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2300 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2301 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2302 || method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE )
2304 if(method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2305 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2306 || !g_graphicsContext.IsFullScreenVideo())
2311 m_config.stats->SetCanSkipDeint(true);
2315 if (0) //m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_DROPDEINT)
2320 if(m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_TOP_FIELD_FIRST)
2321 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2323 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2325 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2326 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2327 DVP_FLAG_REPEAT_TOP_FIELD |
2328 DVP_FLAG_INTERLACED);
2329 m_config.useInteropYuv = false;
2331 else if (method == VS_INTERLACEMETHOD_RENDER_BOB)
2334 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2335 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2336 m_config.useInteropYuv = true;
2340 CLog::Log(LOGERROR, "CMixer::%s - interlace method: %d not supported, setting to AUTO", __FUNCTION__, method);
2342 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2343 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2344 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2345 DVP_FLAG_REPEAT_TOP_FIELD |
2346 DVP_FLAG_INTERLACED);
2348 CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod = VS_INTERLACEMETHOD_AUTO;
2354 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2356 if (m_config.useInteropYuv)
2357 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2360 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2361 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2362 DVP_FLAG_REPEAT_TOP_FIELD |
2363 DVP_FLAG_INTERLACED);
2368 if (m_mixerInput[1].DVDPic.format == RENDER_FMT_VDPAU)
2370 m_processPicture.outputSurface = m_outputSurfaces.front();
2371 m_mixerInput[1].DVDPic.iWidth = m_config.outWidth;
2372 m_mixerInput[1].DVDPic.iHeight = m_config.outHeight;
2373 if (m_SeenInterlaceFlag)
2375 double ratio = (double)m_mixerInput[1].DVDPic.iDisplayHeight / m_mixerInput[1].DVDPic.iHeight;
2376 m_mixerInput[1].DVDPic.iHeight -= 6;
2377 m_mixerInput[1].DVDPic.iDisplayHeight = lrint(ratio*m_mixerInput[1].DVDPic.iHeight);
2382 m_mixerInput[1].DVDPic.iWidth = m_config.vidWidth;
2383 m_mixerInput[1].DVDPic.iHeight = m_config.vidHeight;
2386 m_processPicture.DVDPic = m_mixerInput[1].DVDPic;
2387 m_processPicture.videoSurface = m_mixerInput[1].videoSurface;
2390 void CMixer::FiniCycle()
2392 // Keep video surfaces for one 2 cycles longer than used
2393 // by mixer. This avoids blocking in decoder.
2394 // NVidia recommends num_ref + 5
2395 while (m_mixerInput.size() > 5)
2397 CVdpauDecodedPicture &tmp = m_mixerInput.back();
2398 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
2400 m_config.videoSurfaces->ClearRender(tmp.videoSurface);
2402 m_mixerInput.pop_back();
2406 void CMixer::ProcessPicture()
2408 if (m_processPicture.DVDPic.format == RENDER_FMT_VDPAU_420)
2413 if (m_mixerstep == 1)
2415 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD)
2416 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2418 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2421 VdpVideoSurface past_surfaces[4] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2422 VdpVideoSurface futu_surfaces[2] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2423 uint32_t pastCount = 4;
2424 uint32_t futuCount = 2;
2426 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
2428 // use only 2 past 1 future for progressive/weave
2429 // (only used for postproc anyway eg noise reduction)
2430 if (m_mixerInput.size() > 3)
2431 past_surfaces[1] = m_mixerInput[3].videoSurface;
2432 if (m_mixerInput.size() > 2)
2433 past_surfaces[0] = m_mixerInput[2].videoSurface;
2434 futu_surfaces[0] = m_mixerInput[0].videoSurface;
2440 if(m_mixerstep == 0)
2442 if (m_mixerInput.size() > 3)
2444 past_surfaces[3] = m_mixerInput[3].videoSurface;
2445 past_surfaces[2] = m_mixerInput[3].videoSurface;
2447 if (m_mixerInput.size() > 2)
2449 past_surfaces[1] = m_mixerInput[2].videoSurface;
2450 past_surfaces[0] = m_mixerInput[2].videoSurface;
2452 futu_surfaces[0] = m_mixerInput[1].videoSurface;
2453 futu_surfaces[1] = m_mixerInput[0].videoSurface;
2457 if (m_mixerInput.size() > 3)
2459 past_surfaces[3] = m_mixerInput[3].videoSurface;
2461 if (m_mixerInput.size() > 2)
2463 past_surfaces[2] = m_mixerInput[2].videoSurface;
2464 past_surfaces[1] = m_mixerInput[2].videoSurface;
2466 past_surfaces[0] = m_mixerInput[1].videoSurface;
2467 futu_surfaces[0] = m_mixerInput[0].videoSurface;
2468 futu_surfaces[1] = m_mixerInput[0].videoSurface;
2470 if (m_mixerInput[0].DVDPic.pts != DVD_NOPTS_VALUE &&
2471 m_mixerInput[1].DVDPic.pts != DVD_NOPTS_VALUE)
2473 m_processPicture.DVDPic.pts = m_mixerInput[1].DVDPic.pts +
2474 (m_mixerInput[0].DVDPic.pts -
2475 m_mixerInput[1].DVDPic.pts) / 2;
2478 m_processPicture.DVDPic.pts = DVD_NOPTS_VALUE;
2479 m_processPicture.DVDPic.dts = DVD_NOPTS_VALUE;
2481 m_processPicture.DVDPic.iRepeatPicture = 0.0;
2487 sourceRect.x1 = m_config.vidWidth;
2488 sourceRect.y1 = m_config.vidHeight;
2493 destRect.x1 = m_config.outWidth;
2494 destRect.y1 = m_config.outHeight;
2496 // start vdpau video mixer
2497 vdp_st = m_config.context->GetProcs().vdp_video_mixer_render(m_videoMixer,
2503 m_mixerInput[1].videoSurface,
2507 m_processPicture.outputSurface,
2512 CheckStatus(vdp_st, __LINE__);
2516 bool CMixer::CheckStatus(VdpStatus vdp_st, int line)
2518 if (vdp_st != VDP_STATUS_OK)
2520 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);
2527 //-----------------------------------------------------------------------------
2529 //-----------------------------------------------------------------------------
2531 VdpauBufferPool::VdpauBufferPool()
2533 CVdpauRenderPicture *pic;
2534 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2536 pic = new CVdpauRenderPicture(renderPicSec);
2537 allRenderPics.push_back(pic);
2541 VdpauBufferPool::~VdpauBufferPool()
2543 CVdpauRenderPicture *pic;
2544 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2546 pic = allRenderPics[i];
2549 allRenderPics.clear();
2552 //-----------------------------------------------------------------------------
2554 //-----------------------------------------------------------------------------
2555 COutput::COutput(CEvent *inMsgEvent) :
2556 CThread("Vdpau Output"),
2557 m_controlPort("OutputControlPort", inMsgEvent, &m_outMsgEvent),
2558 m_dataPort("OutputDataPort", inMsgEvent, &m_outMsgEvent),
2559 m_mixer(&m_outMsgEvent)
2561 m_inMsgEvent = inMsgEvent;
2563 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); ++i)
2565 m_bufferPool.freeRenderPics.push_back(i);
2569 void COutput::Start()
2578 m_bufferPool.freeRenderPics.clear();
2579 m_bufferPool.usedRenderPics.clear();
2582 void COutput::Dispose()
2584 CSingleLock lock(g_graphicsContext);
2586 m_outMsgEvent.Set();
2588 m_controlPort.Purge();
2592 void COutput::OnStartup()
2594 CLog::Log(LOGNOTICE, "COutput::OnStartup: Output Thread created");
2597 void COutput::OnExit()
2599 CLog::Log(LOGNOTICE, "COutput::OnExit: Output Thread terminated");
2606 O_TOP_UNCONFIGURED, // 2
2607 O_TOP_CONFIGURED, // 3
2608 O_TOP_CONFIGURED_IDLE, // 4
2609 O_TOP_CONFIGURED_WORK, // 5
2612 int VDPAU_OUTPUT_parentStates[] = {
2615 0, //TOP_UNCONFIGURED
2617 3, //TOP_CONFIGURED_IDLE
2618 3, //TOP_CONFIGURED_WORK
2621 void COutput::StateMachine(int signal, Protocol *port, Message *msg)
2623 for (int state = m_state; ; state = VDPAU_OUTPUT_parentStates[state])
2628 if (port == &m_controlPort)
2632 case COutputControlProtocol::FLUSH:
2633 msg->Reply(COutputControlProtocol::ACC);
2635 case COutputControlProtocol::PRECLEANUP:
2636 msg->Reply(COutputControlProtocol::ACC);
2642 else if (port == &m_dataPort)
2646 case COutputDataProtocol::RETURNPIC:
2647 CVdpauRenderPicture *pic;
2648 pic = *((CVdpauRenderPicture**)msg->data);
2649 QueueReturnPicture(pic);
2656 std::string portName = port == NULL ? "timer" : port->portName;
2657 CLog::Log(LOGWARNING, "COutput::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
2664 case O_TOP_UNCONFIGURED:
2665 if (port == &m_controlPort)
2669 case COutputControlProtocol::INIT:
2671 data = (CVdpauConfig*)msg->data;
2678 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::INIT,
2679 &reply, 1000, &m_config, sizeof(m_config)))
2681 if (reply->signal != CMixerControlProtocol::ACC)
2686 // set initial number of
2687 m_bufferPool.numOutputSurfaces = 4;
2691 m_state = O_TOP_CONFIGURED_IDLE;
2692 msg->Reply(COutputControlProtocol::ACC, &m_config, sizeof(m_config));
2696 m_state = O_TOP_ERROR;
2697 msg->Reply(COutputControlProtocol::ERROR);
2706 case O_TOP_CONFIGURED:
2707 if (port == &m_controlPort)
2711 case COutputControlProtocol::FLUSH:
2713 msg->Reply(COutputControlProtocol::ACC);
2715 case COutputControlProtocol::PRECLEANUP:
2718 msg->Reply(COutputControlProtocol::ACC);
2724 else if (port == &m_dataPort)
2728 case COutputDataProtocol::NEWFRAME:
2729 CVdpauDecodedPicture *frame;
2730 frame = (CVdpauDecodedPicture*)msg->data;
2733 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::FRAME,
2734 frame,sizeof(CVdpauDecodedPicture));
2737 case COutputDataProtocol::RETURNPIC:
2738 CVdpauRenderPicture *pic;
2739 pic = *((CVdpauRenderPicture**)msg->data);
2740 QueueReturnPicture(pic);
2741 m_controlPort.SendInMessage(COutputControlProtocol::STATS);
2742 m_state = O_TOP_CONFIGURED_WORK;
2749 else if (port == &m_mixer.m_dataPort)
2753 case CMixerDataProtocol::PICTURE:
2754 CVdpauProcessedPicture *pic;
2755 pic = (CVdpauProcessedPicture*)msg->data;
2756 m_bufferPool.processedPics.push(*pic);
2757 m_state = O_TOP_CONFIGURED_WORK;
2766 case O_TOP_CONFIGURED_IDLE:
2767 if (port == NULL) // timeout
2771 case COutputControlProtocol::TIMEOUT:
2772 if (ProcessSyncPicture())
2778 m_state = O_TOP_CONFIGURED_WORK;
2788 case O_TOP_CONFIGURED_WORK:
2789 if (port == NULL) // timeout
2793 case COutputControlProtocol::TIMEOUT:
2796 CVdpauRenderPicture *pic;
2797 pic = ProcessMixerPicture();
2800 m_config.stats->DecProcessed();
2801 m_config.stats->IncRender();
2802 m_dataPort.SendInMessage(COutputDataProtocol::PICTURE, &pic, sizeof(pic));
2808 m_state = O_TOP_CONFIGURED_IDLE;
2818 default: // we are in no state, should not happen
2819 CLog::Log(LOGERROR, "COutput::%s - no valid state: %d", __FUNCTION__, m_state);
2825 void COutput::Process()
2827 Message *msg = NULL;
2828 Protocol *port = NULL;
2831 m_state = O_TOP_UNCONFIGURED;
2832 m_extTimeout = 1000;
2833 m_bStateMachineSelfTrigger = false;
2839 if (m_bStateMachineSelfTrigger)
2841 m_bStateMachineSelfTrigger = false;
2842 // self trigger state machine
2843 StateMachine(msg->signal, port, msg);
2844 if (!m_bStateMachineSelfTrigger)
2851 // check control port
2852 else if (m_controlPort.ReceiveOutMessage(&msg))
2855 port = &m_controlPort;
2858 else if (m_dataPort.ReceiveOutMessage(&msg))
2863 // check mixer data port
2864 else if (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2867 port = &m_mixer.m_dataPort;
2871 StateMachine(msg->signal, port, msg);
2872 if (!m_bStateMachineSelfTrigger)
2881 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
2888 msg = m_controlPort.GetMessage();
2889 msg->signal = COutputControlProtocol::TIMEOUT;
2891 // signal timeout to state machine
2892 StateMachine(msg->signal, port, msg);
2893 if (!m_bStateMachineSelfTrigger)
2904 bool COutput::Init()
2906 if (!CreateGlxContext())
2918 bool COutput::Uninit()
2922 ReleaseBufferPool();
2923 DestroyGlxContext();
2927 void COutput::Flush()
2929 if (m_mixer.IsActive())
2932 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::FLUSH,
2939 CLog::Log(LOGERROR, "Coutput::%s - failed to flush mixer", __FUNCTION__);
2943 while (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2945 if (msg->signal == CMixerDataProtocol::PICTURE)
2947 CVdpauProcessedPicture pic = *(CVdpauProcessedPicture*)msg->data;
2948 m_bufferPool.processedPics.push(pic);
2953 while (m_dataPort.ReceiveOutMessage(&msg))
2955 if (msg->signal == COutputDataProtocol::NEWFRAME)
2957 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2958 m_config.videoSurfaces->ClearRender(pic.videoSurface);
2960 else if (msg->signal == COutputDataProtocol::RETURNPIC)
2962 CVdpauRenderPicture *pic;
2963 pic = *((CVdpauRenderPicture**)msg->data);
2964 QueueReturnPicture(pic);
2969 while (m_dataPort.ReceiveInMessage(&msg))
2971 if (msg->signal == COutputDataProtocol::PICTURE)
2973 CVdpauRenderPicture *pic;
2974 pic = *((CVdpauRenderPicture**)msg->data);
2975 QueueReturnPicture(pic);
2979 // reset used render flag which was cleared on mixer flush
2980 std::deque<int>::iterator it;
2981 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
2983 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[*it];
2984 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
2986 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it2;
2987 it2 = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
2988 if (it2 == m_bufferPool.glVideoSurfaceMap.end())
2990 CLog::Log(LOGDEBUG, "COutput::Flush - gl surface not found");
2993 m_config.videoSurfaces->MarkRender(it2->second.sourceVuv);
2997 // clear processed pics
2998 while(!m_bufferPool.processedPics.empty())
3000 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3001 if (procPic.DVDPic.format == RENDER_FMT_VDPAU)
3003 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &procPic.outputSurface, sizeof(procPic.outputSurface));
3005 else if (procPic.DVDPic.format == RENDER_FMT_VDPAU_420)
3007 m_config.videoSurfaces->ClearRender(procPic.videoSurface);
3009 m_bufferPool.processedPics.pop();
3013 bool COutput::HasWork()
3015 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3020 CVdpauRenderPicture* COutput::ProcessMixerPicture()
3022 CVdpauRenderPicture *retPic = NULL;
3024 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
3026 int idx = m_bufferPool.freeRenderPics.front();
3027 retPic = m_bufferPool.allRenderPics[idx];
3028 m_bufferPool.freeRenderPics.pop_front();
3029 m_bufferPool.usedRenderPics.push_back(idx);
3030 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
3031 m_bufferPool.processedPics.pop();
3033 retPic->DVDPic = procPic.DVDPic;
3034 retPic->valid = true;
3035 if (retPic->DVDPic.format == RENDER_FMT_VDPAU)
3037 m_config.useInteropYuv = false;
3038 m_bufferPool.numOutputSurfaces = NUM_RENDER_PICS;
3041 retPic->sourceIdx = procPic.outputSurface;
3042 retPic->texture[0] = m_bufferPool.glOutputSurfaceMap[procPic.outputSurface].texture[0];
3043 retPic->texWidth = m_config.outWidth;
3044 retPic->texHeight = m_config.outHeight;
3045 retPic->crop.x1 = 0;
3046 retPic->crop.y1 = (m_config.outHeight - retPic->DVDPic.iHeight) / 2;
3047 retPic->crop.x2 = m_config.outWidth;
3048 retPic->crop.y2 = m_config.outHeight - retPic->crop.y1;
3052 m_config.useInteropYuv = true;
3054 retPic->sourceIdx = procPic.videoSurface;
3055 for (unsigned int i=0; i<4; ++i)
3056 retPic->texture[i] = m_bufferPool.glVideoSurfaceMap[procPic.videoSurface].texture[i];
3057 retPic->texWidth = m_config.surfaceWidth;
3058 retPic->texHeight = m_config.surfaceHeight;
3059 retPic->crop.x1 = 0;
3060 retPic->crop.y1 = 0;
3061 retPic->crop.x2 = m_config.surfaceWidth - m_config.vidWidth;
3062 retPic->crop.y2 = m_config.surfaceHeight - m_config.vidHeight;
3068 void COutput::QueueReturnPicture(CVdpauRenderPicture *pic)
3070 std::deque<int>::iterator it;
3071 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3073 if (m_bufferPool.allRenderPics[*it] == pic)
3079 if (it == m_bufferPool.usedRenderPics.end())
3081 CLog::Log(LOGWARNING, "COutput::QueueReturnPicture - pic not found");
3085 // check if already queued
3086 std::deque<int>::iterator it2 = find(m_bufferPool.syncRenderPics.begin(),
3087 m_bufferPool.syncRenderPics.end(),
3089 if (it2 == m_bufferPool.syncRenderPics.end())
3091 m_bufferPool.syncRenderPics.push_back(*it);
3094 ProcessSyncPicture();
3097 bool COutput::ProcessSyncPicture()
3099 CVdpauRenderPicture *pic;
3102 std::deque<int>::iterator it;
3103 for (it = m_bufferPool.syncRenderPics.begin(); it != m_bufferPool.syncRenderPics.end(); )
3105 pic = m_bufferPool.allRenderPics[*it];
3110 if (glIsSync(pic->fence))
3114 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3115 if(state == GL_SIGNALED)
3117 glDeleteSync(pic->fence);
3130 m_bufferPool.freeRenderPics.push_back(*it);
3132 std::deque<int>::iterator it2 = find(m_bufferPool.usedRenderPics.begin(),
3133 m_bufferPool.usedRenderPics.end(),
3135 if (it2 == m_bufferPool.usedRenderPics.end())
3137 CLog::Log(LOGERROR, "COutput::ProcessSyncPicture - pic not found in queue");
3141 m_bufferPool.usedRenderPics.erase(it2);
3143 it = m_bufferPool.syncRenderPics.erase(it);
3147 ProcessReturnPicture(pic);
3151 CLog::Log(LOGDEBUG, "COutput::%s - return of invalid render pic", __FUNCTION__);
3157 void COutput::ProcessReturnPicture(CVdpauRenderPicture *pic)
3159 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3161 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3162 it = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3163 if (it == m_bufferPool.glVideoSurfaceMap.end())
3165 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3168 VdpVideoSurface surf = it->second.sourceVuv;
3169 m_config.videoSurfaces->ClearRender(surf);
3171 else if (pic->DVDPic.format == RENDER_FMT_VDPAU)
3173 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3174 it = m_bufferPool.glOutputSurfaceMap.find(pic->sourceIdx);
3175 if (it == m_bufferPool.glOutputSurfaceMap.end())
3177 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3180 VdpOutputSurface outSurf = it->second.sourceRgb;
3181 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &outSurf, sizeof(outSurf));
3185 bool COutput::EnsureBufferPool()
3189 // Creation of outputSurfaces
3190 VdpOutputSurface outputSurface;
3191 for (int i = m_bufferPool.outputSurfaces.size(); i < m_bufferPool.numOutputSurfaces; i++)
3193 vdp_st = m_config.context->GetProcs().vdp_output_surface_create(m_config.context->GetDevice(),
3194 VDP_RGBA_FORMAT_B8G8R8A8,
3198 if (CheckStatus(vdp_st, __LINE__))
3200 m_bufferPool.outputSurfaces.push_back(outputSurface);
3202 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3204 sizeof(VdpOutputSurface));
3205 CLog::Log(LOGNOTICE, "VDPAU::COutput::InitBufferPool - Output Surface created");
3210 void COutput::ReleaseBufferPool()
3214 CSingleLock lock(m_bufferPool.renderPicSec);
3216 // release all output surfaces
3217 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3219 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3221 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3222 CheckStatus(vdp_st, __LINE__);
3224 m_bufferPool.outputSurfaces.clear();
3226 // wait for all fences
3227 XbmcThreads::EndTime timeout(1000);
3228 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3230 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[i];
3234 while (glIsSync(pic->fence))
3238 glGetSynciv(pic->fence, GL_SYNC_STATUS, 1, &length, &state);
3239 if(state == GL_SIGNALED || timeout.IsTimePast())
3241 glDeleteSync(pic->fence);
3252 if (timeout.IsTimePast())
3254 CLog::Log(LOGERROR, "COutput::%s - timeout waiting for fence", __FUNCTION__);
3256 ProcessSyncPicture();
3258 // invalidate all used render pictures
3259 for (unsigned int i = 0; i < m_bufferPool.usedRenderPics.size(); ++i)
3261 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[m_bufferPool.usedRenderPics[i]];
3266 void COutput::PreCleanup()
3272 ProcessSyncPicture();
3274 CSingleLock lock(m_bufferPool.renderPicSec);
3275 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3277 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3280 // check if output surface is in use
3282 std::deque<int>::iterator it;
3283 CVdpauRenderPicture *pic;
3284 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3286 pic = m_bufferPool.allRenderPics[*it];
3287 if ((pic->sourceIdx == m_bufferPool.outputSurfaces[i]) && pic->valid)
3296 #ifdef GL_NV_vdpau_interop
3298 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it_map;
3299 it_map = m_bufferPool.glOutputSurfaceMap.find(m_bufferPool.outputSurfaces[i]);
3300 if (it_map == m_bufferPool.glOutputSurfaceMap.end())
3302 CLog::Log(LOGERROR, "%s - could not find gl surface", __FUNCTION__);
3305 glVDPAUUnregisterSurfaceNV(it_map->second.glVdpauSurface);
3306 glDeleteTextures(1, it_map->second.texture);
3307 m_bufferPool.glOutputSurfaceMap.erase(it_map);
3310 vdp_st = m_config.context->GetProcs().vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3311 CheckStatus(vdp_st, __LINE__);
3313 m_bufferPool.outputSurfaces[i] = VDP_INVALID_HANDLE;
3315 CLog::Log(LOGDEBUG, "VDPAU::PreCleanup - released output surface");
3320 void COutput::InitMixer()
3322 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3324 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3325 &m_bufferPool.outputSurfaces[i],
3326 sizeof(VdpOutputSurface));
3330 bool COutput::GLInit()
3332 #ifdef GL_NV_vdpau_interop
3333 glVDPAUInitNV = NULL;
3334 glVDPAUFiniNV = NULL;
3335 glVDPAURegisterOutputSurfaceNV = NULL;
3336 glVDPAURegisterVideoSurfaceNV = NULL;
3337 glVDPAUIsSurfaceNV = NULL;
3338 glVDPAUUnregisterSurfaceNV = NULL;
3339 glVDPAUSurfaceAccessNV = NULL;
3340 glVDPAUMapSurfacesNV = NULL;
3341 glVDPAUUnmapSurfacesNV = NULL;
3342 glVDPAUGetSurfaceivNV = NULL;
3345 #ifdef GL_NV_vdpau_interop
3347 glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUInitNV");
3349 glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((GLubyte *) "glVDPAUFiniNV");
3350 if (!glVDPAURegisterOutputSurfaceNV)
3351 glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterOutputSurfaceNV");
3352 if (!glVDPAURegisterVideoSurfaceNV)
3353 glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterVideoSurfaceNV");
3354 if (!glVDPAUIsSurfaceNV)
3355 glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUIsSurfaceNV");
3356 if (!glVDPAUUnregisterSurfaceNV)
3357 glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnregisterSurfaceNV");
3358 if (!glVDPAUSurfaceAccessNV)
3359 glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUSurfaceAccessNV");
3360 if (!glVDPAUMapSurfacesNV)
3361 glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUMapSurfacesNV");
3362 if (!glVDPAUUnmapSurfacesNV)
3363 glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnmapSurfacesNV");
3364 if (!glVDPAUGetSurfaceivNV)
3365 glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUGetSurfaceivNV");
3367 while (glGetError() != GL_NO_ERROR);
3368 glVDPAUInitNV(reinterpret_cast<void*>(m_config.context->GetDevice()), reinterpret_cast<void*>(m_config.context->GetProcs().vdp_get_proc_address));
3369 if (glGetError() != GL_NO_ERROR)
3371 CLog::Log(LOGERROR, "VDPAU::COutput - GLInitInterop glVDPAUInitNV failed");
3375 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop initialized");
3379 bool hasfence = glewIsSupported("GL_ARB_sync");
3380 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); i++)
3382 m_bufferPool.allRenderPics[i]->usefence = hasfence;
3389 void COutput::GLMapSurfaces()
3391 #ifdef GL_NV_vdpau_interop
3393 if (m_config.useInteropYuv)
3395 VdpauBufferPool::GLVideoSurface glSurface;
3396 VdpVideoSurface surf;
3397 if (m_config.videoSurfaces->Size() != m_bufferPool.glVideoSurfaceMap.size())
3399 for (unsigned int i = 0; i < m_config.videoSurfaces->Size(); i++)
3401 surf = m_config.videoSurfaces->GetAtIndex(i);
3403 if (surf == VDP_INVALID_HANDLE)
3406 if (m_bufferPool.glVideoSurfaceMap.find(surf) == m_bufferPool.glVideoSurfaceMap.end())
3408 glSurface.sourceVuv = surf;
3409 while (glGetError() != GL_NO_ERROR) ;
3410 glGenTextures(4, glSurface.texture);
3411 if (glGetError() != GL_NO_ERROR)
3413 CLog::Log(LOGERROR, "VDPAU::COutput error creating texture");
3416 glSurface.glVdpauSurface = glVDPAURegisterVideoSurfaceNV(reinterpret_cast<void*>(surf),
3417 GL_TEXTURE_2D, 4, glSurface.texture);
3419 if (glGetError() != GL_NO_ERROR)
3421 CLog::Log(LOGERROR, "VDPAU::COutput error register video surface");
3424 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3425 if (glGetError() != GL_NO_ERROR)
3427 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3430 glVDPAUMapSurfacesNV(1, &glSurface.glVdpauSurface);
3431 if (glGetError() != GL_NO_ERROR)
3433 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3436 m_bufferPool.glVideoSurfaceMap[surf] = glSurface;
3439 CLog::Log(LOGNOTICE, "VDPAU::COutput registered surface");
3446 if (m_bufferPool.glOutputSurfaceMap.size() != m_bufferPool.numOutputSurfaces)
3448 VdpauBufferPool::GLVideoSurface glSurface;
3449 for (unsigned int i = m_bufferPool.glOutputSurfaceMap.size(); i<m_bufferPool.outputSurfaces.size(); i++)
3451 glSurface.sourceRgb = m_bufferPool.outputSurfaces[i];
3452 glGenTextures(1, glSurface.texture);
3453 glSurface.glVdpauSurface = glVDPAURegisterOutputSurfaceNV(reinterpret_cast<void*>(m_bufferPool.outputSurfaces[i]),
3454 GL_TEXTURE_2D, 1, glSurface.texture);
3455 if (glGetError() != GL_NO_ERROR)
3457 CLog::Log(LOGERROR, "VDPAU::COutput error register output surface");
3460 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3461 if (glGetError() != GL_NO_ERROR)
3463 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3466 glVDPAUMapSurfacesNV(1, &glSurface.glVdpauSurface);
3467 if (glGetError() != GL_NO_ERROR)
3469 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3472 m_bufferPool.glOutputSurfaceMap[m_bufferPool.outputSurfaces[i]] = glSurface;
3476 CLog::Log(LOGNOTICE, "VDPAU::COutput registered output surfaces");
3482 void COutput::GLUnmapSurfaces()
3484 #ifdef GL_NV_vdpau_interop
3487 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3488 for (it = m_bufferPool.glVideoSurfaceMap.begin(); it != m_bufferPool.glVideoSurfaceMap.end(); ++it)
3490 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3491 glDeleteTextures(4, it->second.texture);
3493 m_bufferPool.glVideoSurfaceMap.clear();
3496 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3497 for (it = m_bufferPool.glOutputSurfaceMap.begin(); it != m_bufferPool.glOutputSurfaceMap.end(); ++it)
3499 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3500 glDeleteTextures(1, it->second.texture);
3502 m_bufferPool.glOutputSurfaceMap.clear();
3506 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop finished");
3511 bool COutput::CheckStatus(VdpStatus vdp_st, int line)
3513 if (vdp_st != VDP_STATUS_OK)
3515 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);
3522 bool COutput::CreateGlxContext()
3524 GLXContext glContext;
3526 m_Display = g_Windowing.GetDisplay();
3527 glContext = g_Windowing.GetGlxContext();
3528 m_Window = g_Windowing.GetWindow();
3530 // Get our window attribs.
3531 XWindowAttributes wndattribs;
3532 XGetWindowAttributes(m_Display, m_Window, &wndattribs);
3535 XVisualInfo visInfo;
3536 visInfo.visualid = wndattribs.visual->visualid;
3538 XVisualInfo* visuals = XGetVisualInfo(m_Display, VisualIDMask, &visInfo, &nvisuals);
3541 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - could not find visual");
3544 visInfo = visuals[0];
3547 m_pixmap = XCreatePixmap(m_Display,
3554 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - Unable to create XPixmap");
3559 m_glPixmap = glXCreateGLXPixmap(m_Display, &visInfo, m_pixmap);
3563 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not create glPixmap");
3567 m_glContext = glXCreateContext(m_Display, &visInfo, glContext, True);
3569 if (!glXMakeCurrent(m_Display, m_glPixmap, m_glContext))
3571 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not make Pixmap current");
3575 CLog::Log(LOGNOTICE, "VDPAU::COutput::CreateGlxContext - created context");
3579 bool COutput::DestroyGlxContext()
3583 glXMakeCurrent(m_Display, None, NULL);
3584 glXDestroyContext(m_Display, m_glContext);
3589 glXDestroyPixmap(m_Display, m_glPixmap);
3593 XFreePixmap(m_Display, m_pixmap);