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 9
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 //since libvdpau 0.4, vdp_device_create_x11() installs a callback on the Display*,
78 //if we unload libvdpau with dlclose(), we segfault on XCloseDisplay,
79 //so we just keep a static handle to libvdpau around
80 void* CDecoder::dl_handle;
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 CDecoder::CDecoder() : m_vdpauOutput(&m_inMsgEvent)
88 m_vdpauConfig.vdpDevice = VDP_INVALID_HANDLE;
89 m_vdpauConfig.videoSurfaces = &m_videoSurfaces;
90 m_vdpauConfig.videoSurfaceSec = &m_videoSurfaceSec;
92 m_vdpauConfigured = false;
93 m_hwContext.bitstream_buffers_allocated = 0;
94 m_DisplayState = VDPAU_OPEN;
97 bool CDecoder::Open(AVCodecContext* avctx, const enum PixelFormat, unsigned int surfaces)
99 if(avctx->coded_width == 0
100 || avctx->coded_height == 0)
102 CLog::Log(LOGWARNING,"(VDPAU) no width/height available, can't init");
105 m_vdpauConfig.numRenderBuffers = surfaces;
106 m_decoderThread = CThread::GetCurrentThreadId();
108 if ((avctx->codec_id == AV_CODEC_ID_MPEG4) && !g_advancedSettings.m_videoAllowMpeg4VDPAU)
113 dl_handle = dlopen("libvdpau.so.1", RTLD_LAZY);
116 const char* error = dlerror();
118 error = "dlerror() returned NULL";
120 CLog::Log(LOGNOTICE,"(VDPAU) Unable to get handle to libvdpau: %s", error);
125 if (!m_dllAvUtil.Load())
129 m_presentPicture = 0;
131 if (m_vdpauConfig.vdpDevice != VDP_INVALID_HANDLE)
133 SpewHardwareAvailable();
135 VdpDecoderProfile profile = 0;
136 if(avctx->codec_id == AV_CODEC_ID_H264)
137 profile = VDP_DECODER_PROFILE_H264_HIGH;
138 #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
139 else if(avctx->codec_id == AV_CODEC_ID_MPEG4)
140 profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
144 if (!CDVDCodecUtils::IsVP3CompatibleWidth(avctx->coded_width))
145 CLog::Log(LOGWARNING,"(VDPAU) width %i might not be supported because of hardware bug", avctx->width);
147 /* attempt to create a decoder with this width/height, some sizes are not supported by hw */
149 vdp_st = m_vdpauConfig.vdpProcs.vdp_decoder_create(m_vdpauConfig.vdpDevice, profile, avctx->coded_width, avctx->coded_height, 5, &m_vdpauConfig.vdpDecoder);
151 if(vdp_st != VDP_STATUS_OK)
153 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) checking for decoder support\n", m_vdpauConfig.vdpProcs.vdp_get_error_string(vdp_st), vdp_st);
158 m_vdpauConfig.vdpProcs.vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
159 CheckStatus(vdp_st, __LINE__);
162 /* finally setup ffmpeg */
163 memset(&m_hwContext, 0, sizeof(AVVDPAUContext));
164 m_hwContext.render = CDecoder::Render;
165 m_hwContext.bitstream_buffers_allocated = 0;
166 avctx->get_buffer = CDecoder::FFGetBuffer;
167 avctx->release_buffer = CDecoder::FFReleaseBuffer;
168 avctx->draw_horiz_band = CDecoder::FFDrawSlice;
169 avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
170 avctx->hwaccel_context = &m_hwContext;
171 avctx->thread_count = 1;
173 g_Windowing.Register(this);
179 CDecoder::~CDecoder()
184 void CDecoder::Close()
186 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
188 g_Windowing.Unregister(this);
190 CSingleLock lock(m_DecoderSection);
194 m_vdpauOutput.Dispose();
196 while (!m_videoSurfaces.empty())
198 vdpau_render_state *render = m_videoSurfaces.back();
199 m_videoSurfaces.pop_back();
200 if (render->bitstream_buffers_allocated)
201 m_dllAvUtil.av_freep(&render->bitstream_buffers);
202 render->bitstream_buffers_allocated = 0;
206 if (m_hwContext.bitstream_buffers_allocated)
208 m_dllAvUtil.av_freep(&m_hwContext.bitstream_buffers);
211 m_dllAvUtil.Unload();
214 long CDecoder::Release()
216 // check if we should do some pre-cleanup here
217 // a second decoder might need resources
218 if (m_vdpauConfigured == true)
220 CSingleLock lock(m_DecoderSection);
221 CLog::Log(LOGNOTICE,"CVDPAU::Release pre-cleanup");
224 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::PRECLEANUP,
228 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
232 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup returned error", __FUNCTION__);
233 m_DisplayState = VDPAU_ERROR;
238 CLog::Log(LOGERROR, "VDPAU::%s - pre-cleanup timed out", __FUNCTION__);
239 m_DisplayState = VDPAU_ERROR;
242 for(unsigned int i = 0; i < m_videoSurfaces.size(); ++i)
244 vdpau_render_state *render = m_videoSurfaces[i];
245 if (render->surface != VDP_INVALID_HANDLE && !(render->state & FF_VDPAU_STATE_USED_FOR_RENDER))
247 m_vdpauConfig.vdpProcs.vdp_video_surface_destroy(render->surface);
248 render->surface = VDP_INVALID_HANDLE;
252 return IHardwareDecoder::Release();
255 long CDecoder::ReleasePicReference()
257 return IHardwareDecoder::Release();
260 void CDecoder::SetWidthHeight(int width, int height)
262 m_vdpauConfig.upscale = g_advancedSettings.m_videoVDPAUScaling;
264 //pick the smallest dimensions, so we downscale with vdpau and upscale with opengl when appropriate
265 //this requires the least amount of gpu memory bandwidth
266 if (g_graphicsContext.GetWidth() < width || g_graphicsContext.GetHeight() < height || m_vdpauConfig.upscale >= 0)
268 //scale width to desktop size if the aspect ratio is the same or bigger than the desktop
269 if ((double)height * g_graphicsContext.GetWidth() / width <= (double)g_graphicsContext.GetHeight())
271 m_vdpauConfig.outWidth = g_graphicsContext.GetWidth();
272 m_vdpauConfig.outHeight = MathUtils::round_int((double)height * g_graphicsContext.GetWidth() / width);
274 else //scale height to the desktop size if the aspect ratio is smaller than the desktop
276 m_vdpauConfig.outHeight = g_graphicsContext.GetHeight();
277 m_vdpauConfig.outWidth = MathUtils::round_int((double)width * g_graphicsContext.GetHeight() / height);
282 m_vdpauConfig.outWidth = width;
283 m_vdpauConfig.outHeight = height;
285 CLog::Log(LOGDEBUG, "CVDPAU::SetWidthHeight Setting OutWidth: %i OutHeight: %i", m_vdpauConfig.outWidth, m_vdpauConfig.outHeight);
288 void CDecoder::OnLostDevice()
290 CLog::Log(LOGNOTICE,"CVDPAU::OnLostDevice event");
292 CSingleLock lock(m_DecoderSection);
296 m_DisplayState = VDPAU_LOST;
298 m_DisplayEvent.Reset();
301 void CDecoder::OnResetDevice()
303 CLog::Log(LOGNOTICE,"CVDPAU::OnResetDevice event");
305 CSingleLock lock(m_DecoderSection);
306 if (m_DisplayState == VDPAU_LOST)
308 m_DisplayState = VDPAU_RESET;
310 m_DisplayEvent.Set();
314 int CDecoder::Check(AVCodecContext* avctx)
318 { CSingleLock lock(m_DecoderSection);
319 state = m_DisplayState;
322 if (state == VDPAU_LOST)
324 CLog::Log(LOGNOTICE,"CVDPAU::Check waiting for display reset event");
325 if (!m_DisplayEvent.WaitMSec(2000))
327 CLog::Log(LOGERROR, "CVDPAU::Check - device didn't reset in reasonable time");
332 CSingleLock lock(m_DecoderSection);
333 state = m_DisplayState;
336 if (state == VDPAU_RESET || state == VDPAU_ERROR)
338 CSingleLock lock(m_DecoderSection);
345 if (state == VDPAU_RESET)
353 bool CDecoder::IsVDPAUFormat(PixelFormat format)
355 if (format == AV_PIX_FMT_VDPAU)
361 bool CDecoder::Supports(VdpVideoMixerFeature feature)
363 for(int i = 0; i < m_vdpauConfig.featureCount; i++)
365 if(m_vdpauConfig.vdpFeatures[i] == feature)
371 bool CDecoder::Supports(EINTERLACEMETHOD method)
373 if(method == VS_INTERLACEMETHOD_VDPAU_BOB
374 || method == VS_INTERLACEMETHOD_AUTO)
377 if (!m_vdpauConfig.usePixmaps)
379 if (method == VS_INTERLACEMETHOD_RENDER_BOB)
383 if (method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE)
386 for(SInterlaceMapping* p = g_interlace_mapping; p->method != VS_INTERLACEMETHOD_NONE; p++)
388 if(p->method == method)
389 return Supports(p->feature);
394 EINTERLACEMETHOD CDecoder::AutoInterlaceMethod()
396 return VS_INTERLACEMETHOD_RENDER_BOB;
399 void CDecoder::InitVDPAUProcs()
404 dl_vdp_device_create_x11 = (VdpStatus (*)(Display*, int, VdpDevice*, VdpStatus (**)(VdpDevice, VdpFuncId, void**)))dlsym(dl_handle, (const char*)"vdp_device_create_x11");
408 CLog::Log(LOGERROR,"(VDPAU) - %s in %s",error,__FUNCTION__);
409 m_vdpauConfig.vdpDevice = VDP_INVALID_HANDLE;
413 if (dl_vdp_device_create_x11)
415 m_Display = XOpenDisplay(NULL);
418 int mScreen = g_Windowing.GetCurrentScreen();
422 vdp_st = dl_vdp_device_create_x11(m_Display, //x_display,
424 &m_vdpauConfig.vdpDevice,
425 &m_vdpauConfig.vdpProcs.vdp_get_proc_address);
427 CLog::Log(LOGNOTICE,"vdp_device = 0x%08x vdp_st = 0x%08x",m_vdpauConfig.vdpDevice,vdp_st);
428 if (vdp_st != VDP_STATUS_OK)
430 CLog::Log(LOGERROR,"(VDPAU) unable to init VDPAU - vdp_st = 0x%x. Falling back.",vdp_st);
431 m_vdpauConfig.vdpDevice = VDP_INVALID_HANDLE;
435 #define VDP_PROC(id, proc) \
437 vdp_st = m_vdpauConfig.vdpProcs.vdp_get_proc_address(m_vdpauConfig.vdpDevice, id, (void**)&proc); \
438 CheckStatus(vdp_st, __LINE__); \
441 VDP_PROC(VDP_FUNC_ID_GET_ERROR_STRING , m_vdpauConfig.vdpProcs.vdp_get_error_string);
442 VDP_PROC(VDP_FUNC_ID_DEVICE_DESTROY , m_vdpauConfig.vdpProcs.vdp_device_destroy);
443 VDP_PROC(VDP_FUNC_ID_GENERATE_CSC_MATRIX , m_vdpauConfig.vdpProcs.vdp_generate_csc_matrix);
444 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_CREATE , m_vdpauConfig.vdpProcs.vdp_video_surface_create);
445 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY , m_vdpauConfig.vdpProcs.vdp_video_surface_destroy);
446 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR , m_vdpauConfig.vdpProcs.vdp_video_surface_put_bits_y_cb_cr);
447 VDP_PROC(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR , m_vdpauConfig.vdpProcs.vdp_video_surface_get_bits_y_cb_cr);
448 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_Y_CB_CR , m_vdpauConfig.vdpProcs.vdp_output_surface_put_bits_y_cb_cr);
449 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE , m_vdpauConfig.vdpProcs.vdp_output_surface_put_bits_native);
450 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_CREATE , m_vdpauConfig.vdpProcs.vdp_output_surface_create);
451 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY , m_vdpauConfig.vdpProcs.vdp_output_surface_destroy);
452 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE , m_vdpauConfig.vdpProcs.vdp_output_surface_get_bits_native);
453 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE, m_vdpauConfig.vdpProcs.vdp_output_surface_render_output_surface);
454 VDP_PROC(VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED , m_vdpauConfig.vdpProcs.vdp_output_surface_put_bits_indexed);
455 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_CREATE , m_vdpauConfig.vdpProcs.vdp_video_mixer_create);
456 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES , m_vdpauConfig.vdpProcs.vdp_video_mixer_set_feature_enables);
457 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_DESTROY , m_vdpauConfig.vdpProcs.vdp_video_mixer_destroy);
458 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_RENDER , m_vdpauConfig.vdpProcs.vdp_video_mixer_render);
459 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES , m_vdpauConfig.vdpProcs.vdp_video_mixer_set_attribute_values);
460 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_PARAMETER_SUPPORT , m_vdpauConfig.vdpProcs.vdp_video_mixer_query_parameter_support);
461 VDP_PROC(VDP_FUNC_ID_VIDEO_MIXER_QUERY_FEATURE_SUPPORT , m_vdpauConfig.vdpProcs.vdp_video_mixer_query_feature_support);
462 VDP_PROC(VDP_FUNC_ID_DECODER_CREATE , m_vdpauConfig.vdpProcs.vdp_decoder_create);
463 VDP_PROC(VDP_FUNC_ID_DECODER_DESTROY , m_vdpauConfig.vdpProcs.vdp_decoder_destroy);
464 VDP_PROC(VDP_FUNC_ID_DECODER_RENDER , m_vdpauConfig.vdpProcs.vdp_decoder_render);
465 VDP_PROC(VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES , m_vdpauConfig.vdpProcs.vdp_decoder_query_caps);
466 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY , m_vdpauConfig.vdpProcs.vdp_presentation_queue_target_destroy);
467 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE , m_vdpauConfig.vdpProcs.vdp_presentation_queue_create);
468 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY , m_vdpauConfig.vdpProcs.vdp_presentation_queue_destroy);
469 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY , m_vdpauConfig.vdpProcs.vdp_presentation_queue_display);
470 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE, m_vdpauConfig.vdpProcs.vdp_presentation_queue_block_until_surface_idle);
471 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11 , m_vdpauConfig.vdpProcs.vdp_presentation_queue_target_create_x11);
472 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_QUERY_SURFACE_STATUS , m_vdpauConfig.vdpProcs.vdp_presentation_queue_query_surface_status);
473 VDP_PROC(VDP_FUNC_ID_PRESENTATION_QUEUE_GET_TIME , m_vdpauConfig.vdpProcs.vdp_presentation_queue_get_time);
477 // set all vdpau resources to invalid
478 m_DisplayState = VDPAU_OPEN;
479 m_vdpauConfigured = false;
482 void CDecoder::FiniVDPAUProcs()
484 if (m_vdpauConfig.vdpDevice == VDP_INVALID_HANDLE) return;
487 vdp_st = m_vdpauConfig.vdpProcs.vdp_device_destroy(m_vdpauConfig.vdpDevice);
488 CheckStatus(vdp_st, __LINE__);
489 m_vdpauConfig.vdpDevice = VDP_INVALID_HANDLE;
492 void CDecoder::FiniVDPAUOutput()
494 if (m_vdpauConfig.vdpDevice == VDP_INVALID_HANDLE || !m_vdpauConfigured) return;
496 CLog::Log(LOGNOTICE, " (VDPAU) %s", __FUNCTION__);
499 m_vdpauOutput.Dispose();
500 m_vdpauConfigured = false;
504 vdp_st = m_vdpauConfig.vdpProcs.vdp_decoder_destroy(m_vdpauConfig.vdpDecoder);
505 if (CheckStatus(vdp_st, __LINE__))
507 m_vdpauConfig.vdpDecoder = VDP_INVALID_HANDLE;
509 CSingleLock lock(m_videoSurfaceSec);
510 CLog::Log(LOGDEBUG, "CVDPAU::FiniVDPAUOutput destroying %d video surfaces", (int)m_videoSurfaces.size());
512 for(unsigned int i = 0; i < m_videoSurfaces.size(); ++i)
514 vdpau_render_state *render = m_videoSurfaces[i];
515 if (render->surface != VDP_INVALID_HANDLE)
517 vdp_st = m_vdpauConfig.vdpProcs.vdp_video_surface_destroy(render->surface);
518 render->surface = VDP_INVALID_HANDLE;
520 if (CheckStatus(vdp_st, __LINE__))
525 void CDecoder::ReadFormatOf( AVCodecID codec
526 , VdpDecoderProfile &vdp_decoder_profile
527 , VdpChromaType &vdp_chroma_type)
531 case AV_CODEC_ID_MPEG1VIDEO:
532 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
533 vdp_chroma_type = VDP_CHROMA_TYPE_420;
535 case AV_CODEC_ID_MPEG2VIDEO:
536 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
537 vdp_chroma_type = VDP_CHROMA_TYPE_420;
539 case AV_CODEC_ID_H264:
540 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
541 vdp_chroma_type = VDP_CHROMA_TYPE_420;
543 case AV_CODEC_ID_WMV3:
544 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
545 vdp_chroma_type = VDP_CHROMA_TYPE_420;
547 case AV_CODEC_ID_VC1:
548 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
549 vdp_chroma_type = VDP_CHROMA_TYPE_420;
551 case AV_CODEC_ID_MPEG4:
552 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
553 vdp_chroma_type = VDP_CHROMA_TYPE_420;
556 vdp_decoder_profile = 0;
562 bool CDecoder::ConfigVDPAU(AVCodecContext* avctx, int ref_frames)
567 VdpDecoderProfile vdp_decoder_profile;
569 m_vdpauConfig.vidWidth = avctx->width;
570 m_vdpauConfig.vidHeight = avctx->height;
571 m_vdpauConfig.surfaceWidth = avctx->coded_width;
572 m_vdpauConfig.surfaceHeight = avctx->coded_height;
574 SetWidthHeight(avctx->width,avctx->height);
576 CLog::Log(LOGNOTICE, " (VDPAU) screenWidth:%i vidWidth:%i surfaceWidth:%i",m_vdpauConfig.outWidth,m_vdpauConfig.vidWidth,m_vdpauConfig.surfaceWidth);
577 CLog::Log(LOGNOTICE, " (VDPAU) screenHeight:%i vidHeight:%i surfaceHeight:%i",m_vdpauConfig.outHeight,m_vdpauConfig.vidHeight,m_vdpauConfig.surfaceHeight);
579 ReadFormatOf(avctx->codec_id, vdp_decoder_profile, m_vdpauConfig.vdpChromaType);
581 if(avctx->codec_id == AV_CODEC_ID_H264)
583 m_vdpauConfig.maxReferences = ref_frames;
584 if (m_vdpauConfig.maxReferences > 16) m_vdpauConfig.maxReferences = 16;
585 if (m_vdpauConfig.maxReferences < 5) m_vdpauConfig.maxReferences = 5;
588 m_vdpauConfig.maxReferences = 2;
590 vdp_st = m_vdpauConfig.vdpProcs.vdp_decoder_create(m_vdpauConfig.vdpDevice,
592 m_vdpauConfig.surfaceWidth,
593 m_vdpauConfig.surfaceHeight,
594 m_vdpauConfig.maxReferences,
595 &m_vdpauConfig.vdpDecoder);
596 if (CheckStatus(vdp_st, __LINE__))
600 CSingleLock lock(g_graphicsContext);
601 m_vdpauConfig.stats = &m_bufferStats;
602 m_vdpauConfig.vdpau = this;
603 m_bufferStats.Reset();
604 m_vdpauOutput.Start();
606 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::INIT,
610 sizeof(m_vdpauConfig)))
612 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
616 data = (CVdpauConfig*)reply->data;
619 m_vdpauConfig.usePixmaps = data->usePixmaps;
625 CLog::Log(LOGERROR, "VDPAU::%s - vdpau output returned error", __FUNCTION__);
626 m_vdpauOutput.Dispose();
632 CLog::Log(LOGERROR, "VDPAU::%s - failed to init output", __FUNCTION__);
633 m_vdpauOutput.Dispose();
637 m_inMsgEvent.Reset();
638 m_vdpauConfigured = true;
642 void CDecoder::SpewHardwareAvailable() //CopyrighVDPAUt (c) 2008 Wladimir J. van der Laan -- VDPInfo
645 CLog::Log(LOGNOTICE,"VDPAU Decoder capabilities:");
646 CLog::Log(LOGNOTICE,"name level macbs width height");
647 CLog::Log(LOGNOTICE,"------------------------------------");
648 for(unsigned int x=0; x<decoder_profile_count; ++x)
650 VdpBool is_supported = false;
651 uint32_t max_level, max_macroblocks, max_width, max_height;
652 rv = m_vdpauConfig.vdpProcs.vdp_decoder_query_caps(m_vdpauConfig.vdpDevice, decoder_profiles[x].id,
653 &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
654 if(rv == VDP_STATUS_OK && is_supported)
656 CLog::Log(LOGNOTICE,"%-16s %2i %5i %5i %5i\n", decoder_profiles[x].name,
657 max_level, max_macroblocks, max_width, max_height);
660 CLog::Log(LOGNOTICE,"------------------------------------");
661 m_vdpauConfig.featureCount = 0;
662 #define CHECK_SUPPORT(feature) \
665 if(m_vdpauConfig.vdpProcs.vdp_video_mixer_query_feature_support(m_vdpauConfig.vdpDevice, feature, &supported) == VDP_STATUS_OK && supported) { \
666 CLog::Log(LOGNOTICE, "Mixer feature: "#feature); \
667 m_vdpauConfig.vdpFeatures[m_vdpauConfig.featureCount++] = feature; \
671 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION);
672 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_SHARPNESS);
673 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL);
674 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL);
675 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE);
676 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
677 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1);
678 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2);
679 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3);
680 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4);
681 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5);
682 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6);
683 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7);
684 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8);
685 CHECK_SUPPORT(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9);
691 bool CDecoder::IsSurfaceValid(vdpau_render_state *render)
693 // find render state in queue
696 for(i = 0; i < m_videoSurfaces.size(); ++i)
698 if(m_videoSurfaces[i] == render)
706 CLog::Log(LOGERROR,"%s - video surface not found", __FUNCTION__);
709 if (m_videoSurfaces[i]->surface == VDP_INVALID_HANDLE)
711 m_videoSurfaces[i]->state = 0;
718 int CDecoder::FFGetBuffer(AVCodecContext *avctx, AVFrame *pic)
720 //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
721 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
722 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
724 // while we are waiting to recover we can't do anything
725 CSingleLock lock(vdp->m_DecoderSection);
727 if(vdp->m_DisplayState != VDPAU_OPEN)
729 CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - returning due to awaiting recovery");
733 vdpau_render_state * render = NULL;
735 // find unused surface
736 { CSingleLock lock(vdp->m_videoSurfaceSec);
737 for(unsigned int i = 0; i < vdp->m_videoSurfaces.size(); i++)
739 if(!(vdp->m_videoSurfaces[i]->state & (FF_VDPAU_STATE_USED_FOR_REFERENCE | FF_VDPAU_STATE_USED_FOR_RENDER)))
741 render = vdp->m_videoSurfaces[i];
748 VdpStatus vdp_st = VDP_STATUS_ERROR;
751 // create a new surface
752 VdpDecoderProfile profile;
753 ReadFormatOf(avctx->codec_id, profile, vdp->m_vdpauConfig.vdpChromaType);
754 render = (vdpau_render_state*)calloc(sizeof(vdpau_render_state), 1);
757 CLog::Log(LOGWARNING, "CVDPAU::FFGetBuffer - calloc failed");
760 CSingleLock lock(vdp->m_videoSurfaceSec);
761 render->surface = VDP_INVALID_HANDLE;
762 vdp->m_videoSurfaces.push_back(render);
765 if (render->surface == VDP_INVALID_HANDLE)
767 vdp_st = vdp->m_vdpauConfig.vdpProcs.vdp_video_surface_create(vdp->m_vdpauConfig.vdpDevice,
768 vdp->m_vdpauConfig.vdpChromaType,
772 vdp->CheckStatus(vdp_st, __LINE__);
773 if (vdp_st != VDP_STATUS_OK)
776 CLog::Log(LOGERROR, "CVDPAU::FFGetBuffer - No Video surface available could be created");
781 pic->data[1] = pic->data[2] = NULL;
782 pic->data[0] = (uint8_t*)render;
783 pic->data[3] = (uint8_t*)(uintptr_t)render->surface;
785 pic->linesize[0] = pic->linesize[1] = pic->linesize[2] = 0;
787 pic->type= FF_BUFFER_TYPE_USER;
789 render->state |= FF_VDPAU_STATE_USED_FOR_REFERENCE;
790 pic->reordered_opaque= avctx->reordered_opaque;
794 void CDecoder::FFReleaseBuffer(AVCodecContext *avctx, AVFrame *pic)
796 //CLog::Log(LOGNOTICE,"%s",__FUNCTION__);
797 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)avctx->opaque;
798 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
800 vdpau_render_state * render;
803 CSingleLock lock(vdp->m_DecoderSection);
805 render=(vdpau_render_state*)pic->data[0];
808 CLog::Log(LOGERROR, "CVDPAU::FFReleaseBuffer - invalid context handle provided");
812 CSingleLock vLock(vdp->m_videoSurfaceSec);
813 render->state &= ~FF_VDPAU_STATE_USED_FOR_REFERENCE;
817 // find render state in queue
818 if (!vdp->IsSurfaceValid(render))
820 CLog::Log(LOGDEBUG, "CVDPAU::FFReleaseBuffer - ignoring invalid buffer");
824 render->state &= ~FF_VDPAU_STATE_USED_FOR_REFERENCE;
827 VdpStatus CDecoder::Render( VdpDecoder decoder, VdpVideoSurface target,
828 VdpPictureInfo const *picture_info,
829 uint32_t bitstream_buffer_count,
830 VdpBitstreamBuffer const * bitstream_buffers)
832 return VDP_STATUS_OK;
835 void CDecoder::FFDrawSlice(struct AVCodecContext *s,
836 const AVFrame *src, int offset[4],
837 int y, int type, int height)
839 CDVDVideoCodecFFmpeg* ctx = (CDVDVideoCodecFFmpeg*)s->opaque;
840 CDecoder* vdp = (CDecoder*)ctx->GetHardware();
842 // while we are waiting to recover we can't do anything
843 CSingleLock lock(vdp->m_DecoderSection);
845 if(vdp->m_DisplayState != VDPAU_OPEN)
848 if(src->linesize[0] || src->linesize[1] || src->linesize[2]
849 || offset[0] || offset[1] || offset[2])
851 CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid linesizes or offsets provided");
856 vdpau_render_state * render;
858 render = (vdpau_render_state*)src->data[0];
861 CLog::Log(LOGERROR, "CVDPAU::FFDrawSlice - invalid context handle provided");
865 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
866 if (!vdp->IsSurfaceValid(render))
868 CLog::Log(LOGWARNING, "CVDPAU::FFDrawSlice - ignoring invalid buffer");
872 uint32_t max_refs = 0;
873 if(s->codec_id == AV_CODEC_ID_H264)
874 max_refs = vdp->m_hwContext.info.h264.num_ref_frames;
876 if(vdp->m_vdpauConfig.vdpDecoder == VDP_INVALID_HANDLE
877 || vdp->m_vdpauConfigured == false
878 || vdp->m_vdpauConfig.maxReferences < max_refs)
880 if(!vdp->ConfigVDPAU(s, max_refs))
884 // uint64_t startTime = CurrentHostCounter();
885 uint16_t decoded, processed, rend;
886 vdp->m_bufferStats.Get(decoded, processed, rend);
887 vdp_st = vdp->m_vdpauConfig.vdpProcs.vdp_decoder_render(vdp->m_vdpauConfig.vdpDecoder,
889 (VdpPictureInfo const *)&(vdp->m_hwContext.info),
890 vdp->m_hwContext.bitstream_buffers_used,
891 vdp->m_hwContext.bitstream_buffers);
892 vdp->CheckStatus(vdp_st, __LINE__);
893 // uint64_t diff = CurrentHostCounter() - startTime;
894 // if (diff*1000/CurrentHostFrequency() > 30)
895 // CLog::Log(LOGWARNING,"CVDPAU::DrawSlice - VdpDecoderRender long decoding: %d ms, dec: %d, proc: %d, rend: %d", (int)((diff*1000)/CurrentHostFrequency()), decoded, processed, rend);
900 int CDecoder::Decode(AVCodecContext *avctx, AVFrame *pFrame)
902 int result = Check(avctx);
906 CSingleLock lock(m_DecoderSection);
908 if (!m_vdpauConfigured)
912 { // we have a new frame from decoder
914 vdpau_render_state * render = (vdpau_render_state*)pFrame->data[0];
917 CLog::Log(LOGERROR, "CVDPAU::Decode: no valid frame");
921 // ffmpeg vc-1 decoder does not flush, make sure the data buffer is still valid
922 if (!IsSurfaceValid(render))
924 CLog::Log(LOGWARNING, "CVDPAU::Decode - ignoring invalid buffer");
928 CSingleLock lock(m_videoSurfaceSec);
929 render->state |= FF_VDPAU_STATE_USED_FOR_RENDER;
932 // send frame to output for processing
933 CVdpauDecodedPicture pic;
934 memset(&pic.DVDPic, 0, sizeof(pic.DVDPic));
935 ((CDVDVideoCodecFFmpeg*)avctx->opaque)->GetPictureCommon(&pic.DVDPic);
937 pic.DVDPic.color_matrix = avctx->colorspace;
938 m_bufferStats.IncDecoded();
939 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::NEWFRAME, &pic, sizeof(pic));
941 m_codecControl = pic.DVDPic.iFlags & (DVP_FLAG_DRAIN | DVP_FLAG_NO_POSTPROC);
945 uint16_t decoded, processed, render;
947 while (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
949 if (msg->signal == COutputControlProtocol::ERROR)
951 m_DisplayState = VDPAU_ERROR;
957 m_bufferStats.Get(decoded, processed, render);
959 uint64_t startTime = CurrentHostCounter();
962 if (m_vdpauOutput.m_dataPort.ReceiveInMessage(&msg))
964 if (msg->signal == COutputDataProtocol::PICTURE)
966 if (m_presentPicture)
968 m_presentPicture->ReturnUnused();
969 m_presentPicture = 0;
972 m_presentPicture = *(CVdpauRenderPicture**)msg->data;
973 m_presentPicture->vdpau = this;
974 m_bufferStats.DecRender();
975 m_bufferStats.Get(decoded, processed, render);
976 retval |= VC_PICTURE;
982 else if (m_vdpauOutput.m_controlPort.ReceiveInMessage(&msg))
984 if (msg->signal == COutputControlProtocol::STATS)
986 m_bufferStats.Get(decoded, processed, render);
990 m_DisplayState = VDPAU_ERROR;
996 if ((m_codecControl & DVP_FLAG_DRAIN))
998 if (decoded + processed + render < 4)
1000 retval |= VC_BUFFER;
1005 if (decoded < 4 && (processed + render) < 3)
1007 retval |= VC_BUFFER;
1011 if (!retval && !m_inMsgEvent.WaitMSec(2000))
1014 uint64_t diff = CurrentHostCounter() - startTime;
1015 if (retval & VC_PICTURE)
1017 m_bufferStats.SetParams(diff, m_codecControl);
1019 if (diff*1000/CurrentHostFrequency() > 50)
1020 CLog::Log(LOGDEBUG,"CVDPAU::Decode long wait: %d", (int)((diff*1000)/CurrentHostFrequency()));
1024 CLog::Log(LOGERROR, "VDPAU::%s - timed out waiting for output message", __FUNCTION__);
1025 m_DisplayState = VDPAU_ERROR;
1032 bool CDecoder::GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture)
1034 CSingleLock lock(m_DecoderSection);
1036 if (m_DisplayState != VDPAU_OPEN)
1039 *picture = m_presentPicture->DVDPic;
1040 picture->vdpau = m_presentPicture;
1045 void CDecoder::Reset()
1047 CSingleLock lock(m_DecoderSection);
1049 if (!m_vdpauConfigured)
1053 if (m_vdpauOutput.m_controlPort.SendOutMessageSync(COutputControlProtocol::FLUSH,
1057 bool success = reply->signal == COutputControlProtocol::ACC ? true : false;
1061 CLog::Log(LOGERROR, "VDPAU::%s - flush returned error", __FUNCTION__);
1062 m_DisplayState = VDPAU_ERROR;
1065 m_bufferStats.Reset();
1069 CLog::Log(LOGERROR, "VDPAU::%s - flush timed out", __FUNCTION__);
1070 m_DisplayState = VDPAU_ERROR;
1074 bool CDecoder::CanSkipDeint()
1076 return m_bufferStats.CanSkipDeint();
1079 void CDecoder::ReturnRenderPicture(CVdpauRenderPicture *renderPic)
1081 m_vdpauOutput.m_dataPort.SendOutMessage(COutputDataProtocol::RETURNPIC, &renderPic, sizeof(renderPic));
1084 bool CDecoder::CheckStatus(VdpStatus vdp_st, int line)
1086 if (vdp_st != VDP_STATUS_OK)
1088 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) at %s:%d\n", m_vdpauConfig.vdpProcs.vdp_get_error_string(vdp_st), vdp_st, __FILE__, line);
1090 if(m_DisplayState == VDPAU_OPEN)
1092 if (vdp_st == VDP_STATUS_DISPLAY_PREEMPTED)
1094 m_DisplayEvent.Reset();
1095 m_DisplayState = VDPAU_LOST;
1098 m_DisplayState = VDPAU_ERROR;
1106 //-----------------------------------------------------------------------------
1108 //-----------------------------------------------------------------------------
1110 CVdpauRenderPicture* CVdpauRenderPicture::Acquire()
1112 CSingleLock lock(renderPicSection);
1121 long CVdpauRenderPicture::Release()
1123 CSingleLock lock(renderPicSection);
1130 vdpau->ReturnRenderPicture(this);
1131 vdpau->ReleasePicReference();
1136 void CVdpauRenderPicture::ReturnUnused()
1138 { CSingleLock lock(renderPicSection);
1143 vdpau->ReturnRenderPicture(this);
1145 //-----------------------------------------------------------------------------
1147 //-----------------------------------------------------------------------------
1148 CMixer::CMixer(CEvent *inMsgEvent) :
1149 CThread("Vdpau Mixer Thread"),
1150 m_controlPort("ControlPort", inMsgEvent, &m_outMsgEvent),
1151 m_dataPort("DataPort", inMsgEvent, &m_outMsgEvent)
1153 m_inMsgEvent = inMsgEvent;
1161 void CMixer::Start()
1166 void CMixer::Dispose()
1169 m_outMsgEvent.Set();
1172 m_controlPort.Purge();
1176 bool CMixer::IsActive()
1181 void CMixer::OnStartup()
1183 CLog::Log(LOGNOTICE, "CMixer::OnStartup: Output Thread created");
1186 void CMixer::OnExit()
1188 CLog::Log(LOGNOTICE, "CMixer::OnExit: Output Thread terminated");
1195 M_TOP_UNCONFIGURED, // 2
1196 M_TOP_CONFIGURED, // 3
1197 M_TOP_CONFIGURED_WAIT1, // 4
1198 M_TOP_CONFIGURED_STEP1, // 5
1199 M_TOP_CONFIGURED_WAIT2, // 6
1200 M_TOP_CONFIGURED_STEP2, // 7
1203 int MIXER_parentStates[] = {
1206 0, //TOP_UNCONFIGURED
1208 3, //TOP_CONFIGURED_WAIT1
1209 3, //TOP_CONFIGURED_STEP1
1210 3, //TOP_CONFIGURED_WAIT2
1211 3, //TOP_CONFIGURED_STEP2
1214 void CMixer::StateMachine(int signal, Protocol *port, Message *msg)
1216 for (int state = m_state; ; state = MIXER_parentStates[state])
1221 if (port == &m_controlPort)
1225 case CMixerControlProtocol::FLUSH:
1227 msg->Reply(CMixerControlProtocol::ACC);
1234 std::string portName = port == NULL ? "timer" : port->portName;
1235 CLog::Log(LOGWARNING, "CMixer::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
1239 case M_TOP_ERROR: // TOP
1242 case M_TOP_UNCONFIGURED:
1243 if (port == &m_controlPort)
1247 case CMixerControlProtocol::INIT:
1249 data = (CVdpauConfig*)msg->data;
1257 m_state = M_TOP_CONFIGURED_WAIT1;
1258 msg->Reply(CMixerControlProtocol::ACC);
1262 msg->Reply(CMixerControlProtocol::ERROR);
1271 case M_TOP_CONFIGURED:
1272 if (port == &m_dataPort)
1276 case CMixerDataProtocol::FRAME:
1277 CVdpauDecodedPicture *frame;
1278 frame = (CVdpauDecodedPicture*)msg->data;
1281 m_decodedPics.push(*frame);
1285 case CMixerDataProtocol::BUFFER:
1286 VdpOutputSurface *surf;
1287 surf = (VdpOutputSurface*)msg->data;
1290 m_outputSurfaces.push(*surf);
1300 case M_TOP_CONFIGURED_WAIT1:
1301 if (port == NULL) // timeout
1305 case CMixerControlProtocol::TIMEOUT:
1306 if (!m_decodedPics.empty() && !m_outputSurfaces.empty())
1308 m_state = M_TOP_CONFIGURED_STEP1;
1309 m_bStateMachineSelfTrigger = true;
1313 // if (m_extTimeout != 0)
1315 // SetPostProcFeatures(false);
1316 // CLog::Log(LOGWARNING,"CVDPAU::Mixer timeout - decoded: %d, outputSurf: %d", (int)m_decodedPics.size(), (int)m_outputSurfaces.size());
1327 case M_TOP_CONFIGURED_STEP1:
1328 if (port == NULL) // timeout
1332 case CMixerControlProtocol::TIMEOUT:
1333 m_mixerInput.push_front(m_decodedPics.front());
1334 m_decodedPics.pop();
1335 if (m_mixerInput.size() < 2)
1337 m_state = M_TOP_CONFIGURED_WAIT1;
1345 m_state = M_TOP_CONFIGURED_WAIT1;
1346 m_extTimeout = 1000;
1349 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1350 m_outputSurfaces.pop();
1351 m_config.stats->IncProcessed();
1352 m_config.stats->DecDecoded();
1353 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1354 if (m_mixersteps > 1)
1356 m_state = M_TOP_CONFIGURED_WAIT2;
1362 m_state = M_TOP_CONFIGURED_WAIT1;
1372 case M_TOP_CONFIGURED_WAIT2:
1373 if (port == NULL) // timeout
1377 case CMixerControlProtocol::TIMEOUT:
1378 if (!m_outputSurfaces.empty())
1380 m_state = M_TOP_CONFIGURED_STEP2;
1381 m_bStateMachineSelfTrigger = true;
1385 // if (m_extTimeout != 0)
1387 // SetPostProcFeatures(false);
1388 // CLog::Log(LOGNOTICE,"---mixer wait2 decoded: %d, outputSurf: %d", (int)m_decodedPics.size(), (int)m_outputSurfaces.size());
1399 case M_TOP_CONFIGURED_STEP2:
1400 if (port == NULL) // timeout
1404 case CMixerControlProtocol::TIMEOUT:
1405 m_processPicture.outputSurface = m_outputSurfaces.front();
1410 m_state = M_TOP_CONFIGURED_WAIT1;
1411 m_extTimeout = 1000;
1414 if (m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
1415 m_outputSurfaces.pop();
1416 m_config.stats->IncProcessed();
1417 m_dataPort.SendInMessage(CMixerDataProtocol::PICTURE,&m_processPicture,sizeof(m_processPicture));
1419 m_state = M_TOP_CONFIGURED_WAIT1;
1428 default: // we are in no state, should not happen
1429 CLog::Log(LOGERROR, "CMixer::%s - no valid state: %d", __FUNCTION__, m_state);
1435 void CMixer::Process()
1437 Message *msg = NULL;
1438 Protocol *port = NULL;
1441 m_state = M_TOP_UNCONFIGURED;
1442 m_extTimeout = 1000;
1443 m_bStateMachineSelfTrigger = false;
1449 if (m_bStateMachineSelfTrigger)
1451 m_bStateMachineSelfTrigger = false;
1452 // self trigger state machine
1453 StateMachine(msg->signal, port, msg);
1454 if (!m_bStateMachineSelfTrigger)
1461 // check control port
1462 else if (m_controlPort.ReceiveOutMessage(&msg))
1465 port = &m_controlPort;
1468 else if (m_dataPort.ReceiveOutMessage(&msg))
1476 StateMachine(msg->signal, port, msg);
1477 if (!m_bStateMachineSelfTrigger)
1486 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
1493 msg = m_controlPort.GetMessage();
1494 msg->signal = CMixerControlProtocol::TIMEOUT;
1496 // signal timeout to state machine
1497 StateMachine(msg->signal, port, msg);
1498 if (!m_bStateMachineSelfTrigger)
1508 void CMixer::CreateVdpauMixer()
1510 CLog::Log(LOGNOTICE, " (VDPAU) Creating the video mixer");
1512 InitCSCMatrix(m_config.vidWidth);
1514 VdpVideoMixerParameter parameters[] = {
1515 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
1516 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
1517 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE};
1519 void const * parameter_values[] = {
1520 &m_config.surfaceWidth,
1521 &m_config.surfaceHeight,
1522 &m_config.vdpChromaType};
1524 VdpStatus vdp_st = VDP_STATUS_ERROR;
1525 vdp_st = m_config.vdpProcs.vdp_video_mixer_create(m_config.vdpDevice,
1526 m_config.featureCount,
1527 m_config.vdpFeatures,
1532 CheckStatus(vdp_st, __LINE__);
1534 // create 3 pitches of black lines needed for clipping top
1535 // and bottom lines when de-interlacing
1536 m_BlackBar = new uint32_t[3*m_config.outWidth];
1537 memset(m_BlackBar, 0, 3*m_config.outWidth*sizeof(uint32_t));
1541 void CMixer::InitCSCMatrix(int Width)
1543 m_Procamp.struct_version = VDP_PROCAMP_VERSION;
1544 m_Procamp.brightness = 0.0;
1545 m_Procamp.contrast = 1.0;
1546 m_Procamp.saturation = 1.0;
1550 void CMixer::CheckFeatures()
1552 if (m_Upscale != m_config.upscale)
1555 m_Upscale = m_config.upscale;
1557 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness ||
1558 m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast ||
1559 m_ColorMatrix != m_mixerInput[1].DVDPic.color_matrix)
1562 m_Brightness = CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness;
1563 m_Contrast = CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast;
1564 m_ColorMatrix = m_mixerInput[1].DVDPic.color_matrix;
1566 if (m_NoiseReduction != CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1568 m_NoiseReduction = CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction;
1569 SetNoiseReduction();
1571 if (m_Sharpness != CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1573 m_Sharpness = CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness;
1576 if (m_DeintMode != CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode ||
1577 m_Deint != CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod)
1579 m_DeintMode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
1580 m_Deint = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1585 void CMixer::SetPostProcFeatures(bool postProcEnabled)
1587 if (m_PostProc != postProcEnabled)
1589 if (postProcEnabled)
1591 SetNoiseReduction();
1598 m_PostProc = postProcEnabled;
1602 void CMixer::PostProcOff()
1606 if (m_videoMixer == VDP_INVALID_HANDLE)
1609 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1610 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1611 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE};
1613 VdpBool enabled[]={0,0,0};
1614 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1615 CheckStatus(vdp_st, __LINE__);
1617 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1619 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION};
1621 VdpBool enabled[]={0};
1622 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1623 CheckStatus(vdp_st, __LINE__);
1626 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1628 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS};
1630 VdpBool enabled[]={0};
1631 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1632 CheckStatus(vdp_st, __LINE__);
1638 bool CMixer::GenerateStudioCSCMatrix(VdpColorStandard colorStandard, VdpCSCMatrix &studioCSCMatrix)
1640 // instead use studioCSCKCoeffs601[3], studioCSCKCoeffs709[3] to generate float[3][4] matrix (float studioCSC[3][4])
1641 // m00 = mRY = red: luma factor (contrast factor) (1.0)
1642 // m10 = mGY = green: luma factor (contrast factor) (1.0)
1643 // m20 = mBY = blue: luma factor (contrast factor) (1.0)
1645 // m01 = mRB = red: blue color diff coeff (0.0)
1646 // m11 = mGB = green: blue color diff coeff (-2Kb(1-Kb)/(Kg))
1647 // m21 = mBB = blue: blue color diff coeff ((1-Kb)/0.5)
1649 // m02 = mRR = red: red color diff coeff ((1-Kr)/0.5)
1650 // m12 = mGR = green: red color diff coeff (-2Kr(1-Kr)/(Kg))
1651 // m22 = mBR = blue: red color diff coeff (0.0)
1653 // m03 = mRC = red: colour zero offset (brightness factor) (-(1-Kr)/0.5 * (128/255))
1654 // m13 = mGC = green: colour zero offset (brightness factor) ((256/255) * (Kb(1-Kb) + Kr(1-Kr)) / Kg)
1655 // m23 = mBC = blue: colour zero offset (brightness factor) (-(1-Kb)/0.5 * (128/255))
1666 // colour standard coefficients for red, geen, blue
1668 // colour diff zero position (use standard 8-bit coding precision)
1669 double CDZ = 128; //256*0.5
1670 // range excursion (use standard 8-bit coding precision)
1671 double EXC = 255; //256-1
1673 if (colorStandard == VDP_COLOR_STANDARD_ITUR_BT_601)
1675 Kr = studioCSCKCoeffs601[0];
1676 Kg = studioCSCKCoeffs601[1];
1677 Kb = studioCSCKCoeffs601[2];
1679 else // assume VDP_COLOR_STANDARD_ITUR_BT_709
1681 Kr = studioCSCKCoeffs709[0];
1682 Kg = studioCSCKCoeffs709[1];
1683 Kb = studioCSCKCoeffs709[2];
1685 // we keep luma unscaled to retain the levels present in source so that 16-235 luma is converted to RGB 16-235
1686 studioCSCMatrix[R][Y] = 1.0;
1687 studioCSCMatrix[G][Y] = 1.0;
1688 studioCSCMatrix[B][Y] = 1.0;
1690 studioCSCMatrix[R][Cb] = 0.0;
1691 studioCSCMatrix[G][Cb] = (double)-2 * Kb * (1 - Kb) / Kg;
1692 studioCSCMatrix[B][Cb] = (double)(1 - Kb) / 0.5;
1694 studioCSCMatrix[R][Cr] = (double)(1 - Kr) / 0.5;
1695 studioCSCMatrix[G][Cr] = (double)-2 * Kr * (1 - Kr) / Kg;
1696 studioCSCMatrix[B][Cr] = 0.0;
1698 studioCSCMatrix[R][C] = (double)-1 * studioCSCMatrix[R][Cr] * CDZ/EXC;
1699 studioCSCMatrix[G][C] = (double)-1 * (studioCSCMatrix[G][Cb] + studioCSCMatrix[G][Cr]) * CDZ/EXC;
1700 studioCSCMatrix[B][C] = (double)-1 * studioCSCMatrix[B][Cb] * CDZ/EXC;
1705 void CMixer::SetColor()
1709 if (m_Brightness != CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)
1710 m_Procamp.brightness = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness)-50) / 100;
1711 if (m_Contrast != CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)
1712 m_Procamp.contrast = (float)((CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast)+50) / 100;
1714 VdpColorStandard colorStandard;
1715 switch(m_mixerInput[1].DVDPic.color_matrix)
1717 case AVCOL_SPC_BT709:
1718 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1720 case AVCOL_SPC_BT470BG:
1721 case AVCOL_SPC_SMPTE170M:
1722 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1724 case AVCOL_SPC_SMPTE240M:
1725 colorStandard = VDP_COLOR_STANDARD_SMPTE_240M;
1728 case AVCOL_SPC_UNSPECIFIED:
1731 if(m_config.surfaceWidth > 1000)
1732 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_709;
1734 colorStandard = VDP_COLOR_STANDARD_ITUR_BT_601;
1737 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX };
1738 if (CSettings::Get().GetBool("videoscreen.limitedrange"))
1740 float studioCSC[3][4];
1741 GenerateStudioCSCMatrix(colorStandard, studioCSC);
1742 void const * pm_CSCMatix[] = { &studioCSC };
1743 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1747 vdp_st = m_config.vdpProcs.vdp_generate_csc_matrix(&m_Procamp, colorStandard, &m_CSCMatrix);
1748 void const * pm_CSCMatix[] = { &m_CSCMatrix };
1749 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, pm_CSCMatix);
1752 CheckStatus(vdp_st, __LINE__);
1755 void CMixer::SetNoiseReduction()
1757 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION))
1760 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION };
1761 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL };
1764 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction)
1766 VdpBool enabled[]= {0};
1767 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1768 CheckStatus(vdp_st, __LINE__);
1771 VdpBool enabled[]={1};
1772 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1773 CheckStatus(vdp_st, __LINE__);
1774 void* nr[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction };
1775 CLog::Log(LOGNOTICE,"Setting Noise Reduction to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_NoiseReduction);
1776 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, nr);
1777 CheckStatus(vdp_st, __LINE__);
1780 void CMixer::SetSharpness()
1782 if(!m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_SHARPNESS))
1785 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_SHARPNESS };
1786 VdpVideoMixerAttribute attributes[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL };
1789 if (!CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness)
1791 VdpBool enabled[]={0};
1792 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1793 CheckStatus(vdp_st, __LINE__);
1796 VdpBool enabled[]={1};
1797 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1798 CheckStatus(vdp_st, __LINE__);
1799 void* sh[] = { &CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness };
1800 CLog::Log(LOGNOTICE,"Setting Sharpness to %f",CMediaSettings::Get().GetCurrentVideoSettings().m_Sharpness);
1801 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attributes), attributes, sh);
1802 CheckStatus(vdp_st, __LINE__);
1805 EINTERLACEMETHOD CMixer::GetDeinterlacingMethod(bool log /* = false */)
1807 EINTERLACEMETHOD method = CMediaSettings::Get().GetCurrentVideoSettings().m_InterlaceMethod;
1808 if (method == VS_INTERLACEMETHOD_AUTO)
1811 // if (m_config.outHeight >= 720)
1812 // deint = g_advancedSettings.m_videoVDPAUdeintHD;
1814 // deint = g_advancedSettings.m_videoVDPAUdeintSD;
1818 if (m_config.vdpau->Supports(EINTERLACEMETHOD(deint)))
1820 method = EINTERLACEMETHOD(deint);
1822 CLog::Log(LOGNOTICE, "CVDPAU::GetDeinterlacingMethod: set de-interlacing to %d", deint);
1827 CLog::Log(LOGWARNING, "CVDPAU::GetDeinterlacingMethod: method for de-interlacing (advanced settings) not supported");
1834 void CMixer::SetDeinterlacing()
1838 if (m_videoMixer == VDP_INVALID_HANDLE)
1841 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
1842 EINTERLACEMETHOD method = GetDeinterlacingMethod(true);
1844 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL,
1845 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL,
1846 VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE };
1848 if (mode == VS_DEINTERLACEMODE_OFF)
1850 VdpBool enabled[] = {0,0,0};
1851 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1855 if (method == VS_INTERLACEMETHOD_AUTO)
1857 VdpBool enabled[] = {1,0,0};
1858 if (g_advancedSettings.m_videoVDPAUtelecine)
1860 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1862 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
1863 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF)
1865 VdpBool enabled[] = {1,0,0};
1866 if (g_advancedSettings.m_videoVDPAUtelecine)
1868 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1870 else if (method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
1871 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF)
1873 VdpBool enabled[] = {1,1,0};
1874 if (g_advancedSettings.m_videoVDPAUtelecine)
1876 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1880 VdpBool enabled[]={0,0,0};
1881 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1884 CheckStatus(vdp_st, __LINE__);
1886 SetDeintSkipChroma();
1888 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
1891 void CMixer::SetDeintSkipChroma()
1893 VdpVideoMixerAttribute attribute[] = { VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
1897 if (g_advancedSettings.m_videoVDPAUdeintSkipChromaHD && m_config.outHeight >= 720)
1902 void const *values[]={&val};
1903 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_attribute_values(m_videoMixer, ARSIZE(attribute), attribute, values);
1905 CheckStatus(vdp_st, __LINE__);
1908 void CMixer::SetHWUpscaling()
1910 #ifdef VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
1913 VdpBool enabled[]={1};
1914 switch (m_config.upscale)
1917 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
1919 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
1920 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1924 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
1926 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
1927 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1931 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
1933 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
1934 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1938 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
1940 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
1941 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1945 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
1947 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
1948 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1952 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
1954 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
1955 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1959 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
1961 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
1962 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1966 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
1968 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
1969 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1973 if (m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
1975 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
1976 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1983 CheckStatus(vdp_st, __LINE__);
1987 void CMixer::DisableHQScaling()
1991 if (m_videoMixer == VDP_INVALID_HANDLE)
1994 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1))
1996 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 };
1997 VdpBool enabled[]={0};
1998 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
1999 CheckStatus(vdp_st, __LINE__);
2002 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2))
2004 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2 };
2005 VdpBool enabled[]={0};
2006 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2007 CheckStatus(vdp_st, __LINE__);
2010 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3))
2012 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3 };
2013 VdpBool enabled[]={0};
2014 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2015 CheckStatus(vdp_st, __LINE__);
2018 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4))
2020 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4 };
2021 VdpBool enabled[]={0};
2022 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2023 CheckStatus(vdp_st, __LINE__);
2026 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5))
2028 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5 };
2029 VdpBool enabled[]={0};
2030 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2031 CheckStatus(vdp_st, __LINE__);
2034 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6))
2036 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6 };
2037 VdpBool enabled[]={0};
2038 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2039 CheckStatus(vdp_st, __LINE__);
2042 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7))
2044 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7 };
2045 VdpBool enabled[]={0};
2046 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2047 CheckStatus(vdp_st, __LINE__);
2050 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8))
2052 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8 };
2053 VdpBool enabled[]={0};
2054 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2055 CheckStatus(vdp_st, __LINE__);
2058 if(m_config.vdpau->Supports(VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9))
2060 VdpVideoMixerFeature feature[] = { VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9 };
2061 VdpBool enabled[]={0};
2062 vdp_st = m_config.vdpProcs.vdp_video_mixer_set_feature_enables(m_videoMixer, ARSIZE(feature), feature, enabled);
2063 CheckStatus(vdp_st, __LINE__);
2072 m_NoiseReduction = 0.0;
2080 m_config.upscale = g_advancedSettings.m_videoVDPAUScaling;
2081 m_config.useInteropYuv = !CSettings::Get().GetBool("videoplayer.usevdpaumixer");
2086 void CMixer::Uninit()
2089 while (!m_outputSurfaces.empty())
2091 m_outputSurfaces.pop();
2093 m_config.vdpProcs.vdp_video_mixer_destroy(m_videoMixer);
2095 delete [] m_BlackBar;
2098 void CMixer::Flush()
2100 while (!m_mixerInput.empty())
2102 CVdpauDecodedPicture pic = m_mixerInput.back();
2103 m_mixerInput.pop_back();
2104 CSingleLock lock(*m_config.videoSurfaceSec);
2106 pic.render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
2108 while (!m_decodedPics.empty())
2110 CVdpauDecodedPicture pic = m_decodedPics.front();
2111 m_decodedPics.pop();
2112 CSingleLock lock(*m_config.videoSurfaceSec);
2114 pic.render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
2117 while (m_dataPort.ReceiveOutMessage(&msg))
2119 if (msg->signal == CMixerDataProtocol::FRAME)
2121 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2122 CSingleLock lock(*m_config.videoSurfaceSec);
2124 pic.render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
2126 else if (msg->signal == CMixerDataProtocol::BUFFER)
2128 VdpOutputSurface *surf;
2129 surf = (VdpOutputSurface*)msg->data;
2130 m_outputSurfaces.push(*surf);
2136 void CMixer::InitCycle()
2141 m_config.stats->GetParams(latency, flags);
2142 latency = (latency*1000)/CurrentHostFrequency();
2143 if (flags & DVP_FLAG_NO_POSTPROC)
2144 SetPostProcFeatures(false);
2146 SetPostProcFeatures(true);
2148 m_config.stats->SetCanSkipDeint(false);
2150 EDEINTERLACEMODE mode = CMediaSettings::Get().GetCurrentVideoSettings().m_DeinterlaceMode;
2151 EINTERLACEMETHOD method = GetDeinterlacingMethod();
2152 bool interlaced = m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_INTERLACED;
2154 if (!(flags & DVP_FLAG_NO_POSTPROC) &&
2155 (mode == VS_DEINTERLACEMODE_FORCE ||
2156 (mode == VS_DEINTERLACEMODE_AUTO && interlaced)))
2158 if((method == VS_INTERLACEMETHOD_AUTO && interlaced)
2159 || method == VS_INTERLACEMETHOD_VDPAU_BOB
2160 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL
2161 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2162 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL
2163 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2164 || method == VS_INTERLACEMETHOD_VDPAU_INVERSE_TELECINE )
2166 if(method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_HALF
2167 || method == VS_INTERLACEMETHOD_VDPAU_TEMPORAL_SPATIAL_HALF
2168 || !g_graphicsContext.IsFullScreenVideo())
2173 m_config.stats->SetCanSkipDeint(true);
2176 if (m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_DROPDEINT)
2181 if(m_mixerInput[1].DVDPic.iFlags & DVP_FLAG_TOP_FIELD_FIRST)
2182 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2184 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2186 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2187 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2188 DVP_FLAG_REPEAT_TOP_FIELD |
2189 DVP_FLAG_INTERLACED);
2190 m_config.useInteropYuv = false;
2192 else if (method == VS_INTERLACEMETHOD_RENDER_BOB)
2195 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2196 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2197 m_config.useInteropYuv = true;
2201 CLog::Log(LOGERROR, "CMixer::%s - interlace method not supported", __FUNCTION__);
2203 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2204 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2205 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2206 DVP_FLAG_REPEAT_TOP_FIELD |
2207 DVP_FLAG_INTERLACED);
2213 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
2215 if (m_config.useInteropYuv)
2216 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU_420;
2219 m_mixerInput[1].DVDPic.format = RENDER_FMT_VDPAU;
2220 m_mixerInput[1].DVDPic.iFlags &= ~(DVP_FLAG_TOP_FIELD_FIRST |
2221 DVP_FLAG_REPEAT_TOP_FIELD |
2222 DVP_FLAG_INTERLACED);
2227 if (m_mixerInput[1].DVDPic.format == RENDER_FMT_VDPAU)
2229 m_processPicture.outputSurface = m_outputSurfaces.front();
2230 m_mixerInput[1].DVDPic.iWidth = m_config.outWidth;
2231 m_mixerInput[1].DVDPic.iHeight = m_config.outHeight;
2235 m_mixerInput[1].DVDPic.iWidth = m_config.vidWidth;
2236 m_mixerInput[1].DVDPic.iHeight = m_config.vidHeight;
2239 m_processPicture.DVDPic = m_mixerInput[1].DVDPic;
2240 m_processPicture.render = m_mixerInput[1].render;
2243 void CMixer::FiniCycle()
2245 while (m_mixerInput.size() > 3)
2247 CVdpauDecodedPicture &tmp = m_mixerInput.back();
2248 if (tmp.render && m_processPicture.DVDPic.format != RENDER_FMT_VDPAU_420)
2250 CSingleLock lock(*m_config.videoSurfaceSec);
2251 tmp.render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
2253 m_mixerInput.pop_back();
2254 // m_config.stats->DecDecoded();
2258 void CMixer::ProcessPicture()
2260 if (m_processPicture.DVDPic.format == RENDER_FMT_VDPAU_420)
2265 if (m_mixerstep == 1)
2267 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD)
2268 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD;
2270 m_mixerfield = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
2273 VdpVideoSurface past_surfaces[4] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2274 VdpVideoSurface futu_surfaces[2] = { VDP_INVALID_HANDLE, VDP_INVALID_HANDLE };
2275 uint32_t pastCount = 4;
2276 uint32_t futuCount = 2;
2278 if(m_mixerfield == VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
2280 // use only 2 past 1 future for progressive/weave
2281 // (only used for postproc anyway eg noise reduction)
2282 if (m_mixerInput.size() > 3)
2283 past_surfaces[1] = m_mixerInput[3].render->surface;
2284 if (m_mixerInput.size() > 2)
2285 past_surfaces[0] = m_mixerInput[2].render->surface;
2286 futu_surfaces[0] = m_mixerInput[0].render->surface;
2292 if(m_mixerstep == 0)
2294 if (m_mixerInput.size() > 3)
2296 past_surfaces[3] = m_mixerInput[3].render->surface;
2297 past_surfaces[2] = m_mixerInput[3].render->surface;
2299 if (m_mixerInput.size() > 2)
2301 past_surfaces[1] = m_mixerInput[2].render->surface;
2302 past_surfaces[0] = m_mixerInput[2].render->surface;
2304 futu_surfaces[0] = m_mixerInput[1].render->surface;
2305 futu_surfaces[1] = m_mixerInput[0].render->surface;;
2309 if (m_mixerInput.size() > 3)
2311 past_surfaces[3] = m_mixerInput[3].render->surface;
2313 if (m_mixerInput.size() > 2)
2315 past_surfaces[2] = m_mixerInput[2].render->surface;
2316 past_surfaces[1] = m_mixerInput[2].render->surface;
2318 past_surfaces[0] = m_mixerInput[1].render->surface;
2319 futu_surfaces[0] = m_mixerInput[1].render->surface;
2320 futu_surfaces[1] = m_mixerInput[1].render->surface;
2322 m_processPicture.DVDPic.pts = DVD_NOPTS_VALUE;
2323 m_processPicture.DVDPic.dts = DVD_NOPTS_VALUE;
2325 m_processPicture.DVDPic.iRepeatPicture = 0.0;
2331 sourceRect.x1 = m_config.vidWidth;
2332 sourceRect.y1 = m_config.vidHeight;
2337 destRect.x1 = m_config.outWidth;
2338 destRect.y1 = m_config.outHeight;
2340 // start vdpau video mixer
2341 vdp_st = m_config.vdpProcs.vdp_video_mixer_render(m_videoMixer,
2347 m_mixerInput[1].render->surface,
2351 m_processPicture.outputSurface,
2356 CheckStatus(vdp_st, __LINE__);
2358 if (m_mixerfield != VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
2360 // in order to clip top and bottom lines when de-interlacing
2361 // we black those lines as a work around for not working
2362 // background colour using the mixer
2363 // pixel perfect is preferred over overscanning or zooming
2365 VdpRect clipRect = destRect;
2366 clipRect.y1 = clipRect.y0 + 2;
2367 uint32_t *data[] = {m_BlackBar};
2368 uint32_t pitches[] = {destRect.x1};
2369 vdp_st = m_config.vdpProcs.vdp_output_surface_put_bits_native(m_processPicture.outputSurface,
2373 CheckStatus(vdp_st, __LINE__);
2375 clipRect = destRect;
2376 clipRect.y0 = clipRect.y1 - 2;
2377 vdp_st = m_config.vdpProcs.vdp_output_surface_put_bits_native(m_processPicture.outputSurface,
2381 CheckStatus(vdp_st, __LINE__);
2386 bool CMixer::CheckStatus(VdpStatus vdp_st, int line)
2388 if (vdp_st != VDP_STATUS_OK)
2390 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) at %s:%d\n", m_config.vdpProcs.vdp_get_error_string(vdp_st), vdp_st, __FILE__, line);
2397 //-----------------------------------------------------------------------------
2399 //-----------------------------------------------------------------------------
2401 VdpauBufferPool::VdpauBufferPool()
2403 CVdpauRenderPicture *pic;
2404 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2406 pic = new CVdpauRenderPicture(renderPicSec);
2407 allRenderPics.push_back(pic);
2411 VdpauBufferPool::~VdpauBufferPool()
2413 CVdpauRenderPicture *pic;
2414 for (unsigned int i = 0; i < NUM_RENDER_PICS; i++)
2416 pic = allRenderPics[i];
2419 allRenderPics.clear();
2422 //-----------------------------------------------------------------------------
2424 //-----------------------------------------------------------------------------
2425 COutput::COutput(CEvent *inMsgEvent) :
2426 CThread("Vdpau Output Thread"),
2427 m_controlPort("OutputControlPort", inMsgEvent, &m_outMsgEvent),
2428 m_dataPort("OutputDataPort", inMsgEvent, &m_outMsgEvent),
2429 m_mixer(&m_outMsgEvent)
2431 m_inMsgEvent = inMsgEvent;
2433 for (unsigned int i = 0; i < m_bufferPool.allRenderPics.size(); ++i)
2435 m_bufferPool.freeRenderPics.push_back(i);
2439 void COutput::Start()
2448 m_bufferPool.freeRenderPics.clear();
2449 m_bufferPool.usedRenderPics.clear();
2452 void COutput::Dispose()
2454 CSingleLock lock(g_graphicsContext);
2456 m_outMsgEvent.Set();
2458 m_controlPort.Purge();
2462 void COutput::OnStartup()
2464 CLog::Log(LOGNOTICE, "COutput::OnStartup: Output Thread created");
2467 void COutput::OnExit()
2469 CLog::Log(LOGNOTICE, "COutput::OnExit: Output Thread terminated");
2476 O_TOP_UNCONFIGURED, // 2
2477 O_TOP_CONFIGURED, // 3
2478 O_TOP_CONFIGURED_IDLE, // 4
2479 O_TOP_CONFIGURED_WORK, // 5
2482 int VDPAU_OUTPUT_parentStates[] = {
2485 0, //TOP_UNCONFIGURED
2487 3, //TOP_CONFIGURED_IDLE
2488 3, //TOP_CONFIGURED_WORK
2491 void COutput::StateMachine(int signal, Protocol *port, Message *msg)
2493 for (int state = m_state; ; state = VDPAU_OUTPUT_parentStates[state])
2498 if (port == &m_controlPort)
2502 case COutputControlProtocol::FLUSH:
2503 msg->Reply(COutputControlProtocol::ACC);
2505 case COutputControlProtocol::PRECLEANUP:
2506 msg->Reply(COutputControlProtocol::ACC);
2512 else if (port == &m_dataPort)
2516 case COutputDataProtocol::RETURNPIC:
2517 CVdpauRenderPicture *pic;
2518 pic = *((CVdpauRenderPicture**)msg->data);
2519 ProcessReturnPicture(pic);
2526 std::string portName = port == NULL ? "timer" : port->portName;
2527 CLog::Log(LOGWARNING, "COutput::%s - signal: %d form port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
2534 case O_TOP_UNCONFIGURED:
2535 if (port == &m_controlPort)
2539 case COutputControlProtocol::INIT:
2541 data = (CVdpauConfig*)msg->data;
2548 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::INIT,
2549 &reply, 1000, &m_config, sizeof(m_config)))
2551 if (reply->signal != CMixerControlProtocol::ACC)
2556 // set initial number of
2557 m_bufferPool.numOutputSurfaces = 4;
2561 m_state = O_TOP_CONFIGURED_IDLE;
2562 msg->Reply(COutputControlProtocol::ACC, &m_config, sizeof(m_config));
2566 m_state = O_TOP_ERROR;
2567 msg->Reply(COutputControlProtocol::ERROR);
2576 case O_TOP_CONFIGURED:
2577 if (port == &m_controlPort)
2581 case COutputControlProtocol::FLUSH:
2583 msg->Reply(COutputControlProtocol::ACC);
2585 case COutputControlProtocol::PRECLEANUP:
2588 msg->Reply(COutputControlProtocol::ACC);
2594 else if (port == &m_dataPort)
2598 case COutputDataProtocol::NEWFRAME:
2599 CVdpauDecodedPicture *frame;
2600 frame = (CVdpauDecodedPicture*)msg->data;
2603 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::FRAME,
2604 frame,sizeof(CVdpauDecodedPicture));
2607 case COutputDataProtocol::RETURNPIC:
2608 CVdpauRenderPicture *pic;
2609 pic = *((CVdpauRenderPicture**)msg->data);
2610 ProcessReturnPicture(pic);
2611 m_controlPort.SendInMessage(COutputControlProtocol::STATS);
2612 m_state = O_TOP_CONFIGURED_WORK;
2619 else if (port == &m_mixer.m_dataPort)
2623 case CMixerDataProtocol::PICTURE:
2624 CVdpauProcessedPicture *pic;
2625 pic = (CVdpauProcessedPicture*)msg->data;
2626 m_bufferPool.processedPics.push(*pic);
2627 m_state = O_TOP_CONFIGURED_WORK;
2636 case O_TOP_CONFIGURED_IDLE:
2637 if (port == NULL) // timeout
2641 case COutputControlProtocol::TIMEOUT:
2649 case O_TOP_CONFIGURED_WORK:
2650 if (port == NULL) // timeout
2654 case COutputControlProtocol::TIMEOUT:
2657 CVdpauRenderPicture *pic;
2658 pic = ProcessMixerPicture();
2661 m_config.stats->DecProcessed();
2662 m_config.stats->IncRender();
2663 m_dataPort.SendInMessage(COutputDataProtocol::PICTURE, &pic, sizeof(pic));
2669 m_state = O_TOP_CONFIGURED_IDLE;
2679 default: // we are in no state, should not happen
2680 CLog::Log(LOGERROR, "COutput::%s - no valid state: %d", __FUNCTION__, m_state);
2686 void COutput::Process()
2688 Message *msg = NULL;
2689 Protocol *port = NULL;
2692 m_state = O_TOP_UNCONFIGURED;
2693 m_extTimeout = 1000;
2694 m_bStateMachineSelfTrigger = false;
2700 if (m_bStateMachineSelfTrigger)
2702 m_bStateMachineSelfTrigger = false;
2703 // self trigger state machine
2704 StateMachine(msg->signal, port, msg);
2705 if (!m_bStateMachineSelfTrigger)
2712 // check control port
2713 else if (m_controlPort.ReceiveOutMessage(&msg))
2716 port = &m_controlPort;
2719 else if (m_dataPort.ReceiveOutMessage(&msg))
2724 // check mixer data port
2725 else if (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2728 port = &m_mixer.m_dataPort;
2732 StateMachine(msg->signal, port, msg);
2733 if (!m_bStateMachineSelfTrigger)
2742 else if (m_outMsgEvent.WaitMSec(m_extTimeout))
2749 msg = m_controlPort.GetMessage();
2750 msg->signal = COutputControlProtocol::TIMEOUT;
2752 // signal timeout to state machine
2753 StateMachine(msg->signal, port, msg);
2754 if (!m_bStateMachineSelfTrigger)
2765 bool COutput::Init()
2767 if (!CreateGlxContext())
2779 bool COutput::Uninit()
2784 ReleaseBufferPool();
2785 DestroyGlxContext();
2789 void COutput::Flush()
2791 if (m_mixer.IsActive())
2794 if (m_mixer.m_controlPort.SendOutMessageSync(CMixerControlProtocol::FLUSH,
2801 CLog::Log(LOGERROR, "Coutput::%s - failed to flush mixer", __FUNCTION__);
2805 while (m_mixer.m_dataPort.ReceiveInMessage(&msg))
2807 if (msg->signal == CMixerDataProtocol::PICTURE)
2809 CVdpauProcessedPicture pic = *(CVdpauProcessedPicture*)msg->data;
2810 if (pic.DVDPic.format == RENDER_FMT_VDPAU_420)
2812 CSingleLock lock(*m_config.videoSurfaceSec);
2814 pic.render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
2820 while (m_dataPort.ReceiveOutMessage(&msg))
2822 if (msg->signal == COutputDataProtocol::NEWFRAME)
2824 CVdpauDecodedPicture pic = *(CVdpauDecodedPicture*)msg->data;
2825 CSingleLock lock(*m_config.videoSurfaceSec);
2827 pic.render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
2829 else if (msg->signal == COutputDataProtocol::RETURNPIC)
2831 CVdpauRenderPicture *pic;
2832 pic = *((CVdpauRenderPicture**)msg->data);
2833 ProcessReturnPicture(pic);
2838 while (m_dataPort.ReceiveInMessage(&msg))
2840 if (msg->signal == COutputDataProtocol::PICTURE)
2842 CVdpauRenderPicture *pic;
2843 pic = *((CVdpauRenderPicture**)msg->data);
2844 ProcessReturnPicture(pic);
2848 // reset used render flag which was cleared on mixer flush
2849 std::deque<int>::iterator it;
2850 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
2852 CVdpauRenderPicture *pic = m_bufferPool.allRenderPics[*it];
2853 if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
2855 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it2;
2856 it2 = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
2857 if (it2 == m_bufferPool.glVideoSurfaceMap.end())
2859 CLog::Log(LOGDEBUG, "COutput::Flush - gl surface not found");
2862 vdpau_render_state *render = it2->second.sourceVuv;
2864 render->state |= FF_VDPAU_STATE_USED_FOR_RENDER;
2869 bool COutput::HasWork()
2871 if (m_config.usePixmaps)
2873 if (!m_bufferPool.processedPics.empty() && FindFreePixmap() >= 0)
2875 if (!m_bufferPool.notVisiblePixmaps.empty() && !m_bufferPool.freeRenderPics.empty())
2881 if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
2887 CVdpauRenderPicture* COutput::ProcessMixerPicture()
2889 CVdpauRenderPicture *retPic = NULL;
2891 if (m_config.usePixmaps)
2893 if (!m_bufferPool.processedPics.empty() && FindFreePixmap() >= 0)
2895 unsigned int i = FindFreePixmap();
2896 VdpauBufferPool::Pixmaps *pixmap = &m_bufferPool.pixmaps[i];
2897 pixmap->used = true;
2898 CVdpauProcessedPicture pic = m_bufferPool.processedPics.front();
2899 m_bufferPool.processedPics.pop();
2900 pixmap->surface = pic.outputSurface;
2901 pixmap->DVDPic = pic.DVDPic;
2903 m_bufferPool.notVisiblePixmaps.push_back(i);
2904 m_config.vdpProcs.vdp_presentation_queue_display(pixmap->vdp_flip_queue,
2905 pixmap->surface,0,0,0);
2907 if (!m_bufferPool.notVisiblePixmaps.empty() && !m_bufferPool.freeRenderPics.empty())
2911 VdpPresentationQueueStatus status;
2912 int idx = m_bufferPool.notVisiblePixmaps.front();
2913 VdpauBufferPool::Pixmaps *pixmap = &m_bufferPool.pixmaps[idx];
2914 vdp_st = m_config.vdpProcs.vdp_presentation_queue_query_surface_status(
2915 pixmap->vdp_flip_queue, pixmap->surface, &status, &time);
2917 if (vdp_st == VDP_STATUS_OK && status == VDP_PRESENTATION_QUEUE_STATUS_VISIBLE)
2919 int idx = m_bufferPool.freeRenderPics.front();
2920 retPic = m_bufferPool.allRenderPics[idx];
2921 m_bufferPool.freeRenderPics.pop_front();
2922 m_bufferPool.usedRenderPics.push_back(idx);
2923 retPic->sourceIdx = pixmap->id;
2924 retPic->DVDPic = pixmap->DVDPic;
2925 retPic->valid = true;
2926 retPic->texture[0] = pixmap->texture;
2927 retPic->crop = CRect(0,0,0,0);
2928 m_bufferPool.notVisiblePixmaps.pop_front();
2929 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &pixmap->surface, sizeof(pixmap->surface));
2933 else if (!m_bufferPool.processedPics.empty() && !m_bufferPool.freeRenderPics.empty())
2935 int idx = m_bufferPool.freeRenderPics.front();
2936 retPic = m_bufferPool.allRenderPics[idx];
2937 m_bufferPool.freeRenderPics.pop_front();
2938 m_bufferPool.usedRenderPics.push_back(idx);
2939 CVdpauProcessedPicture procPic = m_bufferPool.processedPics.front();
2940 m_bufferPool.processedPics.pop();
2942 retPic->DVDPic = procPic.DVDPic;
2943 retPic->valid = true;
2944 if (retPic->DVDPic.format == RENDER_FMT_VDPAU)
2946 m_config.useInteropYuv = false;
2947 m_bufferPool.numOutputSurfaces = NUM_RENDER_PICS;
2950 retPic->sourceIdx = procPic.outputSurface;
2951 retPic->texture[0] = m_bufferPool.glOutputSurfaceMap[procPic.outputSurface].texture[0];
2952 retPic->crop = CRect(0,0,0,0);
2956 m_config.useInteropYuv = true;
2958 retPic->sourceIdx = procPic.render->surface;
2959 for (unsigned int i=0; i<4; ++i)
2960 retPic->texture[i] = m_bufferPool.glVideoSurfaceMap[procPic.render->surface].texture[i];
2961 retPic->texWidth = m_config.surfaceWidth;
2962 retPic->texHeight = m_config.surfaceHeight;
2963 retPic->crop.x1 = 0;
2964 retPic->crop.y1 = 0;
2965 retPic->crop.x2 = m_config.surfaceWidth - m_config.vidWidth;
2966 retPic->crop.y2 = m_config.surfaceHeight - m_config.vidHeight;
2972 void COutput::ProcessReturnPicture(CVdpauRenderPicture *pic)
2974 std::deque<int>::iterator it;
2975 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
2977 if (m_bufferPool.allRenderPics[*it] == pic)
2983 if (it == m_bufferPool.usedRenderPics.end())
2985 CLog::Log(LOGWARNING, "COutput::ProcessReturnPicture - pic not found");
2988 m_bufferPool.freeRenderPics.push_back(*it);
2989 m_bufferPool.usedRenderPics.erase(it);
2992 CLog::Log(LOGDEBUG, "COutput::%s - return of invalid render pic", __FUNCTION__);
2996 if (m_config.usePixmaps)
2998 m_bufferPool.pixmaps[pic->sourceIdx].used = false;
3001 else if (pic->DVDPic.format == RENDER_FMT_VDPAU_420)
3003 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3004 it = m_bufferPool.glVideoSurfaceMap.find(pic->sourceIdx);
3005 if (it == m_bufferPool.glVideoSurfaceMap.end())
3007 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3010 vdpau_render_state *render = it->second.sourceVuv;
3011 CSingleLock lock(*m_config.videoSurfaceSec);
3012 render->state &= ~FF_VDPAU_STATE_USED_FOR_RENDER;
3014 else if (pic->DVDPic.format == RENDER_FMT_VDPAU)
3016 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3017 it = m_bufferPool.glOutputSurfaceMap.find(pic->sourceIdx);
3018 if (it == m_bufferPool.glOutputSurfaceMap.end())
3020 CLog::Log(LOGDEBUG, "COutput::ProcessReturnPicture - gl surface not found");
3023 VdpOutputSurface outSurf = it->second.sourceRgb;
3024 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER, &outSurf, sizeof(outSurf));
3028 int COutput::FindFreePixmap()
3032 for (i = 0; i < m_bufferPool.pixmaps.size(); ++i)
3034 if (!m_bufferPool.pixmaps[i].used)
3037 if (i == m_bufferPool.pixmaps.size())
3043 bool COutput::EnsureBufferPool()
3047 // Creation of outputSurfaces
3048 VdpOutputSurface outputSurface;
3049 for (int i = m_bufferPool.outputSurfaces.size(); i < m_bufferPool.numOutputSurfaces; i++)
3051 vdp_st = m_config.vdpProcs.vdp_output_surface_create(m_config.vdpDevice,
3052 VDP_RGBA_FORMAT_B8G8R8A8,
3056 if (CheckStatus(vdp_st, __LINE__))
3058 m_bufferPool.outputSurfaces.push_back(outputSurface);
3060 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3062 sizeof(VdpOutputSurface));
3063 CLog::Log(LOGNOTICE, "VDPAU::COutput::InitBufferPool - Output Surface created");
3067 if (m_config.usePixmaps && m_bufferPool.pixmaps.empty())
3070 VdpauBufferPool::Pixmaps pixmap;
3071 unsigned int numPixmaps = NUM_RENDER_PICS;
3072 for (unsigned int i = 0; i < numPixmaps; i++)
3074 pixmap.pixmap = None;
3075 pixmap.glPixmap = None;
3076 pixmap.vdp_flip_queue = VDP_INVALID_HANDLE;
3077 pixmap.vdp_flip_target = VDP_INVALID_HANDLE;
3079 glXMakeCurrent(m_Display, None, NULL);
3080 vdp_st = m_config.vdpProcs.vdp_presentation_queue_target_create_x11(m_config.vdpDevice,
3081 pixmap.pixmap, //x_window,
3082 &pixmap.vdp_flip_target);
3084 CheckStatus(vdp_st, __LINE__);
3086 vdp_st = m_config.vdpProcs.vdp_presentation_queue_create(m_config.vdpDevice,
3087 pixmap.vdp_flip_target,
3088 &pixmap.vdp_flip_queue);
3089 CheckStatus(vdp_st, __LINE__);
3090 glXMakeCurrent(m_Display, m_glPixmap, m_glContext);
3093 pixmap.used = false;
3094 m_bufferPool.pixmaps.push_back(pixmap);
3102 void COutput::ReleaseBufferPool()
3106 CSingleLock lock(m_bufferPool.renderPicSec);
3108 if (m_config.usePixmaps)
3110 for (unsigned int i = 0; i < m_bufferPool.pixmaps.size(); ++i)
3112 if (m_bufferPool.pixmaps[i].vdp_flip_queue != VDP_INVALID_HANDLE)
3114 vdp_st = m_config.vdpProcs.vdp_presentation_queue_destroy(m_bufferPool.pixmaps[i].vdp_flip_queue);
3115 CheckStatus(vdp_st, __LINE__);
3117 if (m_bufferPool.pixmaps[i].vdp_flip_target != VDP_INVALID_HANDLE)
3119 vdp_st = m_config.vdpProcs.vdp_presentation_queue_target_destroy(m_bufferPool.pixmaps[i].vdp_flip_target);
3120 CheckStatus(vdp_st, __LINE__);
3122 if (m_bufferPool.pixmaps[i].glPixmap)
3124 glXDestroyPixmap(m_Display, m_bufferPool.pixmaps[i].glPixmap);
3126 if (m_bufferPool.pixmaps[i].pixmap)
3128 XFreePixmap(m_Display, m_bufferPool.pixmaps[i].pixmap);
3131 m_bufferPool.pixmaps.clear();
3134 // release all output surfaces
3135 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3137 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3139 vdp_st = m_config.vdpProcs.vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3140 CheckStatus(vdp_st, __LINE__);
3142 m_bufferPool.outputSurfaces.clear();
3144 // invalidate all used render pictures
3145 for (unsigned int i = 0; i < m_bufferPool.usedRenderPics.size(); ++i)
3147 m_bufferPool.allRenderPics[m_bufferPool.usedRenderPics[i]]->valid = false;
3151 void COutput::PreCleanup()
3158 CSingleLock lock(m_bufferPool.renderPicSec);
3159 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3161 if (m_bufferPool.outputSurfaces[i] == VDP_INVALID_HANDLE)
3164 // check if output surface is in use
3166 std::deque<int>::iterator it;
3167 CVdpauRenderPicture *pic;
3168 for (it = m_bufferPool.usedRenderPics.begin(); it != m_bufferPool.usedRenderPics.end(); ++it)
3170 pic = m_bufferPool.allRenderPics[*it];
3171 if ((pic->sourceIdx == m_bufferPool.outputSurfaces[i]) && pic->valid)
3180 #ifdef GL_NV_vdpau_interop
3182 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it_map;
3183 it_map = m_bufferPool.glOutputSurfaceMap.find(m_bufferPool.outputSurfaces[i]);
3184 if (it_map == m_bufferPool.glOutputSurfaceMap.end())
3186 CLog::Log(LOGERROR, "%s - could not find gl surface", __FUNCTION__);
3189 glVDPAUUnregisterSurfaceNV(it_map->second.glVdpauSurface);
3190 glDeleteTextures(1, it_map->second.texture);
3191 m_bufferPool.glOutputSurfaceMap.erase(it_map);
3194 vdp_st = m_config.vdpProcs.vdp_output_surface_destroy(m_bufferPool.outputSurfaces[i]);
3195 CheckStatus(vdp_st, __LINE__);
3197 m_bufferPool.outputSurfaces[i] = VDP_INVALID_HANDLE;
3199 CLog::Log(LOGDEBUG, "VDPAU::PreCleanup - released output surface");
3204 void COutput::InitMixer()
3206 for (unsigned int i = 0; i < m_bufferPool.outputSurfaces.size(); ++i)
3208 m_mixer.m_dataPort.SendOutMessage(CMixerDataProtocol::BUFFER,
3209 &m_bufferPool.outputSurfaces[i],
3210 sizeof(VdpOutputSurface));
3214 bool COutput::MakePixmap(VdpauBufferPool::Pixmaps &pixmap)
3216 CLog::Log(LOGNOTICE,"Creating %ix%i pixmap", m_config.outWidth, m_config.outHeight);
3218 // Get our window attribs.
3219 XWindowAttributes wndattribs;
3220 XGetWindowAttributes(m_Display, g_Windowing.GetWindow(), &wndattribs);
3222 pixmap.pixmap = XCreatePixmap(m_Display,
3223 g_Windowing.GetWindow(),
3229 CLog::Log(LOGERROR, "VDPAU::COUtput::MakePixmap - GLX Error: MakePixmap: Unable to create XPixmap");
3233 // XGCValues values = {};
3235 // values.foreground = BlackPixel (m_Display, DefaultScreen (m_Display));
3236 // xgc = XCreateGC(m_Display, pixmap.pixmap, GCForeground, &values);
3237 // XFillRectangle(m_Display, pixmap.pixmap, xgc, 0, 0, m_config.outWidth, m_config.outHeight);
3238 // XFreeGC(m_Display, xgc);
3240 if(!MakePixmapGL(pixmap))
3246 bool COutput::MakePixmapGL(VdpauBufferPool::Pixmaps &pixmap)
3249 int fbConfigIndex = 0;
3251 int doubleVisAttributes[] = {
3252 GLX_RENDER_TYPE, GLX_RGBA_BIT,
3258 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
3259 GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
3260 GLX_DOUBLEBUFFER, False,
3261 GLX_Y_INVERTED_EXT, True,
3262 GLX_X_RENDERABLE, True,
3266 int pixmapAttribs[] = {
3267 GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
3268 GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
3272 GLXFBConfig *fbConfigs;
3273 fbConfigs = glXChooseFBConfig(m_Display, g_Windowing.GetCurrentScreen(), doubleVisAttributes, &num);
3274 if (fbConfigs==NULL)
3276 CLog::Log(LOGERROR, "VDPAU::COutput::MakPixmapGL - No compatible framebuffers found");
3281 pixmap.glPixmap = glXCreatePixmap(m_Display, fbConfigs[fbConfigIndex], pixmap.pixmap, pixmapAttribs);
3283 if (!pixmap.glPixmap)
3285 CLog::Log(LOGERROR, "VDPAU::COutput::MakPixmapGL - Could not create Pixmap");
3293 bool COutput::GLInit()
3295 glXBindTexImageEXT = NULL;
3296 glXReleaseTexImageEXT = NULL;
3297 #ifdef GL_NV_vdpau_interop
3298 glVDPAUInitNV = NULL;
3299 glVDPAUFiniNV = NULL;
3300 glVDPAURegisterOutputSurfaceNV = NULL;
3301 glVDPAURegisterVideoSurfaceNV = NULL;
3302 glVDPAUIsSurfaceNV = NULL;
3303 glVDPAUUnregisterSurfaceNV = NULL;
3304 glVDPAUSurfaceAccessNV = NULL;
3305 glVDPAUMapSurfacesNV = NULL;
3306 glVDPAUUnmapSurfacesNV = NULL;
3307 glVDPAUGetSurfaceivNV = NULL;
3310 m_config.usePixmaps = false;
3312 #ifdef GL_NV_vdpau_interop
3313 if (glewIsSupported("GL_NV_vdpau_interop"))
3316 glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUInitNV");
3318 glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((GLubyte *) "glVDPAUFiniNV");
3319 if (!glVDPAURegisterOutputSurfaceNV)
3320 glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterOutputSurfaceNV");
3321 if (!glVDPAURegisterVideoSurfaceNV)
3322 glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAURegisterVideoSurfaceNV");
3323 if (!glVDPAUIsSurfaceNV)
3324 glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUIsSurfaceNV");
3325 if (!glVDPAUUnregisterSurfaceNV)
3326 glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnregisterSurfaceNV");
3327 if (!glVDPAUSurfaceAccessNV)
3328 glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUSurfaceAccessNV");
3329 if (!glVDPAUMapSurfacesNV)
3330 glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUMapSurfacesNV");
3331 if (!glVDPAUUnmapSurfacesNV)
3332 glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUUnmapSurfacesNV");
3333 if (!glVDPAUGetSurfaceivNV)
3334 glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((GLubyte *) "glVDPAUGetSurfaceivNV");
3336 CLog::Log(LOGNOTICE, "VDPAU::COutput GL interop supported");
3341 m_config.usePixmaps = true;
3342 CSettings::Get().SetBool("videoplayer.usevdpaumixer",true);
3344 if (!glXBindTexImageEXT)
3345 glXBindTexImageEXT = (PFNGLXBINDTEXIMAGEEXTPROC)glXGetProcAddress((GLubyte *) "glXBindTexImageEXT");
3346 if (!glXReleaseTexImageEXT)
3347 glXReleaseTexImageEXT = (PFNGLXRELEASETEXIMAGEEXTPROC)glXGetProcAddress((GLubyte *) "glXReleaseTexImageEXT");
3349 #ifdef GL_NV_vdpau_interop
3350 if (!m_config.usePixmaps)
3352 while (glGetError() != GL_NO_ERROR);
3353 glVDPAUInitNV(reinterpret_cast<void*>(m_config.vdpDevice), reinterpret_cast<void*>(m_config.vdpProcs.vdp_get_proc_address));
3354 if (glGetError() != GL_NO_ERROR)
3356 CLog::Log(LOGERROR, "VDPAU::COutput - GLInitInterop glVDPAUInitNV failed");
3360 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop initialized");
3366 void COutput::GLMapSurfaces()
3368 #ifdef GL_NV_vdpau_interop
3369 if (m_config.usePixmaps)
3372 if (m_config.useInteropYuv)
3374 VdpauBufferPool::GLVideoSurface glSurface;
3375 if (m_config.videoSurfaces->size() != m_bufferPool.glVideoSurfaceMap.size())
3377 CSingleLock lock(*m_config.videoSurfaceSec);
3378 for (unsigned int i = 0; i < m_config.videoSurfaces->size(); i++)
3380 if ((*m_config.videoSurfaces)[i]->surface == VDP_INVALID_HANDLE)
3383 if (m_bufferPool.glVideoSurfaceMap.find((*m_config.videoSurfaces)[i]->surface) == m_bufferPool.glVideoSurfaceMap.end())
3385 glSurface.sourceVuv = (*m_config.videoSurfaces)[i];
3386 while (glGetError() != GL_NO_ERROR) ;
3387 glGenTextures(4, glSurface.texture);
3388 if (glGetError() != GL_NO_ERROR)
3390 CLog::Log(LOGERROR, "VDPAU::COutput error creating texture");
3393 glSurface.glVdpauSurface = glVDPAURegisterVideoSurfaceNV(reinterpret_cast<void*>((*m_config.videoSurfaces)[i]->surface),
3394 GL_TEXTURE_2D, 4, glSurface.texture);
3396 if (glGetError() != GL_NO_ERROR)
3398 CLog::Log(LOGERROR, "VDPAU::COutput error register video surface");
3401 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3402 if (glGetError() != GL_NO_ERROR)
3404 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3407 glVDPAUMapSurfacesNV(1, &glSurface.glVdpauSurface);
3408 if (glGetError() != GL_NO_ERROR)
3410 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3413 m_bufferPool.glVideoSurfaceMap[(*m_config.videoSurfaces)[i]->surface] = glSurface;
3416 CLog::Log(LOGNOTICE, "VDPAU::COutput registered surface");
3423 if (m_bufferPool.glOutputSurfaceMap.size() != m_bufferPool.numOutputSurfaces)
3425 VdpauBufferPool::GLVideoSurface glSurface;
3426 for (unsigned int i = m_bufferPool.glOutputSurfaceMap.size(); i<m_bufferPool.outputSurfaces.size(); i++)
3428 glSurface.sourceRgb = m_bufferPool.outputSurfaces[i];
3429 glGenTextures(1, glSurface.texture);
3430 glSurface.glVdpauSurface = glVDPAURegisterOutputSurfaceNV(reinterpret_cast<void*>(m_bufferPool.outputSurfaces[i]),
3431 GL_TEXTURE_2D, 1, glSurface.texture);
3432 if (glGetError() != GL_NO_ERROR)
3434 CLog::Log(LOGERROR, "VDPAU::COutput error register output surface");
3437 glVDPAUSurfaceAccessNV(glSurface.glVdpauSurface, GL_READ_ONLY);
3438 if (glGetError() != GL_NO_ERROR)
3440 CLog::Log(LOGERROR, "VDPAU::COutput error setting access");
3443 glVDPAUMapSurfacesNV(1, &glSurface.glVdpauSurface);
3444 if (glGetError() != GL_NO_ERROR)
3446 CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface");
3449 m_bufferPool.glOutputSurfaceMap[m_bufferPool.outputSurfaces[i]] = glSurface;
3453 CLog::Log(LOGNOTICE, "VDPAU::COutput registered output surfaces");
3459 void COutput::GLUnmapSurfaces()
3461 #ifdef GL_NV_vdpau_interop
3462 if (m_config.usePixmaps)
3465 { CSingleLock lock(*m_config.videoSurfaceSec);
3466 std::map<VdpVideoSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3467 for (it = m_bufferPool.glVideoSurfaceMap.begin(); it != m_bufferPool.glVideoSurfaceMap.end(); ++it)
3469 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3470 glDeleteTextures(4, it->second.texture);
3472 m_bufferPool.glVideoSurfaceMap.clear();
3475 std::map<VdpOutputSurface, VdpauBufferPool::GLVideoSurface>::iterator it;
3476 for (it = m_bufferPool.glOutputSurfaceMap.begin(); it != m_bufferPool.glOutputSurfaceMap.end(); ++it)
3478 glVDPAUUnregisterSurfaceNV(it->second.glVdpauSurface);
3479 glDeleteTextures(1, it->second.texture);
3481 m_bufferPool.glOutputSurfaceMap.clear();
3485 CLog::Log(LOGNOTICE, "VDPAU::COutput: vdpau gl interop finished");
3490 void COutput::GLBindPixmaps()
3492 if (!m_config.usePixmaps)
3495 for (unsigned int i = 0; i < m_bufferPool.pixmaps.size(); i++)
3498 glGenTextures(1, &m_bufferPool.pixmaps[i].texture);
3501 glBindTexture(GL_TEXTURE_2D, m_bufferPool.pixmaps[i].texture);
3504 glXBindTexImageEXT(m_Display, m_bufferPool.pixmaps[i].glPixmap, GLX_FRONT_LEFT_EXT, NULL);
3506 glBindTexture(GL_TEXTURE_2D, 0);
3509 CLog::Log(LOGNOTICE, "VDPAU::COutput: bound pixmaps");
3512 void COutput::GLUnbindPixmaps()
3514 if (!m_config.usePixmaps)
3517 for (unsigned int i = 0; i < m_bufferPool.pixmaps.size(); i++)
3520 if (!glIsTexture(m_bufferPool.pixmaps[i].texture))
3524 glBindTexture(GL_TEXTURE_2D, m_bufferPool.pixmaps[i].texture);
3527 glXReleaseTexImageEXT(m_Display, m_bufferPool.pixmaps[i].glPixmap, GLX_FRONT_LEFT_EXT);
3529 glBindTexture(GL_TEXTURE_2D, 0);
3531 glDeleteTextures(1, &m_bufferPool.pixmaps[i].texture);
3533 CLog::Log(LOGNOTICE, "VDPAU::COutput: unbound pixmaps");
3536 bool COutput::CheckStatus(VdpStatus vdp_st, int line)
3538 if (vdp_st != VDP_STATUS_OK)
3540 CLog::Log(LOGERROR, " (VDPAU) Error: %s(%d) at %s:%d\n", m_config.vdpProcs.vdp_get_error_string(vdp_st), vdp_st, __FILE__, line);
3547 bool COutput::CreateGlxContext()
3549 GLXContext glContext;
3551 m_Display = g_Windowing.GetDisplay();
3552 glContext = g_Windowing.GetGlxContext();
3553 m_Window = g_Windowing.GetWindow();
3555 // Get our window attribs.
3556 XWindowAttributes wndattribs;
3557 XGetWindowAttributes(m_Display, m_Window, &wndattribs);
3560 XVisualInfo visInfo;
3561 visInfo.visualid = wndattribs.visual->visualid;
3563 XVisualInfo* visuals = XGetVisualInfo(m_Display, VisualIDMask, &visInfo, &nvisuals);
3566 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - could not find visual");
3569 visInfo = visuals[0];
3572 m_pixmap = XCreatePixmap(m_Display,
3579 CLog::Log(LOGERROR, "VDPAU::COutput::CreateGlxContext - Unable to create XPixmap");
3584 m_glPixmap = glXCreateGLXPixmap(m_Display, &visInfo, m_pixmap);
3588 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not create glPixmap");
3592 m_glContext = glXCreateContext(m_Display, &visInfo, glContext, True);
3594 if (!glXMakeCurrent(m_Display, m_glPixmap, m_glContext))
3596 CLog::Log(LOGINFO, "VDPAU::COutput::CreateGlxContext - Could not make Pixmap current");
3600 CLog::Log(LOGNOTICE, "VDPAU::COutput::CreateGlxContext - created context");
3604 bool COutput::DestroyGlxContext()
3608 glXMakeCurrent(m_Display, None, NULL);
3609 glXDestroyContext(m_Display, m_glContext);
3614 glXDestroyPixmap(m_Display, m_glPixmap);
3618 XFreePixmap(m_Display, m_pixmap);