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