/xbmc/cores/dvdplayer/DVDCodecs/Video/VDPAU.cpp
http://github.com/xbmc/xbmc · C++ · 3624 lines · 3084 code · 410 blank · 130 comment · 602 complexity · 8e1d48e662ca0fb9bcbbc97751e74240 MD5 · raw file
Large files are truncated click here to view the full file
- /*
- * Copyright (C) 2005-2013 Team XBMC
- * http://xbmc.org
- *
- * This Program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * This Program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with XBMC; see the file COPYING. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- */
- #include "system.h"
- #ifdef HAVE_LIBVDPAU
- #include <dlfcn.h>
- #include "windowing/WindowingFactory.h"
- #include "VDPAU.h"
- #include "guilib/GraphicContext.h"
- #include "guilib/TextureManager.h"
- #include "cores/VideoRenderers/RenderManager.h"
- #include "DVDVideoCodecFFmpeg.h"
- #include "DVDClock.h"
- #include "settings/Settings.h"
- #include "settings/AdvancedSettings.h"
- #include "settings/MediaSettings.h"
- #include "Application.h"
- #include "utils/MathUtils.h"
- #include "utils/TimeUtils.h"
- #include "DVDCodecs/DVDCodecUtils.h"
- #include "cores/VideoRenderers/RenderFlags.h"
- using namespace VDPAU;
- #define NUM_RENDER_PICS 7
- #define ARSIZE(x) (sizeof(x) / sizeof((x)[0]))
- CDecoder::Desc decoder_profiles[] = {
- {"MPEG1", VDP_DECODER_PROFILE_MPEG1},
- {"MPEG2_SIMPLE", VDP_DECODER_PROFILE_MPEG2_SIMPLE},
- {"MPEG2_MAIN", VDP_DECODER_PROFILE_MPEG2_MAIN},
- {"H264_BASELINE",VDP_DECODER_PROFILE_H264_BASELINE},
- {"H264_MAIN", VDP_DECODER_PROFILE_H264_MAIN},
- {"H264_HIGH", VDP_DECODER_PROFILE_H264_HIGH},
- {"VC1_SIMPLE", VDP_DECODER_PROFILE_VC1_SIMPLE},
- {"VC1_MAIN", VDP_DECODER_PROFILE_VC1_MAIN},
- {"VC1_ADVANCED", VDP_DECODER_PROFILE_VC1_ADVANCED},
- #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
- {"MPEG4_PART2_ASP", VDP_DECODER_PROFILE_MPEG4_PART2_ASP},
- #endif
- };
- const size_t decoder_profile_count = sizeof(decoder_profiles)/sizeof(CDecoder::Desc);
- static struct SInterlaceMapping
- {
- const EINTERLACEMETHOD method;
- const VdpVideoMixerFeature feature;
- } g_interlace_mapping[] =
- { {VS_INTERLACEMETHOD_VDPAU_TEMPORAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
- , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL}
- , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL , VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
- , {VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF, VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL}
- , {VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE , VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE}
- , {VS_INTERLACEMETHOD_NONE , (VdpVideoMixerFeature)-1}
- };
- static float studioCSCKCoeffs601[3] = {0.299, 0.587, 0.114}; //BT601 {Kr, Kg, Kb}
- static float studioCSCKCoeffs709[3] = {0.2126, 0.7152, 0.0722}; //BT709 {Kr, Kg, Kb}
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- CVDPAUContext *CVDPAUContext::m_context = 0;
- CCriticalSection CVDPAUContext::m_section;
- Display *CVDPAUContext::m_display = 0;
- void *CVDPAUContext::m_dlHandle = 0;
- CVDPAUContext::CVDPAUContext()
- {
- m_context = 0;
- m_refCount = 0;
- }
- void CVDPAUContext::Release()
- {
- CSingleLock lock(m_section);
- m_refCount--;
- if (m_refCount <= 0)
- {
- Close();
- delete this;
- m_context = 0;
- }
- }
- void CVDPAUContext::Close()
- {
- CLog::Log(LOGNOTICE, "VDPAU::Close - closing decoder context");
- DestroyContext();
- }
- bool CVDPAUContext::EnsureContext(CVDPAUContext **ctx)
- {
- CSingleLock lock(m_section);
- if (m_context)
- {
- m_context->m_refCount++;
- *ctx = m_context;
- return true;
- }
- m_context = new CVDPAUContext();
- *ctx = m_context;
- {
- CSingleLock gLock(g_graphicsContext);
- if (!m_context->LoadSymbols() || !m_context->CreateContext())
- {
- delete m_context;
- m_context = 0;
- return false;
- }
- }
- m_context->m_refCount++;
- *ctx = m_context;
- return true;
- }
- VDPAU_procs& CVDPAUContext::GetProcs()
- {
- return m_vdpProcs;
- }
- VdpVideoMixerFeature* CVDPAUContext::GetFeatures()
- {
- return m_vdpFeatures;
- }
- int CVDPAUContext::GetFeatureCount()
- {
- return m_featureCount;
- }
- bool CVDPAUContext::LoadSymbols()
- {
- if (!m_dlHandle)
- {
- m_dlHandle = dlopen("libvdpau.so.1", RTLD_LAZY);
- if (!m_dlHandle)
- {
- const char* error = dlerror();
- if (!error)
- error = "dlerror() returned NULL";
- CLog::Log(LOGERROR,"VDPAU::LoadSymbols: Unable to get handle to lib: %s", error);
- return false;
- }
- }
- char* error;
- (void)dlerror();
- dl_vdp_device_create_x11 = (VdpStatus (*)(Display*, int, VdpDevice*, VdpStatus (**)(VdpDevice, VdpFuncId, void**)))dlsym(m_dlHandle, (const char*)"vdp_device_create_x11");
- error = dlerror();
- if (error)
- {
- CLog::Log(LOGERROR,"(VDPAU) - %s in %s",error,__FUNCTION__);
- m_vdpDevice = VDP_INVALID_HANDLE;
- return false;
- }
- return true;
- }
- bool CVDPAUContext::CreateContext()
- {
- CLog::Log(LOGNOTICE,"VDPAU::CreateContext - creating decoder context");
- int mScreen;
- { CSingleLock lock(g_graphicsContext);
- if (!m_display)
- m_display = XOpenDisplay(NULL);
- mScreen = g_Windowing.GetCurrentScreen();
- }
- VdpStatus vdp_st;
- // Create Device
- vdp_st = dl_vdp_device_create_x11(m_display,
- mScreen,
- &m_vdpDevice,
- &m_vdpProcs.vdp_get_proc_address);
- CLog::Log(LOGNOTICE,"vdp_device = 0x%08x vdp_st = 0x%08x",m_vdpDevice,vdp_st);
- if (vdp_st != VDP_STATUS_OK)
- {
- CLog::Log(LOGERROR,"(VDPAU) unable to init VDPAU - vdp_st = 0x%x. Falling back.",vdp_st);
- m_vdpDevice = VDP_INVALID_HANDLE;
- return false;
- }
- QueryProcs();
- SpewHardwareAvailable();
- return true;
- }
- void CVDPAUContext::QueryProcs()
- {
- VdpStatus vdp_st;
- #define VDP_PROC(id, proc) \
- do { \
- vdp_st = m_vdpProcs.vdp_get_proc_address(m_vdpDevice, id, (void**)&proc); \
- if (vdp_st != VDP_STATUS_OK) \
- { \
- CLog::Log(LOGERROR, "CVDPAUContext::GetProcs - failed to get proc id"); \
- } \
- } while(0);
- VDP_PROC(VDP_FUNC_ID_GET_ERROR_STRING , m_vdpProcs.vdp_get_error_string);
- VDP_PROC(VDP_FUNC_ID_DEVICE_DESTROY , m_vdpProcs.vdp_device_destroy);
- VDP_PROC(VDP_FUNC_ID_GENERATE_CSC_MATRIX , m_vdpProcs.vdp_generate_csc_matrix);
- VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_CREATE , m_vdpProcs.vdp_video_surface_create);
- VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY , m_vdpProcs.vdp_video_surface_destroy);
- VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_put_bits_y_cb_cr);
- VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR , m_vdpProcs.vdp_video_surface_get_bits_y_cb_cr);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_Y_CB_CR , m_vdpProcs.vdp_output_surface_put_bits_y_cb_cr);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE , m_vdpProcs.vdp_output_surface_put_bits_native);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_CREATE , m_vdpProcs.vdp_output_surface_create);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY , m_vdpProcs.vdp_output_surface_destroy);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE , m_vdpProcs.vdp_output_surface_get_bits_native);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, m_vdpProcs.vdp_output_surface_render_output_surface);
- VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED , m_vdpProcs.vdp_output_surface_put_bits_indexed);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_CREATE , m_vdpProcs.vdp_video_mixer_create);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES , m_vdpProcs.vdp_video_mixer_set_feature_enables);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_DESTROY , m_vdpProcs.vdp_video_mixer_destroy);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_RENDER , m_vdpProcs.vdp_video_mixer_render);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES , m_vdpProcs.vdp_video_mixer_set_attribute_values);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_PARAMETER_SUPPORT , m_vdpProcs.vdp_video_mixer_query_parameter_support);
- VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_FEATURE_SUPPORT , m_vdpProcs.vdp_video_mixer_query_feature_support);
- VDP_PROC(VDP_FUNC_ID_DECODER_CREATE , m_vdpProcs.vdp_decoder_create);
- VDP_PROC(VDP_FUNC_ID_DECODER_DESTROY , m_vdpProcs.vdp_decoder_destroy);
- VDP_PROC(VDP_FUNC_ID_DECODER_RENDER , m_vdpProcs.vdp_decoder_render);
- VDP_PROC(VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES , m_vdpProcs.vdp_decoder_query_caps);
- #undef VDP_PROC
- }
- VdpDevice CVDPAUContext::GetDevice()
- {
- return m_vdpDevice;
- }
- void CVDPAUContext::DestroyContext()
- {
- if (!m_vdpProcs.vdp_device_destroy)
- return;
- m_vdpProcs.vdp_device_destroy(m_vdpDevice);
- m_vdpDevice = VDP_INVALID_HANDLE;
- }
- void CVDPAUContext::SpewHardwareAvailable() //CopyrighVDPAUt (c) 2008 Wladimir J. van der Laan -- VDPInfo
- {
- VdpStatus rv;
- CLog::Log(LOGNOTICE,"VDPAU Decoder capabilities:");
- CLog::Log(LOGNOTICE,"name level macbs width height");
- CLog::Log(LOGNOTICE,"------------------------------------");
- for(unsigned int x=0; x<decoder_profile_count; ++x)
- {
- VdpBool is_supported = false;
- uint32_t max_level, max_macroblocks, max_width, max_height;
- rv = m_vdpProcs.vdp_decoder_query_caps(m_vdpDevice, decoder_profiles[x].id,
- &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
- if(rv == VDP_STATUS_OK && is_supported)
- {
- CLog::Log(LOGNOTICE,"%-16s %2i %5i %5i %5i\n", decoder_profiles[x].name,
- max_level, max_macroblocks, max_width, max_height);
- }
- }
- CLog::Log(LOGNOTICE,"------------------------------------");
- m_featureCount = 0;
- #define CHECK_SUPPORT(feature) \
- do { \
- VdpBool supported; \
- if(m_vdpProcs.vdp_video_mixer_query_feature_support(m_vdpDevice, feature, &supported) == VDP_STATUS_OK && supported) { \
- CLog::Log(LOGNOTICE, "Mixer feature: "#feature); \
- m_vdpFeatures[m_featureCount++] = feature; \
- } \
- } while(false)
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_SHARPNESS);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE);
- #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8);
- CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9);
- #endif
- #undef CHECK_SUPPORT
- }
- bool CVDPAUContext::Supports(VdpVideoMixerFeature feature)
- {
- for(int i = 0; i < m_featureCount; i++)
- {
- if(m_vdpFeatures[i] == feature)
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- // VDPAU Video Surface states
- //-----------------------------------------------------------------------------
- #define SURFACE_USED_FOR_REFERENCE 0x01
- #define SURFACE_USED_FOR_RENDER 0x02
- void CVideoSurfaces::AddSurface(VdpVideoSurface surf)
- {
- CSingleLock lock(m_section);
- m_state[surf] = SURFACE_USED_FOR_REFERENCE;
- }
- void CVideoSurfaces::ClearReference(VdpVideoSurface surf)
- {
- CSingleLock lock(m_section);
- if (m_state.find(surf) == m_state.end())
- {
- CLog::Log(LOGWARNING, "CVideoSurfaces::ClearReference - surface invalid");
- return;
- }
- m_state[surf] &= ~SURFACE_USED_FOR_REFERENCE;
- if (m_state[surf] == 0)
- {
- m_freeSurfaces.push_back(surf);
- }
- }
- bool CVideoSurfaces::MarkRender(VdpVideoSurface surf)
- {
- CSingleLock lock(m_section);
- if (m_state.find(surf) == m_state.end())
- {
- CLog::Log(LOGWARNING, "CVideoSurfaces::MarkRender - surface invalid");
- return false;
- }
- std::list<VdpVideoSurface>::iterator it;
- it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
- if (it != m_freeSurfaces.end())
- {
- m_freeSurfaces.erase(it);
- }
- m_state[surf] |= SURFACE_USED_FOR_RENDER;
- return true;
- }
- void CVideoSurfaces::ClearRender(VdpVideoSurface surf)
- {
- CSingleLock lock(m_section);
- if (m_state.find(surf) == m_state.end())
- {
- CLog::Log(LOGWARNING, "CVideoSurfaces::ClearRender - surface invalid");
- return;
- }
- m_state[surf] &= ~SURFACE_USED_FOR_RENDER;
- if (m_state[surf] == 0)
- {
- m_freeSurfaces.push_back(surf);
- }
- }
- bool CVideoSurfaces::IsValid(VdpVideoSurface surf)
- {
- CSingleLock lock(m_section);
- if (m_state.find(surf) != m_state.end())
- return true;
- else
- return false;
- }
- VdpVideoSurface CVideoSurfaces::GetFree(VdpVideoSurface surf)
- {
- CSingleLock lock(m_section);
- if (m_state.find(surf) != m_state.end())
- {
- std::list<VdpVideoSurface>::iterator it;
- it = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
- if (it == m_freeSurfaces.end())
- {
- CLog::Log(LOGWARNING, "CVideoSurfaces::GetFree - surface not free");
- }
- else
- {
- m_freeSurfaces.erase(it);
- m_state[surf] = SURFACE_USED_FOR_REFERENCE;
- return surf;
- }
- }
- if (!m_freeSurfaces.empty())
- {
- VdpVideoSurface freeSurf = m_freeSurfaces.front();
- m_freeSurfaces.pop_front();
- m_state[freeSurf] = SURFACE_USED_FOR_REFERENCE;
- return freeSurf;
- }
- return VDP_INVALID_HANDLE;
- }
- VdpVideoSurface CVideoSurfaces::GetAtIndex(int idx)
- {
- if (idx >= m_state.size())
- return VDP_INVALID_HANDLE;
- std::map<VdpVideoSurface, int>::iterator it = m_state.begin();
- for(int i = 0; i < idx; i++)
- ++it;
- return it->first;
- }
- VdpVideoSurface CVideoSurfaces::RemoveNext(bool skiprender)
- {
- CSingleLock lock(m_section);
- VdpVideoSurface surf;
- std::map<VdpVideoSurface, int>::iterator it;
- for(it = m_state.begin(); it != m_state.end(); ++it)
- {
- if (skiprender && it->second & SURFACE_USED_FOR_RENDER)
- continue;
- surf = it->first;
- m_state.erase(surf);
- std::list<VdpVideoSurface>::iterator it2;
- it2 = std::find(m_freeSurfaces.begin(), m_freeSurfaces.end(), surf);
- if (it2 != m_freeSurfaces.end())
- m_freeSurfaces.erase(it2);
- return surf;
- }
- return VDP_INVALID_HANDLE;
- }
- void CVideoSurfaces::Reset()
- {
- CSingleLock lock(m_section);
- m_freeSurfaces.clear();
- m_state.clear();
- }
- int CVideoSurfaces::Size()
- {
- CSingleLock lock(m_section);
- return m_state.size();
- }
- //-----------------------------------------------------------------------------
- // CVDPAU
- //-----------------------------------------------------------------------------
- CDecoder::CDecoder() : m_vdpauOutput(&m_inMsgEvent)
- {
- m_vdpauConfig.videoSurfaces = &m_videoSurfaces;
- m_vdpauConfigured = false;
- m_hwContext.bitstream_buffers_allocated = 0;
- m_DisplayState = VDPAU_OPEN;
- m_vdpauConfig.context = 0;
- }
- bool CDecoder::Open(AVCodecContext* avctx, const enum PixelFormat, unsigned int surfaces)
- {
- if(avctx->coded_width == 0
- || avctx->coded_height == 0)
- {
- CLog::Log(LOGWARNING,"(VDPAU) no width/height available, can't init");
- return false;
- }
- m_vdpauConfig.numRenderBuffers = surfaces;
- m_decoderThread = CThread::GetCurrentThreadId();
- if ((avctx->codec_id == AV_CODEC_ID_MPEG4) && !g_advancedSettings.m_videoAllowMpeg4VDPAU)
- return false;
- if (!CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
- return false;
- m_DisplayState = VDPAU_OPEN;
- m_vdpauConfigured = false;
- if (!m_dllAvUtil.Load())
- return false;
- m_presentPicture = 0;
- {
- VdpDecoderProfile profile = 0;
- if(avctx->codec_id == AV_CODEC_ID_H264)
- profile = VDP_DECODER_PROFILE_H264_HIGH;
- #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
- else if(avctx->codec_id == AV_CODEC_ID_MPEG4)
- profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
- #endif
- if(profile)
- {
- if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
- CLog::Log(LOGWARNING,"(VDPAU) width %i might not be supported because of hardware bug", avctx->width);
-
- /* attempt to create a decoder with this width/height, some sizes are not supported by hw */
- VdpStatus vdp_st;
- vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(), profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
- if(vdp_st != VDP_STATUS_OK)
- {
- CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) checking for decoder support\n", m_vdpauConfig.context->GetProcs().vdp_get_error_string(vdp_st), vdp_st);
- return false;
- }
- m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
- CheckStatus(vdp_st, __LINE__);
- }
- /* finally setup ffmpeg */
- memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
- m_hwContext.render = CDecoder::Render;
- m_hwContext.bitstream_buffers_allocated = 0;
- avctx->get_buffer = CDecoder::FFGetBuffer;
- avctx->reget_buffer = CDecoder::FFGetBuffer;
- avctx->release_buffer = CDecoder::FFReleaseBuffer;
- avctx->draw_horiz_band = CDecoder::FFDrawSlice;
- avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
- avctx->hwaccel_context = &m_hwContext;
- avctx->thread_count = 1;
- g_Windowing.Register(this);
- return true;
- }
- return false;
- }
- CDecoder::~CDecoder()
- {
- Close();
- }
- void CDecoder::Close()
- {
- CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
- g_Windowing.Unregister(this);
- CSingleLock lock(m_DecoderSection);
- FiniVDPAUOutput();
- m_vdpauOutput.Dispose();
- if (m_hwContext.bitstream_buffers_allocated)
- {
- m_dllAvUtil.av_freep(&m_hwContext.bitstream_buffers);
- }
- m_dllAvUtil.Unload();
- if (m_vdpauConfig.context)
- m_vdpauConfig.context->Release();
- m_vdpauConfig.context = 0;
- }
- long CDecoder::Release()
- {
- // check if we should do some pre-cleanup here
- // a second decoder might need resources
- if (m_vdpauConfigured == true)
- {
- CSingleLock lock(m_DecoderSection);
- CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
- Message *reply;
- if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
- &reply,
- 2000))
- {
- bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
- reply->Release();
- if (!success)
- {
- CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
- m_DisplayState = VDPAU_ERROR;
- }
- }
- else
- {
- CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
- m_DisplayState = VDPAU_ERROR;
- }
- VdpVideoSurface surf;
- while((surf = m_videoSurfaces.RemoveNext(true)) != VDP_INVALID_HANDLE)
- {
- m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
- }
- }
- return IHardwareDecoder::Release();
- }
- long CDecoder::ReleasePicReference()
- {
- return IHardwareDecoder::Release();
- }
- void CDecoder::SetWidthHeight(int width, int height)
- {
- m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
- //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
- //this requires the least amount of gpu memory bandwidth
- if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
- {
- //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
- if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
- {
- m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
- m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
- }
- else //scale height to the desktop size if the aspect ratio is smaller than the desktop
- {
- m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
- m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
- }
- }
- else
- { //let opengl scale
- m_vdpauConfig.outWidth = width;
- m_vdpauConfig.outHeight = height;
- }
- CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
- }
- void CDecoder::OnLostDevice()
- {
- CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
- int count = g_graphicsContext.exit();
- CSingleLock lock(m_DecoderSection);
- FiniVDPAUOutput();
- if (m_vdpauConfig.context)
- m_vdpauConfig.context->Release();
- m_vdpauConfig.context = 0;
- m_DisplayState = VDPAU_LOST;
- lock.Leave();
- m_DisplayEvent.Reset();
- g_graphicsContext.restore(count);
- }
- void CDecoder::OnResetDevice()
- {
- CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
- int count = g_graphicsContext.exit();
- CSingleLock lock(m_DecoderSection);
- if (m_DisplayState == VDPAU_LOST)
- {
- m_DisplayState = VDPAU_RESET;
- lock.Leave();
- m_DisplayEvent.Set();
- }
- g_graphicsContext.restore(count);
- }
- int CDecoder::Check(AVCodecContext* avctx)
- {
- EDisplayState state;
- { CSingleLock lock(m_DecoderSection);
- state = m_DisplayState;
- }
- if (state == VDPAU_LOST)
- {
- CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
- if (!m_DisplayEvent.WaitMSec(4000))
- {
- CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
- state = VDPAU_RESET;
- }
- else
- {
- CSingleLock lock(m_DecoderSection);
- state = m_DisplayState;
- }
- }
- if (state == VDPAU_RESET || state == VDPAU_ERROR)
- {
- CSingleLock lock(m_DecoderSection);
- FiniVDPAUOutput();
- if (m_vdpauConfig.context)
- m_vdpauConfig.context->Release();
- m_vdpauConfig.context = 0;
- if (CVDPAUContext::EnsureContext(&m_vdpauConfig.context))
- {
- m_DisplayState = VDPAU_OPEN;
- m_vdpauConfigured = false;
- }
- if (state == VDPAU_RESET)
- return VC_FLUSHED;
- else
- return VC_ERROR;
- }
- return 0;
- }
- bool CDecoder::IsVDPAUFormat(PixelFormat format)
- {
- if (format == AV_PIX_FMT_VDPAU)
- return true;
- else
- return false;
- }
- bool CDecoder::Supports(VdpVideoMixerFeature feature)
- {
- return m_vdpauConfig.context->Supports(feature);
- }
- bool CDecoder::Supports(EINTERLACEMETHOD method)
- {
- if(method == VS_INTERLACEMETHOD_VDPAU_BOB
- || method == VS_INTERLACEMETHOD_AUTO)
- return true;
- if (method == VS_INTERLACEMETHOD_RENDER_BOB)
- return true;
- if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
- return false;
- for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
- {
- if(p->method == method)
- return Supports(p->feature);
- }
- return false;
- }
- EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
- {
- return VS_INTERLACEMETHOD_RENDER_BOB;
- }
- void CDecoder::FiniVDPAUOutput()
- {
- if (!m_vdpauConfigured)
- return;
- CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
- // uninit output
- m_vdpauOutput.Dispose();
- m_vdpauConfigured = false;
- VdpStatus vdp_st;
- vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
- if (CheckStatus(vdp_st, __LINE__))
- return;
- m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
- CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", m_videoSurfaces.Size());
- VdpVideoSurface surf;
- while((surf = m_videoSurfaces.RemoveNext()) != VDP_INVALID_HANDLE)
- {
- m_vdpauConfig.context->GetProcs().vdp_video_surface_destroy(surf);
- if (CheckStatus(vdp_st, __LINE__))
- return;
- }
- m_videoSurfaces.Reset();
- }
- void CDecoder::ReadFormatOf( AVCodecID codec
- , VdpDecoderProfile &vdp_decoder_profile
- , VdpChromaType &vdp_chroma_type)
- {
- switch (codec)
- {
- case AV_CODEC_ID_MPEG1VIDEO:
- vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
- vdp_chroma_type = VDP_CHROMA_TYPE_420;
- break;
- case AV_CODEC_ID_MPEG2VIDEO:
- vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
- vdp_chroma_type = VDP_CHROMA_TYPE_420;
- break;
- case AV_CODEC_ID_H264:
- vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
- vdp_chroma_type = VDP_CHROMA_TYPE_420;
- break;
- case AV_CODEC_ID_WMV3:
- vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
- vdp_chroma_type = VDP_CHROMA_TYPE_420;
- break;
- case AV_CODEC_ID_VC1:
- vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
- vdp_chroma_type = VDP_CHROMA_TYPE_420;
- break;
- case AV_CODEC_ID_MPEG4:
- vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
- vdp_chroma_type = VDP_CHROMA_TYPE_420;
- break;
- default:
- vdp_decoder_profile = 0;
- vdp_chroma_type = 0;
- break;
- }
- }
- bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
- {
- FiniVDPAUOutput();
- VdpStatus vdp_st;
- VdpDecoderProfile vdp_decoder_profile;
- m_vdpauConfig.vidWidth = avctx->width;
- m_vdpauConfig.vidHeight = avctx->height;
- m_vdpauConfig.surfaceWidth = avctx->coded_width;
- m_vdpauConfig.surfaceHeight = avctx->coded_height;
- SetWidthHeight(avctx->width,avctx->height);
- CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
- CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
- ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
- if(avctx->codec_id == AV_CODEC_ID_H264)
- {
- m_vdpauConfig.maxReferences = ref_frames;
- if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
- if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
- }
- else
- m_vdpauConfig.maxReferences = 2;
- vdp_st = m_vdpauConfig.context->GetProcs().vdp_decoder_create(m_vdpauConfig.context->GetDevice(),
- vdp_decoder_profile,
- m_vdpauConfig.surfaceWidth,
- m_vdpauConfig.surfaceHeight,
- m_vdpauConfig.maxReferences,
- &m_vdpauConfig.vdpDecoder);
- if (CheckStatus(vdp_st, __LINE__))
- return false;
- // initialize output
- CSingleLock lock(g_graphicsContext);
- m_vdpauConfig.stats = &m_bufferStats;
- m_vdpauConfig.vdpau = this;
- m_bufferStats.Reset();
- m_vdpauOutput.Start();
- Message *reply;
- if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::INIT,
- &reply,
- 2000,
- &m_vdpauConfig,
- sizeof(m_vdpauConfig)))
- {
- bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
- reply->Release();
- if (!success)
- {
- CLog::Log(LOGERROR, "VDPAU::%s - vdpau output returned error", __FUNCTION__);
- m_vdpauOutput.Dispose();
- return false;
- }
- }
- else
- {
- CLog::Log(LOGERROR, "VDPAU::%s - failed to init output", __FUNCTION__);
- m_vdpauOutput.Dispose();
- return false;
- }
- m_inMsgEvent.Reset();
- m_vdpauConfigured = true;
- return true;
- }
- int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic)
- {
- //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
- CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
- CDecoder* vdp = (CDecoder*)ctx->GetHardware();
- // while we are waiting to recover we can't do anything
- CSingleLock lock(vdp->m_DecoderSection);
- if(vdp->m_DisplayState != VDPAU_OPEN)
- {
- CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
- return -1;
- }
- VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
- surf = vdp->m_videoSurfaces.GetFree(surf != 0 ? surf : VDP_INVALID_HANDLE);
- VdpStatus vdp_st = VDP_STATUS_ERROR;
- if (surf == VDP_INVALID_HANDLE)
- {
- // create a new surface
- VdpDecoderProfile profile;
- ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
- vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_video_surface_create(vdp->m_vdpauConfig.context->GetDevice(),
- vdp->m_vdpauConfig.vdpChromaType,
- avctx->coded_width,
- avctx->coded_height,
- &surf);
- vdp->CheckStatus(vdp_st, __LINE__);
- if (vdp_st != VDP_STATUS_OK)
- {
- CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
- return -1;
- }
- vdp->m_videoSurfaces.AddSurface(surf);
- }
- pic->data[1] = pic->data[2] = NULL;
- pic->data[0] = (uint8_t*)(uintptr_t)surf;
- pic->data[3] = (uint8_t*)(uintptr_t)surf;
- pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
- pic->type= FF_BUFFER_TYPE_USER;
- pic->reordered_opaque= avctx->reordered_opaque;
- return 0;
- }
- void CDecoder::FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic)
- {
- CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
- CDecoder* vdp = (CDecoder*)ctx->GetHardware();
- VdpVideoSurface surf;
- unsigned int i;
- CSingleLock lock(vdp->m_DecoderSection);
- surf = (VdpVideoSurface)(uintptr_t)pic->data[3];
- vdp->m_videoSurfaces.ClearReference(surf);
- for(i=0; i<4; i++)
- pic->data[i]= NULL;
- }
- VdpStatus CDecoder::Render( VdpDecoder decoder, VdpVideoSurface target,
- VdpPictureInfo const *picture_info,
- uint32_t bitstream_buffer_count,
- VdpBitstreamBuffer const * bitstream_buffers)
- {
- return VDP_STATUS_OK;
- }
- void CDecoder::FFDrawSlice(struct AVCodecContext *s,
- const AVFrame *src, int offset[4],
- int y, int type, int height)
- {
- CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
- CDecoder* vdp = (CDecoder*)ctx->GetHardware();
- // while we are waiting to recover we can't do anything
- CSingleLock lock(vdp->m_DecoderSection);
- if(vdp->m_DisplayState != VDPAU_OPEN)
- return;
- if(src->linesize[0] || src->linesize[1] || src->linesize[2]
- || offset[0] || offset[1] || offset[2])
- {
- CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
- return;
- }
- VdpStatus vdp_st;
- VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
- // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
- if (!vdp->m_videoSurfaces.IsValid(surf))
- {
- CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
- return;
- }
- uint32_t max_refs = 0;
- if(s->codec_id == AV_CODEC_ID_H264)
- max_refs = vdp->m_hwContext.info.h264.num_ref_frames;
- if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
- || vdp->m_vdpauConfigured == false
- || vdp->m_vdpauConfig.maxReferences < max_refs)
- {
- if(!vdp->ConfigVDPAU(s, max_refs))
- return;
- }
- uint64_t startTime = CurrentHostCounter();
- uint16_t decoded, processed, rend;
- vdp->m_bufferStats.Get(decoded, processed, rend);
- vdp_st = vdp->m_vdpauConfig.context->GetProcs().vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
- surf,
- (VdpPictureInfo const *)&(vdp->m_hwContext.info),
- vdp->m_hwContext.bitstream_buffers_used,
- vdp->m_hwContext.bitstream_buffers);
- vdp->CheckStatus(vdp_st, __LINE__);
- uint64_t diff = CurrentHostCounter() - startTime;
- if (diff*1000/CurrentHostFrequency() > 30)
- CLog::Log(LOGDEBUG, "CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
- }
- int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
- {
- int result = Check(avctx);
- if (result)
- return result;
- CSingleLock lock(m_DecoderSection);
- if (!m_vdpauConfigured)
- return VC_ERROR;
- if(pFrame)
- { // we have a new frame from decoder
- VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)pFrame->data[3];
- // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
- if (!m_videoSurfaces.IsValid(surf))
- {
- CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
- return VC_BUFFER;
- }
- m_videoSurfaces.MarkRender(surf);
- // send frame to output for processing
- CVdpauDecodedPicture pic;
- memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
- ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
- pic.videoSurface = surf;
- pic.DVDPic.color_matrix = avctx->colorspace;
- m_bufferStats.IncDecoded();
- m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
- //TODO
- // m_codecControl = pic.DVDPic.iFlags & (DVP_FLAG_DRAIN | DVP_FLAG_NO_POSTPROC);
- }
- int retval = 0;
- uint16_t decoded, processed, render;
- Message *msg;
- while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
- {
- if (msg->signal == COutputControlProtocol::ERROR)
- {
- m_DisplayState = VDPAU_ERROR;
- retval |= VC_ERROR;
- }
- msg->Release();
- }
- m_bufferStats.Get(decoded, processed, render);
- uint64_t startTime = CurrentHostCounter();
- while (!retval)
- {
- // first fill the buffers to keep vdpau busy
- // mixer will run with decoded >= 2. output is limited by number of output surfaces
- // In case mixer is bypassed we limit by looking at processed
- if (decoded < 3 && processed < 3)
- {
- retval |= VC_BUFFER;
- }
- else if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
- {
- if (msg->signal == COutputDataProtocol::PICTURE)
- {
- if (m_presentPicture)
- {
- m_presentPicture->ReturnUnused();
- m_presentPicture = 0;
- }
- m_presentPicture = *(CVdpauRenderPicture**)msg->data;
- m_presentPicture->vdpau = this;
- m_bufferStats.DecRender();
- m_bufferStats.Get(decoded, processed, render);
- retval |= VC_PICTURE;
- msg->Release();
- break;
- }
- msg->Release();
- }
- else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
- {
- if (msg->signal == COutputControlProtocol::STATS)
- {
- m_bufferStats.Get(decoded, processed, render);
- }
- else
- {
- m_DisplayState = VDPAU_ERROR;
- retval |= VC_ERROR;
- }
- msg->Release();
- }
- if (decoded < 3 && processed < 3)
- {
- retval |= VC_BUFFER;
- }
- if (!retval && !m_inMsgEvent.WaitMSec(2000))
- break;
- }
- uint64_t diff = CurrentHostCounter() - startTime;
- if (retval & VC_PICTURE)
- {
- m_bufferStats.SetParams(diff, m_codecControl);
- }
- if (diff*1000/CurrentHostFrequency() > 50)
- CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
- if (!retval)
- {
- CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
- m_DisplayState = VDPAU_ERROR;
- retval |= VC_ERROR;
- }
- return retval;
- }
- bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
- {
- CSingleLock lock(m_DecoderSection);
- if (m_DisplayState != VDPAU_OPEN)
- return false;
- *picture = m_presentPicture->DVDPic;
- picture->vdpau = m_presentPicture;
- return true;
- }
- void CDecoder::Reset()
- {
- CSingleLock lock(m_DecoderSection);
- if (!m_vdpauConfigured)
- return;
- Message *reply;
- if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
- &reply,
- 2000))
- {
- bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
- reply->Release();
- if (!success)
- {
- CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
- m_DisplayState = VDPAU_ERROR;
- }
- else
- m_bufferStats.Reset();
- }
- else
- {
- CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
- m_DisplayState = VDPAU_ERROR;
- }
- }
- bool CDecoder::CanSkipDeint()
- {
- return m_bufferStats.CanSkipDeint();
- }
- void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
- {
- m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
- }
- bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
- {
- if (vdp_st != VDP_STATUS_OK)
- {
- 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);
- if(m_DisplayState == VDPAU_OPEN)
- {
- if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
- {
- m_DisplayEvent.Reset();
- m_DisplayState = VDPAU_LOST;
- }
- else
- m_DisplayState = VDPAU_ERROR;
- }
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- // RenderPicture
- //-----------------------------------------------------------------------------
- CVdpauRenderPicture* CVdpauRenderPicture::Acquire()
- {
- CSingleLock lock(renderPicSection);
- if (refCount == 0)
- vdpau->Acquire();
- refCount++;
- return this;
- }
- long CVdpauRenderPicture::Release()
- {
- CSingleLock lock(renderPicSection);
- refCount--;
- if (refCount > 0)
- return refCount;
- lock.Leave();
- vdpau->ReturnRenderPicture(this);
- vdpau->ReleasePicReference();
- return refCount;
- }
- void CVdpauRenderPicture::ReturnUnused()
- {
- { CSingleLock lock(renderPicSection);
- if (refCount > 0)
- return;
- }
- if (vdpau)
- vdpau->ReturnRenderPicture(this);
- }
- void CVdpauRenderPicture::Sync()
- {
- #ifdef GL_ARB_sync
- CSingleLock lock(renderPicSection);
- if (usefence)
- {
- if(glIsSync(fence))
- {
- glDeleteSync(fence);
- fence = None;
- }
- fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
- }
- #endif
- }
- //-----------------------------------------------------------------------------
- // Mixer
- //-----------------------------------------------------------------------------
- CMixer::CMixer(CEvent *inMsgEvent) :
- CThread("Vdpau Mixer"),
- m_controlPort("ControlPort", inMsgEvent, &m_outMsgEvent),
- m_dataPort("DataPort", inMsgEvent, &m_outMsgEvent)
- {
- m_inMsgEvent = inMsgEvent;
- }
- CMixer::~CMixer()
- {
- Dispose();
- }
- void CMixer::Start()
- {
- Create();
- }
- void CMixer::Dispose()
- {
- m_bStop = true;
- m_outMsgEvent.Set();
- StopThread();
- m_controlPort.Purge();
- m_dataPort.Purge();
- }
- bool CMixer::IsActive()
- {
- return IsRunning();
- }
- void CMixer::OnStartup()
- {
- CLog::Log(LOGNOTICE, "CMixer::OnStartup: Output Thread created");
- }
- void CMixer::OnExit()
- {
- CLog::Log(LOGNOTICE, "CMixer::OnExit: Output Thread terminated");
- }
- enum MIXER_STATES
- {
- M_TOP = 0, // 0
- M_TOP_ERROR, // 1
- M_TOP_UNCONFIGURED, // 2
- M_TOP_CONFIGURED, // 3
- M_TOP_CONFIGURED_WAIT1, // 4
- M_TOP_CONFIGURED_STEP1, // 5
- M_TOP_CONFIGURED_WAIT2, // 6
- M_TOP_CONFIGURED_STEP2, // 7
- };
- int MIXER_parentStates[] = {
- -1,
- 0, //TOP_ERROR
- 0, //TOP_UNCONFIGURED
- 0, //TOP_CONFIGURED
- 3, //TOP_CONFIGURED_WAIT1
- 3, //TOP_CONFIGURED_STEP1
- 3, //TOP_CONFIGURED_WAIT2
- 3, //TOP_CONFIGURED_STEP2
- };
- void CMixer::StateMachine(int signal, Protocol *port, Message *msg)
- {
- for (int state = m_state; ; state = MIXER_parentStates[state])
- {
- switch (state)
- {
- case M_TOP: // TOP
- if (port == &m_controlPort)
- {
- switch (signal)
- {
- case CMixerControlProtocol::FLUSH:
- Flush();
- msg->Reply(CMixerControlProtocol::ACC);
- return;
- default:
- break;
- }
- }
- {
- std::string portName = port == NULL ? "timer" : port->portName;
- CLog::Log(LOGWARNING, "CMixer::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
- }
- return;
- case M_TOP_ERROR: // TOP
- break;
- case M_TOP_UNCONFIGURED:
- if (port == &m_controlPort)
- {
- switch (signal)
- {
- case CMixerControlProtocol::INIT:
- CVdpauConfig *data;
- data = (CVdpauConfig*)msg->data;
- if (data)
- {
- m_config = *data;
- }
- Init();
- if (!m_vdpError)
- {
- m_state = M_TOP_CONFIGURED_WAIT1;
- msg->Reply(CMixerControlProtocol::ACC);
- }
- else
- {
- msg->Reply(CMixerControlProtocol::ERROR);
- }
- return;
- default:
- break;
- }
- }
- break;
- case M_TOP_CONFIGURED:
- if (port == &m_dataPort)
- {
- switch (signal)
- {
- case CMixerDataProtocol::FRAME:
- CVdpauDecodedPicture *frame;
- frame = (CVdpauDecodedPicture*)msg->data;
- if (frame)
- {
- m_decodedPics.push(*frame);
- }
- m_extTimeout = 0;
- return;
- case CMixerDataProtocol::BUFFER:
- VdpOutputSurface *surf;
- surf = (VdpOutputSurface*)msg->data;
- if (surf)
- {
- m_outputSurfaces.push(*surf);
- }
- m_extTimeout = 0;
- return;
- default:
- break;
- }
- }
- break;
- case M_TOP_CONFIGURED_WAIT1:
- if (port == NULL) // timeout
- {
- switch (signal)
- {
- case CMixerControlProtocol::TIMEOUT:
- if (!m_decodedPics.empty() && !m_outputSurfaces.empty())
- {
- m_state = M_TOP_CONFIGURED_STEP1;
- m_bStateMachineSelfTrigger = true;
- }
- else
- {
- m_extTimeout = 100;
- }
- return;
- default:
- break;
- }
- }
- break;
- case M_TOP_CONFIGURED_STEP1:
- if (port == NULL) // timeout
- {
- switch (signal)
- {
- case CMixerControlProtocol::TIMEOUT:
- m_mixerInput.push_front(m_decodedPics.front());
- m_decodedPics.pop();
- if (m_mixerInput.size() < 2)
- {
- m_state = M_TOP_CONFIGURED_WAIT1;
- m_extTimeout = 0;
- return;
- }
- InitCycle();
- ProcessPicture();
- if (m_vdpError)
- {
- m_state = M_TOP_CONFIGURED_WAIT1;
- m_extTimeout = 1000;
- return;
- }
- if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
- m_outputSurfaces.pop();
- m_config.stats->IncProcessed();
- m_config.stats->DecDecoded();
- m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
- if (m_mixersteps > 1)
- {
- m_state = M_TOP_CONFIGURED_WAIT2;
- m_extTimeout = 0;
- }
- else
- {
- FiniCycle();
- m_state = M_TOP_CONFIGURED_WAIT1;
- m_extTimeout = 0;
- }
- return;
- default:
- break;
- }
- }
- break;
- case M_TOP_CONFIGURED_WAIT2:
- if (port == NULL) // timeout
- {
- switch (signal)
- {
- case CMixerControlProtocol::TIMEOUT:
- if (!m_outputSurfaces.empty())
- {
- m_state = M_TOP_CONFIGURED_STEP2;
- m_bStateMachineSelfTrigger = true;
- }
- else
- {
- m_extTimeout = 100;
- }
- return;
- default:
- break;
- }
- }
- break;
- case M_TOP_CONFIGURED_STEP2:
- if (port == NULL) // timeout
- {
- switch (signal)
- {
- case CMixerControlProtocol::TIMEOUT:
- m_processPicture.outputSurface = m_outputSurfaces.front();
- m_mixerstep = 1;
- ProcessPicture();
- if (m_vdpError)
- {
- m_state = M_TOP_CONFIGURED_WAIT1;
- m_extTimeout = 1000;
- return;
- }
- if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
- m_outputSurfaces.pop();
- m_config.stats->IncProcessed();
- m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
- FiniCycle();
- m_state = M_TOP_CONFIGURED_WAIT1;
- m_extTimeout = 0;
- return;
- default:
- break;
- }
- }
- break;
- default: // we are in no state, should not happen
- CLog::Log(LOGERROR, "CMixer::%s - no valid state: %d", __FUNCTION__, m_state);
- return;
- }
- } // for
- }
- void CMixer::Process()
- {
- Message *msg = NULL;
- Protocol *port = NULL;
- bool gotMsg;
- m_state = M_TOP_UNCONFIGURED;
- m_extTimeout = 1000;
- m_bStateMachineSelfTrigger = false;
- while (!m_bStop)
- {
- gotMsg = false;
- if (m_bStateMachineSelfTrigger)
- {
- m_bStateMachineSelfTrigger = false;
- // self trigger state machine
- StateMachine(msg->signal, port, msg);
- if (!m_bStateMachineSelfTrigger)
- {
- msg->Release();
- msg = NULL;
- }
- continue;
- }
- // check control port
- else if (m_controlPort.ReceiveOutMessage(&msg))
- {
- gotMsg = true;
- port = &m_controlPort;
- }
- // check data port
- else if (m_dataPort.ReceiveOutMessage(&msg))
- {
- gotMsg = true;
- port = &m_dataPort;
- }
- if (gotMsg)
- {
- StateMachine(msg->signal, port, msg);
- if (!m_bStateMachineSelfTrigger)
- {
- msg->Release();
- msg = NULL;
- }
- continue;
- }
- // wait for message
- else if (m_outMsgEvent.WaitMSec(m_extTimeout))
- {
- continue;
- }
- // time out
- else
- {
- msg = m_controlPort.GetMessage();
- msg->signal = CMixerControlProtocol::TIMEOUT;
- port = 0;
- // signal timeout to state machine
- StateMachine(msg->signal, port, msg);
- if (!m_bStateMachineSelfTrigger)
- {
- msg->Release();
- msg = NULL;
- }
- }
- }
- Uninit();
- }
- void CMixer::CreateVdpauMixer()
- {
- CLog::Log(LOGNOTICE, " (VDPAU) Creating the video mixer");
- InitCSCMatrix(m_config.vidWidth);
- VdpVideoMixerParameter parameters[] = {
- VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
- VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
- VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE};
- void const * parameter_values[] = {
- &m_config.surfaceWidth,
- &m_config.surfaceHeight,
- &m_config.vdpChromaType};
- VdpStatus vdp_st = VDP_STATUS_ERROR;
- vdp_st = m_config.context->GetProcs().vdp_video_mixer_create(m_config.context->GetDevice(),
- m_config.context->GetFeatureCount(),
- m_config.context->GetFeatures(),
- ARSIZE(parameters),
- parameters,
- parameter_values,
- &m_videoMixer);
- CheckStatus(vdp_st, __LINE__);
- // create 3 pitches of black lines needed for clipping top
- // and bottom lines when de-interlacing
- m_BlackBar = new uint32_t[3*m_config.outWidth];
- memset(m_BlackBar, 0, 3*m_config.outWidth*sizeof(uint32_t));
- }
- void CMixer::InitCSCMatrix(int Width)
- {
- m_Procamp.struct_version = VDP_PROCAMP_VERSION;
- m_Procamp.brightness = 0.0;
- m_Procamp.contrast = 1.0;
- m_Procamp.saturation = 1.0;
- m_Procamp.hue = 0;
- }
- void CMixer::CheckFeatures()
- {
- if (m_Upscale != m_config.upscale)
- {
- SetHWUpscaling();
- m_Upscale = m_config.upscale;
- }
- if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness ||
- m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast ||
- m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
- {
- SetColor();
- m_Brightness = CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness;
- m_Contrast = CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast;
- m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
- }
- if (m_NoiseReduction != CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
- {
- m_NoiseReduction = CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction;
- SetNoiseReduction();
- }
- if (m_Sharpness != CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
- {
- m_Sharpness = CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness;
- SetSharpness();
- }
- if (m_DeintMode != CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode ||
- m_Deint != CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod)
- {
- m_DeintMode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
- m_Deint = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
- SetDeinterlacing();
- }
- }
- void CMixer::SetPostProcFeatures(bool postProcEnabled)
- {
- if (m_PostProc != postProcEnabled)
- {
- if (postProcEnabled)
- {
- SetNoiseReduction();
- SetSharpness();
- SetDeinterlacing();
- SetHWUpscaling();
- }
- else
- PostProcOff();
- m_PostProc = postProcEnabled;
- }
- }
- void CMixer::P…