video_output.c 83.7 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2 3 4
 * video_output.c : video output thread
 * This module describes the programming interface for video output threads.
 * It includes functions allowing to open a new thread, send pictures to a
5 6 7 8 9 10 11 12 13 14
 * thread, and destroy a previously oppened video output thread.
 *****************************************************************************
 * Copyright (C) 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.
Sam Hocevar's avatar
Sam Hocevar committed
15
 *
16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21 22 23
 * 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.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

26
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
27
 * Preamble
28
 *****************************************************************************/
29 30
#include "defs.h"

31 32 33 34
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */
Vincent Seguin's avatar
Vincent Seguin committed
35

36 37 38 39
#ifdef STATS
#   include <sys/times.h>
#endif

Michel Kaempf's avatar
Michel Kaempf committed
40
#include "config.h"
41 42
#include "common.h"
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
43
#include "mtime.h"
44
#include "plugins.h"
Michel Kaempf's avatar
Michel Kaempf committed
45 46
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
47
#include "video_text.h"
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
48
#include "video_spu.h"
49
#include "video_yuv.h"
50

Vincent Seguin's avatar
Vincent Seguin committed
51
#include "intf_msg.h"
52
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
53

54
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
55
 * Local prototypes
56
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
57 58
static int      BinaryLog         ( u32 i );
static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
59 60 61 62 63
static int      InitThread        ( vout_thread_t *p_vout );
static void     RunThread         ( vout_thread_t *p_vout );
static void     ErrorThread       ( vout_thread_t *p_vout );
static void     EndThread         ( vout_thread_t *p_vout );
static void     DestroyThread     ( vout_thread_t *p_vout, int i_status );
64
static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
65 66 67 68 69 70 71 72 73 74 75
                                    int i_h_align, int i_v_align,
                                    unsigned char *psz_text );
static void     SetBufferArea     ( vout_thread_t *p_vout, int i_x, int i_y,
                                    int i_w, int i_h );
static void     SetBufferPicture  ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderPicture     ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderSubPicture  ( vout_thread_t *p_vout,
                                    subpicture_t *p_subpic );
static void     RenderInterface   ( vout_thread_t *p_vout );
static int      RenderIdle        ( vout_thread_t *p_vout );
Renaud Dartus's avatar
 
Renaud Dartus committed
76
static int      RenderSplash      ( vout_thread_t *p_vout );
77 78 79 80 81
static void     RenderInfo        ( vout_thread_t *p_vout );
static int      Manage            ( vout_thread_t *p_vout );
static int      Align             ( vout_thread_t *p_vout, int *pi_x,
                                    int *pi_y, int i_width, int i_height,
                                    int i_h_align, int i_v_align );
82 83
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
84

85
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
86
 * vout_CreateThread: creates a new video output thread
87
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
88 89 90
 * This function creates a new video output thread, and returns a pointer
 * to its description. On error, it returns NULL.
 * If pi_status is NULL, then the function will block until the thread is ready.
91
 * If not, it will be updated using one of the THREAD_* constants.
92
 *****************************************************************************/
93
vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
94 95
                          int i_width, int i_height, int *pi_status,
                          int i_method, void *p_data )
Michel Kaempf's avatar
Michel Kaempf committed
96
{
97
    vout_thread_t * p_vout;                             /* thread descriptor */
98
    typedef void    ( vout_getplugin_t ) ( vout_thread_t * p_vout );
99 100
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
Sam Hocevar's avatar
Sam Hocevar committed
101
    int             i_best_index = 0, i_best_score = 0;
Michel Kaempf's avatar
Michel Kaempf committed
102

103
    /* Allocate descriptor */
Michel Kaempf's avatar
Michel Kaempf committed
104
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
105
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
106
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
107 108
        intf_ErrMsg( "vout error: vout thread creation returned %s",
                     strerror(ENOMEM) );
Michel Kaempf's avatar
Michel Kaempf committed
109 110
        return( NULL );
    }
111

112
    p_vout->p_set_palette       = SetPalette;
113

114 115
    /* Get a suitable video plugin */
    for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
116
    {
117 118 119 120 121 122
        /* If there's a plugin in p_info ... */
        if( p_main->p_bank->p_info[ i_index ] != NULL )
        {
            /* ... and if this plugin provides the functions we want ... */
            if( p_main->p_bank->p_info[ i_index ]->vout_GetPlugin != NULL )
            {
Sam Hocevar's avatar
Sam Hocevar committed
123 124 125 126 127 128 129
                /* ... and if this plugin has a good score ... */
                if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
                {
                    /* ... then take it */
                    i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
                    i_best_index = i_index;
                }
130 131
            }
        }
132
    }
133

Sam Hocevar's avatar
Sam Hocevar committed
134 135 136 137 138 139 140 141 142 143
    if( i_best_score == 0 )
    {
        free( p_vout );
        return( NULL );
    }

    /* Get the plugin functions */
    ( (vout_getplugin_t *)
      p_main->p_bank->p_info[ i_best_index ]->vout_GetPlugin )( p_vout );

144
    /* Initialize thread properties - thread id and locks will be initialized
145
     * later */
146
    p_vout->b_die               = 0;
147
    p_vout->b_error             = 0;
148 149
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
150
    *p_vout->pi_status          = THREAD_CREATE;
151

152 153 154
    /* Initialize some fields used by the system-dependant method - these
     * fields will probably be modified by the method, and are only
     * preferences */
Renaud Dartus's avatar
 
Renaud Dartus committed
155 156 157 158 159 160 161
    p_vout->i_changes             = 0;
    p_vout->i_width               = i_width;
    p_vout->i_height              = i_height;
    p_vout->i_bytes_per_line      = i_width * 2;
    p_vout->i_screen_depth        = 15;
    p_vout->i_bytes_per_pixel     = 2;
    p_vout->f_gamma               = VOUT_GAMMA;
162
    p_vout->b_need_render         = 1;
Renaud Dartus's avatar
 
Renaud Dartus committed
163 164

    p_vout->b_grayscale           = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
Sam Hocevar's avatar
Sam Hocevar committed
165
                                                       VOUT_GRAYSCALE_DEFAULT );
Renaud Dartus's avatar
 
Renaud Dartus committed
166 167 168
    p_vout->b_info                = 0;
    p_vout->b_interface           = 0;
    p_vout->b_scale               = 1;
169

Sam Hocevar's avatar
 
Sam Hocevar committed
170
    intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)",
Sam Hocevar's avatar
Sam Hocevar committed
171 172
                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
173

174
    /* Initialize idle screen */
175
    p_vout->last_display_date   = 0;
176
    p_vout->last_idle_date      = 0;
177
    p_vout->init_display_date   = mdate();
178
    p_vout->render_time         = 10000;
179

180
    /* Initialize statistics fields */
181
    p_vout->c_fps_samples       = 0;
182 183 184 185

    /* Initialize buffer index */
    p_vout->i_buffer_index      = 0;

186
    /* Initialize pictures and subpictures - translation tables and functions
187
     * will be initialized later in InitThread */
188 189
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
190 191
        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
192 193 194
    }
    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
    {
195 196
        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
197
    }
198
    p_vout->i_pictures = 0;
199

200 201
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
202
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
Michel Kaempf's avatar
Michel Kaempf committed
203
    {
204 205
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
206
    }
207
    intf_DbgMsg( "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
Sam Hocevar's avatar
 
Sam Hocevar committed
208
                 "masks: 0x%x/0x%x/0x%x",
209 210 211 212
                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
                 p_vout->i_red_mask, p_vout->i_green_mask,
                 p_vout->i_blue_mask );
Vincent Seguin's avatar
Vincent Seguin committed
213 214

    /* Calculate shifts from system-updated masks */
215 216 217 218 219 220
    MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift,
                 p_vout->i_red_mask );
    MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift,
                 p_vout->i_green_mask );
    MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift,
                 p_vout->i_blue_mask );
Vincent Seguin's avatar
Vincent Seguin committed
221

222
    /* Set some useful colors */
223
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
224 225
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
226
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
227

228 229
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
230
    p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
231 232
    if( p_vout->p_default_font == NULL )
    {
233
        p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
234
    }
Vincent Seguin's avatar
Vincent Seguin committed
235 236
    if( p_vout->p_default_font == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
237
        intf_ErrMsg( "vout error: could not load default font" );
238
        p_vout->p_sys_destroy( p_vout );
239 240 241
        free( p_vout );
        return( NULL );
    }
242
    p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
243 244
    if( p_vout->p_large_font == NULL )
    {
245
        p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
246
    }
Vincent Seguin's avatar
Vincent Seguin committed
247 248
    if( p_vout->p_large_font == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
249
        intf_ErrMsg( "vout error: could not load large font" );
250
        vout_UnloadFont( p_vout->p_default_font );
251
        p_vout->p_sys_destroy( p_vout );
252 253 254
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
255

Michel Kaempf's avatar
Michel Kaempf committed
256
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
257
    vlc_mutex_init( &p_vout->picture_lock );
258 259
    vlc_mutex_init( &p_vout->subpicture_lock );
    vlc_mutex_init( &p_vout->change_lock );
260

261 262
    if( vlc_thread_create( &p_vout->thread_id, "video output",
                           (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
263
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
264
        intf_ErrMsg("vout error: %s", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
265
        vout_UnloadFont( p_vout->p_default_font );
266
        vout_UnloadFont( p_vout->p_large_font );
267
        p_vout->p_sys_destroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
268 269
        free( p_vout );
        return( NULL );
270
    }
Michel Kaempf's avatar
Michel Kaempf committed
271

Sam Hocevar's avatar
 
Sam Hocevar committed
272 273
    intf_Msg( "vout: video display initialized (%dx%d, %d/%d bpp)",
              p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
274
              p_vout->i_bytes_per_pixel * 8 );
275

Michel Kaempf's avatar
Michel Kaempf committed
276 277 278 279
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
280
        {
Michel Kaempf's avatar
Michel Kaempf committed
281
            msleep( THREAD_SLEEP );
282
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
283 284 285
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
286 287
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
288 289 290 291
    }
    return( p_vout );
}

292
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
293
 * vout_DestroyThread: destroys a previously created thread
294 295
 *****************************************************************************
 * Destroy a terminated thread.
Michel Kaempf's avatar
Michel Kaempf committed
296
 * The function will request a destruction of the specified thread. If pi_error
297
 * is NULL, it will return once the thread is destroyed. Else, it will be
Michel Kaempf's avatar
Michel Kaempf committed
298
 * update using one of the THREAD_* constants.
299
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
300
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
301 302
{
    int     i_status;                                       /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
303 304

    /* Set status */
Sam Hocevar's avatar
 
Sam Hocevar committed
305
    intf_DbgMsg("");
Michel Kaempf's avatar
Michel Kaempf committed
306
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
307 308
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
309 310 311 312
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
313
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
314 315 316 317
    {
        do
        {
            msleep( THREAD_SLEEP );
318 319
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
320 321 322
    }
}

323
/*****************************************************************************
324
 * vout_DisplaySubPicture: display a subpicture unit
325 326
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
327
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
328
 * the output thread if is reserved.
329
 *****************************************************************************/
330
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
331
{
332
#ifdef DEBUG_VOUT
333 334
    char        psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
    char        psz_end_date[MSTRTIME_MAX_SIZE];   /* buffer for date string */
Vincent Seguin's avatar
Vincent Seguin committed
335 336 337 338
#endif

#ifdef DEBUG
    /* Check if status is valid */
339
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
340
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
341
        intf_DbgMsg("error: subpicture %p has invalid status %d", p_subpic,
342 343
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
344 345 346
#endif

    /* Remove reservation flag */
347
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
348

349
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
Sam Hocevar committed
350
    /* Send subpicture information */
Sam Hocevar's avatar
 
Sam Hocevar committed
351
    intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s",
352 353 354
                p_subpic, p_subpic->i_type,
                mstrtime( psz_begin_date, p_subpic->begin_date ),
                mstrtime( psz_end_date, p_subpic->end_date ) );
Vincent Seguin's avatar
Vincent Seguin committed
355 356 357
#endif
}

358
/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
359
 * vout_CreateSubPicture: allocate a subpicture in the video output heap.
360 361
 *****************************************************************************
 * This function create a reserved subpicture in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
362
 * A null pointer is returned if the function fails. This method provides an
363
 * already allocated zone of memory in the spu data fields. It needs locking
364 365 366
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
367
                                     int i_size )
Vincent Seguin's avatar
Vincent Seguin committed
368
{
369 370 371
    int                 i_subpic;                        /* subpicture index */
    subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
    subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
372 373 374 375

    /* Get lock */
    vlc_mutex_lock( &p_vout->subpicture_lock );

376 377
    /*
     * Look for an empty place
378
     */
379
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
380
    {
381 382 383 384 385 386
        if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
        {
            /* Subpicture is marked for destruction, but is still allocated */
            if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
                (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
            {
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
387 388 389 390
                /* Memory size do match or is smaller : memory will not be
                 * reallocated, and function can end immediately - this is
                 * the best possible case, since no memory allocation needs
                 * to be done */
391
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
392
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
393
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)",
394
                            &p_vout->p_subpicture[i_subpic] );
395
#endif
396 397 398 399 400 401 402 403 404 405
                vlc_mutex_unlock( &p_vout->subpicture_lock );
                return( &p_vout->p_subpicture[i_subpic] );
            }
            else if( p_destroyed_subpic == NULL )
            {
                /* Memory size do not match, but subpicture index will be kept in
                 * case no other place are left */
                p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
            }
        }
406
        else if( (p_free_subpic == NULL) &&
407 408
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
409
            /* Subpicture is empty and ready for allocation */
410 411 412 413
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

Sam Hocevar's avatar
Sam Hocevar committed
414
    /* If no free subpictures are available, use a destroyed subpicture */
415
    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
416
    {
Sam Hocevar's avatar
Sam Hocevar committed
417
        /* No free subpicture or matching destroyed subpictures have been
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
418
         * found, but a destroyed subpicture is still avalaible */
419
        free( p_destroyed_subpic->p_data );
420 421 422 423 424 425 426 427 428 429 430
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
431 432
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
433
            break;
434 435 436
        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
            p_free_subpic->p_data = malloc( i_size );
            break;
437 438
#ifdef DEBUG
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
439
            intf_DbgMsg("error: unknown subpicture type %d", i_type );
440 441 442
            p_free_subpic->p_data   =  NULL;
            break;
#endif
443 444 445
        }

        if( p_free_subpic->p_data != NULL )
Sam Hocevar's avatar
Sam Hocevar committed
446 447
        {
            /* Copy subpicture information, set some default values */
448 449
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
450
            p_free_subpic->i_size                      = i_size;
451
            p_free_subpic->i_x                         = 0;
452
            p_free_subpic->i_y                         = 0;
453 454
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
455 456
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
457 458 459 460
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
461 462 463
            p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
            p_free_subpic->i_status =  FREE_SUBPICTURE;
            p_free_subpic =            NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
464 465
            intf_ErrMsg( "vout error: spu allocation returned %s",
                         strerror( ENOMEM ) );
466
        }
467

468
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
469
        intf_DbgMsg("subpicture %p (in free subpicture slot)", p_free_subpic );
470 471 472 473
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
474

475
    /* No free or destroyed subpicture could be found */
Sam Hocevar's avatar
 
Sam Hocevar committed
476
    intf_DbgMsg( "warning: subpicture heap is full" );
477 478
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
479 480
}

481
/*****************************************************************************
482
 * vout_DestroySubPicture: remove a subpicture from the heap
483
 *****************************************************************************
484
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
485
 * It is meant to be used when the construction of a picture aborted.
486
 * This function does not need locking since reserved subpictures are ignored
487
 * by the output thread.
488
 *****************************************************************************/
489
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
490 491
{
#ifdef DEBUG
492 493
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
494
   {
Sam Hocevar's avatar
 
Sam Hocevar committed
495
       intf_DbgMsg("error: subpicture %p has invalid status %d",
496 497
                   p_subpic, p_subpic->i_status );
   }
Vincent Seguin's avatar
Vincent Seguin committed
498 499
#endif

500
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
501

502
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
503
    intf_DbgMsg("subpicture %p", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
504 505 506
#endif
}

507
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
508
 * vout_DisplayPicture: display a picture
509
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
510
 * Remove the reservation flag of a picture, which will cause it to be ready for
511
 * display. The picture won't be displayed until vout_DatePicture has been
512
 * called.
513
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
514
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
515
{
516 517
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
518
    {
519
    case RESERVED_PICTURE:
520
        p_pic->i_status = RESERVED_DISP_PICTURE;
521
        break;
522 523
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
524
        break;
525
#ifdef DEBUG
526
    default:
Sam Hocevar's avatar
 
Sam Hocevar committed
527
        intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
528
        break;
529
#endif
530
    }
Michel Kaempf's avatar
Michel Kaempf committed
531

532
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
533
    intf_DbgMsg("picture %p", p_pic);
534
#endif
535
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
536 537
}

538
/*****************************************************************************
539
 * vout_DatePicture: date a picture
540
 *****************************************************************************
541
 * Remove the reservation flag of a picture, which will cause it to be ready for
542
 * display. The picture won't be displayed until vout_DisplayPicture has been
543
 * called.
544
 *****************************************************************************/
545 546
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
547
#ifdef DEBUG_VOUT
548
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
549 550
#endif

551
    vlc_mutex_lock( &p_vout->picture_lock );
552
    p_pic->date = date;
553 554
    switch( p_pic->i_status )
    {
555
    case RESERVED_PICTURE:
556
        p_pic->i_status = RESERVED_DATED_PICTURE;
557
        break;
558 559
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
560
        break;
561
#ifdef DEBUG
562
    default:
Sam Hocevar's avatar
 
Sam Hocevar committed
563
        intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
564
        break;
565 566 567
#endif
    }

568
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
569
    intf_DbgMsg("picture %p, display date: %s", p_pic, mstrtime( psz_date, p_pic->date) );
570
#endif
571
    vlc_mutex_unlock( &p_vout->picture_lock );
572 573
}

574
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
575
 * vout_CreatePicture: allocate a picture in the video output heap.
576 577
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
578
 * A null pointer is returned if the function fails. This method provides an
579
 * already allocated zone of memory in the picture data fields. It needs locking
580 581 582
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
583
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
584
{
585 586 587 588
    int         i_picture;                                  /* picture index */
    int         i_chroma_width = 0;                          /* chroma width */
    picture_t * p_free_picture = NULL;                 /* first free picture */
    picture_t * p_destroyed_picture = NULL;       /* first destroyed picture */
Michel Kaempf's avatar
Michel Kaempf committed
589

Vincent Seguin's avatar
Vincent Seguin committed
590
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
591
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
592

593 594
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
595
     */
596
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
597
    {
598 599 600
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
601
             * that if width and type are the same for two pictures, chroma_width
602
             * should also be the same */
603 604 605 606 607
            if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
                (p_vout->p_picture[i_picture].i_height         == i_height) &&
                (p_vout->p_picture[i_picture].i_width          == i_width) )
            {
                /* Memory size do match : memory will not be reallocated, and function
Vincent Seguin's avatar
Vincent Seguin committed
608 609
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
610
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
611
                p_vout->i_pictures++;
612
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
613
                intf_DbgMsg("picture %p (in destroyed picture slot)",
614
                            &p_vout->p_picture[i_picture] );
615
#endif
616 617 618 619 620 621 622 623 624 625
                vlc_mutex_unlock( &p_vout->picture_lock );
                return( &p_vout->p_picture[i_picture] );
            }
            else if( p_destroyed_picture == NULL )
            {
                /* Memory size do not match, but picture index will be kept in
                 * case no other place are left */
                p_destroyed_picture = &p_vout->p_picture[i_picture];
            }
        }
626
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
627 628
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
629
            /* Picture is empty and ready for allocation */
630
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
631
        }
Michel Kaempf's avatar
Michel Kaempf committed
632 633
    }

Vincent Seguin's avatar
Vincent Seguin committed
634 635
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
636
    {
637 638
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
639 640
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
641 642
    }

Vincent Seguin's avatar
Vincent Seguin committed
643 644 645 646 647 648 649
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
650
        {
651 652
        case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
            i_chroma_width = i_width / 2;
Vincent Seguin's avatar
Vincent Seguin committed
653
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
654
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
655 656
            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
657
            break;
658 659
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
660 661
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
662 663
            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
664
            break;
665 666
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
667 668
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
669 670
            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
671
            break;
Vincent Seguin's avatar
Vincent Seguin committed
672 673
#ifdef DEBUG
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
674
            intf_DbgMsg("error: unknown picture type %d", i_type );
675 676 677
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
678 679
        }

Vincent Seguin's avatar
Vincent Seguin committed
680
        if( p_free_picture->p_data != NULL )
681
        {
Sam Hocevar's avatar
Sam Hocevar committed
682
            /* Copy picture information, set some default values */
683 684
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
685
            p_free_picture->i_matrix_coefficients       = 1;
686 687
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
688
            p_free_picture->i_chroma_width              = i_chroma_width;
689
            p_free_picture->i_display_horizontal_offset = 0;
690
            p_free_picture->i_display_vertical_offset   = 0;
691 692
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
693 694
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
695
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
696 697 698 699
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
700 701 702
            p_free_picture->i_type   =  EMPTY_PICTURE;
            p_free_picture->i_status =  FREE_PICTURE;
            p_free_picture =            NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
703 704
            intf_ErrMsg( "vout error: picture allocation returned %s",
                         strerror( ENOMEM ) );
Vincent Seguin's avatar
Vincent Seguin committed
705
        }
706

707
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
708
        intf_DbgMsg("picture %p (in free picture slot)", p_free_picture );
709
#endif
Vincent Seguin's avatar
Vincent Seguin committed
710
        vlc_mutex_unlock( &p_vout->picture_lock );
Henri Fallon's avatar
 
Henri Fallon committed
711 712 713 714

        /* Initialize mutex */
        vlc_mutex_init( &(p_free_picture->lock_deccount) );
        
Vincent Seguin's avatar
Vincent Seguin committed
715
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
716
    }
717

718
    /* No free or destroyed picture could be found */
Sam Hocevar's avatar
 
Sam Hocevar committed
719
    intf_DbgMsg( "warning: picture heap is full" );
Vincent Seguin's avatar
Vincent Seguin committed
720
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
721
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
722 723
}

724
/*****************************************************************************
725
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
726
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
727
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
728 729
 * picture. It is meant to be used when the construction of a picture aborted.
 * Note that the picture will be destroyed even if it is linked !
730
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
731
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
732
{
Henri Fallon's avatar
 
Henri Fallon committed
733
    vlc_mutex_lock( &p_vout->picture_lock );
734

735
#ifdef DEBUG
Henri Fallon's avatar
 
Henri Fallon committed
736 737 738 739 740 741 742
    /* Check if picture status is valid */
    if( (p_pic->i_status != RESERVED_PICTURE) &&
        (p_pic->i_status != RESERVED_DATED_PICTURE) &&
        (p_pic->i_status != RESERVED_DISP_PICTURE) )
    {
        intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
    }
743 744
#endif

Henri Fallon's avatar
 
Henri Fallon committed
745 746
    p_pic->i_status = DESTROYED_PICTURE;
    p_vout->i_pictures--;
747

748
#ifdef DEBUG_VOUT
Henri Fallon's avatar
 
Henri Fallon committed
749
    intf_DbgMsg("picture %p", p_pic);
750
#endif
Henri Fallon's avatar
 
Henri Fallon committed
751

Henri Fallon's avatar
 
Henri Fallon committed
752 753
    /* destroy the lock that had been initialized in CreatePicture */
    vlc_mutex_destroy( &(p_pic->lock_deccount) );
Henri Fallon's avatar
 
Henri Fallon committed
754
   
Henri Fallon's avatar
 
Henri Fallon committed
755
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
756 757
}

758
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
759
 * vout_LinkPicture: increment reference counter of a picture
760
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
761
 * This function increment the reference counter of a picture in the video
762
 * heap. It needs a lock since several producer threads can access the picture.
763
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
764 765
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
766
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
767
    p_pic->i_refcount++;
768

769
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
770
    intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
771
#endif
772 773

    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
774 775
}

776
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
777
 * vout_UnlinkPicture: decrement reference counter of a picture
778
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
779
 * This function decrement the reference counter of a picture in the video heap.
780
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
781 782
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
783
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
784
    p_pic->i_refcount--;
785

786
#ifdef DEBUG_VOUT
787 788
    if( p_pic->i_refcount < 0 )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
789
        intf_DbgMsg("error: refcount < 0");
790 791
        p_pic->i_refcount = 0;
    }