sdl.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:31k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * sdl.c: SDL video output display method
  3.  *****************************************************************************
  4.  * Copyright (C) 1998-2001 VideoLAN
  5.  * $Id: sdl.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *          Pierre Baillet <oct@zoy.org>
  9.  *          Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  24.  *****************************************************************************/
  25. /*****************************************************************************
  26.  * Preamble
  27.  *****************************************************************************/
  28. #include <errno.h>                                                 /* ENOMEM */
  29. #include <stdlib.h>                                                /* free() */
  30. #include <string.h>                                            /* strerror() */
  31. #include <vlc/vlc.h>
  32. #include <vlc/intf.h>
  33. #include <vlc/vout.h>
  34. #include <vlc/aout.h>
  35. #include <sys/types.h>
  36. #ifndef WIN32
  37. #   include <netinet/in.h>                            /* BSD: struct in_addr */
  38. #endif
  39. #include SDL_INCLUDE_FILE
  40. #define SDL_MAX_DIRECTBUFFERS 10
  41. #define SDL_DEFAULT_BPP 16
  42. /*****************************************************************************
  43.  * vout_sys_t: video output SDL method descriptor
  44.  *****************************************************************************
  45.  * This structure is part of the video output thread descriptor.
  46.  * It describes the SDL specific properties of an output thread.
  47.  *****************************************************************************/
  48. struct vout_sys_t
  49. {
  50.     SDL_Surface *   p_display;                             /* display device */
  51.     int i_width;
  52.     int i_height;
  53.     /* For YUV output */
  54.     SDL_Overlay * p_overlay;   /* An overlay we keep to grab the XVideo port */
  55.     /* For RGB output */
  56.     int i_surfaces;
  57.     vlc_bool_t  b_cursor;
  58.     vlc_bool_t  b_cursor_autohidden;
  59.     mtime_t     i_lastmoved;
  60.     mtime_t     i_lastpressed;                        /* to track dbl-clicks */
  61. };
  62. /*****************************************************************************
  63.  * picture_sys_t: direct buffer method descriptor
  64.  *****************************************************************************
  65.  * This structure is part of the picture descriptor, it describes the
  66.  * SDL specific properties of a direct buffer.
  67.  *****************************************************************************/
  68. struct picture_sys_t
  69. {
  70.     SDL_Overlay *p_overlay;
  71. };
  72. /*****************************************************************************
  73.  * Local prototypes
  74.  *****************************************************************************/
  75. static int  Open      ( vlc_object_t * );
  76. static void Close     ( vlc_object_t * );
  77. static int  Init      ( vout_thread_t * );
  78. static void End       ( vout_thread_t * );
  79. static int  Manage    ( vout_thread_t * );
  80. static void Display   ( vout_thread_t *, picture_t * );
  81. static int  OpenDisplay     ( vout_thread_t * );
  82. static void CloseDisplay    ( vout_thread_t * );
  83. static int  NewPicture      ( vout_thread_t *, picture_t * );
  84. static void SetPalette      ( vout_thread_t *,
  85.                               uint16_t *, uint16_t *, uint16_t * );
  86. /*****************************************************************************
  87.  * Module descriptor
  88.  *****************************************************************************/
  89. vlc_module_begin();
  90.     set_description( _("Simple DirectMedia Layer video output") );
  91.     set_capability( "video output", 60 );
  92.     add_shortcut( "sdl" );
  93.     set_callbacks( Open, Close );
  94.     /* XXX: check for conflicts with the SDL audio output */
  95.     var_Create( p_module->p_libvlc, "sdl", VLC_VAR_MUTEX );
  96. #if defined( __i386__ )
  97.     /* On i386, SDL is linked against svgalib */
  98.     linked_with_a_crap_library_which_uses_atexit();
  99. #endif
  100. vlc_module_end();
  101. /*****************************************************************************
  102.  * OpenVideo: allocate SDL video thread output method
  103.  *****************************************************************************
  104.  * This function allocate and initialize a SDL vout method. It uses some of the
  105.  * vout properties to choose the correct mode, and change them according to the
  106.  * mode actually used.
  107.  *****************************************************************************/
  108. static int Open ( vlc_object_t *p_this )
  109. {
  110.     vout_thread_t * p_vout = (vout_thread_t *)p_this;
  111.     vlc_value_t lockval;
  112. #ifdef HAVE_SETENV
  113.     char *psz_method;
  114. #endif
  115.     var_Get( p_this->p_libvlc, "sdl", &lockval );
  116.     vlc_mutex_lock( lockval.p_address );
  117.     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
  118.     {
  119.         vlc_mutex_unlock( lockval.p_address );
  120.         return VLC_EGENERIC;
  121.     }
  122.     /* Allocate structure */
  123.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  124.     if( p_vout->p_sys == NULL )
  125.     {
  126.         msg_Err( p_vout, "out of memory" );
  127.         vlc_mutex_unlock( lockval.p_address );
  128.         return VLC_ENOMEM;
  129.     }
  130.     p_vout->pf_init = Init;
  131.     p_vout->pf_end = End;
  132.     p_vout->pf_manage = Manage;
  133.     p_vout->pf_render = NULL;
  134.     p_vout->pf_display = Display;
  135. #ifdef HAVE_SETENV
  136.     psz_method = config_GetPsz( p_vout, "vout" );
  137.     if( psz_method )
  138.     {
  139.         while( *psz_method && *psz_method != ':' )
  140.         {
  141.             psz_method++;
  142.         }
  143.         if( *psz_method )
  144.         {
  145.             setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
  146.         }
  147.     }
  148. #endif
  149.     /* Initialize library */
  150.     if( SDL_Init( SDL_INIT_VIDEO
  151. #ifndef WIN32
  152.     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
  153.                 | SDL_INIT_EVENTTHREAD
  154. #endif
  155. #ifdef DEBUG
  156.     /* In debug mode you may want vlc to dump a core instead of staying
  157.      * stuck */
  158.                 | SDL_INIT_NOPARACHUTE
  159. #endif
  160.                 ) < 0 )
  161.     {
  162.         msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
  163.         free( p_vout->p_sys );
  164.         vlc_mutex_unlock( lockval.p_address );
  165.         return VLC_EGENERIC;
  166.     }
  167.     vlc_mutex_unlock( lockval.p_address );
  168.     p_vout->p_sys->b_cursor = 1;
  169.     p_vout->p_sys->b_cursor_autohidden = 0;
  170.     p_vout->p_sys->i_lastmoved = mdate();
  171.     if( OpenDisplay( p_vout ) )
  172.     {
  173.         msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
  174.         SDL_QuitSubSystem( SDL_INIT_VIDEO );
  175.         free( p_vout->p_sys );
  176.         return VLC_EGENERIC;
  177.     }
  178.     return VLC_SUCCESS;
  179. }
  180. /*****************************************************************************
  181.  * Init: initialize SDL video thread output method
  182.  *****************************************************************************
  183.  * This function initialize the SDL display device.
  184.  *****************************************************************************/
  185. static int Init( vout_thread_t *p_vout )
  186. {
  187.     int i_index;
  188.     picture_t *p_pic;
  189.     p_vout->p_sys->i_surfaces = 0;
  190.     I_OUTPUTPICTURES = 0;
  191.     /* Initialize the output structure */
  192.     if( p_vout->p_sys->p_overlay == NULL )
  193.     {
  194.         /* All we have is an RGB image with square pixels */
  195.         p_vout->output.i_width  = p_vout->p_sys->i_width;
  196.         p_vout->output.i_height = p_vout->p_sys->i_height;
  197.         p_vout->output.i_aspect = p_vout->output.i_width
  198.                                    * VOUT_ASPECT_FACTOR
  199.                                    / p_vout->output.i_height;
  200.     }
  201.     else
  202.     {
  203.         /* We may need to convert the chroma, but at least we keep the
  204.          * aspect ratio */
  205.         p_vout->output.i_width  = p_vout->render.i_width;
  206.         p_vout->output.i_height = p_vout->render.i_height;
  207.         p_vout->output.i_aspect = p_vout->render.i_aspect;
  208.     }
  209.     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
  210.     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
  211.     {
  212.         p_pic = NULL;
  213.         /* Find an empty picture slot */
  214.         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  215.         {
  216.             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  217.             {
  218.                 p_pic = p_vout->p_picture + i_index;
  219.                 break;
  220.             }
  221.         }
  222.         /* Allocate the picture if we found one */
  223.         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
  224.         {
  225.             break;
  226.         }
  227.         p_pic->i_status = DESTROYED_PICTURE;
  228.         p_pic->i_type   = DIRECT_PICTURE;
  229.         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
  230.         I_OUTPUTPICTURES++;
  231.     }
  232.     return VLC_SUCCESS;
  233. }
  234. /*****************************************************************************
  235.  * End: terminate Sys video thread output method
  236.  *****************************************************************************
  237.  * Terminate an output method created by OpenVideo
  238.  *****************************************************************************/
  239. static void End( vout_thread_t *p_vout )
  240. {
  241.     int i_index;
  242.     /* Free the output buffers we allocated */
  243.     for( i_index = I_OUTPUTPICTURES ; i_index ; )
  244.     {
  245.         i_index--;
  246.         if( p_vout->p_sys->p_overlay == NULL )
  247.         {
  248.             /* RGB picture */
  249.         }
  250.         else
  251.         {
  252.             SDL_UnlockYUVOverlay(
  253.                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
  254.             SDL_FreeYUVOverlay(
  255.                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
  256.         }
  257.         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
  258.     }
  259. }
  260. /*****************************************************************************
  261.  * CloseVideo: destroy Sys video thread output method
  262.  *****************************************************************************
  263.  * Terminate an output method created by vout_SDLCreate
  264.  *****************************************************************************/
  265. static void Close ( vlc_object_t *p_this )
  266. {
  267.     vout_thread_t * p_vout = (vout_thread_t *)p_this;
  268.     CloseDisplay( p_vout );
  269.     SDL_QuitSubSystem( SDL_INIT_VIDEO );
  270.     free( p_vout->p_sys );
  271. }
  272. /*****************************************************************************
  273.  * Manage: handle Sys events
  274.  *****************************************************************************
  275.  * This function should be called regularly by video output thread. It returns
  276.  * a non null value if an error occurred.
  277.  *****************************************************************************/
  278. static int Manage( vout_thread_t *p_vout )
  279. {
  280.     SDL_Event event;                                            /* SDL event */
  281.     vlc_value_t val;
  282.     int i_width, i_height, i_x, i_y;
  283.     /* Process events */
  284.     while( SDL_PollEvent(&event) )
  285.     {
  286.         switch( event.type )
  287.         {
  288.         case SDL_VIDEORESIZE:                          /* Resizing of window */
  289.             /* Update dimensions */
  290.             p_vout->i_changes |= VOUT_SIZE_CHANGE;
  291.             p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
  292.             p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
  293.             break;
  294.         case SDL_MOUSEMOTION:
  295.             vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
  296.                                p_vout->p_sys->i_height,
  297.                                &i_x, &i_y, &i_width, &i_height );
  298.             val.i_int = ( event.motion.x - i_x )
  299.                          * p_vout->render.i_width / i_width;
  300.             var_Set( p_vout, "mouse-x", val );
  301.             val.i_int = ( event.motion.y - i_y )
  302.                          * p_vout->render.i_height / i_height;
  303.             var_Set( p_vout, "mouse-y", val );
  304.             val.b_bool = VLC_TRUE;
  305.             var_Set( p_vout, "mouse-moved", val );
  306.             if( p_vout->p_sys->b_cursor &&
  307.                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
  308.             {
  309.                 if( p_vout->p_sys->b_cursor_autohidden )
  310.                 {
  311.                     p_vout->p_sys->b_cursor_autohidden = 0;
  312.                     SDL_ShowCursor( 1 );
  313.                 }
  314.                 else
  315.                 {
  316.                     p_vout->p_sys->i_lastmoved = mdate();
  317.                 }
  318.             }
  319.             break;
  320.         case SDL_MOUSEBUTTONUP:
  321.             switch( event.button.button )
  322.             {
  323.             case SDL_BUTTON_LEFT:
  324.                 val.b_bool = VLC_TRUE;
  325.                 var_Set( p_vout, "mouse-clicked", val );
  326.                 break;
  327.             case SDL_BUTTON_RIGHT:
  328.                 {
  329.                     intf_thread_t *p_intf;
  330.                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
  331.                                                       FIND_ANYWHERE );
  332.                     if( p_intf )
  333.                     {
  334.                         p_intf->b_menu_change = 1;
  335.                         vlc_object_release( p_intf );
  336.                     }
  337.                 }
  338.                 break;
  339.             }
  340.             break;
  341.         case SDL_MOUSEBUTTONDOWN:
  342.             switch( event.button.button )
  343.             {
  344.             case SDL_BUTTON_LEFT:
  345.                 /* In this part we will eventually manage
  346.                  * clicks for DVD navigation for instance. */
  347.                 /* detect double-clicks */
  348.                 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
  349.                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
  350.                 p_vout->p_sys->i_lastpressed = mdate();
  351.                 break;
  352.             case 4:
  353.                 break;
  354.             case 5:
  355.                 break;
  356.             }
  357.             break;
  358.         case SDL_QUIT:
  359.             p_vout->p_vlc->b_die = 1;
  360.             break;
  361.         case SDL_KEYDOWN:                             /* if a key is pressed */
  362.             switch( event.key.keysym.sym )
  363.             {
  364.             case SDLK_ESCAPE:
  365.                 if( p_vout->b_fullscreen )
  366.                 {
  367.                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
  368.                 }
  369.                 else
  370.                 {
  371.                     p_vout->p_vlc->b_die = 1;
  372.                 }
  373.                 break;
  374.             case SDLK_q:                                             /* quit */
  375.                 p_vout->p_vlc->b_die = 1;
  376.                 break;
  377.             case SDLK_f:                             /* switch to fullscreen */
  378.                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
  379.                 break;
  380.             case SDLK_c:                                 /* toggle grayscale */
  381.                 p_vout->b_grayscale = ! p_vout->b_grayscale;
  382.                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
  383.                 break;
  384.             case SDLK_i:                                      /* toggle info */
  385.                 p_vout->b_info = ! p_vout->b_info;
  386.                 p_vout->i_changes |= VOUT_INFO_CHANGE;
  387.                 break;
  388.             case SDLK_s:                                   /* toggle scaling */
  389.                 p_vout->b_scale = ! p_vout->b_scale;
  390.                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
  391.                 break;
  392.             case SDLK_SPACE:                             /* toggle interface */
  393.                 p_vout->b_interface = ! p_vout->b_interface;
  394.                 p_vout->i_changes |= VOUT_INTF_CHANGE;
  395.                 break;
  396.             case SDLK_MENU:
  397.                 {
  398.                     intf_thread_t *p_intf;
  399.                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
  400.                                                       FIND_ANYWHERE );
  401.                     if( p_intf != NULL )
  402.                     {
  403.                         p_intf->b_menu_change = 1;
  404.                         vlc_object_release( p_intf );
  405.                     }
  406.                 }
  407.                 break;
  408.             case SDLK_LEFT:
  409.                 break;
  410.             case SDLK_RIGHT:
  411.                 break;
  412.             case SDLK_UP:
  413.                 break;
  414.             case SDLK_DOWN:
  415.                 break;
  416.             case SDLK_b:
  417.                 {
  418.                     audio_volume_t i_volume;
  419.                     if ( !aout_VolumeDown( p_vout, 1, &i_volume ) )
  420.                     {
  421.                         msg_Dbg( p_vout, "audio volume is now %d", i_volume );
  422.                     }
  423.                     else
  424.                     {
  425.                         msg_Dbg( p_vout, "audio volume: operation not supported" );
  426.                     }
  427.                 }
  428.                 break;
  429.             case SDLK_n:
  430.                 {
  431.                     audio_volume_t i_volume;
  432.                     if ( !aout_VolumeUp( p_vout, 1, &i_volume ) )
  433.                     {
  434.                         msg_Dbg( p_vout, "audio volume is now %d", i_volume );
  435.                     }
  436.                     else
  437.                     {
  438.                         msg_Dbg( p_vout, "audio volume: operation not supported" );
  439.                     }
  440.                 }
  441.                 break;
  442.              default:
  443.                 break;
  444.             }
  445.             break;
  446.         default:
  447.             break;
  448.         }
  449.     }
  450.     /* Fullscreen change */
  451.     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
  452.     {
  453.         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
  454.         p_vout->p_sys->b_cursor_autohidden = 0;
  455.         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
  456.                         ! p_vout->p_sys->b_cursor_autohidden );
  457.         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
  458.         p_vout->i_changes |= VOUT_SIZE_CHANGE;
  459.     }
  460.     /*
  461.      * Size change
  462.      */
  463.     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
  464.     {
  465.         msg_Dbg( p_vout, "video display resized (%dx%d)",
  466.                  p_vout->p_sys->i_width, p_vout->p_sys->i_height );
  467.         CloseDisplay( p_vout );
  468.         OpenDisplay( p_vout );
  469.         /* We don't need to signal the vout thread about the size change if
  470.          * we can handle rescaling ourselves */
  471.         if( p_vout->p_sys->p_overlay != NULL )
  472.             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
  473.     }
  474.     /* Pointer change */
  475.     if( ! p_vout->p_sys->b_cursor_autohidden &&
  476.         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
  477.     {
  478.         /* Hide the mouse automatically */
  479.         p_vout->p_sys->b_cursor_autohidden = 1;
  480.         SDL_ShowCursor( 0 );
  481.     }
  482.     return VLC_SUCCESS;
  483. }
  484. /*****************************************************************************
  485.  * Display: displays previously rendered output
  486.  *****************************************************************************
  487.  * This function sends the currently rendered image to the display.
  488.  *****************************************************************************/
  489. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  490. {
  491.     int x, y, w, h;
  492.     SDL_Rect disp;
  493.     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
  494.                        &x, &y, &w, &h );
  495.     disp.x = x;
  496.     disp.y = y;
  497.     disp.w = w;
  498.     disp.h = h;
  499.     if( p_vout->p_sys->p_overlay == NULL )
  500.     {
  501.         /* RGB picture */
  502.         SDL_Flip( p_vout->p_sys->p_display );
  503.     }
  504.     else
  505.     {
  506.         /* Overlay picture */
  507.         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
  508.         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
  509.         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
  510.     }
  511. }
  512. /* following functions are local */
  513. /*****************************************************************************
  514.  * OpenDisplay: open and initialize SDL device
  515.  *****************************************************************************
  516.  * Open and initialize display according to preferences specified in the vout
  517.  * thread fields.
  518.  *****************************************************************************/
  519. static int OpenDisplay( vout_thread_t *p_vout )
  520. {
  521.     uint32_t i_flags;
  522.     int i_bpp;
  523.     /* SDL fucked up fourcc definitions on bigendian machines */
  524.     uint32_t i_sdl_chroma;
  525.     /* Set main window's size */
  526.     p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->output.i_width :
  527.                                                     p_vout->i_window_width;
  528.     p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->output.i_height :
  529.                                                      p_vout->i_window_height;
  530.     /* Initialize flags and cursor */
  531.     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
  532.     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
  533.     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
  534.                              SDL_DEFAULT_BPP, i_flags );
  535.     if( i_bpp == 0 )
  536.     {
  537.         msg_Err( p_vout, "no video mode available" );
  538.         return VLC_EGENERIC;
  539.     }
  540.     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
  541.                                                  p_vout->p_sys->i_height,
  542.                                                  i_bpp, i_flags );
  543.     if( p_vout->p_sys->p_display == NULL )
  544.     {
  545.         msg_Err( p_vout, "cannot set video mode" );
  546.         return VLC_EGENERIC;
  547.     }
  548.     SDL_LockSurface( p_vout->p_sys->p_display );
  549.     /* Choose the chroma we will try first. */
  550.     switch( p_vout->render.i_chroma )
  551.     {
  552.         case VLC_FOURCC('Y','U','Y','2'):
  553.         case VLC_FOURCC('Y','U','N','V'):
  554.             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
  555.             i_sdl_chroma = SDL_YUY2_OVERLAY;
  556.             break;
  557.         case VLC_FOURCC('U','Y','V','Y'):
  558.         case VLC_FOURCC('U','Y','N','V'):
  559.         case VLC_FOURCC('Y','4','2','2'):
  560.             p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
  561.             i_sdl_chroma = SDL_UYVY_OVERLAY;
  562.             break;
  563.         case VLC_FOURCC('Y','V','Y','U'):
  564.             p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
  565.             i_sdl_chroma = SDL_YVYU_OVERLAY;
  566.             break;
  567.         case VLC_FOURCC('Y','V','1','2'):
  568.         case VLC_FOURCC('I','4','2','0'):
  569.         case VLC_FOURCC('I','Y','U','V'):
  570.         default:
  571.             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
  572.             i_sdl_chroma = SDL_YV12_OVERLAY;
  573.             break;
  574.     }
  575.     p_vout->p_sys->p_overlay =
  576.         SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma, p_vout->p_sys->p_display );
  577.     /* FIXME: if the first overlay we find is software, don't stop,
  578.      * because we may find a hardware one later ... */
  579.     /* If this best choice failed, fall back to other chromas */
  580.     if( p_vout->p_sys->p_overlay == NULL )
  581.     {
  582.         p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
  583.         p_vout->p_sys->p_overlay =
  584.             SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
  585.                                   p_vout->p_sys->p_display );
  586.     }
  587.     if( p_vout->p_sys->p_overlay == NULL )
  588.     {
  589.         p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
  590.         p_vout->p_sys->p_overlay =
  591.             SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
  592.                                   p_vout->p_sys->p_display );
  593.     }
  594.     if( p_vout->p_sys->p_overlay == NULL )
  595.     {
  596.         p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
  597.         p_vout->p_sys->p_overlay =
  598.             SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
  599.                                   p_vout->p_sys->p_display );
  600.     }
  601.     if( p_vout->p_sys->p_overlay == NULL )
  602.     {
  603.         msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
  604.                   p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma );
  605.         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
  606.         {
  607.             case 8:
  608.                 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
  609.                 p_vout->output.pf_setpalette = SetPalette;
  610.                 break;
  611.             case 15:
  612.                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
  613.                 break;
  614.             case 16:
  615.                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
  616.                 break;
  617.             case 24:
  618.                 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
  619.                 break;
  620.             case 32:
  621.                 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
  622.                 break;
  623.             default:
  624.                 msg_Err( p_vout, "unknown screen depth %i",
  625.                          p_vout->p_sys->p_display->format->BitsPerPixel );
  626.                 SDL_UnlockSurface( p_vout->p_sys->p_display );
  627.                 SDL_FreeSurface( p_vout->p_sys->p_display );
  628.                 return VLC_EGENERIC;
  629.         }
  630.         p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
  631.         p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
  632.         p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
  633.         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
  634.                            VOUT_TITLE " (software RGB SDL output)" );
  635.     }
  636.     else
  637.     {
  638.         if( p_vout->p_sys->p_overlay->hw_overlay )
  639.         {
  640.             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
  641.                                VOUT_TITLE " (hardware YUV SDL output)" );
  642.         }
  643.         else
  644.         {
  645.             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
  646.                                VOUT_TITLE " (software YUV SDL output)" );
  647.         }
  648.     }
  649.     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
  650.     return VLC_SUCCESS;
  651. }
  652. /*****************************************************************************
  653.  * CloseDisplay: close and reset SDL device
  654.  *****************************************************************************
  655.  * This function returns all resources allocated by OpenDisplay and restore
  656.  * the original state of the device.
  657.  *****************************************************************************/
  658. static void CloseDisplay( vout_thread_t *p_vout )
  659. {
  660.     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
  661.     SDL_UnlockSurface ( p_vout->p_sys->p_display );
  662.     SDL_FreeSurface( p_vout->p_sys->p_display );
  663. }
  664. /*****************************************************************************
  665.  * NewPicture: allocate a picture
  666.  *****************************************************************************
  667.  * Returns 0 on success, -1 otherwise
  668.  *****************************************************************************/
  669. static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
  670. {
  671.     int i_width  = p_vout->output.i_width;
  672.     int i_height = p_vout->output.i_height;
  673.     if( p_vout->p_sys->p_overlay == NULL )
  674.     {
  675.         /* RGB picture */
  676.         if( p_vout->p_sys->i_surfaces )
  677.         {
  678.             /* We already allocated this surface, return */
  679.             return VLC_EGENERIC;
  680.         }
  681.         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
  682.         if( p_pic->p_sys == NULL )
  683.         {
  684.             return VLC_ENOMEM;
  685.         }
  686.         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
  687.         {
  688.             case 8:
  689.                 p_pic->p->i_pixel_pitch = 1;
  690.                 break;
  691.             case 15:
  692.             case 16:
  693.                 p_pic->p->i_pixel_pitch = 2;
  694.                 break;
  695.             case 24:
  696.             case 32:
  697.                 p_pic->p->i_pixel_pitch = 4;
  698.                 break;
  699.             default:
  700.                 return VLC_EGENERIC;
  701.         }
  702.         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
  703.         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
  704.         p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
  705.         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
  706.         p_pic->p->i_visible_pitch =
  707.             p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
  708.         p_vout->p_sys->i_surfaces++;
  709.         p_pic->i_planes = 1;
  710.     }
  711.     else
  712.     {
  713.         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
  714.         if( p_pic->p_sys == NULL )
  715.         {
  716.             return VLC_ENOMEM;
  717.         }
  718.         p_pic->p_sys->p_overlay =
  719.             SDL_CreateYUVOverlay( i_width, i_height,
  720.                                   p_vout->output.i_chroma,
  721.                                   p_vout->p_sys->p_display );
  722.         if( p_pic->p_sys->p_overlay == NULL )
  723.         {
  724.             free( p_pic->p_sys );
  725.             return VLC_EGENERIC;
  726.         }
  727.         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
  728.         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
  729.         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
  730.         p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
  731.         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
  732.         switch( p_vout->output.i_chroma )
  733.         {
  734.         case SDL_YV12_OVERLAY:
  735.             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
  736.             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
  737.             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
  738.             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
  739.             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
  740.             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
  741.             p_pic->p[U_PLANE].i_pixel_pitch = 1;
  742.             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
  743.             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
  744.             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
  745.             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
  746.             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
  747.             p_pic->p[V_PLANE].i_pixel_pitch = 1;
  748.             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
  749.             p_pic->i_planes = 3;
  750.             break;
  751.         case SDL_IYUV_OVERLAY:
  752.             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
  753.             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
  754.             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
  755.             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
  756.             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
  757.             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
  758.             p_pic->p[U_PLANE].i_pixel_pitch = 1;
  759.             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
  760.             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
  761.             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
  762.             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
  763.             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
  764.             p_pic->p[V_PLANE].i_pixel_pitch = 1;
  765.             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
  766.             p_pic->i_planes = 3;
  767.             break;
  768.         default:
  769.             p_pic->p[Y_PLANE].i_pixel_pitch = 2;
  770.             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
  771.             p_pic->i_planes = 1;
  772.             break;
  773.         }
  774.     }
  775.     return VLC_SUCCESS;
  776. }
  777. /*****************************************************************************
  778.  * SetPalette: sets an 8 bpp palette
  779.  *****************************************************************************/
  780. static void SetPalette( vout_thread_t *p_vout,
  781.                         uint16_t *red, uint16_t *green, uint16_t *blue )
  782. {
  783.     SDL_Color colors[256];
  784.     int i;
  785.     /* Fill colors with color information */
  786.     for( i = 0; i < 256; i++ )
  787.     {
  788.         colors[ i ].r = red[ i ] >> 8;
  789.         colors[ i ].g = green[ i ] >> 8;
  790.         colors[ i ].b = blue[ i ] >> 8;
  791.     }
  792.     /* Set palette */
  793.     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
  794.     {
  795.         msg_Err( p_vout, "failed setting palette" );
  796.     }
  797. }