]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
- fixed pitch bug;
[vlc] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <SDL/SDL.h>
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "video.h"
41 #include "video_output.h"
42
43 #include "intf_msg.h"
44
45 /*****************************************************************************
46  * vout_sys_t: video output SDL method descriptor
47  *****************************************************************************
48  * This structure is part of the video output thread descriptor.
49  * It describes the SDL specific properties of an output thread.
50  *****************************************************************************/
51 typedef struct vout_sys_s
52 {
53     int i_width;
54     int i_height;
55     SDL_Surface *   p_display;                             /* display device */
56     SDL_Overlay *   p_overlay;                             /* overlay device */
57     boolean_t   b_fullscreen;
58     boolean_t   b_reopen_display;
59     Uint8   *   p_buffer[2];
60                                                      /* Buffers informations */
61     boolean_t   b_must_acquire;           /* must be acquired before writing */
62 }   vout_sys_t;
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int     SDLOpenDisplay   ( vout_thread_t *p_vout );
68 static void    SDLCloseDisplay  ( vout_thread_t *p_vout );
69
70 /*****************************************************************************
71  * vout_SDLCreate: allocate SDL video thread output method
72  *****************************************************************************
73  * This function allocate and initialize a SDL vout method. It uses some of the
74  * vout properties to choose the correct mode, and change them according to the
75  * mode actually used.
76  *****************************************************************************/
77 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
78                     int i_root_window, void *p_data )
79 {
80     /* Allocate structure */
81     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
82     if( p_vout->p_sys == NULL )
83     {
84         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
85         return( 1 );
86     }
87
88     p_vout->p_sys->p_display = NULL;
89     p_vout->p_sys->p_overlay = NULL;
90     p_vout->p_sys->b_must_acquire = 0;
91
92     /* Initialize library */
93     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
94     {
95         intf_ErrMsg( "error: can't initialize SDL library: %s",
96                      SDL_GetError() );
97         free( p_vout->p_sys );
98         return( 1 );
99     }
100
101     /* Force the software yuv even if it is not used */
102     /* If we don't do this, p_vout is not correctly initialized
103        and it's impossible to switch between soft/hard yuv */
104     p_vout->b_need_render = 1;
105
106     if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
107     {
108         p_vout->p_sys->b_fullscreen = 1;
109     }
110     else
111     {
112         p_vout->p_sys->b_fullscreen = 0;
113     }
114
115     p_vout->p_sys->i_width = VOUT_WIDTH_DEFAULT;
116     p_vout->p_sys->i_height = VOUT_HEIGHT_DEFAULT;
117
118     p_vout->p_sys->b_reopen_display = 0;
119
120     if( SDLOpenDisplay(p_vout) )
121     {
122         intf_ErrMsg( "error: can't initialize SDL library: %s",
123                      SDL_GetError() );
124         free( p_vout->p_sys );
125         return( 1 );
126     }
127
128     return( 0 );
129 }
130
131 /*****************************************************************************
132  * vout_SDLInit: initialize SDL video thread output method
133  *****************************************************************************
134  * This function initialize the SDL display device.
135  *****************************************************************************/
136 int vout_SDLInit( vout_thread_t *p_vout )
137 {
138     /* Acquire first buffer */
139     if( p_vout->p_sys->b_must_acquire )
140     {
141         SDL_LockSurface(p_vout->p_sys->p_display);
142     }
143
144     return( 0 );
145 }
146
147 /*****************************************************************************
148  * vout_SDLEnd: terminate Sys video thread output method
149  *****************************************************************************
150  * Terminate an output method created by vout_SDLCreate
151  *****************************************************************************/
152 void vout_SDLEnd( vout_thread_t *p_vout )
153 {
154     /* Release buffer */
155     if( p_vout->p_sys->b_must_acquire )
156     {
157         SDL_UnlockSurface ( p_vout->p_sys->p_display );
158     }
159     free( p_vout->p_sys );
160 }
161
162 /*****************************************************************************
163  * vout_SDLDestroy: destroy Sys video thread output method
164  *****************************************************************************
165  * Terminate an output method created by vout_SDLCreate
166  *****************************************************************************/
167 void vout_SDLDestroy( vout_thread_t *p_vout )
168 {
169     SDLCloseDisplay( p_vout );
170     free( p_vout->p_sys );
171 }
172
173 /*****************************************************************************
174  * vout_SDLManage: handle Sys events
175  *****************************************************************************
176  * This function should be called regularly by video output thread. It returns
177  * a non null value if an error occured.
178  *****************************************************************************/
179 int vout_SDLManage( vout_thread_t *p_vout )
180 {
181     /* If the display has to be reopened we do so */
182     if( p_vout->p_sys->b_reopen_display )
183     {
184         p_vout->p_sys->b_must_acquire = 0;
185         if( p_vout->p_sys->p_display )
186         {
187             if( p_vout->p_sys->p_overlay )
188             {
189                 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
190                 p_vout->p_sys->p_overlay = NULL;
191             }
192             SDL_FreeSurface( p_vout->p_sys->p_display );
193             p_vout->p_sys->p_display = NULL;
194         }
195
196         if( SDLOpenDisplay(p_vout) )
197         {
198             intf_ErrMsg( "error: can't open DISPLAY default display" );
199             return( 1 );
200         }
201         p_vout->p_sys->b_reopen_display = 0;
202     }
203     return( 0 );
204 }
205
206 /*****************************************************************************
207  * vout_SDLDisplay: displays previously rendered output
208  *****************************************************************************
209  * This function send the currently rendered image to the display, wait until
210  * it is displayed and switch the two rendering buffer, preparing next frame.
211  *****************************************************************************/
212 void vout_SDLDisplay( vout_thread_t *p_vout )
213 {
214     SDL_Rect    disp;
215     if(p_vout->b_need_render)
216     {  
217         /* Change display frame */
218         if( p_vout->p_sys->b_must_acquire )
219         {
220             
221             SDL_Flip( p_vout->p_sys->p_display );
222             
223             /* Swap buffers and change write frame */
224             SDL_LockSurface ( p_vout->p_sys->p_display );
225         }
226      }
227      else
228      {
229         
230         /*
231          * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to render 
232          */
233         if( p_vout->p_sys->b_must_acquire )
234         {
235             /* TODO: support for streams other than 4:2:0 */
236             /* create the overlay if necessary */
237             if( p_vout->p_sys->p_overlay == NULL )
238             {
239                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
240                                              p_vout->p_rendered_pic->i_width, 
241                                              p_vout->p_rendered_pic->i_height,
242                                              SDL_YV12_OVERLAY, 
243                                              p_vout->p_sys->p_display
244                                            );
245                 intf_Msg("[YUV acceleration] : %d,",
246                             p_vout->p_sys->p_overlay->hw_overlay); 
247             }
248
249             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
250             /* copy the data into video buffers */
251             /* Y first */
252             memcpy(p_vout->p_sys->p_overlay->pixels[0],
253                    p_vout->p_rendered_pic->p_y,
254                    p_vout->p_sys->p_overlay->h *
255                    p_vout->p_sys->p_overlay->pitches[0]);
256             /* then V */
257             memcpy(p_vout->p_sys->p_overlay->pixels[1],
258                    p_vout->p_rendered_pic->p_v,
259                    p_vout->p_sys->p_overlay->h *
260                    p_vout->p_sys->p_overlay->pitches[1] / 2);
261             /* and U */
262             memcpy(p_vout->p_sys->p_overlay->pixels[2],
263                    p_vout->p_rendered_pic->p_u,
264                    p_vout->p_sys->p_overlay->h *
265                    p_vout->p_sys->p_overlay->pitches[2] / 2);
266
267             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
268             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
269             disp.x = (p_vout->i_width - disp.w)/2;
270             disp.y = (p_vout->i_height - disp.h)/2;
271
272             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
273             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
274         }
275     }
276 }
277
278 /* following functions are local */
279
280 /*****************************************************************************
281  * SDLOpenDisplay: open and initialize SDL device
282  *****************************************************************************
283  * Open and initialize display according to preferences specified in the vout
284  * thread fields.
285  *****************************************************************************/
286 static int SDLOpenDisplay( vout_thread_t *p_vout )
287 {
288     SDL_Rect    clipping_rect;
289     Uint32      flags;
290     int bpp;
291     /* Open display 
292      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
293      */
294
295     /* init flags and cursor */
296     flags = SDL_ANYFORMAT;
297
298     if( p_vout->p_sys->b_fullscreen )
299         flags |= SDL_FULLSCREEN;
300     else
301         flags |= SDL_RESIZABLE;
302
303     if( p_vout->b_need_render )
304         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
305     else
306         flags |= SDL_SWSURFACE; /* save video memory */
307
308     bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
309                           p_vout->p_sys->i_height,
310                           p_vout->i_screen_depth, flags);
311
312     if(bpp == 0)
313     {
314         intf_ErrMsg( "error: can't open DISPLAY default display" );
315         return( 1 );
316     }
317
318     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
319                                                 p_vout->p_sys->i_height,
320                                                 bpp, flags);
321
322     if( p_vout->p_sys->p_display == NULL )
323     {
324         intf_ErrMsg( "error: can't open DISPLAY default display" );
325         return( 1 );
326     }
327
328     if( p_vout->p_sys->b_fullscreen )
329         SDL_ShowCursor( 0 );
330     else
331         SDL_ShowCursor( 1 );
332
333     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
334     SDL_EventState(SDL_KEYUP , SDL_IGNORE);     /* ignore keys up */
335
336     if( p_vout->b_need_render )
337     {
338         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
339         SDL_Flip(p_vout->p_sys->p_display);
340         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
341         SDL_Flip(p_vout->p_sys->p_display);
342
343         /* Set clipping for text */
344         clipping_rect.x = 0;
345         clipping_rect.y = 0;
346         clipping_rect.w = p_vout->p_sys->p_display->w;
347         clipping_rect.h = p_vout->p_sys->p_display->h;
348         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
349
350         /* Set thread information */
351         p_vout->i_width =           p_vout->p_sys->p_display->w;
352         p_vout->i_height =          p_vout->p_sys->p_display->h;
353         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
354
355         p_vout->i_screen_depth =
356             p_vout->p_sys->p_display->format->BitsPerPixel;
357         p_vout->i_bytes_per_pixel =
358             p_vout->p_sys->p_display->format->BytesPerPixel;
359
360         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
361         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
362         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
363
364         /* FIXME: palette in 8bpp ?? */
365         /* Set and initialize buffers */
366         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
367                                  p_vout->p_sys->p_buffer[ 1 ] );
368     }
369     else
370     {
371         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
372         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
373
374         /* Set thread information */
375         p_vout->i_width =           p_vout->p_sys->p_display->w;
376         p_vout->i_height =          p_vout->p_sys->p_display->h;
377         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
378
379         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
380                                  p_vout->p_sys->p_buffer[ 1 ] );
381     }
382
383     p_vout->i_changes |= VOUT_YUV_CHANGE;
384
385     /* Check buffers properties */      
386     p_vout->p_sys->b_must_acquire = 1;          /* always acquire */
387
388     return( 0 );
389 }
390
391 /*****************************************************************************
392  * SDLCloseDisplay: close and reset SDL device
393  *****************************************************************************
394  * This function returns all resources allocated by SDLOpenDisplay and restore
395  * the original state of the device.
396  *****************************************************************************/
397 static void SDLCloseDisplay( vout_thread_t *p_vout )
398 {
399     if( p_vout->p_sys->p_display )
400     {
401         p_vout->p_sys->b_must_acquire = 0;
402         if( p_vout->p_sys->p_overlay )
403         {
404             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
405             p_vout->p_sys->p_overlay = NULL;
406         }
407         SDL_UnlockSurface(p_vout->p_sys->p_display);
408         SDL_FreeSurface( p_vout->p_sys->p_display );
409         p_vout->p_sys->p_display = NULL;
410     }
411
412     SDL_Quit();
413 }
414