]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* ./configure.in: more modules are plugins.
[vlc] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: vout_sdl.c,v 1.83 2002/03/16 23:03:19 sam Exp $
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 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <videolan/vlc.h>
34
35 #include <sys/types.h>
36 #ifndef WIN32
37 #   include <netinet/in.h>                            /* BSD: struct in_addr */
38 #endif
39
40 #include SDL_INCLUDE_FILE
41
42 #include "netutils.h"
43
44 #include "video.h"
45 #include "video_output.h"
46
47 #include "interface.h"
48
49 #include "stream_control.h"                 /* needed by input_ext-intf.h... */
50 #include "input_ext-intf.h"
51
52 #define SDL_MAX_DIRECTBUFFERS 10
53 #define SDL_DEFAULT_BPP 16
54
55 /*****************************************************************************
56  * vout_sys_t: video output SDL method descriptor
57  *****************************************************************************
58  * This structure is part of the video output thread descriptor.
59  * It describes the SDL specific properties of an output thread.
60  *****************************************************************************/
61 typedef struct vout_sys_s
62 {
63     SDL_Surface *   p_display;                             /* display device */
64
65     int i_width;
66     int i_height;
67
68     /* For YUV output */
69     SDL_Overlay * p_overlay;   /* An overlay we keep to grab the XVideo port */
70
71     /* For RGB output */
72     int i_surfaces;
73
74     boolean_t   b_cursor;
75     boolean_t   b_cursor_autohidden;
76     mtime_t     i_lastmoved;
77
78 } vout_sys_t;
79
80 /*****************************************************************************
81  * picture_sys_t: direct buffer method descriptor
82  *****************************************************************************
83  * This structure is part of the picture descriptor, it describes the
84  * SDL specific properties of a direct buffer.
85  *****************************************************************************/
86 typedef struct picture_sys_s
87 {
88     SDL_Overlay *p_overlay;
89
90 } picture_sys_t;
91
92 /*****************************************************************************
93  * Seeking function TODO: put this in a generic location !
94  *****************************************************************************/
95 static __inline__ void vout_Seek( off_t i_seek )
96 {
97     off_t i_tell;
98
99     vlc_mutex_lock( &p_input_bank->lock );
100     if( p_input_bank->pp_input[0] != NULL )
101     {
102 #define S p_input_bank->pp_input[0]->stream
103         i_tell = S.p_selected_area->i_tell + i_seek * (off_t)50 * S.i_mux_rate;
104
105         i_tell = ( i_tell <= 0 /*S.p_selected_area->i_start*/ )
106                    ? 0 /*S.p_selected_area->i_start*/
107                    : ( i_tell >= S.p_selected_area->i_size )
108                        ? S.p_selected_area->i_size
109                        : i_tell;
110
111         input_Seek( p_input_bank->pp_input[0], i_tell );
112 #undef S
113     }
114     vlc_mutex_unlock( &p_input_bank->lock );
115 }
116
117 /*****************************************************************************
118  * Local prototypes.
119  *****************************************************************************/
120 static int  vout_Create     ( struct vout_thread_s * );
121 static int  vout_Init       ( struct vout_thread_s * );
122 static void vout_End        ( struct vout_thread_s * );
123 static void vout_Destroy    ( struct vout_thread_s * );
124 static int  vout_Manage     ( struct vout_thread_s * );
125 static void vout_Render     ( struct vout_thread_s *, struct picture_s * );
126 static void vout_Display    ( struct vout_thread_s *, struct picture_s * );
127
128 static int  SDLOpenDisplay      ( vout_thread_t *p_vout );
129 static void SDLCloseDisplay     ( vout_thread_t *p_vout );
130 static int  SDLNewPicture       ( vout_thread_t *p_vout, picture_t *p_pic );
131
132 /*****************************************************************************
133  * Functions exported as capabilities. They are declared as static so that
134  * we don't pollute the namespace too much.
135  *****************************************************************************/
136 void _M( vout_getfunctions )( function_list_t * p_function_list )
137 {
138     p_function_list->functions.vout.pf_create     = vout_Create;
139     p_function_list->functions.vout.pf_init       = vout_Init;
140     p_function_list->functions.vout.pf_end        = vout_End;
141     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
142     p_function_list->functions.vout.pf_manage     = vout_Manage;
143     p_function_list->functions.vout.pf_render     = vout_Render;
144     p_function_list->functions.vout.pf_display    = vout_Display;
145 }
146
147 /*****************************************************************************
148  * vout_Create: allocate SDL video thread output method
149  *****************************************************************************
150  * This function allocate and initialize a SDL vout method. It uses some of the
151  * vout properties to choose the correct mode, and change them according to the
152  * mode actually used.
153  *****************************************************************************/
154 static int vout_Create( vout_thread_t *p_vout )
155 {
156     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
157     {
158         return( 1 );
159     }
160
161     /* Allocate structure */
162     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
163     if( p_vout->p_sys == NULL )
164     {
165         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
166         return( 1 );
167     }
168
169     /* Initialize library */
170     if( SDL_Init( SDL_INIT_VIDEO
171 #ifndef WIN32
172     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
173                 | SDL_INIT_EVENTTHREAD
174 #endif
175 #ifdef DEBUG
176     /* In debug mode you may want vlc to dump a core instead of staying
177      * stuck */
178                 | SDL_INIT_NOPARACHUTE
179 #endif
180                 ) < 0 )
181     {
182         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
183         free( p_vout->p_sys );
184         return( 1 );
185     }
186
187     p_vout->p_sys->b_cursor = 1;
188     p_vout->p_sys->b_cursor_autohidden = 0;
189     p_vout->p_sys->i_lastmoved = mdate();
190
191     if( p_vout->render.i_height * p_vout->render.i_aspect
192          >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
193     {
194         p_vout->p_sys->i_width = p_vout->render.i_height
195             * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
196         p_vout->p_sys->i_height = p_vout->render.i_height;
197     }
198     else
199     {
200         p_vout->p_sys->i_width = p_vout->render.i_width;
201         p_vout->p_sys->i_height = p_vout->render.i_width
202             * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
203     }
204
205 #if 0
206     if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
207     {
208         p_vout->p_sys->i_width <<= 1;
209         p_vout->p_sys->i_height <<= 1;
210     }
211     else if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 400 )
212     {
213         p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
214         p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
215     }
216 #endif
217
218     if( SDLOpenDisplay( p_vout ) )
219     {
220         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
221         SDL_QuitSubSystem( SDL_INIT_VIDEO );
222         free( p_vout->p_sys );
223         return( 1 );
224     }
225
226     return( 0 );
227 }
228
229 /*****************************************************************************
230  * vout_Init: initialize SDL video thread output method
231  *****************************************************************************
232  * This function initialize the SDL display device.
233  *****************************************************************************/
234 static int vout_Init( vout_thread_t *p_vout )
235 {
236     int i_index;
237     picture_t *p_pic;
238
239     p_vout->p_sys->i_surfaces = 0;
240
241     I_OUTPUTPICTURES = 0;
242
243     /* Initialize the output structure */
244     if( p_vout->p_sys->p_overlay == NULL )
245     {
246         /* All we have is an RGB image with square pixels */
247         p_vout->output.i_width  = p_vout->p_sys->i_width;
248         p_vout->output.i_height = p_vout->p_sys->i_height;
249         p_vout->output.i_aspect = p_vout->p_sys->i_width
250                                    * VOUT_ASPECT_FACTOR
251                                    / p_vout->p_sys->i_height;
252     }
253     else
254     {
255         /* We may need to convert the chroma, but at least we keep the
256          * aspect ratio */
257         p_vout->output.i_width  = p_vout->render.i_width;
258         p_vout->output.i_height = p_vout->render.i_height;
259         p_vout->output.i_aspect = p_vout->render.i_aspect;
260     }
261
262     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
263     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
264     {
265         p_pic = NULL;
266
267         /* Find an empty picture slot */
268         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
269         {
270             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
271             {
272                 p_pic = p_vout->p_picture + i_index;
273                 break;
274             }
275         }
276
277         /* Allocate the picture if we found one */
278         if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
279         {
280             break;
281         }
282
283         p_pic->i_status = DESTROYED_PICTURE;
284         p_pic->i_type   = DIRECT_PICTURE;
285
286         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
287
288         I_OUTPUTPICTURES++;
289     }
290
291     return( 0 );
292 }
293
294 /*****************************************************************************
295  * vout_End: terminate Sys video thread output method
296  *****************************************************************************
297  * Terminate an output method created by vout_SDLCreate
298  *****************************************************************************/
299 static void vout_End( vout_thread_t *p_vout )
300 {
301     int i_index;
302
303     /* Free the output buffers we allocated */
304     for( i_index = I_OUTPUTPICTURES ; i_index ; )
305     {
306         i_index--;
307         if( p_vout->p_sys->p_overlay == NULL )
308         {
309             /* RGB picture */
310         }
311         else
312         {
313             SDL_UnlockYUVOverlay(
314                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
315             SDL_FreeYUVOverlay(
316                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
317         }
318         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
319     }
320 }
321
322 /*****************************************************************************
323  * vout_Destroy: destroy Sys video thread output method
324  *****************************************************************************
325  * Terminate an output method created by vout_SDLCreate
326  *****************************************************************************/
327 static void vout_Destroy( vout_thread_t *p_vout )
328 {
329     SDLCloseDisplay( p_vout );
330
331     SDL_QuitSubSystem( SDL_INIT_VIDEO );
332
333     free( p_vout->p_sys );
334 }
335
336 /*****************************************************************************
337  * vout_Manage: handle Sys events
338  *****************************************************************************
339  * This function should be called regularly by video output thread. It returns
340  * a non null value if an error occured.
341  *****************************************************************************/
342 static int vout_Manage( vout_thread_t *p_vout )
343 {
344     SDL_Event event;                                            /* SDL event */
345
346     /* Process events */
347     while( SDL_PollEvent(&event) )
348     {
349         switch( event.type )
350         {
351         case SDL_VIDEORESIZE:                          /* Resizing of window */
352             p_vout->p_sys->i_width = event.resize.w;
353             p_vout->p_sys->i_height = event.resize.h;
354             SDLCloseDisplay( p_vout );
355             SDLOpenDisplay( p_vout );
356             break;
357
358         case SDL_MOUSEMOTION:
359             if( p_vout->p_sys->b_cursor &&
360                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
361             {
362                 if( p_vout->p_sys->b_cursor_autohidden )
363                 {
364                     p_vout->p_sys->b_cursor_autohidden = 0;
365                     SDL_ShowCursor( 1 );
366                 }
367                 else
368                 {
369                     p_vout->p_sys->i_lastmoved = mdate();
370                 }
371             }
372             break;
373
374         case SDL_MOUSEBUTTONUP:
375             switch( event.button.button )
376             {
377             case SDL_BUTTON_RIGHT:
378                 p_main->p_intf->b_menu_change = 1;
379                 break;
380             }
381             break;
382
383         case SDL_MOUSEBUTTONDOWN:
384             switch( event.button.button )
385             {
386             case SDL_BUTTON_LEFT:
387                 /* In this part we will eventually manage
388                  * clicks for DVD navigation for instance. For the
389                  * moment just pause the stream. */
390                 input_SetStatus( p_input_bank->pp_input[0],
391                                  INPUT_STATUS_PAUSE );
392                 break;
393
394             case 4:
395                 vout_Seek( 15 );
396                 break;
397
398             case 5:
399                 vout_Seek( -15 );
400                 break;
401             }
402             break;
403
404         case SDL_QUIT:
405             p_main->p_intf->b_die = 1;
406             break;
407
408         case SDL_KEYDOWN:                             /* if a key is pressed */
409
410             switch( event.key.keysym.sym )
411             {
412             case SDLK_q:                                             /* quit */
413             case SDLK_ESCAPE:
414                 p_main->p_intf->b_die = 1;
415                 break;
416
417             case SDLK_f:                             /* switch to fullscreen */
418                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
419                 break;
420
421             case SDLK_c:                                 /* toggle grayscale */
422                 p_vout->b_grayscale = ! p_vout->b_grayscale;
423                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
424                 break;
425
426             case SDLK_i:                                      /* toggle info */
427                 p_vout->b_info = ! p_vout->b_info;
428                 p_vout->i_changes |= VOUT_INFO_CHANGE;
429                 break;
430
431             case SDLK_s:                                   /* toggle scaling */
432                 p_vout->b_scale = ! p_vout->b_scale;
433                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
434                 break;
435
436             case SDLK_SPACE:                             /* toggle interface */
437                 p_vout->b_interface = ! p_vout->b_interface;
438                 p_vout->i_changes |= VOUT_INTF_CHANGE;
439                 break;
440             
441             case SDLK_MENU:
442                 p_main->p_intf->b_menu_change = 1;
443                 break;
444
445             case SDLK_LEFT:
446                 vout_Seek( -5 );
447                 break;
448
449             case SDLK_RIGHT:
450                 vout_Seek( 5 );
451                 break;
452
453             case SDLK_UP:
454                 vout_Seek( 60 );
455                 break;
456
457             case SDLK_DOWN:
458                 vout_Seek( -60 );
459                 break;
460
461             case SDLK_F10: network_ChannelJoin( 0 ); break;
462             case SDLK_F1:  network_ChannelJoin( 1 ); break;
463             case SDLK_F2:  network_ChannelJoin( 2 ); break;
464             case SDLK_F3:  network_ChannelJoin( 3 ); break;
465             case SDLK_F4:  network_ChannelJoin( 4 ); break;
466             case SDLK_F5:  network_ChannelJoin( 5 ); break;
467             case SDLK_F6:  network_ChannelJoin( 6 ); break;
468             case SDLK_F7:  network_ChannelJoin( 7 ); break;
469             case SDLK_F8:  network_ChannelJoin( 8 ); break;
470             case SDLK_F9:  network_ChannelJoin( 9 ); break;
471
472             default:
473                 break;
474             }
475             break;
476
477         default:
478             break;
479         }
480     }
481
482     /* Fullscreen change */
483     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
484     {
485         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
486
487         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
488
489         p_vout->p_sys->b_cursor_autohidden = 0;
490         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
491                         ! p_vout->p_sys->b_cursor_autohidden );
492
493         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
494     }
495
496     /* Pointer change */
497     if( ! p_vout->p_sys->b_cursor_autohidden &&
498         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
499     {
500         /* Hide the mouse automatically */
501         p_vout->p_sys->b_cursor_autohidden = 1;
502         SDL_ShowCursor( 0 );
503     }
504
505     return( 0 );
506 }
507
508 /*****************************************************************************
509  * vout_Render: render previously calculated output
510  *****************************************************************************/
511 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
512 {
513     ;
514 }
515
516 /*****************************************************************************
517  * vout_Display: displays previously rendered output
518  *****************************************************************************
519  * This function sends the currently rendered image to the display.
520  *****************************************************************************/
521 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
522 {
523     int x, y, w, h;
524     SDL_Rect disp;
525
526     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
527                        &x, &y, &w, &h );
528     disp.x = x;
529     disp.y = y;
530     disp.w = w;
531     disp.h = h;
532
533     if( p_vout->p_sys->p_overlay == NULL )
534     {
535         /* RGB picture */
536         SDL_Flip( p_vout->p_sys->p_display );
537     }
538     else
539     {
540         /* Overlay picture */
541         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
542         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
543         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
544     }
545 }
546
547 /* following functions are local */
548
549 /*****************************************************************************
550  * SDLOpenDisplay: open and initialize SDL device
551  *****************************************************************************
552  * Open and initialize display according to preferences specified in the vout
553  * thread fields.
554  *****************************************************************************/
555 static int SDLOpenDisplay( vout_thread_t *p_vout )
556 {
557     Uint32 i_flags;
558     int    i_bpp;
559
560     /* Initialize flags and cursor */
561     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
562     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
563
564     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
565                              SDL_DEFAULT_BPP, i_flags );
566     if( i_bpp == 0 )
567     {
568         intf_ErrMsg( "vout error: no video mode available" );
569         return( 1 );
570     }
571
572     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
573                                                  p_vout->p_sys->i_height,
574                                                  i_bpp, i_flags );
575
576     if( p_vout->p_sys->p_display == NULL )
577     {
578         intf_ErrMsg( "vout error: cannot set video mode" );
579         return( 1 );
580     }
581
582     SDL_LockSurface( p_vout->p_sys->p_display );
583
584     /* Choose the chroma we will try first. */
585     switch( p_vout->render.i_chroma )
586     {
587         case FOURCC_YUY2:
588         case FOURCC_YUNV:
589             p_vout->output.i_chroma = SDL_YUY2_OVERLAY;
590             break;
591         case FOURCC_UYVY:
592         case FOURCC_UYNV:
593         case FOURCC_Y422:
594             p_vout->output.i_chroma = SDL_UYVY_OVERLAY;
595             break;
596         case FOURCC_YVYU:
597             p_vout->output.i_chroma = SDL_YVYU_OVERLAY;
598             break;
599         case FOURCC_YV12:
600         case FOURCC_I420:
601         case FOURCC_IYUV:
602         default:
603             p_vout->output.i_chroma = SDL_YV12_OVERLAY;
604             break;
605     }
606
607     p_vout->p_sys->p_overlay =
608         SDL_CreateYUVOverlay( 32, 32, p_vout->output.i_chroma,
609                               p_vout->p_sys->p_display );
610
611     /* FIXME: if the first overlay we find is software, don't stop,
612      * because we may find a hardware one later ... */
613
614     /* If this best choice failed, fall back to other chromas */
615     if( p_vout->p_sys->p_overlay == NULL )
616     {
617         p_vout->output.i_chroma = SDL_IYUV_OVERLAY;
618         p_vout->p_sys->p_overlay =
619             SDL_CreateYUVOverlay( 32, 32, p_vout->output.i_chroma,
620                                   p_vout->p_sys->p_display );
621     }
622
623     if( p_vout->p_sys->p_overlay == NULL )
624     {
625         p_vout->output.i_chroma = SDL_YV12_OVERLAY;
626         p_vout->p_sys->p_overlay =
627             SDL_CreateYUVOverlay( 32, 32, p_vout->output.i_chroma,
628                                   p_vout->p_sys->p_display );
629     }
630
631     if( p_vout->p_sys->p_overlay == NULL )
632     {
633         p_vout->output.i_chroma = SDL_YUY2_OVERLAY;
634         p_vout->p_sys->p_overlay =
635             SDL_CreateYUVOverlay( 32, 32, p_vout->output.i_chroma,
636                                   p_vout->p_sys->p_display );
637     }
638
639     if( p_vout->p_sys->p_overlay == NULL )
640     {
641         intf_WarnMsg( 3, "vout warning: no SDL overlay for 0x%.8x (%4.4s)",
642                          p_vout->render.i_chroma,
643                          (char*)&p_vout->render.i_chroma );
644
645         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
646         {
647             case 8:
648                 p_vout->output.i_chroma = FOURCC_RGB;
649                 break;
650             case 15:
651                 p_vout->output.i_chroma = FOURCC_RV15;
652                 break;
653             case 16:
654                 p_vout->output.i_chroma = FOURCC_RV16;
655                 break;
656             case 24:
657                 p_vout->output.i_chroma = FOURCC_RV24;
658                 break;
659             case 32:
660                 p_vout->output.i_chroma = FOURCC_RV32;
661                 break;
662             default:
663                 intf_ErrMsg( "vout error: unknown screen depth" );
664                 SDL_UnlockSurface( p_vout->p_sys->p_display );
665                 SDL_FreeSurface( p_vout->p_sys->p_display );
666                 return( -1 );
667         }
668
669         p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
670         p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
671         p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
672
673         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
674                            VOUT_TITLE " (software RGB SDL output)" );
675     }
676     else
677     {
678         if( p_vout->p_sys->p_overlay->hw_overlay )
679         {
680             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
681                                VOUT_TITLE " (hardware YUV SDL output)" );
682         }
683         else
684         {
685             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
686                                VOUT_TITLE " (software YUV SDL output)" );
687         }
688     }
689
690     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
691
692     return( 0 );
693 }
694
695 /*****************************************************************************
696  * SDLCloseDisplay: close and reset SDL device
697  *****************************************************************************
698  * This function returns all resources allocated by SDLOpenDisplay and restore
699  * the original state of the device.
700  *****************************************************************************/
701 static void SDLCloseDisplay( vout_thread_t *p_vout )
702 {
703     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
704     SDL_UnlockSurface ( p_vout->p_sys->p_display );
705     SDL_FreeSurface( p_vout->p_sys->p_display );
706 }
707
708 /*****************************************************************************
709  * SDLNewPicture: allocate a picture
710  *****************************************************************************
711  * Returns 0 on success, -1 otherwise
712  *****************************************************************************/
713 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
714 {
715     int i_width  = p_vout->output.i_width;
716     int i_height = p_vout->output.i_height;
717
718     if( p_vout->p_sys->p_overlay == NULL )
719     {
720         /* RGB picture */
721         if( p_vout->p_sys->i_surfaces )
722         {
723             /* We already allocated this surface, return */
724             return -1;
725         }
726
727         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
728
729         if( p_pic->p_sys == NULL )
730         {
731             return -1;
732         }
733
734         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
735         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
736         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
737         p_pic->p->i_pixel_bytes = 2;
738
739         if( p_pic->p->i_pitch == 2 * p_vout->p_sys->p_display->w )
740         {
741             p_pic->p->b_margin = 0;
742         }
743         else
744         {
745             p_pic->p->b_margin = 1;
746             p_pic->p->b_hidden = 1;
747             p_pic->p->i_visible_bytes = 2 * p_vout->p_sys->p_display->w;
748         }
749
750         p_vout->p_sys->i_surfaces++;
751
752         p_pic->i_planes = 1;
753     }
754     else
755     {
756         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
757
758         if( p_pic->p_sys == NULL )
759         {
760             return -1;
761         }
762
763         p_pic->p_sys->p_overlay =
764             SDL_CreateYUVOverlay( i_width, i_height,
765                                   p_vout->output.i_chroma,
766                                   p_vout->p_sys->p_display );
767
768         if( p_pic->p_sys->p_overlay == NULL )
769         {
770             free( p_pic->p_sys );
771             return -1;
772         }
773
774         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
775
776         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
777         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
778         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
779
780         switch( p_vout->output.i_chroma )
781         {
782         case SDL_YV12_OVERLAY:
783             p_pic->p[Y_PLANE].i_pixel_bytes = 1;
784             p_pic->p[Y_PLANE].b_margin = 0;
785
786             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
787             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
788             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
789             p_pic->p[U_PLANE].i_pixel_bytes = 1;
790             p_pic->p[U_PLANE].b_margin = 0;
791
792             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
793             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
794             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
795             p_pic->p[V_PLANE].i_pixel_bytes = 1;
796             p_pic->p[V_PLANE].b_margin = 0;
797
798             p_pic->i_planes = 3;
799             break;
800
801         case SDL_IYUV_OVERLAY:
802             p_pic->p[Y_PLANE].i_pixel_bytes = 1;
803             p_pic->p[Y_PLANE].b_margin = 0;
804
805             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
806             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
807             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
808             p_pic->p[U_PLANE].i_pixel_bytes = 1;
809             p_pic->p[U_PLANE].b_margin = 0;
810
811             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
812             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
813             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
814             p_pic->p[V_PLANE].i_pixel_bytes = 1;
815             p_pic->p[V_PLANE].b_margin = 0;
816
817             p_pic->i_planes = 3;
818             break;
819
820         default:
821             p_pic->p[Y_PLANE].i_pixel_bytes = 2;
822             p_pic->p[Y_PLANE].b_margin = 0;
823
824             p_pic->i_planes = 1;
825             break;
826         }
827     }
828
829     return 0;
830 }
831