video_output.c 80.9 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
 * video_output.c : video output thread
3
 * (c)2000 VideoLAN
4
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
5 6 7
 * This module describes the programming interface for video output threads.
 * It includes functions allowing to open a new thread, send pictures to a
 * thread, and destroy a previously oppenned video output thread.
8
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
9

10
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
11
 * Preamble
12
 *****************************************************************************/
13
#include <errno.h>
Michel Kaempf's avatar
Michel Kaempf committed
14 15 16
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Vincent Seguin's avatar
Vincent Seguin committed
17

Michel Kaempf's avatar
Michel Kaempf committed
18 19 20
#include "common.h"
#include "config.h"
#include "mtime.h"
21
#include "vlc_thread.h"
Michel Kaempf's avatar
Michel Kaempf committed
22 23
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
24
#include "video_text.h"
Vincent Seguin's avatar
Vincent Seguin committed
25
#include "video_sys.h"
26
#include "video_yuv.h"
Vincent Seguin's avatar
Vincent Seguin committed
27
#include "intf_msg.h"
28
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
29

30
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
31
 * Local prototypes
32
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
33 34
static int      BinaryLog         ( u32 i );
static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
35 36 37 38 39
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 );
40
static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
41 42 43 44 45 46 47 48 49 50 51 52
                                    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 );
static void     RenderInfo        ( vout_thread_t *p_vout );
53
static void     Synchronize       ( vout_thread_t *p_vout, s64 i_delay );
54 55 56 57
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 );
58 59
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
60

61
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
62
 * vout_CreateThread: creates a new video output thread
63
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
64 65 66
 * 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.
67
 * If not, it will be updated using one of the THREAD_* constants.
68
 *****************************************************************************/
69 70
vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
                          int i_width, int i_height, int *pi_status, int i_method )
Michel Kaempf's avatar
Michel Kaempf committed
71
{
72 73 74
    vout_thread_t * p_vout;                             /* thread descriptor */
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
Michel Kaempf's avatar
Michel Kaempf committed
75

76
    /* Allocate descriptor */
77
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
78
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
79
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
80
    {
81
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Michel Kaempf's avatar
Michel Kaempf committed
82 83
        return( NULL );
    }
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    /* Sets method-specific functions */
    switch( i_method )
    {
        case VOUT_DUMMY_METHOD:
            p_vout->p_sys_create =    vout_DummySysCreate;
            p_vout->p_sys_init =      vout_DummySysInit;
            p_vout->p_sys_end =       vout_DummySysEnd;
            p_vout->p_sys_destroy =   vout_DummySysDestroy;
            p_vout->p_sys_manage =    vout_DummySysManage;
            p_vout->p_sys_display =   vout_DummySysDisplay;
            break;
#ifdef VIDEO_X11
        case VOUT_X11_METHOD:
            p_vout->p_sys_create =    vout_X11SysCreate;
            p_vout->p_sys_init =      vout_X11SysInit;
            p_vout->p_sys_end =       vout_X11SysEnd;
            p_vout->p_sys_destroy =   vout_X11SysDestroy;
            p_vout->p_sys_manage =    vout_X11SysManage;
            p_vout->p_sys_display =   vout_X11SysDisplay;
            break;
#endif
#ifdef VIDEO_FB
        case VOUT_FB_METHOD:
            p_vout->p_sys_create =    vout_FBSysCreate;
            p_vout->p_sys_init =      vout_FBSysInit;
            p_vout->p_sys_end =       vout_FBSysEnd;
            p_vout->p_sys_destroy =   vout_FBSysDestroy;
            p_vout->p_sys_manage =    vout_FBSysManage;
            p_vout->p_sys_display =   vout_FBSysDisplay;
            break;
#endif
#ifdef VIDEO_GLIDE
        case VOUT_GLIDE_METHOD:
            p_vout->p_sys_create =    vout_GlideSysCreate;
            p_vout->p_sys_init =      vout_GlideSysInit;
            p_vout->p_sys_end =       vout_GlideSysEnd;
            p_vout->p_sys_destroy =   vout_GlideSysDestroy;
            p_vout->p_sys_manage =    vout_GlideSysManage;
            p_vout->p_sys_display =   vout_GlideSysDisplay;
            break;
#endif
#ifdef VIDEO_DGA
        case VOUT_DGA_METHOD:
            p_vout->p_sys_create =    vout_DGASysCreate;
            p_vout->p_sys_init =      vout_DGASysInit;
            p_vout->p_sys_end =       vout_DGASysEnd;
            p_vout->p_sys_destroy =   vout_DGASysDestroy;
            p_vout->p_sys_manage =    vout_DGASysManage;
            p_vout->p_sys_display =   vout_DGASysDisplay;
            break;
#endif
#ifdef VIDEO_GGI
        case VOUT_GGI_METHOD:
            p_vout->p_sys_create =    vout_GGISysCreate;
            p_vout->p_sys_init =      vout_GGISysInit;
            p_vout->p_sys_end =       vout_GGISysEnd;
            p_vout->p_sys_destroy =   vout_GGISysDestroy;
            p_vout->p_sys_manage =    vout_GGISysManage;
            p_vout->p_sys_display =   vout_GGISysDisplay;
            break;
#endif
#ifdef VIDEO_BEOS
        case VOUT_BEOS_METHOD:
148 149 150 151 152 153
            p_vout->p_sys_create =    vout_BSysCreate;
            p_vout->p_sys_init =      vout_BSysInit;
            p_vout->p_sys_end =       vout_BSysEnd;
            p_vout->p_sys_destroy =   vout_BSysDestroy;
            p_vout->p_sys_manage =    vout_BSysManage;
            p_vout->p_sys_display =   vout_BSysDisplay;
154 155 156 157 158 159 160 161
            break;
#endif
        default:
            intf_ErrMsg( "error: video output method not available\n" );
            free( p_vout );
            return( NULL );
    }

162
    /* Initialize thread properties - thread id and locks will be initialized
163
     * later */
164
    p_vout->b_die               = 0;
165
    p_vout->b_error             = 0;
166 167
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
168
    *p_vout->pi_status          = THREAD_CREATE;
169

170
    /* Initialize some fields used by the system-dependant method - these fields will
171
     * probably be modified by the method, and are only preferences */
172
    p_vout->i_changes           = 0;
173 174
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
175
    p_vout->i_bytes_per_line    = i_width * 2;
176 177
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
178
    p_vout->f_gamma             = VOUT_GAMMA;
179

Vincent Seguin's avatar
Vincent Seguin committed
180
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
181
    p_vout->b_info              = 0;
182 183
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
184 185

    p_vout->p_set_palette       = SetPalette;
186

Vincent Seguin's avatar
Vincent Seguin committed
187
    intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
188
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
189
                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
190

191 192
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
193
    p_vout->last_display_date   = 0;
194 195
    p_vout->last_idle_date      = 0;

196 197
#ifdef STATS
    /* Initialize statistics fields */
198 199 200
    p_vout->render_time         = 0;
    p_vout->c_fps_samples       = 0;
#endif
201 202 203 204

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

205
    /* Initialize pictures and subpictures - translation tables and functions
206
     * will be initialized later in InitThread */
207 208
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
209 210 211 212
        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
213
    }
214 215 216 217
    p_vout->i_pictures = 0;    

    /* Initialize synchronization informations */
    p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
218

219 220
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
221
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
222
    {
Vincent Seguin's avatar
Vincent Seguin committed
223 224
      free( p_vout );
      return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
225
    }
Vincent Seguin's avatar
Vincent Seguin committed
226
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
227
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
228 229 230 231 232 233 234 235
                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 );

    /* Calculate shifts from system-updated masks */
    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 );

236
    /* Set some useful colors */
237
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
238 239
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
240
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
241

242 243
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
244
    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
245 246
    if( p_vout->p_default_font == NULL )
    {
247
        p_vout->p_sys_destroy( p_vout );
248 249 250 251
        free( p_vout );
        return( NULL );
    }
    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
252 253
    if( p_vout->p_large_font == NULL )
    {
254
        vout_UnloadFont( p_vout->p_default_font );
255
        p_vout->p_sys_destroy( p_vout );
256 257 258
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
259

Michel Kaempf's avatar
Michel Kaempf committed
260
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
261
    vlc_mutex_init( &p_vout->picture_lock );
262 263 264
    vlc_mutex_init( &p_vout->subpicture_lock );
    vlc_mutex_init( &p_vout->change_lock );
    vlc_mutex_lock( &p_vout->change_lock );
Vincent Seguin's avatar
Vincent Seguin committed
265
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
266
    {
267
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
268
        vout_UnloadFont( p_vout->p_default_font );
269
        vout_UnloadFont( p_vout->p_large_font );
270
        p_vout->p_sys_destroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
271 272
        free( p_vout );
        return( NULL );
273
    }
Michel Kaempf's avatar
Michel Kaempf committed
274

275
    intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
Vincent Seguin's avatar
Vincent Seguin committed
276
             p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
277

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

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

    /* Set status */
307
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
308
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
309 310
    *p_vout->pi_status = THREAD_DESTROY;

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

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

325
/*****************************************************************************
326
 * vout_DisplaySubPicture: display a subpicture unit
327 328
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
329
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
330
 * the output thread if is reserved.
331
 *****************************************************************************/
332
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
333 334
{
#ifdef DEBUG_VIDEO
335 336
    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
337 338 339 340
#endif

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

    /* Remove reservation flag */
349
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
350 351

#ifdef DEBUG_VIDEO
352
    /* Send subpicture informations */
353 354 355 356
    intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
                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
357 358 359
#endif
}

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

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

378 379
    /*
     * Look for an empty place
380 381 382
     */
    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
    {
383 384 385 386 387 388 389
        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) )
            {
                /* Memory size do match or is smaller : memory will not be reallocated,
390
                 * and function can end immediately - this is the best possible case,
391
                 * since no memory allocation needs to be done */
392
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
393
#ifdef DEBUG_VIDEO
394 395
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
396
#endif
397 398 399 400 401 402 403 404 405 406
                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];
            }
        }
407
        else if( (p_free_subpic == NULL) &&
408 409
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
410
            /* Subpicture is empty and ready for allocation */
411 412 413 414 415 416
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

    /* If no free subpicture is available, use a destroyed subpicture */
    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
417
    {
418 419
        /* No free subpicture or matching destroyed subpicture has been found, but
         * a destroyed subpicture is still avalaible */
420
        free( p_destroyed_subpic->p_data );
421 422 423 424 425 426 427 428 429 430 431
        p_free_subpic = p_destroyed_subpic;
    }

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

        if( p_free_subpic->p_data != NULL )
        {                    /* Copy subpicture informations, set some default values */
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
447
            p_free_subpic->i_size                      = i_size;
448
            p_free_subpic->i_x                         = 0;
449
            p_free_subpic->i_y                         = 0;
450 451
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
452 453
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
454 455 456 457
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
458 459 460 461
            p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
            p_free_subpic->i_status =  FREE_SUBPICTURE;
            p_free_subpic =            NULL;
            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
462
        }
463

464
#ifdef DEBUG_VIDEO
465
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
466 467 468 469
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
470

471 472 473 474
    /* No free or destroyed subpicture could be found */
    intf_DbgMsg( "warning: heap is full\n" );
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
475 476
}

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

496
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
497 498

#ifdef DEBUG_VIDEO
499
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
500 501 502
#endif
}

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

528
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
529
    intf_DbgMsg("picture %p\n", p_pic);
530
#endif
531
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
532 533
}

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

547
    vlc_mutex_lock( &p_vout->picture_lock );
548
    p_pic->date = date;
549 550
    switch( p_pic->i_status )
    {
551
    case RESERVED_PICTURE:
552
        p_pic->i_status = RESERVED_DATED_PICTURE;
553
        break;
554 555
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
556
        break;
557
#ifdef DEBUG
558 559 560
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
561 562 563 564
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
565
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
566
#endif
567
    vlc_mutex_unlock( &p_vout->picture_lock );
568 569
}

570
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
571
 * vout_CreatePicture: allocate a picture in the video output heap.
572 573
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
574
 * A null pointer is returned if the function fails. This method provides an
575
 * already allocated zone of memory in the picture data fields. It needs locking
576 577 578
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
579
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
580
{
581 582 583 584
    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
585

Vincent Seguin's avatar
Vincent Seguin committed
586
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
587
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
588

589 590
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
591
     */
592
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
593
    {
594 595 596
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
597
             * that if width and type are the same for two pictures, chroma_width
598
             * should also be the same */
599 600 601 602 603
            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
604 605
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
606
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
607
                p_vout->i_pictures++;                
608
#ifdef DEBUG_VIDEO
609 610
                intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                            &p_vout->p_picture[i_picture] );
611
#endif
612 613 614 615 616 617 618 619 620 621
                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];
            }
        }
622
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
623 624
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
625
            /* Picture is empty and ready for allocation */
626
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
627
        }
Michel Kaempf's avatar
Michel Kaempf committed
628 629
    }

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

Vincent Seguin's avatar
Vincent Seguin committed
639 640 641 642 643 644 645
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
646
        {
647 648
        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
649
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
650
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
651 652
            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;
653
            break;
654 655
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
656 657
            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
658 659
            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;
660
            break;
661 662
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
663 664
            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
665 666
            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;
667
            break;
Vincent Seguin's avatar
Vincent Seguin committed
668 669
#ifdef DEBUG
        default:
670
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
671 672 673
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
674 675
        }

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

702
#ifdef DEBUG_VIDEO
703
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
704
#endif
Vincent Seguin's avatar
Vincent Seguin committed
705
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
706
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
707
    }
708

709
    /* No free or destroyed picture could be found */
710
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
711
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
712
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
713 714
}

715
/*****************************************************************************
716
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
717
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
718
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
719 720
 * 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 !
721
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
722
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
723
{
724 725
   vlc_mutex_lock( &p_vout->picture_lock );   

726
#ifdef DEBUG
727
   /* Check if picture status is valid */
728
   if( (p_pic->i_status != RESERVED_PICTURE) &&
729 730
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
731
   {
732 733
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
734 735
#endif

736 737
   p_pic->i_status = DESTROYED_PICTURE;
   p_vout->i_pictures--;    
738 739

#ifdef DEBUG_VIDEO
740
   intf_DbgMsg("picture %p\n", p_pic);
741
#endif
742
   vlc_mutex_unlock( &p_vout->picture_lock );   
Michel Kaempf's avatar
Michel Kaempf committed
743 744
}

745
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
746
 * vout_LinkPicture: increment reference counter of a picture
747
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
748
 * This function increment the reference counter of a picture in the video
749
 * heap. It needs a lock since several producer threads can access the picture.
750
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
751 752
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
753
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
754
    p_pic->i_refcount++;
755 756

#ifdef DEBUG_VIDEO
757
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
758
#endif
759 760

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

763
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
764
 * vout_UnlinkPicture: decrement reference counter of a picture
765
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
766
 * This function decrement the reference counter of a picture in the video heap.
767
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
768 769
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
770
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
771
    p_pic->i_refcount--;
772 773 774 775 776

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
777 778
        p_pic->i_refcount = 0;
    }
779 780
#endif

Vincent Seguin's avatar
Vincent Seguin committed
781
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
782
    {
783
        p_pic->i_status = DESTROYED_PICTURE;
784
        p_vout->i_pictures--;        
Michel Kaempf's avatar
Michel Kaempf committed
785
    }
786 787

#ifdef DEBUG_VIDEO
788
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
789
#endif
790 791

    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
792