1 /*****************************************************************************
2 * xcommon.c: Functions common to the X11 and XVideo plugins
3 *****************************************************************************
4 * Copyright (C) 1998-2006 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Sam Hocevar <sam@zoy.org>
9 * David Kennedy <dkennedy@tinytoad.com>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
38 #include <vlc_vout_window.h>
41 #ifdef HAVE_MACHINE_PARAM_H
43 # include <machine/param.h>
44 # include <sys/types.h> /* typedef ushort */
49 #include <X11/extensions/Xsp.h>
53 # include <sys/shm.h> /* shmget(), shmctl() */
57 #include <X11/Xproto.h>
59 #include <X11/Xutil.h>
60 #include <X11/keysym.h>
61 #include <X11/XF86keysym.h>
63 # include <X11/extensions/XShm.h>
65 #ifdef DPMSINFO_IN_DPMS_H
66 # include <X11/extensions/dpms.h>
69 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
70 # include <X11/extensions/Xv.h>
71 # include <X11/extensions/Xvlib.h>
74 #ifdef MODULE_NAME_IS_glx
78 #ifdef MODULE_NAME_IS_xvmc
79 # include <X11/extensions/vldXvMC.h>
80 # include "../../codec/xvmc/accel_xvmc.h"
85 /*****************************************************************************
87 *****************************************************************************/
88 int Activate ( vlc_object_t * );
89 void Deactivate ( vlc_object_t * );
91 #ifndef MODULE_NAME_IS_glx
92 static int InitVideo ( vout_thread_t * );
93 static void EndVideo ( vout_thread_t * );
94 static void DisplayVideo ( vout_thread_t *, picture_t * );
96 static int ManageVideo ( vout_thread_t * );
97 static int Control ( vout_thread_t *, int, va_list );
99 static int InitDisplay ( vout_thread_t * );
101 static int CreateWindow ( vout_thread_t *, x11_window_t * );
102 static void DestroyWindow ( vout_thread_t *, x11_window_t * );
104 #ifndef MODULE_NAME_IS_glx
105 static int NewPicture ( vout_thread_t *, picture_t * );
106 static void FreePicture ( vout_thread_t *, picture_t * );
109 #ifdef HAVE_SYS_SHM_H
110 static int i_shm_major = 0;
113 static void ToggleFullScreen ( vout_thread_t * );
115 static void EnableXScreenSaver ( vout_thread_t * );
116 static void DisableXScreenSaver ( vout_thread_t * );
118 static void CreateCursor ( vout_thread_t * );
119 static void DestroyCursor ( vout_thread_t * );
120 static void ToggleCursor ( vout_thread_t * );
122 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
123 static int XVideoGetPort ( vout_thread_t *, vlc_fourcc_t, picture_heap_t * );
124 static void XVideoReleasePort( vout_thread_t *, int );
127 #ifdef MODULE_NAME_IS_x11
128 static void SetPalette ( vout_thread_t *,
129 uint16_t *, uint16_t *, uint16_t * );
132 #ifdef MODULE_NAME_IS_xvmc
133 static void RenderVideo ( vout_thread_t *, picture_t * );
134 static int xvmc_check_yv12( Display *display, XvPortID port );
135 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout );
138 static int X11ErrorHandler( Display *, XErrorEvent * );
141 static void EnablePixelDoubling( vout_thread_t *p_vout );
142 static void DisablePixelDoubling( vout_thread_t *p_vout );
146 static const int i_backlight_on_interval = 300;
151 /*****************************************************************************
152 * Activate: allocate X11 video thread output method
153 *****************************************************************************
154 * This function allocate and initialize a X11 vout method. It uses some of the
155 * vout properties to choose the window size, and change them according to the
156 * actual properties of the display.
157 *****************************************************************************/
158 int Activate ( vlc_object_t *p_this )
160 vout_thread_t *p_vout = (vout_thread_t *)p_this;
162 #if defined(MODULE_NAME_IS_xvmc)
165 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
167 vlc_fourcc_t i_chroma = 0;
171 #ifndef MODULE_NAME_IS_glx
172 p_vout->pf_init = InitVideo;
173 p_vout->pf_end = EndVideo;
174 p_vout->pf_display = DisplayVideo;
176 #ifdef MODULE_NAME_IS_xvmc
177 p_vout->pf_render = RenderVideo;
179 p_vout->pf_render = NULL;
181 p_vout->pf_manage = ManageVideo;
182 p_vout->pf_control = Control;
184 /* Allocate structure */
185 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
186 if( p_vout->p_sys == NULL )
189 /* key and mouse event handling */
190 p_vout->p_sys->i_vout_event = var_CreateGetInteger( p_vout, "vout-event" );
192 /* Open display, using the "display" config variable or the DISPLAY
193 * environment variable */
194 psz_display = config_GetPsz( p_vout, MODULE_STRING "-display" );
196 p_vout->p_sys->p_display = XOpenDisplay( psz_display );
198 if( p_vout->p_sys->p_display == NULL ) /* error */
200 msg_Err( p_vout, "cannot open display %s",
201 XDisplayName( psz_display ) );
202 free( p_vout->p_sys );
208 /* Replace error handler so we can intercept some non-fatal errors */
209 XSetErrorHandler( X11ErrorHandler );
211 /* Get a screen ID matching the XOpenDisplay return value */
212 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
214 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
215 psz_chroma = config_GetPsz( p_vout, "xvideo-chroma" );
218 if( strlen( psz_chroma ) >= 4 )
220 /* Do not use direct assignment because we are not sure of the
222 memcpy(&i_chroma, psz_chroma, 4);
231 msg_Dbg( p_vout, "forcing chroma 0x%.8x (%4.4s)",
232 i_chroma, (char*)&i_chroma );
236 i_chroma = p_vout->render.i_chroma;
239 /* Check that we have access to an XVideo port providing this chroma */
240 p_vout->p_sys->i_xvport = XVideoGetPort( p_vout, VLC2X11_FOURCC(i_chroma),
242 if( p_vout->p_sys->i_xvport < 0 )
244 /* If a specific chroma format was requested, then we don't try to
245 * be cleverer than the user. He knew pretty well what he wanted. */
248 XCloseDisplay( p_vout->p_sys->p_display );
249 free( p_vout->p_sys );
253 /* It failed, but it's not completely lost ! We try to open an
254 * XVideo port for an YUY2 picture. We'll need to do an YUV
255 * conversion, but at least it has got scaling. */
256 p_vout->p_sys->i_xvport =
257 XVideoGetPort( p_vout, X11_FOURCC('Y','U','Y','2'),
259 if( p_vout->p_sys->i_xvport < 0 )
261 /* It failed, but it's not completely lost ! We try to open an
262 * XVideo port for a simple 16bpp RGB picture. We'll need to do
263 * an YUV conversion, but at least it has got scaling. */
264 p_vout->p_sys->i_xvport =
265 XVideoGetPort( p_vout, X11_FOURCC('R','V','1','6'),
267 if( p_vout->p_sys->i_xvport < 0 )
269 XCloseDisplay( p_vout->p_sys->p_display );
270 free( p_vout->p_sys );
275 p_vout->output.i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, X112VLC_FOURCC(p_vout->output.i_chroma) );
276 #elif defined(MODULE_NAME_IS_glx)
278 int i_opcode, i_evt, i_err = 0;
279 int i_maj, i_min = 0;
281 /* Check for GLX extension */
282 if( !XQueryExtension( p_vout->p_sys->p_display, "GLX",
283 &i_opcode, &i_evt, &i_err ) )
285 msg_Err( p_this, "GLX extension not supported" );
286 XCloseDisplay( p_vout->p_sys->p_display );
287 free( p_vout->p_sys );
290 if( !glXQueryExtension( p_vout->p_sys->p_display, &i_err, &i_evt ) )
292 msg_Err( p_this, "glXQueryExtension failed" );
293 XCloseDisplay( p_vout->p_sys->p_display );
294 free( p_vout->p_sys );
298 /* Check GLX version */
299 if (!glXQueryVersion( p_vout->p_sys->p_display, &i_maj, &i_min ) )
301 msg_Err( p_this, "glXQueryVersion failed" );
302 XCloseDisplay( p_vout->p_sys->p_display );
303 free( p_vout->p_sys );
306 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
308 p_vout->p_sys->b_glx13 = false;
309 msg_Dbg( p_this, "using GLX 1.2 API" );
313 p_vout->p_sys->b_glx13 = true;
314 msg_Dbg( p_this, "using GLX 1.3 API" );
319 /* Create blank cursor (for mouse cursor autohiding) */
320 p_vout->p_sys->i_time_mouse_last_moved = mdate();
321 p_vout->p_sys->i_mouse_hide_timeout =
322 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
323 p_vout->p_sys->b_mouse_pointer_visible = 1;
324 CreateCursor( p_vout );
326 /* Set main window's size */
327 p_vout->p_sys->window.i_x = 0;
328 p_vout->p_sys->window.i_y = 0;
329 p_vout->p_sys->window.i_width = p_vout->i_window_width;
330 p_vout->p_sys->window.i_height = p_vout->i_window_height;
331 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
332 /* Spawn base window - this window will include the video output window,
333 * but also command buttons, subtitles and other indicators */
334 if( CreateWindow( p_vout, &p_vout->p_sys->window ) )
336 msg_Err( p_vout, "cannot create X11 window" );
337 DestroyCursor( p_vout );
338 XCloseDisplay( p_vout->p_sys->p_display );
339 free( p_vout->p_sys );
343 /* Open and initialize device. */
344 if( InitDisplay( p_vout ) )
346 msg_Err( p_vout, "cannot initialize X11 display" );
347 DestroyCursor( p_vout );
348 DestroyWindow( p_vout, &p_vout->p_sys->window );
349 XCloseDisplay( p_vout->p_sys->p_display );
350 free( p_vout->p_sys );
354 /* Disable screen saver */
355 DisableXScreenSaver( p_vout );
358 p_vout->p_sys->i_time_button_last_pressed = 0;
360 #ifdef MODULE_NAME_IS_xvmc
361 p_vout->p_sys->p_last_subtitle_save = NULL;
362 psz_value = config_GetPsz( p_vout, "xvmc-deinterlace-mode" );
364 /* Look what method was requested */
365 //var_Create( p_vout, "xvmc-deinterlace-mode", VLC_VAR_STRING );
366 //var_Change( p_vout, "xvmc-deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
369 if( (strcmp(psz_value, "bob") == 0) ||
370 (strcmp(psz_value, "blend") == 0) )
371 p_vout->p_sys->xvmc_deinterlace_method = 2;
372 else if (strcmp(psz_value, "discard") == 0)
373 p_vout->p_sys->xvmc_deinterlace_method = 1;
375 p_vout->p_sys->xvmc_deinterlace_method = 0;
379 p_vout->p_sys->xvmc_deinterlace_method = 0;
381 /* Look what method was requested */
382 //var_Create( p_vout, "xvmc-crop-style", VLC_VAR_STRING );
383 //var_Change( p_vout, "xvmc-crop-style", VLC_VAR_INHERITVALUE, &val, NULL );
384 psz_value = config_GetPsz( p_vout, "xvmc-crop-style" );
388 if( strncmp( psz_value, "eq", 2 ) == 0 )
389 p_vout->p_sys->xvmc_crop_style = 1;
390 else if( strncmp( psz_value, "4-16", 4 ) == 0)
391 p_vout->p_sys->xvmc_crop_style = 2;
392 else if( strncmp( psz_value, "16-4", 4 ) == 0)
393 p_vout->p_sys->xvmc_crop_style = 3;
395 p_vout->p_sys->xvmc_crop_style = 0;
399 p_vout->p_sys->xvmc_crop_style = 0;
401 msg_Dbg(p_vout, "Deinterlace = %d", p_vout->p_sys->xvmc_deinterlace_method);
402 msg_Dbg(p_vout, "Crop = %d", p_vout->p_sys->xvmc_crop_style);
404 if( checkXvMCCap( p_vout ) == VLC_EGENERIC )
406 msg_Err( p_vout, "no XVMC capability found" );
407 Deactivate( p_this );
410 subpicture_t sub_pic;
411 sub_pic.p_sys = NULL;
412 p_vout->p_sys->last_date = 0;
416 p_vout->p_sys->i_hw_scale = 1;
420 p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
421 p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
422 if ( p_vout->p_sys->p_octx == NULL ) {
423 msg_Err( p_vout, "Could not get osso context" );
425 msg_Dbg( p_vout, "Initialized osso context" );
429 /* Variable to indicate if the window should be on top of others */
430 /* Trigger a callback right now */
431 var_TriggerCallback( p_vout, "video-on-top" );
436 /*****************************************************************************
437 * Deactivate: destroy X11 video thread output method
438 *****************************************************************************
439 * Terminate an output method created by Open
440 *****************************************************************************/
441 void Deactivate ( vlc_object_t *p_this )
443 vout_thread_t *p_vout = (vout_thread_t *)p_this;
445 /* Restore cursor if it was blanked */
446 if( !p_vout->p_sys->b_mouse_pointer_visible )
448 ToggleCursor( p_vout );
451 #ifdef MODULE_NAME_IS_x11
452 /* Destroy colormap */
453 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
455 XFreeColormap( p_vout->p_sys->p_display, p_vout->p_sys->colormap );
457 #elif defined(MODULE_NAME_IS_xvideo)
458 XVideoReleasePort( p_vout, p_vout->p_sys->i_xvport );
459 #elif defined(MODULE_NAME_IS_xvmc)
460 if( p_vout->p_sys->xvmc_cap )
462 xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
463 xxmc_dispose_context( p_vout );
464 if( p_vout->p_sys->old_subpic )
466 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->old_subpic );
467 p_vout->p_sys->old_subpic = NULL;
469 if( p_vout->p_sys->new_subpic )
471 xxmc_xvmc_free_subpicture( p_vout, p_vout->p_sys->new_subpic );
472 p_vout->p_sys->new_subpic = NULL;
474 free( p_vout->p_sys->xvmc_cap );
475 xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
480 DisablePixelDoubling(p_vout);
483 DestroyCursor( p_vout );
484 EnableXScreenSaver( p_vout );
485 DestroyWindow( p_vout, &p_vout->p_sys->window );
486 XCloseDisplay( p_vout->p_sys->p_display );
488 /* Destroy structure */
489 #ifdef MODULE_NAME_IS_xvmc
490 free_context_lock( &p_vout->p_sys->xvmc_lock );
494 if ( p_vout->p_sys->p_octx != NULL ) {
495 msg_Dbg( p_vout, "Deinitializing osso context" );
496 osso_deinitialize( p_vout->p_sys->p_octx );
500 free( p_vout->p_sys );
503 #ifdef MODULE_NAME_IS_xvmc
505 #define XINE_IMGFMT_YV12 (('2'<<24)|('1'<<16)|('V'<<8)|'Y')
508 static int xvmc_check_yv12( Display *display, XvPortID port )
510 XvImageFormatValues *formatValues;
514 formatValues = XvListImageFormats( display, port, &formats );
516 for( i = 0; i < formats; i++ )
518 if( ( formatValues[i].id == XINE_IMGFMT_YV12 ) &&
519 ( !( strncmp( formatValues[i].guid, "YV12", 4 ) ) ) )
521 XFree (formatValues);
526 XFree (formatValues);
530 static void xvmc_sync_surface( vout_thread_t *p_vout, XvMCSurface * srf )
532 XvMCSyncSurface( p_vout->p_sys->p_display, srf );
535 static void xvmc_update_XV_DOUBLE_BUFFER( vout_thread_t *p_vout )
538 int xv_double_buffer;
540 xv_double_buffer = 1;
542 XLockDisplay( p_vout->p_sys->p_display );
543 atom = XInternAtom( p_vout->p_sys->p_display, "XV_DOUBLE_BUFFER", False );
545 XvSetPortAttribute (p_vout->p_sys->p_display, p_vout->p_sys->i_xvport, atom, xv_double_buffer);
547 XvMCSetAttribute( p_vout->p_sys->p_display, &p_vout->p_sys->context, atom, xv_double_buffer );
548 XUnlockDisplay( p_vout->p_sys->p_display );
550 //xprintf(this->xine, XINE_VERBOSITY_DEBUG,
551 // "video_out_xxmc: double buffering mode = %d\n", xv_double_buffer);
554 static void RenderVideo( vout_thread_t *p_vout, picture_t *p_pic )
556 vlc_xxmc_t *xxmc = NULL;
558 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
560 xxmc = &p_pic->p_sys->xxmc_data;
561 if( (!xxmc->decoded ||
562 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf )) )
564 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
569 vlc_mutex_lock( &p_vout->lastsubtitle_lock );
570 if (p_vout->p_sys->p_last_subtitle != NULL)
572 if( p_vout->p_sys->p_last_subtitle_save != p_vout->p_sys->p_last_subtitle )
574 p_vout->p_sys->new_subpic =
575 xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
576 p_vout->p_sys->xvmc_width,
577 p_vout->p_sys->xvmc_height,
578 p_vout->p_sys->xvmc_cap[p_vout->p_sys->xvmc_cur_cap].subPicType.id );
580 if (p_vout->p_sys->new_subpic)
582 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
583 XvMCClearSubpicture( p_vout->p_sys->p_display,
584 p_vout->p_sys->new_subpic,
587 p_vout->p_sys->xvmc_width,
588 p_vout->p_sys->xvmc_height,
590 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
591 clear_xx44_palette( &p_vout->p_sys->palette );
593 if( sub_pic.p_sys == NULL )
595 sub_pic.p_sys = malloc( sizeof( picture_sys_t ) );
596 if( sub_pic.p_sys != NULL )
598 sub_pic.p_sys->p_vout = p_vout;
599 sub_pic.p_sys->xvmc_surf = NULL;
600 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
603 sub_pic.p_sys->p_image = p_vout->p_sys->subImage;
604 sub_pic.p->p_pixels = sub_pic.p_sys->p_image->data;
605 sub_pic.p->i_pitch = p_vout->output.i_width;
607 memset( p_vout->p_sys->subImage->data, 0,
608 (p_vout->p_sys->subImage->width * p_vout->p_sys->subImage->height) );
610 if (p_vout->p_last_subtitle != NULL)
612 blend_xx44( p_vout->p_sys->subImage->data,
613 p_vout->p_last_subtitle,
614 p_vout->p_sys->subImage->width,
615 p_vout->p_sys->subImage->height,
616 p_vout->p_sys->subImage->width,
617 &p_vout->p_sys->palette,
618 (p_vout->p_sys->subImage->id == FOURCC_IA44) );
621 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
622 XvMCCompositeSubpicture( p_vout->p_sys->p_display,
623 p_vout->p_sys->new_subpic,
624 p_vout->p_sys->subImage,
627 p_vout->output.i_width, /* overlay->width, */
628 p_vout->output.i_height, /* overlay->height */
631 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
632 if (p_vout->p_sys->old_subpic)
634 xxmc_xvmc_free_subpicture( p_vout,
635 p_vout->p_sys->old_subpic);
636 p_vout->p_sys->old_subpic = NULL;
638 if (p_vout->p_sys->new_subpic)
640 p_vout->p_sys->old_subpic = p_vout->p_sys->new_subpic;
641 p_vout->p_sys->new_subpic = NULL;
642 xx44_to_xvmc_palette( &p_vout->p_sys->palette,
643 p_vout->p_sys->xvmc_palette,
645 p_vout->p_sys->old_subpic->num_palette_entries,
646 p_vout->p_sys->old_subpic->entry_bytes,
647 p_vout->p_sys->old_subpic->component_order );
648 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
649 XvMCSetSubpicturePalette( p_vout->p_sys->p_display,
650 p_vout->p_sys->old_subpic,
651 p_vout->p_sys->xvmc_palette );
652 XvMCFlushSubpicture( p_vout->p_sys->p_display,
653 p_vout->p_sys->old_subpic);
654 XvMCSyncSubpicture( p_vout->p_sys->p_display,
655 p_vout->p_sys->old_subpic );
656 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
659 XVMCLOCKDISPLAY( p_vout->p_sys->p_display);
660 if (p_vout->p_sys->xvmc_backend_subpic )
662 XvMCBlendSubpicture( p_vout->p_sys->p_display,
663 p_pic->p_sys->xvmc_surf,
664 p_vout->p_sys->old_subpic,
667 p_vout->p_sys->xvmc_width,
668 p_vout->p_sys->xvmc_height,
671 p_vout->p_sys->xvmc_width,
672 p_vout->p_sys->xvmc_height );
676 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
677 p_pic->p_sys->xvmc_surf,
678 p_pic->p_sys->xvmc_surf,
679 p_vout->p_sys->old_subpic,
682 p_vout->p_sys->xvmc_width,
683 p_vout->p_sys->xvmc_height,
686 p_vout->p_sys->xvmc_width,
687 p_vout->p_sys->xvmc_height );
689 XVMCUNLOCKDISPLAY(p_vout->p_sys->p_display);
694 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
695 if( p_vout->p_sys->xvmc_backend_subpic )
697 XvMCBlendSubpicture( p_vout->p_sys->p_display,
698 p_pic->p_sys->xvmc_surf,
699 p_vout->p_sys->old_subpic,
701 p_vout->p_sys->xvmc_width,
702 p_vout->p_sys->xvmc_height,
704 p_vout->p_sys->xvmc_width,
705 p_vout->p_sys->xvmc_height );
709 XvMCBlendSubpicture2( p_vout->p_sys->p_display,
710 p_pic->p_sys->xvmc_surf,
711 p_pic->p_sys->xvmc_surf,
712 p_vout->p_sys->old_subpic,
714 p_vout->p_sys->xvmc_width,
715 p_vout->p_sys->xvmc_height,
717 p_vout->p_sys->xvmc_width,
718 p_vout->p_sys->xvmc_height );
720 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
723 p_vout->p_sys->p_last_subtitle_save = p_vout->p_last_subtitle;
725 vlc_mutex_unlock( &p_vout->lastsubtitle_lock );
727 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
732 /*****************************************************************************
733 * EnablePixelDoubling: Enables pixel doubling
734 *****************************************************************************
735 * Checks if the double size image fits in current window, and enables pixel
736 * doubling accordingly. The i_hw_scale is the integer scaling factor.
737 *****************************************************************************/
738 static void EnablePixelDoubling( vout_thread_t *p_vout )
740 int i_hor_scale = ( p_vout->p_sys->window.i_width ) / p_vout->render.i_width;
741 int i_vert_scale = ( p_vout->p_sys->window.i_height ) / p_vout->render.i_height;
742 if ( ( i_hor_scale > 1 ) && ( i_vert_scale > 1 ) ) {
743 p_vout->p_sys->i_hw_scale = 2;
744 msg_Dbg( p_vout, "Enabling pixel doubling, scaling factor %d", p_vout->p_sys->i_hw_scale );
745 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 1 );
749 /*****************************************************************************
750 * DisablePixelDoubling: Disables pixel doubling
751 *****************************************************************************
752 * The scaling factor i_hw_scale is reset to the no-scaling value 1.
753 *****************************************************************************/
754 static void DisablePixelDoubling( vout_thread_t *p_vout )
756 if ( p_vout->p_sys->i_hw_scale > 1 ) {
757 msg_Dbg( p_vout, "Disabling pixel doubling" );
758 XSPSetPixelDoubling( p_vout->p_sys->p_display, 0, 0 );
759 p_vout->p_sys->i_hw_scale = 1;
765 #if !defined(MODULE_NAME_IS_glx)
766 /*****************************************************************************
767 * InitVideo: initialize X11 video thread output method
768 *****************************************************************************
769 * This function create the XImages needed by the output thread. It is called
770 * at the beginning of the thread, but also each time the window is resized.
771 *****************************************************************************/
772 static int InitVideo( vout_thread_t *p_vout )
774 unsigned int i_index = 0;
777 I_OUTPUTPICTURES = 0;
779 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
780 /* Initialize the output structure; we already found an XVideo port,
781 * and the corresponding chroma we will be using. Since we can
782 * arbitrary scale, stick to the coordinates and aspect. */
783 p_vout->output.i_width = p_vout->render.i_width;
784 p_vout->output.i_height = p_vout->render.i_height;
785 p_vout->output.i_aspect = p_vout->render.i_aspect;
787 p_vout->fmt_out = p_vout->fmt_in;
788 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
790 #if XvVersion < 2 || ( XvVersion == 2 && XvRevision < 2 )
791 switch( p_vout->output.i_chroma )
793 case VLC_CODEC_RGB16:
794 #if defined( WORDS_BIGENDIAN )
795 p_vout->output.i_rmask = 0xf800;
796 p_vout->output.i_gmask = 0x07e0;
797 p_vout->output.i_bmask = 0x001f;
799 p_vout->output.i_rmask = 0x001f;
800 p_vout->output.i_gmask = 0x07e0;
801 p_vout->output.i_bmask = 0xf800;
804 case VLC_CODEC_RGB15:
805 #if defined( WORDS_BIGENDIAN )
806 p_vout->output.i_rmask = 0x7c00;
807 p_vout->output.i_gmask = 0x03e0;
808 p_vout->output.i_bmask = 0x001f;
810 p_vout->output.i_rmask = 0x001f;
811 p_vout->output.i_gmask = 0x03e0;
812 p_vout->output.i_bmask = 0x7c00;
818 #elif defined(MODULE_NAME_IS_x11)
819 /* Initialize the output structure: RGB with square pixels, whatever
820 * the input format is, since it's the only format we know */
821 switch( p_vout->p_sys->i_screen_depth )
823 case 8: /* FIXME: set the palette */
824 p_vout->output.i_chroma = VLC_CODEC_RGB8; break;
826 p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
828 p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
831 p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
833 msg_Err( p_vout, "unknown screen depth %i",
834 p_vout->p_sys->i_screen_depth );
839 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width / p_vout->p_sys->i_hw_scale,
840 p_vout->p_sys->window.i_height / p_vout->p_sys->i_hw_scale,
842 &p_vout->fmt_out.i_visible_width,
843 &p_vout->fmt_out.i_visible_height );
845 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
846 p_vout->p_sys->window.i_height,
848 &p_vout->fmt_out.i_visible_width,
849 &p_vout->fmt_out.i_visible_height );
852 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
854 p_vout->output.i_width = p_vout->fmt_out.i_width =
855 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_width /
856 p_vout->fmt_in.i_visible_width;
857 p_vout->output.i_height = p_vout->fmt_out.i_height =
858 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_height /
859 p_vout->fmt_in.i_visible_height;
860 p_vout->fmt_out.i_x_offset =
861 p_vout->fmt_out.i_visible_width * p_vout->fmt_in.i_x_offset /
862 p_vout->fmt_in.i_visible_width;
863 p_vout->fmt_out.i_y_offset =
864 p_vout->fmt_out.i_visible_height * p_vout->fmt_in.i_y_offset /
865 p_vout->fmt_in.i_visible_height;
867 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
868 p_vout->output.i_aspect = p_vout->fmt_out.i_aspect =
869 p_vout->fmt_out.i_width * VOUT_ASPECT_FACTOR /p_vout->fmt_out.i_height;
871 msg_Dbg( p_vout, "x11 image size %ix%i (%i,%i,%ix%i)",
872 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
873 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
874 p_vout->fmt_out.i_visible_width,
875 p_vout->fmt_out.i_visible_height );
878 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
879 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
883 /* Find an empty picture slot */
884 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
886 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
888 p_pic = p_vout->p_picture + i_index;
893 /* Allocate the picture */
894 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
899 p_pic->i_status = DESTROYED_PICTURE;
900 p_pic->i_type = DIRECT_PICTURE;
902 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
907 if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
909 /* U and V inverted compared to I420
910 * Fixme: this should be handled by the vout core */
911 p_vout->output.i_chroma = VLC_CODEC_I420;
912 p_vout->fmt_out.i_chroma = VLC_CODEC_I420;
918 /*****************************************************************************
919 * DisplayVideo: displays previously rendered output
920 *****************************************************************************
921 * This function sends the currently rendered image to X11 server.
922 * (The Xv extension takes care of "double-buffering".)
923 *****************************************************************************/
924 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
926 unsigned int i_width, i_height, i_x, i_y;
928 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
929 p_vout->p_sys->window.i_height,
930 &i_x, &i_y, &i_width, &i_height );
932 #ifdef MODULE_NAME_IS_xvmc
933 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
935 vlc_xxmc_t *xxmc = &p_pic->p_sys->xxmc_data;
936 if( !xxmc->decoded ||
937 !xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) )
939 msg_Dbg( p_vout, "DisplayVideo decoded=%d\tsurfacevalid=%d",
941 xxmc_xvmc_surface_valid( p_vout, p_pic->p_sys->xvmc_surf ) );
942 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
946 int src_width = p_vout->output.i_width;
947 int src_height = p_vout->output.i_height;
950 if( p_vout->p_sys->xvmc_crop_style == 1 )
957 else if( p_vout->p_sys->xvmc_crop_style == 2 )
964 else if( p_vout->p_sys->xvmc_crop_style == 3 )
978 if( p_vout->p_sys->xvmc_deinterlace_method > 0 )
979 { /* BOB DEINTERLACE */
980 if( (p_pic->p_sys->nb_display == 0) ||
981 (p_vout->p_sys->xvmc_deinterlace_method == 1) )
983 first_field = (p_pic->b_top_field_first) ?
984 XVMC_BOTTOM_FIELD : XVMC_TOP_FIELD;
988 first_field = (p_pic->b_top_field_first) ?
989 XVMC_TOP_FIELD : XVMC_BOTTOM_FIELD;
994 first_field = XVMC_FRAME_PICTURE;
997 XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
998 XvMCFlushSurface( p_vout->p_sys->p_display, p_pic->p_sys->xvmc_surf );
999 /* XvMCSyncSurface(p_vout->p_sys->p_display, p_picture->p_sys->xvmc_surf); */
1000 XvMCPutSurface( p_vout->p_sys->p_display,
1001 p_pic->p_sys->xvmc_surf,
1002 p_vout->p_sys->window.video_window,
1013 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1014 if( p_vout->p_sys->xvmc_deinterlace_method == 2 )
1015 { /* BOB DEINTERLACE */
1016 if( p_pic->p_sys->nb_display == 0 )/* && ((t2-t1) < 15000)) */
1018 mtime_t last_date = p_pic->date;
1020 vlc_mutex_lock( &p_vout->picture_lock );
1021 if( !p_vout->p_sys->last_date )
1023 p_pic->date += 20000;
1027 p_pic->date = ((3 * p_pic->date -
1028 p_vout->p_sys->last_date) / 2 );
1030 p_vout->p_sys->last_date = last_date;
1032 p_pic->p_sys->nb_display = 1;
1033 vlc_mutex_unlock( &p_vout->picture_lock );
1037 p_pic->p_sys->nb_display = 0;
1041 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1044 #ifdef HAVE_SYS_SHM_H
1045 if( p_vout->p_sys->i_shm_opcode )
1047 /* Display rendered image using shared memory extension */
1048 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1049 XvShmPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1050 p_vout->p_sys->window.video_window,
1051 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1052 p_vout->fmt_out.i_x_offset,
1053 p_vout->fmt_out.i_y_offset,
1054 p_vout->fmt_out.i_visible_width,
1055 p_vout->fmt_out.i_visible_height,
1056 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height,
1057 False /* Don't put True here or you'll waste your CPU */ );
1059 XShmPutImage( p_vout->p_sys->p_display,
1060 p_vout->p_sys->window.video_window,
1061 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1062 p_vout->fmt_out.i_x_offset,
1063 p_vout->fmt_out.i_y_offset,
1064 0 /*dest_x*/, 0 /*dest_y*/,
1065 p_vout->fmt_out.i_visible_width,
1066 p_vout->fmt_out.i_visible_height,
1067 False /* Don't put True here ! */ );
1071 #endif /* HAVE_SYS_SHM_H */
1073 /* Use standard XPutImage -- this is gonna be slow ! */
1074 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1075 XvPutImage( p_vout->p_sys->p_display, p_vout->p_sys->i_xvport,
1076 p_vout->p_sys->window.video_window,
1077 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1078 p_vout->fmt_out.i_x_offset,
1079 p_vout->fmt_out.i_y_offset,
1080 p_vout->fmt_out.i_visible_width,
1081 p_vout->fmt_out.i_visible_height,
1082 0 /*dest_x*/, 0 /*dest_y*/, i_width, i_height );
1084 XPutImage( p_vout->p_sys->p_display,
1085 p_vout->p_sys->window.video_window,
1086 p_vout->p_sys->window.gc, p_pic->p_sys->p_image,
1087 p_vout->fmt_out.i_x_offset,
1088 p_vout->fmt_out.i_y_offset,
1089 0 /*dest_x*/, 0 /*dest_y*/,
1090 p_vout->fmt_out.i_visible_width,
1091 p_vout->fmt_out.i_visible_height );
1095 /* Make sure the command is sent now - do NOT use XFlush !*/
1096 XSync( p_vout->p_sys->p_display, False );
1100 /*****************************************************************************
1101 * ManageVideo: handle X11 events
1102 *****************************************************************************
1103 * This function should be called regularly by video output thread. It manages
1104 * X11 events and allows window resizing. It returns a non null value on
1106 *****************************************************************************/
1107 static int ManageVideo( vout_thread_t *p_vout )
1109 XEvent xevent; /* X11 event */
1112 #ifdef MODULE_NAME_IS_xvmc
1113 xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1116 /* Handle events from the owner window */
1117 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1118 p_vout->p_sys->window.owner_window->handle.xid,
1119 StructureNotifyMask, &xevent ) == True )
1121 /* ConfigureNotify event: prepare */
1122 if( xevent.type == ConfigureNotify )
1123 /* Update dimensions */
1124 XResizeWindow( p_vout->p_sys->p_display,
1125 p_vout->p_sys->window.base_window,
1126 xevent.xconfigure.width,
1127 xevent.xconfigure.height );
1130 /* Handle X11 events: ConfigureNotify events are parsed to know if the
1131 * output window's size changed, MapNotify and UnmapNotify to know if the
1132 * window is mapped (and if the display is useful), and ClientMessages
1133 * to intercept window destruction requests */
1135 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1136 p_vout->p_sys->window.base_window,
1137 StructureNotifyMask |
1138 ButtonPressMask | ButtonReleaseMask |
1139 PointerMotionMask | Button1MotionMask , &xevent )
1142 /* ConfigureNotify event: prepare */
1143 if( xevent.type == ConfigureNotify )
1145 if( (unsigned int)xevent.xconfigure.width
1146 != p_vout->p_sys->window.i_width
1147 || (unsigned int)xevent.xconfigure.height
1148 != p_vout->p_sys->window.i_height )
1150 /* Update dimensions */
1151 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1152 p_vout->p_sys->window.i_width = xevent.xconfigure.width;
1153 p_vout->p_sys->window.i_height = xevent.xconfigure.height;
1157 else if( xevent.type == ButtonPress )
1159 switch( ((XButtonEvent *)&xevent)->button )
1162 var_Get( p_vout, "mouse-button-down", &val );
1164 var_Set( p_vout, "mouse-button-down", val );
1166 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
1168 /* detect double-clicks */
1169 if( ( ((XButtonEvent *)&xevent)->time -
1170 p_vout->p_sys->i_time_button_last_pressed ) < 300 )
1172 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1175 p_vout->p_sys->i_time_button_last_pressed =
1176 ((XButtonEvent *)&xevent)->time;
1179 var_Get( p_vout, "mouse-button-down", &val );
1181 var_Set( p_vout, "mouse-button-down", val );
1185 var_Get( p_vout, "mouse-button-down", &val );
1187 var_Set( p_vout, "mouse-button-down", val );
1188 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
1192 var_Get( p_vout, "mouse-button-down", &val );
1194 var_Set( p_vout, "mouse-button-down", val );
1198 var_Get( p_vout, "mouse-button-down", &val );
1200 var_Set( p_vout, "mouse-button-down", val );
1205 else if( xevent.type == ButtonRelease )
1207 switch( ((XButtonEvent *)&xevent)->button )
1211 var_Get( p_vout, "mouse-button-down", &val );
1213 var_Set( p_vout, "mouse-button-down", val );
1215 var_SetBool( p_vout, "mouse-clicked", true );
1221 var_Get( p_vout, "mouse-button-down", &val );
1223 var_Set( p_vout, "mouse-button-down", val );
1225 var_Get( p_vout->p_libvlc, "intf-show", &val );
1226 val.b_bool = !val.b_bool;
1227 var_Set( p_vout->p_libvlc, "intf-show", val );
1233 var_Get( p_vout, "mouse-button-down", &val );
1235 var_Set( p_vout, "mouse-button-down", val );
1241 var_Get( p_vout, "mouse-button-down", &val );
1243 var_Set( p_vout, "mouse-button-down", val );
1247 var_Get( p_vout, "mouse-button-down", &val );
1249 var_Set( p_vout, "mouse-button-down", val );
1255 else if( xevent.type == MotionNotify )
1257 unsigned int i_width, i_height, i_x, i_y;
1259 /* somewhat different use for vout_PlacePicture:
1260 * here the values are needed to give to mouse coordinates
1261 * in the original picture space */
1262 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
1263 p_vout->p_sys->window.i_height,
1264 &i_x, &i_y, &i_width, &i_height );
1266 /* Compute the x coordinate and check if the value is
1267 in [0,p_vout->fmt_in.i_visible_width] */
1268 val.i_int = ( xevent.xmotion.x - i_x ) *
1269 p_vout->fmt_in.i_visible_width / i_width +
1270 p_vout->fmt_in.i_x_offset;
1272 if( (int)(xevent.xmotion.x - i_x) < 0 )
1274 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
1275 val.i_int = p_vout->fmt_in.i_visible_width;
1277 var_Set( p_vout, "mouse-x", val );
1279 /* compute the y coordinate and check if the value is
1280 in [0,p_vout->fmt_in.i_visible_height] */
1281 val.i_int = ( xevent.xmotion.y - i_y ) *
1282 p_vout->fmt_in.i_visible_height / i_height +
1283 p_vout->fmt_in.i_y_offset;
1285 if( (int)(xevent.xmotion.y - i_y) < 0 )
1287 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
1288 val.i_int = p_vout->fmt_in.i_visible_height;
1290 var_Set( p_vout, "mouse-y", val );
1292 var_SetBool( p_vout, "mouse-moved", true );
1294 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1295 if( ! p_vout->p_sys->b_mouse_pointer_visible )
1297 ToggleCursor( p_vout );
1300 else if( xevent.type == ReparentNotify /* XXX: why do we get this? */
1301 || xevent.type == MapNotify
1302 || xevent.type == UnmapNotify )
1304 /* Ignore these events */
1306 else /* Other events */
1308 msg_Warn( p_vout, "unhandled event %d received", xevent.type );
1312 /* Handle events for video output sub-window */
1313 while( XCheckWindowEvent( p_vout->p_sys->p_display,
1314 p_vout->p_sys->window.video_window,
1315 ExposureMask, &xevent ) == True )
1317 /* Window exposed (only handled if stream playback is paused) */
1318 if( xevent.type == Expose )
1320 if( ((XExposeEvent *)&xevent)->count == 0 )
1322 /* (if this is the last a collection of expose events...) */
1324 #if defined(MODULE_NAME_IS_xvideo)
1325 x11_window_t *p_win = &p_vout->p_sys->window;
1327 /* Paint the colour key if needed */
1328 if( p_vout->p_sys->b_paint_colourkey &&
1329 xevent.xexpose.window == p_win->video_window )
1331 XSetForeground( p_vout->p_sys->p_display,
1332 p_win->gc, p_vout->p_sys->i_colourkey );
1333 XFillRectangle( p_vout->p_sys->p_display,
1334 p_win->video_window, p_win->gc, 0, 0,
1335 p_win->i_width, p_win->i_height );
1340 if( p_vout->p_libvlc->p_input_bank->pp_input[0] != NULL )
1342 if( PAUSE_S == p_vout->p_libvlc->p_input_bank->pp_input[0]
1343 ->stream.control.i_status )
1345 /* XVideoDisplay( p_vout )*/;
1353 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
1354 * are handled - according to the man pages, the format is always 32
1356 while( XCheckTypedEvent( p_vout->p_sys->p_display,
1357 ClientMessage, &xevent ) )
1359 if( (xevent.xclient.message_type == p_vout->p_sys->window.wm_protocols)
1360 && ((Atom)xevent.xclient.data.l[0]
1361 == p_vout->p_sys->window.wm_delete_window ) )
1363 /* the user wants to close the window */
1364 playlist_t * p_playlist = pl_Hold( p_vout );
1365 if( p_playlist != NULL )
1367 playlist_Stop( p_playlist );
1368 pl_Release( p_vout );
1376 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
1378 /* Update the object variable and trigger callback */
1379 var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
1381 ToggleFullScreen( p_vout );
1382 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
1385 /* autoscale toggle */
1386 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
1388 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
1390 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
1391 p_vout->i_zoom = ZOOM_FP_FACTOR;
1393 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1396 /* scaling factor */
1397 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
1399 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
1401 p_vout->b_autoscale = false;
1403 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
1405 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1408 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
1409 p_vout->i_changes & VOUT_ASPECT_CHANGE )
1411 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
1412 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
1414 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
1415 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
1416 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
1417 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
1418 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
1419 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
1420 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
1421 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
1423 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1429 * (Needs to be placed after VOUT_FULLSREEN_CHANGE because we can activate
1430 * the size flag inside the fullscreen routine)
1432 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1434 unsigned int i_width, i_height, i_x, i_y;
1436 #ifdef MODULE_NAME_IS_x11
1437 /* We need to signal the vout thread about the size change because it
1438 * is doing the rescaling */
1440 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1443 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
1444 p_vout->p_sys->window.i_height,
1445 &i_x, &i_y, &i_width, &i_height );
1447 XMoveResizeWindow( p_vout->p_sys->p_display,
1448 p_vout->p_sys->window.video_window,
1449 i_x, i_y, i_width, i_height );
1452 /* cursor hiding depending on --vout-event option
1454 * value = 1 (Fullsupport) (default value)
1455 * or value = 2 (Fullscreen-Only) and condition met
1457 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1458 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1461 /* Autohide Cursour */
1462 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved >
1463 p_vout->p_sys->i_mouse_hide_timeout )
1465 /* Hide the mouse automatically */
1466 if( b_vout_event && p_vout->p_sys->b_mouse_pointer_visible )
1468 ToggleCursor( p_vout );
1472 #ifdef MODULE_NAME_IS_xvmc
1473 xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1477 if ( p_vout->p_sys->p_octx != NULL ) {
1478 if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
1479 if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
1480 msg_Err( p_vout, "Could not disable backlight blanking" );
1482 msg_Dbg( p_vout, "Backlight blanking disabled" );
1484 p_vout->p_sys->i_backlight_on_counter = 0;
1486 p_vout->p_sys->i_backlight_on_counter ++;
1493 #if !defined( MODULE_NAME_IS_glx )
1494 /*****************************************************************************
1495 * EndVideo: terminate X11 video thread output method
1496 *****************************************************************************
1497 * Destroy the X11 XImages created by Init. It is called at the end of
1498 * the thread, but also each time the window is resized.
1499 *****************************************************************************/
1500 static void EndVideo( vout_thread_t *p_vout )
1504 /* Free the direct buffers we allocated */
1505 for( i_index = I_OUTPUTPICTURES ; i_index ; )
1508 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
1513 /* following functions are local */
1515 /*****************************************************************************
1516 * CreateWindow: open and set-up X11 main window
1517 *****************************************************************************/
1518 static int CreateWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1520 XSizeHints xsize_hints;
1521 XSetWindowAttributes xwindow_attributes;
1522 XGCValues xgcvalues;
1525 bool b_map_notify = false;
1527 /* Prepare window manager hints and properties */
1528 p_win->wm_protocols =
1529 XInternAtom( p_vout->p_sys->p_display, "WM_PROTOCOLS", True );
1530 p_win->wm_delete_window =
1531 XInternAtom( p_vout->p_sys->p_display, "WM_DELETE_WINDOW", True );
1533 /* Never have a 0-pixel-wide window */
1534 xsize_hints.min_width = 2;
1535 xsize_hints.min_height = 1;
1537 /* Prepare window attributes */
1538 xwindow_attributes.backing_store = Always; /* save the hidden part */
1539 xwindow_attributes.background_pixel = BlackPixel(p_vout->p_sys->p_display,
1540 p_vout->p_sys->i_screen);
1541 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
1544 vout_window_cfg_t wnd_cfg;
1545 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
1546 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
1547 wnd_cfg.x = p_win->i_x;
1548 wnd_cfg.y = p_win->i_y;
1549 wnd_cfg.width = p_win->i_width;
1550 wnd_cfg.height = p_win->i_height;
1552 p_win->owner_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
1553 if( !p_win->owner_window )
1554 return VLC_EGENERIC;
1555 xsize_hints.base_width = xsize_hints.width = p_win->i_width;
1556 xsize_hints.base_height = xsize_hints.height = p_win->i_height;
1557 xsize_hints.flags = PSize | PMinSize;
1559 if( p_win->i_x >=0 || p_win->i_y >= 0 )
1561 xsize_hints.x = p_win->i_x;
1562 xsize_hints.y = p_win->i_y;
1563 xsize_hints.flags |= PPosition;
1566 /* Select events we are interested in. */
1567 XSelectInput( p_vout->p_sys->p_display,
1568 p_win->owner_window->handle.xid, StructureNotifyMask );
1570 /* Get the parent window's geometry information */
1571 XGetGeometry( p_vout->p_sys->p_display,
1572 p_win->owner_window->handle.xid,
1573 &(Window){ 0 }, &(int){ 0 }, &(int){ 0 },
1576 &(unsigned){ 0 }, &(unsigned){ 0 } );
1578 /* From man XSelectInput: only one client at a time can select a
1579 * ButtonPress event, so we need to open a new window anyway. */
1580 p_win->base_window =
1581 XCreateWindow( p_vout->p_sys->p_display,
1582 p_win->owner_window->handle.xid,
1584 p_win->i_width, p_win->i_height,
1586 0, CopyFromParent, 0,
1587 CWBackingStore | CWBackPixel | CWEventMask,
1588 &xwindow_attributes );
1591 if( (p_win->wm_protocols == None) /* use WM_DELETE_WINDOW */
1592 || (p_win->wm_delete_window == None)
1593 || !XSetWMProtocols( p_vout->p_sys->p_display, p_win->base_window,
1594 &p_win->wm_delete_window, 1 ) )
1596 /* WM_DELETE_WINDOW is not supported by window manager */
1597 msg_Warn( p_vout, "missing or bad window manager" );
1600 /* Creation of a graphic context that doesn't generate a GraphicsExpose
1601 * event when using functions like XCopyArea */
1602 xgcvalues.graphics_exposures = False;
1603 p_win->gc = XCreateGC( p_vout->p_sys->p_display,
1605 GCGraphicsExposures, &xgcvalues );
1607 /* Wait till the window is mapped */
1608 XMapWindow( p_vout->p_sys->p_display, p_win->base_window );
1611 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1612 SubstructureNotifyMask | StructureNotifyMask, &xevent);
1613 if( (xevent.type == MapNotify)
1614 && (xevent.xmap.window == p_win->base_window) )
1616 b_map_notify = true;
1618 else if( (xevent.type == ConfigureNotify)
1619 && (xevent.xconfigure.window == p_win->base_window) )
1621 p_win->i_width = xevent.xconfigure.width;
1622 p_win->i_height = xevent.xconfigure.height;
1624 } while( !b_map_notify );
1626 /* key and mouse events handling depending on --vout-event option
1628 * value = 1 (Fullsupport) (default value)
1629 * or value = 2 (Fullscreen-Only) and condition met
1631 bool b_vout_event = ( ( p_vout->p_sys->i_vout_event == 1 )
1632 || ( p_vout->p_sys->i_vout_event == 2 && p_vout->b_fullscreen )
1635 XSelectInput( p_vout->p_sys->p_display, p_win->base_window,
1636 StructureNotifyMask |
1637 ButtonPressMask | ButtonReleaseMask |
1638 PointerMotionMask );
1640 #ifdef MODULE_NAME_IS_x11
1641 if( XDefaultDepth(p_vout->p_sys->p_display, p_vout->p_sys->i_screen) == 8 )
1643 /* Allocate a new palette */
1644 p_vout->p_sys->colormap =
1645 XCreateColormap( p_vout->p_sys->p_display,
1646 DefaultRootWindow( p_vout->p_sys->p_display ),
1647 DefaultVisual( p_vout->p_sys->p_display,
1648 p_vout->p_sys->i_screen ),
1651 xwindow_attributes.colormap = p_vout->p_sys->colormap;
1652 XChangeWindowAttributes( p_vout->p_sys->p_display, p_win->base_window,
1653 CWColormap, &xwindow_attributes );
1657 /* Create video output sub-window. */
1658 p_win->video_window = XCreateSimpleWindow(
1659 p_vout->p_sys->p_display,
1660 p_win->base_window, 0, 0,
1661 p_win->i_width, p_win->i_height,
1663 BlackPixel( p_vout->p_sys->p_display,
1664 p_vout->p_sys->i_screen ),
1665 WhitePixel( p_vout->p_sys->p_display,
1666 p_vout->p_sys->i_screen ) );
1668 XSetWindowBackground( p_vout->p_sys->p_display, p_win->video_window,
1669 BlackPixel( p_vout->p_sys->p_display,
1670 p_vout->p_sys->i_screen ) );
1672 XMapWindow( p_vout->p_sys->p_display, p_win->video_window );
1673 XSelectInput( p_vout->p_sys->p_display, p_win->video_window,
1676 /* make sure the video window will be centered in the next ManageVideo() */
1677 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1679 /* If the cursor was formerly blank than blank it again */
1680 if( !p_vout->p_sys->b_mouse_pointer_visible )
1682 ToggleCursor( p_vout );
1683 ToggleCursor( p_vout );
1686 /* Do NOT use XFlush here ! */
1687 XSync( p_vout->p_sys->p_display, False );
1692 /*****************************************************************************
1693 * DestroyWindow: destroy the window
1694 *****************************************************************************
1696 *****************************************************************************/
1697 static void DestroyWindow( vout_thread_t *p_vout, x11_window_t *p_win )
1699 /* Do NOT use XFlush here ! */
1700 XSync( p_vout->p_sys->p_display, False );
1702 if( p_win->video_window != None )
1703 XDestroyWindow( p_vout->p_sys->p_display, p_win->video_window );
1705 XFreeGC( p_vout->p_sys->p_display, p_win->gc );
1707 XUnmapWindow( p_vout->p_sys->p_display, p_win->base_window );
1708 XDestroyWindow( p_vout->p_sys->p_display, p_win->base_window );
1710 /* make sure base window is destroyed before proceeding further */
1711 bool b_destroy_notify = false;
1715 XWindowEvent( p_vout->p_sys->p_display, p_win->base_window,
1716 SubstructureNotifyMask | StructureNotifyMask, &xevent);
1717 if( (xevent.type == DestroyNotify)
1718 && (xevent.xmap.window == p_win->base_window) )
1720 b_destroy_notify = true;
1722 } while( !b_destroy_notify );
1724 vout_window_Delete( p_win->owner_window );
1727 /*****************************************************************************
1728 * NewPicture: allocate a picture
1729 *****************************************************************************
1730 * Returns 0 on success, -1 otherwise
1731 *****************************************************************************/
1732 #if !defined(MODULE_NAME_IS_glx)
1733 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
1735 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1739 /* We know the chroma, allocate a buffer which will be used
1740 * directly by the decoder */
1741 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1743 if( p_pic->p_sys == NULL )
1748 #ifdef MODULE_NAME_IS_xvmc
1749 p_pic->p_sys->p_vout = p_vout;
1750 p_pic->p_sys->xvmc_surf = NULL;
1751 p_pic->p_sys->xxmc_data.decoded = 0;
1752 p_pic->p_sys->xxmc_data.proc_xxmc_update_frame = xxmc_do_update_frame;
1753 // p_pic->p_accel_data = &p_pic->p_sys->xxmc_data;
1754 p_pic->p_sys->nb_display = 0;
1757 /* Fill in picture_t fields */
1758 if( picture_Setup( p_pic, p_vout->output.i_chroma,
1759 p_vout->output.i_width, p_vout->output.i_height,
1760 p_vout->output.i_aspect ) )
1763 #ifdef HAVE_SYS_SHM_H
1764 if( p_vout->p_sys->i_shm_opcode )
1766 /* Create image using XShm extension */
1767 p_pic->p_sys->p_image =
1768 CreateShmImage( p_vout, p_vout->p_sys->p_display,
1769 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1770 p_vout->p_sys->i_xvport,
1771 VLC2X11_FOURCC(p_vout->output.i_chroma),
1773 p_vout->p_sys->p_visual,
1774 p_vout->p_sys->i_screen_depth,
1776 &p_pic->p_sys->shminfo,
1777 p_vout->output.i_width, p_vout->output.i_height );
1780 if( !p_vout->p_sys->i_shm_opcode || !p_pic->p_sys->p_image )
1781 #endif /* HAVE_SYS_SHM_H */
1783 /* Create image without XShm extension */
1784 p_pic->p_sys->p_image =
1785 CreateImage( p_vout, p_vout->p_sys->p_display,
1786 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1787 p_vout->p_sys->i_xvport,
1788 VLC2X11_FOURCC(p_vout->output.i_chroma),
1789 p_pic->format.i_bits_per_pixel,
1791 p_vout->p_sys->p_visual,
1792 p_vout->p_sys->i_screen_depth,
1793 p_vout->p_sys->i_bytes_per_pixel,
1795 p_vout->output.i_width, p_vout->output.i_height );
1797 #ifdef HAVE_SYS_SHM_H
1798 if( p_pic->p_sys->p_image && p_vout->p_sys->i_shm_opcode )
1800 msg_Warn( p_vout, "couldn't create SHM image, disabling SHM" );
1801 p_vout->p_sys->i_shm_opcode = 0;
1803 #endif /* HAVE_SYS_SHM_H */
1806 if( p_pic->p_sys->p_image == NULL )
1808 free( p_pic->p_sys );
1812 switch( p_vout->output.i_chroma )
1814 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
1815 case VLC_CODEC_I420:
1816 case VLC_CODEC_YV12:
1817 case VLC_CODEC_Y211:
1818 case VLC_CODEC_YUYV:
1819 case VLC_CODEC_UYVY:
1820 case VLC_CODEC_RGB15:
1821 case VLC_CODEC_RGB16:
1822 case VLC_CODEC_RGB24: /* Fixme: pixel pitch == 4 ? */
1823 case VLC_CODEC_RGB32:
1825 for( i_plane = 0; i_plane < p_pic->p_sys->p_image->num_planes;
1828 p_pic->p[i_plane].p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1829 + p_pic->p_sys->p_image->offsets[i_plane];
1830 p_pic->p[i_plane].i_pitch =
1831 p_pic->p_sys->p_image->pitches[i_plane];
1833 if( p_vout->output.i_chroma == VLC_CODEC_YV12 )
1835 /* U and V inverted compared to I420
1836 * Fixme: this should be handled by the vout core */
1837 p_pic->U_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1838 + p_pic->p_sys->p_image->offsets[2];
1839 p_pic->V_PIXELS = (uint8_t*)p_pic->p_sys->p_image->data
1840 + p_pic->p_sys->p_image->offsets[1];
1846 case VLC_CODEC_RGB8:
1847 case VLC_CODEC_RGB16:
1848 case VLC_CODEC_RGB15:
1849 case VLC_CODEC_RGB24:
1850 case VLC_CODEC_RGB32:
1852 p_pic->p->i_lines = p_pic->p_sys->p_image->height;
1853 p_pic->p->i_visible_lines = p_pic->p_sys->p_image->height;
1854 p_pic->p->p_pixels = (uint8_t*)p_pic->p_sys->p_image->data
1855 + p_pic->p_sys->p_image->xoffset;
1856 p_pic->p->i_pitch = p_pic->p_sys->p_image->bytes_per_line;
1858 /* p_pic->p->i_pixel_pitch = 4 for RV24 but this should be set
1859 * properly by picture_Setup() */
1860 p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
1861 * p_pic->p_sys->p_image->width;
1866 /* Unknown chroma, tell the guy to get lost */
1867 IMAGE_FREE( p_pic->p_sys->p_image );
1868 free( p_pic->p_sys );
1869 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1870 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1871 p_pic->i_planes = 0;
1877 /*****************************************************************************
1878 * FreePicture: destroy a picture allocated with NewPicture
1879 *****************************************************************************
1880 * Destroy XImage AND associated data. If using Shm, detach shared memory
1881 * segment from server and process, then free it. The XDestroyImage manpage
1882 * says that both the image structure _and_ the data pointed to by the
1883 * image structure are freed, so no need to free p_image->data.
1884 *****************************************************************************/
1885 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
1887 /* The order of operations is correct */
1888 #ifdef HAVE_SYS_SHM_H
1889 if( p_vout->p_sys->i_shm_opcode )
1891 XShmDetach( p_vout->p_sys->p_display, &p_pic->p_sys->shminfo );
1892 IMAGE_FREE( p_pic->p_sys->p_image );
1894 shmctl( p_pic->p_sys->shminfo.shmid, IPC_RMID, 0 );
1895 if( shmdt( p_pic->p_sys->shminfo.shmaddr ) )
1897 msg_Err( p_vout, "cannot detach shared memory (%m)" );
1903 IMAGE_FREE( p_pic->p_sys->p_image );
1906 #ifdef MODULE_NAME_IS_xvmc
1907 if( p_pic->p_sys->xvmc_surf != NULL )
1909 xxmc_xvmc_free_surface(p_vout , p_pic->p_sys->xvmc_surf);
1910 p_pic->p_sys->xvmc_surf = NULL;
1914 /* Do NOT use XFlush here ! */
1915 XSync( p_vout->p_sys->p_display, False );
1917 free( p_pic->p_sys );
1919 #endif /* !MODULE_NAME_IS_glx */
1921 /*****************************************************************************
1922 * ToggleFullScreen: Enable or disable full screen mode
1923 *****************************************************************************
1924 * This function will switch between fullscreen and window mode.
1925 *****************************************************************************/
1926 static void ToggleFullScreen ( vout_thread_t *p_vout )
1928 p_vout->b_fullscreen = !p_vout->b_fullscreen;
1929 vout_window_SetFullScreen( p_vout->p_sys->window.owner_window,
1930 p_vout->b_fullscreen );
1933 if( p_vout->b_fullscreen )
1934 EnablePixelDoubling( p_vout );
1936 DisablePixelDoubling( p_vout );
1940 /*****************************************************************************
1941 * EnableXScreenSaver: enable screen saver
1942 *****************************************************************************
1943 * This function enables the screen saver on a display after it has been
1944 * disabled by XDisableScreenSaver.
1945 * FIXME: what happens if multiple vlc sessions are running at the same
1947 *****************************************************************************/
1948 static void EnableXScreenSaver( vout_thread_t *p_vout )
1950 #ifdef DPMSINFO_IN_DPMS_H
1954 if( p_vout->p_sys->i_ss_timeout )
1956 XSetScreenSaver( p_vout->p_sys->p_display, p_vout->p_sys->i_ss_timeout,
1957 p_vout->p_sys->i_ss_interval,
1958 p_vout->p_sys->i_ss_blanking,
1959 p_vout->p_sys->i_ss_exposure );
1962 /* Restore DPMS settings */
1963 #ifdef DPMSINFO_IN_DPMS_H
1964 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
1966 if( p_vout->p_sys->b_ss_dpms )
1968 DPMSEnable( p_vout->p_sys->p_display );
1974 /*****************************************************************************
1975 * DisableXScreenSaver: disable screen saver
1976 *****************************************************************************
1977 * See XEnableXScreenSaver
1978 *****************************************************************************/
1979 static void DisableXScreenSaver( vout_thread_t *p_vout )
1981 #ifdef DPMSINFO_IN_DPMS_H
1985 /* Save screen saver information */
1986 XGetScreenSaver( p_vout->p_sys->p_display, &p_vout->p_sys->i_ss_timeout,
1987 &p_vout->p_sys->i_ss_interval,
1988 &p_vout->p_sys->i_ss_blanking,
1989 &p_vout->p_sys->i_ss_exposure );
1991 /* Disable screen saver */
1992 if( p_vout->p_sys->i_ss_timeout )
1994 XSetScreenSaver( p_vout->p_sys->p_display, 0,
1995 p_vout->p_sys->i_ss_interval,
1996 p_vout->p_sys->i_ss_blanking,
1997 p_vout->p_sys->i_ss_exposure );
2001 #ifdef DPMSINFO_IN_DPMS_H
2002 if( DPMSQueryExtension( p_vout->p_sys->p_display, &dummy, &dummy ) )
2005 /* Save DPMS current state */
2006 DPMSInfo( p_vout->p_sys->p_display, &unused,
2007 &p_vout->p_sys->b_ss_dpms );
2008 DPMSDisable( p_vout->p_sys->p_display );
2013 /*****************************************************************************
2014 * CreateCursor: create a blank mouse pointer
2015 *****************************************************************************/
2016 static void CreateCursor( vout_thread_t *p_vout )
2018 XColor cursor_color;
2020 p_vout->p_sys->cursor_pixmap =
2021 XCreatePixmap( p_vout->p_sys->p_display,
2022 DefaultRootWindow( p_vout->p_sys->p_display ),
2025 XParseColor( p_vout->p_sys->p_display,
2026 XCreateColormap( p_vout->p_sys->p_display,
2028 p_vout->p_sys->p_display ),
2030 p_vout->p_sys->p_display,
2031 p_vout->p_sys->i_screen ),
2033 "black", &cursor_color );
2035 p_vout->p_sys->blank_cursor =
2036 XCreatePixmapCursor( p_vout->p_sys->p_display,
2037 p_vout->p_sys->cursor_pixmap,
2038 p_vout->p_sys->cursor_pixmap,
2039 &cursor_color, &cursor_color, 1, 1 );
2042 /*****************************************************************************
2043 * DestroyCursor: destroy the blank mouse pointer
2044 *****************************************************************************/
2045 static void DestroyCursor( vout_thread_t *p_vout )
2047 XFreePixmap( p_vout->p_sys->p_display, p_vout->p_sys->cursor_pixmap );
2050 /*****************************************************************************
2051 * ToggleCursor: hide or show the mouse pointer
2052 *****************************************************************************
2053 * This function hides the X pointer if it is visible by setting the pointer
2054 * sprite to a blank one. To show it again, we disable the sprite.
2055 *****************************************************************************/
2056 static void ToggleCursor( vout_thread_t *p_vout )
2058 if( p_vout->p_sys->b_mouse_pointer_visible )
2060 XDefineCursor( p_vout->p_sys->p_display,
2061 p_vout->p_sys->window.base_window,
2062 p_vout->p_sys->blank_cursor );
2063 p_vout->p_sys->b_mouse_pointer_visible = 0;
2067 XUndefineCursor( p_vout->p_sys->p_display,
2068 p_vout->p_sys->window.base_window );
2069 p_vout->p_sys->b_mouse_pointer_visible = 1;
2073 #if defined(MODULE_NAME_IS_xvideo) || defined(MODULE_NAME_IS_xvmc)
2074 /*****************************************************************************
2075 * XVideoGetPort: get YUV12 port
2076 *****************************************************************************/
2077 static int XVideoGetPort( vout_thread_t *p_vout,
2078 vlc_fourcc_t i_chroma, picture_heap_t *p_heap )
2080 XvAdaptorInfo *p_adaptor;
2082 unsigned int i_adaptor, i_num_adaptors;
2083 int i_requested_adaptor;
2084 int i_selected_port;
2086 switch( XvQueryExtension( p_vout->p_sys->p_display, &i, &i, &i, &i, &i ) )
2091 case XvBadExtension:
2092 msg_Warn( p_vout, "XvBadExtension" );
2096 msg_Warn( p_vout, "XvBadAlloc" );
2100 msg_Warn( p_vout, "XvQueryExtension failed" );
2104 switch( XvQueryAdaptors( p_vout->p_sys->p_display,
2105 DefaultRootWindow( p_vout->p_sys->p_display ),
2106 &i_num_adaptors, &p_adaptor ) )
2111 case XvBadExtension:
2112 msg_Warn( p_vout, "XvBadExtension for XvQueryAdaptors" );
2116 msg_Warn( p_vout, "XvBadAlloc for XvQueryAdaptors" );
2120 msg_Warn( p_vout, "XvQueryAdaptors failed" );
2124 i_selected_port = -1;
2125 #ifdef MODULE_NAME_IS_xvmc
2126 i_requested_adaptor = config_GetInt( p_vout, "xvmc-adaptor" );
2128 i_requested_adaptor = config_GetInt( p_vout, "xvideo-adaptor" );
2130 for( i_adaptor = 0; i_adaptor < i_num_adaptors; ++i_adaptor )
2132 XvImageFormatValues *p_formats;
2133 int i_format, i_num_formats;
2136 /* If we requested an adaptor and it's not this one, we aren't
2138 if( i_requested_adaptor != -1 && ((int)i_adaptor != i_requested_adaptor) )
2143 /* If the adaptor doesn't have the required properties, skip it */
2144 if( !( p_adaptor[ i_adaptor ].type & XvInputMask ) ||
2145 !( p_adaptor[ i_adaptor ].type & XvImageMask ) )
2150 /* Check that adaptor supports our requested format... */
2151 p_formats = XvListImageFormats( p_vout->p_sys->p_display,
2152 p_adaptor[i_adaptor].base_id,
2156 i_format < i_num_formats && ( i_selected_port == -1 );
2159 XvAttribute *p_attr;
2160 int i_attr, i_num_attributes;
2161 Atom autopaint = None, colorkey = None;
2163 /* If this is not the format we want, or at least a
2164 * similar one, forget it */
2165 if( !vout_ChromaCmp( p_formats[ i_format ].id, i_chroma ) )
2170 /* Look for the first available port supporting this format */
2171 for( i_port = p_adaptor[i_adaptor].base_id;
2172 ( i_port < (int)(p_adaptor[i_adaptor].base_id
2173 + p_adaptor[i_adaptor].num_ports) )
2174 && ( i_selected_port == -1 );
2177 if( XvGrabPort( p_vout->p_sys->p_display, i_port, CurrentTime )
2180 i_selected_port = i_port;
2181 p_heap->i_chroma = p_formats[ i_format ].id;
2182 #if XvVersion > 2 || ( XvVersion == 2 && XvRevision >= 2 )
2183 p_heap->i_rmask = p_formats[ i_format ].red_mask;
2184 p_heap->i_gmask = p_formats[ i_format ].green_mask;
2185 p_heap->i_bmask = p_formats[ i_format ].blue_mask;
2190 /* If no free port was found, forget it */
2191 if( i_selected_port == -1 )
2196 /* If we found a port, print information about it */
2197 msg_Dbg( p_vout, "adaptor %i, port %i, format 0x%x (%4.4s) %s",
2198 i_adaptor, i_selected_port, p_formats[ i_format ].id,
2199 (char *)&p_formats[ i_format ].id,
2200 ( p_formats[ i_format ].format == XvPacked ) ?
2201 "packed" : "planar" );
2203 /* Use XV_AUTOPAINT_COLORKEY if supported, otherwise we will
2204 * manually paint the colour key */
2205 p_attr = XvQueryPortAttributes( p_vout->p_sys->p_display,
2207 &i_num_attributes );
2209 for( i_attr = 0; i_attr < i_num_attributes; i_attr++ )
2211 if( !strcmp( p_attr[i_attr].name, "XV_AUTOPAINT_COLORKEY" ) )
2213 autopaint = XInternAtom( p_vout->p_sys->p_display,
2214 "XV_AUTOPAINT_COLORKEY", False );
2215 XvSetPortAttribute( p_vout->p_sys->p_display,
2216 i_selected_port, autopaint, 1 );
2218 if( !strcmp( p_attr[i_attr].name, "XV_COLORKEY" ) )
2220 /* Find out the default colour key */
2221 colorkey = XInternAtom( p_vout->p_sys->p_display,
2222 "XV_COLORKEY", False );
2223 XvGetPortAttribute( p_vout->p_sys->p_display,
2224 i_selected_port, colorkey,
2225 &p_vout->p_sys->i_colourkey );
2228 p_vout->p_sys->b_paint_colourkey =
2229 autopaint == None && colorkey != None;
2231 if( p_attr != NULL )
2237 if( p_formats != NULL )
2244 if( i_num_adaptors > 0 )
2246 XvFreeAdaptorInfo( p_adaptor );
2249 if( i_selected_port == -1 )
2251 int i_chroma_tmp = X112VLC_FOURCC( i_chroma );
2252 if( i_requested_adaptor == -1 )
2254 msg_Warn( p_vout, "no free XVideo port found for format "
2255 "0x%.8x (%4.4s)", i_chroma_tmp, (char*)&i_chroma_tmp );
2259 msg_Warn( p_vout, "XVideo adaptor %i does not have a free "
2260 "XVideo port for format 0x%.8x (%4.4s)",
2261 i_requested_adaptor, i_chroma_tmp, (char*)&i_chroma_tmp );
2265 return i_selected_port;
2268 /*****************************************************************************
2269 * XVideoReleasePort: release YUV12 port
2270 *****************************************************************************/
2271 static void XVideoReleasePort( vout_thread_t *p_vout, int i_port )
2273 XvUngrabPort( p_vout->p_sys->p_display, i_port, CurrentTime );
2277 /*****************************************************************************
2278 * InitDisplay: open and initialize X11 device
2279 *****************************************************************************
2280 * Create a window according to video output given size, and set other
2281 * properties according to the display properties.
2282 *****************************************************************************/
2283 static int InitDisplay( vout_thread_t *p_vout )
2285 #ifdef MODULE_NAME_IS_x11
2286 XPixmapFormatValues * p_formats; /* pixmap formats */
2287 XVisualInfo * p_xvisual; /* visuals information */
2288 XVisualInfo xvisual_template; /* visual template */
2289 int i_count, i; /* array size */
2292 #ifdef HAVE_SYS_SHM_H
2293 p_vout->p_sys->i_shm_opcode = 0;
2295 if( config_GetInt( p_vout, MODULE_STRING "-shm" ) > 0 )
2297 int major, evt, err;
2299 if( XQueryExtension( p_vout->p_sys->p_display, "MIT-SHM", &major,
2301 && XShmQueryExtension( p_vout->p_sys->p_display ) )
2302 p_vout->p_sys->i_shm_opcode = major;
2304 if( p_vout->p_sys->i_shm_opcode )
2309 XShmQueryVersion( p_vout->p_sys->p_display, &major, &minor,
2311 msg_Dbg( p_vout, "XShm video extension v%d.%d "
2312 "(with%s pixmaps, opcode: %d)",
2313 major, minor, pixmaps ? "" : "out",
2314 p_vout->p_sys->i_shm_opcode );
2317 msg_Warn( p_vout, "XShm video extension not available" );
2320 msg_Dbg( p_vout, "XShm video extension disabled" );
2323 #ifdef MODULE_NAME_IS_xvideo
2324 /* XXX The brightness and contrast values should be read from environment
2325 * XXX variables... */
2327 XVideoSetAttribute( p_vout, "XV_BRIGHTNESS", 0.5 );
2328 XVideoSetAttribute( p_vout, "XV_CONTRAST", 0.5 );
2332 #ifdef MODULE_NAME_IS_x11
2333 /* Initialize structure */
2334 p_vout->p_sys->i_screen = DefaultScreen( p_vout->p_sys->p_display );
2336 /* Get screen depth */
2337 p_vout->p_sys->i_screen_depth = XDefaultDepth( p_vout->p_sys->p_display,
2338 p_vout->p_sys->i_screen );
2339 switch( p_vout->p_sys->i_screen_depth )
2343 * Screen depth is 8bpp. Use PseudoColor visual with private colormap.
2345 xvisual_template.screen = p_vout->p_sys->i_screen;
2346 xvisual_template.class = DirectColor;
2347 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2348 VisualScreenMask | VisualClassMask,
2349 &xvisual_template, &i_count );
2350 if( p_xvisual == NULL )
2352 msg_Err( p_vout, "no PseudoColor visual available" );
2353 return VLC_EGENERIC;
2355 p_vout->p_sys->i_bytes_per_pixel = 1;
2356 p_vout->output.pf_setpalette = SetPalette;
2363 * Screen depth is higher than 8bpp. TrueColor visual is used.
2365 xvisual_template.screen = p_vout->p_sys->i_screen;
2366 xvisual_template.class = TrueColor;
2367 /* In some cases, we get a truecolor class adaptor that has a different
2368 color depth. So try to get a real true color one first */
2369 xvisual_template.depth = p_vout->p_sys->i_screen_depth;
2371 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2372 VisualScreenMask | VisualClassMask |
2374 &xvisual_template, &i_count );
2375 if( p_xvisual == NULL )
2377 msg_Warn( p_vout, "No screen matching the required color depth" );
2378 p_xvisual = XGetVisualInfo( p_vout->p_sys->p_display,
2379 VisualScreenMask | VisualClassMask,
2380 &xvisual_template, &i_count );
2381 if( p_xvisual == NULL )
2384 msg_Err( p_vout, "no TrueColor visual available" );
2385 return VLC_EGENERIC;
2389 p_vout->output.i_rmask = p_xvisual->red_mask;
2390 p_vout->output.i_gmask = p_xvisual->green_mask;
2391 p_vout->output.i_bmask = p_xvisual->blue_mask;
2393 /* There is no difference yet between 3 and 4 Bpp. The only way
2394 * to find the actual number of bytes per pixel is to list supported
2395 * pixmap formats. */
2396 p_formats = XListPixmapFormats( p_vout->p_sys->p_display, &i_count );
2397 p_vout->p_sys->i_bytes_per_pixel = 0;
2399 for( i = 0; i < i_count; i++ )
2401 /* Under XFree4.0, the list contains pixmap formats available
2402 * through all video depths ; so we have to check against current
2404 if( p_formats[i].depth == (int)p_vout->p_sys->i_screen_depth )
2406 if( p_formats[i].bits_per_pixel / 8
2407 > (int)p_vout->p_sys->i_bytes_per_pixel )
2409 p_vout->p_sys->i_bytes_per_pixel =
2410 p_formats[i].bits_per_pixel / 8;
2414 if( p_formats ) XFree( p_formats );
2418 p_vout->p_sys->p_visual = p_xvisual->visual;
2425 #ifndef MODULE_NAME_IS_glx
2427 #ifdef HAVE_SYS_SHM_H
2428 /*****************************************************************************
2429 * CreateShmImage: create an XImage or XvImage using shared memory extension
2430 *****************************************************************************
2431 * Prepare an XImage or XvImage for display function.
2432 * The order of the operations respects the recommandations of the mit-shm
2433 * document by J.Corbet and K.Packard. Most of the parameters were copied from
2434 * there. See http://ftp.xfree86.org/pub/XFree86/4.0/doc/mit-shm.TXT
2435 *****************************************************************************/
2436 IMAGE_TYPE * CreateShmImage( vout_thread_t *p_vout,
2437 Display* p_display, EXTRA_ARGS_SHM,
2438 int i_width, int i_height )
2440 IMAGE_TYPE *p_image;
2443 /* Create XImage / XvImage */
2444 #ifdef MODULE_NAME_IS_xvideo
2445 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2446 i_width, i_height, p_shm );
2447 #elif defined(MODULE_NAME_IS_xvmc)
2448 p_image = XvShmCreateImage( p_display, i_xvport, i_chroma, 0,
2449 i_width, i_height, p_shm );
2451 p_image = XShmCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2452 p_shm, i_width, i_height );
2454 if( p_image == NULL )
2456 msg_Err( p_vout, "image creation failed" );
2460 /* For too big image, the buffer returned is sometimes too small, prevent
2461 * VLC to segfault because of it
2462 * FIXME is it normal ? Is there a way to detect it
2463 * before (XvQueryBestSize did not) ? */
2464 if( p_image->width < i_width || p_image->height < i_height )
2466 msg_Err( p_vout, "cannot allocate shared image data with the right size "
2467 "(%dx%d instead of %dx%d)",
2468 p_image->width, p_image->height,
2469 i_width, i_height );
2470 IMAGE_FREE( p_image );
2474 /* Allocate shared memory segment. */
2475 p_shm->shmid = shmget( IPC_PRIVATE, DATA_SIZE(p_image), IPC_CREAT | 0600 );
2476 if( p_shm->shmid < 0 )
2478 msg_Err( p_vout, "cannot allocate shared image data (%m)" );
2479 IMAGE_FREE( p_image );
2483 /* Attach shared memory segment to process (read/write) */
2484 p_shm->shmaddr = p_image->data = shmat( p_shm->shmid, 0, 0 );
2485 if(! p_shm->shmaddr )
2487 msg_Err( p_vout, "cannot attach shared memory (%m)" );
2488 IMAGE_FREE( p_image );
2489 shmctl( p_shm->shmid, IPC_RMID, 0 );
2493 /* Read-only data. We won't be using XShmGetImage */
2494 p_shm->readOnly = True;
2496 /* Attach shared memory segment to X server */
2497 XSynchronize( p_display, True );
2498 i_shm_major = p_vout->p_sys->i_shm_opcode;
2499 result = XShmAttach( p_display, p_shm );
2500 if( result == False || !i_shm_major )
2502 msg_Err( p_vout, "cannot attach shared memory to X server" );
2503 IMAGE_FREE( p_image );
2504 shmctl( p_shm->shmid, IPC_RMID, 0 );
2505 shmdt( p_shm->shmaddr );
2508 XSynchronize( p_display, False );
2510 /* Send image to X server. This instruction is required, since having
2511 * built a Shm XImage and not using it causes an error on XCloseDisplay,
2512 * and remember NOT to use XFlush ! */
2513 XSync( p_display, False );
2516 /* Mark the shm segment to be removed when there are no more
2517 * attachements, so it is automatic on process exit or after shmdt */
2518 shmctl( p_shm->shmid, IPC_RMID, 0 );
2525 /*****************************************************************************
2526 * CreateImage: create an XImage or XvImage
2527 *****************************************************************************
2528 * Create a simple image used as a buffer.
2529 *****************************************************************************/
2530 static IMAGE_TYPE * CreateImage( vout_thread_t *p_vout,
2531 Display *p_display, EXTRA_ARGS,
2532 int i_width, int i_height )
2534 uint8_t * p_data; /* image data storage zone */
2535 IMAGE_TYPE *p_image;
2536 #ifdef MODULE_NAME_IS_x11
2537 int i_quantum; /* XImage quantum (see below) */
2538 int i_bytes_per_line;
2541 /* Allocate memory for image */
2542 #ifdef MODULE_NAME_IS_xvideo
2543 p_data = malloc( i_width * i_height * i_bits_per_pixel / 8 );
2544 #elif defined(MODULE_NAME_IS_x11)
2545 i_bytes_per_line = i_width * i_bytes_per_pixel;
2546 p_data = malloc( i_bytes_per_line * i_height );
2551 #ifdef MODULE_NAME_IS_x11
2552 /* Optimize the quantum of a scanline regarding its size - the quantum is
2553 a diviser of the number of bits between the start of two scanlines. */
2554 if( i_bytes_per_line & 0xf )
2558 else if( i_bytes_per_line & 0x10 )
2568 /* Create XImage. p_data will be automatically freed */
2569 #ifdef MODULE_NAME_IS_xvideo
2570 p_image = XvCreateImage( p_display, i_xvport, i_chroma,
2571 (char *)p_data, i_width, i_height );
2572 #elif defined(MODULE_NAME_IS_x11)
2573 p_image = XCreateImage( p_display, p_visual, i_depth, ZPixmap, 0,
2574 (char *)p_data, i_width, i_height, i_quantum, 0 );
2576 if( p_image == NULL )
2578 msg_Err( p_vout, "XCreateImage() failed" );
2587 /*****************************************************************************
2588 * X11ErrorHandler: replace error handler so we can intercept some of them
2589 *****************************************************************************/
2590 static int X11ErrorHandler( Display * display, XErrorEvent * event )
2594 XGetErrorText( display, event->error_code, txt, sizeof( txt ) );
2596 "[????????] x11 video output error: X11 request %u.%u failed "
2597 "with error code %u:\n %s\n",
2598 event->request_code, event->minor_code, event->error_code, txt );
2600 switch( event->request_code )
2602 case X_SetInputFocus:
2603 /* Ignore errors on XSetInputFocus()
2604 * (they happen when a window is not yet mapped) */
2608 #ifdef HAVE_SYS_SHM_H
2609 if( event->request_code == i_shm_major ) /* MIT-SHM */
2612 "[????????] x11 video output notice:"
2613 " buggy X11 server claims shared memory\n"
2614 "[????????] x11 video output notice:"
2615 " support though it does not work (OpenSSH?)\n" );
2616 return i_shm_major = 0;
2621 XSetErrorHandler(NULL);
2622 return (XSetErrorHandler(X11ErrorHandler))( display, event );
2624 /* Work-around Maemo Xvideo bug */
2629 #ifdef MODULE_NAME_IS_x11
2630 /*****************************************************************************
2631 * SetPalette: sets an 8 bpp palette
2632 *****************************************************************************
2633 * This function sets the palette given as an argument. It does not return
2634 * anything, but could later send information on which colors it was unable
2636 *****************************************************************************/
2637 static void SetPalette( vout_thread_t *p_vout,
2638 uint16_t *red, uint16_t *green, uint16_t *blue )
2641 XColor p_colors[255];
2643 /* allocate palette */
2644 for( i = 0; i < 255; i++ )
2646 /* kludge: colors are indexed reversely because color 255 seems
2647 * to be reserved for black even if we try to set it to white */
2648 p_colors[ i ].pixel = 255 - i;
2649 p_colors[ i ].pad = 0;
2650 p_colors[ i ].flags = DoRed | DoGreen | DoBlue;
2651 p_colors[ i ].red = red[ 255 - i ];
2652 p_colors[ i ].blue = blue[ 255 - i ];
2653 p_colors[ i ].green = green[ 255 - i ];
2656 XStoreColors( p_vout->p_sys->p_display,
2657 p_vout->p_sys->colormap, p_colors, 255 );
2661 /*****************************************************************************
2662 * Control: control facility for the vout
2663 *****************************************************************************/
2664 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
2666 unsigned int i_width, i_height;
2671 i_width = va_arg( args, unsigned int );
2672 i_height = va_arg( args, unsigned int );
2673 if( !i_width ) i_width = p_vout->i_window_width;
2674 if( !i_height ) i_height = p_vout->i_window_height;
2676 return vout_window_SetSize( p_vout->p_sys->window.owner_window,
2679 case VOUT_SET_STAY_ON_TOP:
2680 return vout_window_SetOnTop( p_vout->p_sys->window.owner_window,
2681 va_arg( args, int ) );
2684 return VLC_EGENERIC;