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