vout_sdl.c 18.1 KB
Newer Older
Pierre Baillet's avatar
Pierre Baillet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*****************************************************************************
 * vout_sdl.c: SDL video output display method
 *****************************************************************************
 * Copyright (C) 1998, 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

#include <SDL/SDL.h>

#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
#include "plugins.h"

#include "video.h"
#include "video_output.h"

#include "intf_msg.h"
44
#include "main.h"
Pierre Baillet's avatar
Pierre Baillet committed
45
46
47
48
49
50
51

/*****************************************************************************
 * vout_sys_t: video output SDL method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the SDL specific properties of an output thread.
 *****************************************************************************/
52
/* FIXME: SOME CLUELESS MORON DEFINED THIS STRUCTURE IN INTF_SDL.C AS WELL   */
Pierre Baillet's avatar
Pierre Baillet committed
53
54
typedef struct vout_sys_s
{
55
56
    int i_width;
    int i_height;
Pierre Baillet's avatar
Pierre Baillet committed
57
    SDL_Surface *   p_display;                             /* display device */
58
59
    SDL_Overlay *   p_overlay;                             /* overlay device */
    boolean_t   b_fullscreen;
Sam Hocevar's avatar
   
Sam Hocevar committed
60
    boolean_t   b_overlay;
61
    boolean_t   b_cursor;
62
    boolean_t   b_reopen_display;
Pierre Baillet's avatar
Pierre Baillet committed
63
64
65
66
67
68
69
    Uint8   *   p_buffer[2];
                                                     /* Buffers informations */
}   vout_sys_t;

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
70
71
72
static int     SDLOpenDisplay       ( vout_thread_t *p_vout );
static void    SDLCloseDisplay      ( vout_thread_t *p_vout );
static void    SDLToggleFullScreen  ( vout_thread_t *p_vout );
73
static void    SDLTogglePointer     ( vout_thread_t *p_vout );
Pierre Baillet's avatar
Pierre Baillet committed
74
75
76
77
78
79
80
/*****************************************************************************
 * vout_SDLCreate: allocate SDL video thread output method
 *****************************************************************************
 * This function allocate and initialize a SDL vout method. It uses some of the
 * vout properties to choose the correct mode, and change them according to the
 * mode actually used.
 *****************************************************************************/
81
82
int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
                    int i_root_window, void *p_data )
Pierre Baillet's avatar
Pierre Baillet committed
83
84
85
86
87
{
    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
88
        intf_ErrMsg( "error: %s", strerror(ENOMEM) );
Pierre Baillet's avatar
Pierre Baillet committed
89
90
91
        return( 1 );
    }

92
93
94
95
    p_vout->p_sys->p_display = NULL;
    p_vout->p_sys->p_overlay = NULL;

    /* Initialize library */
96
97
    if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
            < 0 )
Pierre Baillet's avatar
Pierre Baillet committed
98
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
99
        intf_ErrMsg( "error: can't initialize SDL library: %s",
100
                     SDL_GetError() );
Pierre Baillet's avatar
Pierre Baillet committed
101
102
103
        free( p_vout->p_sys );
        return( 1 );
    }
104
105
106
107
108
109

    /* Force the software yuv even if it is not used */
    /* If we don't do this, p_vout is not correctly initialized
       and it's impossible to switch between soft/hard yuv */
    p_vout->b_need_render = 1;

Sam Hocevar's avatar
   
Sam Hocevar committed
110
111
112
113
    p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
                                VOUT_FULLSCREEN_DEFAULT );
    p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
                                VOUT_OVERLAY_DEFAULT );
114
115
116
117
    p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, 
                                VOUT_WIDTH_DEFAULT );
    p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
                                VOUT_HEIGHT_DEFAULT );
118

119
120
    p_vout->p_sys->b_cursor = 0 ;   // TODO should be done with a main_GetInt..
    
121
122
123
124
    if( SDLOpenDisplay(p_vout) )
    {
      intf_ErrMsg( "error: can't initialize SDL library: %s",
                   SDL_GetError() );
125
      free( p_vout->p_sys );
126
127
128
      return( 1 );
    }

Pierre Baillet's avatar
Pierre Baillet committed
129
    return( 0 );
130
}
Pierre Baillet's avatar
Pierre Baillet committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

/*****************************************************************************
 * vout_SDLInit: initialize SDL video thread output method
 *****************************************************************************
 * This function initialize the SDL display device.
 *****************************************************************************/
int vout_SDLInit( vout_thread_t *p_vout )
{
    return( 0 );
}

/*****************************************************************************
 * vout_SDLEnd: terminate Sys video thread output method
 *****************************************************************************
 * Terminate an output method created by vout_SDLCreate
 *****************************************************************************/
void vout_SDLEnd( vout_thread_t *p_vout )
{
149
150
    SDLCloseDisplay( p_vout );
    SDL_Quit();
Pierre Baillet's avatar
Pierre Baillet committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
}

/*****************************************************************************
 * vout_SDLDestroy: destroy Sys video thread output method
 *****************************************************************************
 * Terminate an output method created by vout_SDLCreate
 *****************************************************************************/
void vout_SDLDestroy( vout_thread_t *p_vout )
{
    free( p_vout->p_sys );
}

/*****************************************************************************
 * vout_SDLManage: handle Sys events
 *****************************************************************************
 * This function should be called regularly by video output thread. It returns
 * a non null value if an error occured.
 *****************************************************************************/
int vout_SDLManage( vout_thread_t *p_vout )
{
171
172
173
174
175

    /*
     * Size Change 
     */
    if ( p_vout->i_changes & VOUT_SIZE_CHANGE )
176
    {
177
178
        p_vout->p_sys->i_width = p_vout->i_width;
        p_vout->p_sys->i_height = p_vout->i_height;
179

180
181
182
        /* Need to reopen display */
        SDLCloseDisplay( p_vout );
        if ( SDLOpenDisplay( p_vout ) )
183
        {
184
185
          intf_ErrMsg( "error: can't open DISPLAY default display" );
          return( 1 );
186
        }
187
        p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
188
    }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    
    /*
     * YUV Change 
     */
    if ( p_vout->i_changes & VOUT_YUV_CHANGE )
    {
        p_vout->b_need_render = 1 - p_vout->b_need_render;
        
        /* Need to reopen display */
        SDLCloseDisplay( p_vout );
        if ( SDLOpenDisplay( p_vout ) )
        {
          intf_ErrMsg( "error: can't open DISPLAY default display" );
          return( 1 );
        }
        p_vout->i_changes &= ~VOUT_YUV_CHANGE;
    }
    
    /*
     * Fullscreen change
     */
    if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
211
    {
212
213
214
        p_vout->p_sys->b_fullscreen = 1 - p_vout->p_sys->b_fullscreen;
        SDLToggleFullScreen( p_vout );
        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
215
    }
216

217
    
218
219
220
221
222

    /*
     * Pointer change
     */
    if ( p_vout->i_changes & VOUT_CURSOR_CHANGE )
223
    {
224
225
        p_vout->p_sys->b_cursor = 1 - p_vout->p_sys->b_cursor;
        SDLTogglePointer( p_vout );
226
227
    }
    
Pierre Baillet's avatar
Pierre Baillet committed
228
229
    return( 0 );
}
Sam Hocevar's avatar
   
Sam Hocevar committed
230

231
232
233
234
235
236
237
/*****************************************************************************
 * vout_SDLSetPalette: sets an 8 bpp palette
 *****************************************************************************
 * This function sets the palette given as an argument. It does not return
 * anything, but could later send information on which colors it was unable
 * to set.
 *****************************************************************************/
238
239
void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
                         u16 *blue, u16 *transp)
240
241
242
243
244
245
{
     /* Create a display surface with a grayscale palette */
    SDL_Color colors[256];
    int i;
  
    /* Fill colors with color information */
Sam Hocevar's avatar
   
Sam Hocevar committed
246
247
248
249
250
    for( i = 0; i < 256; i++ )
    {
        colors[ i ].r = red[ i ] >> 8;
        colors[ i ].g = green[ i ] >> 8;
        colors[ i ].b = blue[ i ] >> 8;
251
252
253
    }
    
    /* Set palette */
Sam Hocevar's avatar
   
Sam Hocevar committed
254
255
256
257
    if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
    {
        intf_ErrMsg( "vout error: failed setting palette\n" );
    }
258
259
260

}

Pierre Baillet's avatar
Pierre Baillet committed
261
262
263
264
265
266
267
268
/*****************************************************************************
 * vout_SDLDisplay: displays previously rendered output
 *****************************************************************************
 * This function send the currently rendered image to the display, wait until
 * it is displayed and switch the two rendering buffer, preparing next frame.
 *****************************************************************************/
void vout_SDLDisplay( vout_thread_t *p_vout )
{
Pierre Baillet's avatar
Pierre Baillet committed
269
    SDL_Rect    disp;
270
    if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
271
272
273
274
    {
        if(p_vout->b_need_render)
        {  
            /* Change display frame */
Pierre Baillet's avatar
Pierre Baillet committed
275
276
            SDL_Flip( p_vout->p_sys->p_display );
        }
277
        else
278
        {
279
280
281
282
283
        
            /*
             * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
             * render 
             */
284
285
            /* TODO: support for streams other than 4:2:0 */
            /* create the overlay if necessary */
286
            if( p_vout->p_sys->p_overlay == NULL )
287
288
289
            {
                p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
                                             p_vout->p_rendered_pic->i_width, 
290
                                             p_vout->p_rendered_pic->i_height,
291
292
293
                                             SDL_YV12_OVERLAY, 
                                             p_vout->p_sys->p_display
                                           );
Sam Hocevar's avatar
   
Sam Hocevar committed
294
295
296
                intf_Msg("vout: YUV acceleration %s",
                            p_vout->p_sys->p_overlay->hw_overlay
                            ? "activated" : "unavailable !" ); 
297
            }
298

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
            SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
            /* copy the data into video buffers */
            /* Y first */
            memcpy(p_vout->p_sys->p_overlay->pixels[0],
                   p_vout->p_rendered_pic->p_y,
                   p_vout->p_sys->p_overlay->h *
                   p_vout->p_sys->p_overlay->pitches[0]);
            /* then V */
            memcpy(p_vout->p_sys->p_overlay->pixels[1],
                   p_vout->p_rendered_pic->p_v,
                   p_vout->p_sys->p_overlay->h *
                   p_vout->p_sys->p_overlay->pitches[1] / 2);
            /* and U */
            memcpy(p_vout->p_sys->p_overlay->pixels[2],
                   p_vout->p_rendered_pic->p_u,
                   p_vout->p_sys->p_overlay->h *
                   p_vout->p_sys->p_overlay->pitches[2] / 2);
316

317
318
319
320
            disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
            disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
            disp.x = (p_vout->i_width - disp.w)/2;
            disp.y = (p_vout->i_height - disp.h)/2;
321

322
323
324
325
            SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
            SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
        }
    }
Pierre Baillet's avatar
Pierre Baillet committed
326
327
328
329
330
331
332
333
334
335
}

/* following functions are local */

/*****************************************************************************
 * SDLOpenDisplay: open and initialize SDL device
 *****************************************************************************
 * Open and initialize display according to preferences specified in the vout
 * thread fields.
 *****************************************************************************/
336
static int SDLOpenDisplay( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
337
{
338
    SDL_Rect    clipping_rect;
339
340
    Uint32      flags;
    int bpp;
Pierre Baillet's avatar
Pierre Baillet committed
341
342
343
    /* Open display 
     * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
     */
344
345

    /* init flags and cursor */
346
    flags = SDL_ANYFORMAT | SDL_HWPALETTE;
347

348
    if( p_vout->p_sys->b_fullscreen )
349
350
351
352
353
354
355
356
357
358
359
360
361
362
        flags |= SDL_FULLSCREEN;
    else
        flags |= SDL_RESIZABLE;

    if( p_vout->b_need_render )
        flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
    else
        flags |= SDL_SWSURFACE; /* save video memory */

    bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
                          p_vout->p_sys->i_height,
                          p_vout->i_screen_depth, flags);

    if(bpp == 0)
Pierre Baillet's avatar
Pierre Baillet committed
363
    {
364
365
        intf_ErrMsg( "error: can't open DISPLAY default display" );
        return( 1 );
Pierre Baillet's avatar
Pierre Baillet committed
366
    }
367
368
369
370
371

    p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
                                                p_vout->p_sys->i_height,
                                                bpp, flags);

Pierre Baillet's avatar
Pierre Baillet committed
372
373
    if( p_vout->p_sys->p_display == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
374
        intf_ErrMsg( "error: can't open DISPLAY default display" );
Pierre Baillet's avatar
Pierre Baillet committed
375
376
        return( 1 );
    }
377

378
379
    SDL_LockSurface(p_vout->p_sys->p_display);

380
381
382
383
    if( p_vout->p_sys->b_fullscreen )
        SDL_ShowCursor( 0 );
    else
        SDL_ShowCursor( 1 );
384

385
    SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
386
387
    SDL_EventState(SDL_KEYUP , SDL_IGNORE);                 /* ignore keys up */
    SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);          
388
    if( p_vout->b_need_render )
Pierre Baillet's avatar
Pierre Baillet committed
389
    {
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
        p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
        SDL_Flip(p_vout->p_sys->p_display);
        p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
        SDL_Flip(p_vout->p_sys->p_display);

        /* Set clipping for text */
        clipping_rect.x = 0;
        clipping_rect.y = 0;
        clipping_rect.w = p_vout->p_sys->p_display->w;
        clipping_rect.h = p_vout->p_sys->p_display->h;
        SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);

        /* Set thread information */
        p_vout->i_width =           p_vout->p_sys->p_display->w;
        p_vout->i_height =          p_vout->p_sys->p_display->h;
        p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;

        p_vout->i_screen_depth =
            p_vout->p_sys->p_display->format->BitsPerPixel;
        p_vout->i_bytes_per_pixel =
            p_vout->p_sys->p_display->format->BytesPerPixel;

        p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
        p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
        p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;

        /* FIXME: palette in 8bpp ?? */
        /* Set and initialize buffers */
        vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
                                 p_vout->p_sys->p_buffer[ 1 ] );
Pierre Baillet's avatar
Pierre Baillet committed
420
    }
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    else
    {
        p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
        p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;

        /* Set thread information */
        p_vout->i_width =           p_vout->p_sys->p_display->w;
        p_vout->i_height =          p_vout->p_sys->p_display->h;
        p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;

        vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
                                 p_vout->p_sys->p_buffer[ 1 ] );
    }

435
    p_vout->p_sys->b_reopen_display = 0;
Pierre Baillet's avatar
Pierre Baillet committed
436
437
438
439
440
441
442
443
444
445
446
447

    return( 0 );
}

/*****************************************************************************
 * SDLCloseDisplay: close and reset SDL device
 *****************************************************************************
 * This function returns all resources allocated by SDLOpenDisplay and restore
 * the original state of the device.
 *****************************************************************************/
static void SDLCloseDisplay( vout_thread_t *p_vout )
{
Pierre Baillet's avatar
Pierre Baillet committed
448
    if( p_vout->p_sys->p_display != NULL )
449
    {
Pierre Baillet's avatar
Pierre Baillet committed
450
451
        if( p_vout->p_sys->p_overlay != NULL )
        {            
452
453
454
            SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
            p_vout->p_sys->p_overlay = NULL;
        }
455
        SDL_UnlockSurface ( p_vout->p_sys->p_display );
456
457
458
        SDL_FreeSurface( p_vout->p_sys->p_display );
        p_vout->p_sys->p_display = NULL;
    }
Pierre Baillet's avatar
Pierre Baillet committed
459
460
}

461
462
463
464
/*****************************************************************************
 * SDLToggleFullScreen: toggle fullscreen
 *****************************************************************************
 * This function toggles the fullscreen state of the surface.
465
466
 * And  - hide the pointer if switching to fullscreen
 *      - show the pointer if leaving fullscreen state
467
468
469
470
471
472
 *****************************************************************************/
static void SDLToggleFullScreen( vout_thread_t *p_vout )
{
    SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);

    if( p_vout->p_sys->b_fullscreen )
473
474
475
    {
        p_vout->p_sys->b_cursor=1;
    }
476
    else
477
478
479
480
    {
        p_vout->p_sys->b_cursor=0;
    }
    
481
    SDLTogglePointer( p_vout );
482
483
}

484
/*****************************************************************************
485
 * SDLTogglePointer: Hide/Show mouse pointer
486
487
488
 *****************************************************************************
 * This function hides/shows the mouse pointer inside the main window.
 *****************************************************************************/
489
static void SDLTogglePointer( vout_thread_t *p_vout )
490
491
492
493
494
495
496
497
498
499
{
    if( p_vout->p_sys->b_cursor==1 )
    {
        SDL_ShowCursor( 0 );
    }
    else
    {
        SDL_ShowCursor( 1 );
    }
}