video_output.c 77 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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 );
static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y, 
                                    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 );
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 );
57 58
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
59

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

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

84 85
    /* Initialize thread properties - thread id and locks will be initialized 
     * later */
86 87 88 89 90 91
    p_vout->b_die               = 0;
    p_vout->b_error             = 0;    
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
    *p_vout->pi_status          = THREAD_CREATE;    

92
    /* Initialize some fields used by the system-dependant method - these fields will
93
     * probably be modified by the method, and are only preferences */
94
    p_vout->i_changes           = 0;
95 96
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
97
    p_vout->i_bytes_per_line    = i_width * 2;    
98 99
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
100
    p_vout->f_gamma             = VOUT_GAMMA;    
101

Vincent Seguin's avatar
Vincent Seguin committed
102
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
Vincent Seguin's avatar
Vincent Seguin committed
103
    p_vout->b_info              = 0;    
104 105
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
106 107

    p_vout->p_set_palette       = SetPalette;
108
    
Vincent Seguin's avatar
Vincent Seguin committed
109
    intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
110
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
111
                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
112

113 114 115 116
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
    p_vout->last_idle_date      = 0;

117 118 119 120 121 122 123 124 125
#ifdef STATS
    /* Initialize statistics fields */
    p_vout->render_time         = 0;    
    p_vout->c_fps_samples       = 0;    
#endif      

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

126
    /* Initialize pictures and subpictures - translation tables and functions
127 128 129
     * will be initialized later in InitThread */    
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
130 131 132 133
        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;
134
    }
135 136 137
   
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
Vincent Seguin's avatar
Vincent Seguin committed
138
    if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
139
    {
Vincent Seguin's avatar
Vincent Seguin committed
140 141
      free( p_vout );
      return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
142
    }
Vincent Seguin's avatar
Vincent Seguin committed
143
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
144
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
145 146 147 148 149 150 151 152
                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 );

153 154
    /* Set some useful colors */
/*    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
155 156 157
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );    
158
*/
159 160
    /* Load fonts - fonts must be initialized after the systme method since
     * they may be dependant of screen depth and other thread properties */
Vincent Seguin's avatar
Vincent Seguin committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );    
    if( p_vout->p_default_font == NULL )
    {
        vout_SysDestroy( p_vout );        
        free( p_vout );        
        return( NULL );        
    }    
    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );        
    if( p_vout->p_large_font == NULL )
    {
        vout_UnloadFont( p_vout->p_default_font );        
        vout_SysDestroy( p_vout );        
        free( p_vout );        
        return( NULL );        
175
    }     
Vincent Seguin's avatar
Vincent Seguin committed
176

Michel Kaempf's avatar
Michel Kaempf committed
177
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
178
    vlc_mutex_init( &p_vout->picture_lock );
179
    vlc_mutex_init( &p_vout->subpicture_lock );    
Vincent Seguin's avatar
Vincent Seguin committed
180 181 182
    vlc_mutex_init( &p_vout->change_lock );    
    vlc_mutex_lock( &p_vout->change_lock );    
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
183
    {
184
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
185 186
        vout_UnloadFont( p_vout->p_default_font );
        vout_UnloadFont( p_vout->p_large_font );        
Vincent Seguin's avatar
Vincent Seguin committed
187
	vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
188 189 190 191
        free( p_vout );
        return( NULL );
    }   

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

Michel Kaempf's avatar
Michel Kaempf committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
        {            
            msleep( THREAD_SLEEP );
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR) 
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
            return( NULL );            
        }        
    }
    return( p_vout );
}

211
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
212
 * vout_DestroyThread: destroys a previously created thread
213
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
214 215 216 217
 * Destroy a terminated thread. 
 * The function will request a destruction of the specified thread. If pi_error
 * is NULL, it will return once the thread is destroyed. Else, it will be 
 * update using one of the THREAD_* constants.
218
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
219 220
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
{  
221
    int     i_status;                                        /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
222 223

    /* Set status */
224
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
225
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
Vincent Seguin's avatar
Vincent Seguin committed
226
    *p_vout->pi_status = THREAD_DESTROY;    
Michel Kaempf's avatar
Michel Kaempf committed
227 228 229 230 231
     
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
232
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
233 234 235 236 237 238 239 240 241
    {
        do
        {
            msleep( THREAD_SLEEP );
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
                && (i_status != THREAD_FATAL) );   
    }
}

242
/******************************************************************************
243
 * vout_DisplaySubPicture: display a subpicture unit
244
 ******************************************************************************
245 246
 * Remove the reservation flag of an subpicture, which will cause it to be ready 
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
247
 * the output thread if is reserved.
248
 ******************************************************************************/
249
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
250 251
{
#ifdef DEBUG_VIDEO
252 253
    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
254 255 256 257
#endif

#ifdef DEBUG
    /* Check if status is valid */
258
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
259
    {
260 261
        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic, 
                    p_subpic->i_status );       
Vincent Seguin's avatar
Vincent Seguin committed
262 263 264 265
    }   
#endif

    /* Remove reservation flag */
266
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
267 268

#ifdef DEBUG_VIDEO
269
    /* Send subpicture informations */
270 271 272 273
    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
274 275 276
#endif
}

277
/******************************************************************************
278
 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
279
 ******************************************************************************
280
 * This function create a reserved subpicture in the video output heap. 
Vincent Seguin's avatar
Vincent Seguin committed
281
 * A null pointer is returned if the function fails. This method provides an
282
 * already allocated zone of memory in the spu data fields. It needs locking
Vincent Seguin's avatar
Vincent Seguin committed
283
 * since several pictures can be created by several producers threads. 
284
 ******************************************************************************/
285 286
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type, 
                                     int i_size )
Vincent Seguin's avatar
Vincent Seguin committed
287
{
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    int                 i_subpic;                          /* subpicture index */
    subpicture_t *      p_free_subpic = NULL;         /* first free subpicture */    
    subpicture_t *      p_destroyed_subpic = NULL;   /* first destroyed subpic */

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

    /* 
     * Look for an empty place 
     */
    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
    {
	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, 
                 * and function can end immediately - this is the best possible case, 
                 * since no memory allocation needs to be done */
		p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
#ifdef DEBUG_VIDEO
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n", 
                            &p_vout->p_subpicture[i_subpic] );                
#endif
		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];                
	    }	    
	}
        else if( (p_free_subpic == NULL) && 
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
	    /* Subpicture is empty and ready for allocation */
            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 ) )
    { 
	/* No free subpicture or matching destroyed subpicture has been found, but
	 * a destroyed subpicture is still avalaible */
        free( p_destroyed_subpic->p_data );        
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
        case TEXT_SUBPICTURE:                               /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );             
            break;
#ifdef DEBUG
        default:
            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
            p_free_subpic->p_data   =  NULL;            
            break;            
#endif    
        }

        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;
            p_free_subpic->i_size                      = i_size;            
            p_free_subpic->i_x                         = 0;
            p_free_subpic->i_y                         = 0;             
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;             
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;             
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
            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 ) );            
        }
        
#ifdef DEBUG_VIDEO
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );        
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
    
    /* 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
392 393
}

394
/******************************************************************************
395
 * vout_DestroySubPicture: remove a subpicture from the heap
396
 ******************************************************************************
397
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
398
 * It is meant to be used when the construction of a picture aborted.
399 400
 * This function does not need locking since reserved subpictures are ignored 
 * by the output thread.
401
 ******************************************************************************/
402
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
403 404
{
#ifdef DEBUG
405 406
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
407
   {
408 409
       intf_DbgMsg("error: subpicture %p has invalid status %d\n", 
                   p_subpic, p_subpic->i_status );       
Vincent Seguin's avatar
Vincent Seguin committed
410 411 412
   }   
#endif

413
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
414 415

#ifdef DEBUG_VIDEO
416
    intf_DbgMsg("subpicture %p\n", p_subpic);    
Vincent Seguin's avatar
Vincent Seguin committed
417 418 419
#endif
}

Vincent Seguin's avatar
Vincent Seguin committed
420
/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
421
 * vout_DisplayPicture: display a picture
Vincent Seguin's avatar
Vincent Seguin committed
422
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
423
 * Remove the reservation flag of a picture, which will cause it to be ready for
424 425
 * display. The picture won't be displayed until vout_DatePicture has been 
 * called.
Vincent Seguin's avatar
Vincent Seguin committed
426
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
427
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
428
{
429 430
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
431
    {
432 433 434 435 436 437 438 439 440 441
    case RESERVED_PICTURE:        
        p_pic->i_status = RESERVED_DISP_PICTURE;
        break;        
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
        break;        
#ifdef DEBUG
    default:        
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );    
        break;        
442
#endif
443
    }
Michel Kaempf's avatar
Michel Kaempf committed
444

445
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
446
    intf_DbgMsg("picture %p\n", p_pic);
447
#endif
448
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
449 450
}

Vincent Seguin's avatar
Vincent Seguin committed
451
/*******************************************************************************
452
 * vout_DatePicture: date a picture
Vincent Seguin's avatar
Vincent Seguin committed
453
 *******************************************************************************
454
 * Remove the reservation flag of a picture, which will cause it to be ready for
455 456
 * display. The picture won't be displayed until vout_DisplayPicture has been 
 * called.
Vincent Seguin's avatar
Vincent Seguin committed
457
 *******************************************************************************/
458 459
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
Vincent Seguin's avatar
Vincent Seguin committed
460 461 462 463
#ifdef DEBUG_VIDEO
    char        psz_date[MSTRTIME_MAX_SIZE];                           /* date */
#endif

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    vlc_mutex_lock( &p_vout->picture_lock );
    p_pic->date = date;    
    switch( p_pic->i_status )
    {
    case RESERVED_PICTURE:        
        p_pic->i_status = RESERVED_DATED_PICTURE;
        break;        
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
        break;        
#ifdef DEBUG
    default:        
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );    
        break;        
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
482
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
483
#endif
484
    vlc_mutex_unlock( &p_vout->picture_lock );
485 486
}

487
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
488
 * vout_CreatePicture: allocate a picture in the video output heap.
489
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
490
 * This function create a reserved image in the video output heap. 
Vincent Seguin's avatar
Vincent Seguin committed
491
 * A null pointer is returned if the function fails. This method provides an
492
 * already allocated zone of memory in the picture data fields. It needs locking
493
 * since several pictures can be created by several producers threads. 
494
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
495
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type, 
496
			       int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
497
{
498 499 500 501
    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
502

Vincent Seguin's avatar
Vincent Seguin committed
503
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
504
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
505

Vincent Seguin's avatar
Vincent Seguin committed
506 507 508
    /* 
     * Look for an empty place 
     */
509
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
510 511 512
    {
	if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
	{
513 514 515
	    /* Picture is marked for destruction, but is still allocated - note
             * that if width and type are the same for two pictures, chroma_width 
             * should also be the same */
Vincent Seguin's avatar
Vincent Seguin committed
516 517
	    if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
		(p_vout->p_picture[i_picture].i_height         == i_height) &&
518
		(p_vout->p_picture[i_picture].i_width          == i_width) )
Vincent Seguin's avatar
Vincent Seguin committed
519 520 521 522 523
	    {
		/* Memory size do match : memory will not be reallocated, and function
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
		p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
524 525 526 527
#ifdef DEBUG_VIDEO
                intf_DbgMsg("picture %p (in destroyed picture slot)\n", 
                            &p_vout->p_picture[i_picture] );                
#endif
Vincent Seguin's avatar
Vincent Seguin committed
528
		vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		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];                
	    }	    
	}
        else if( (p_free_picture == NULL) && 
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
	    /* Picture is empty and ready for allocation */
            p_free_picture = &p_vout->p_picture[i_picture];            
        }
Michel Kaempf's avatar
Michel Kaempf committed
544 545
    }

Vincent Seguin's avatar
Vincent Seguin committed
546 547 548 549 550 551 552
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
    { 
	/* No free picture or matching destroyed picture has been found, but
	 * a destroyed picture is still avalaible */
        free( p_destroyed_picture->p_data );        
        p_free_picture = p_destroyed_picture;        
Michel Kaempf's avatar
Michel Kaempf committed
553 554
    }

Vincent Seguin's avatar
Vincent Seguin committed
555 556 557 558 559 560 561
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
562
        {
563
        case YUV_420_PICTURE:          /* YUV 420: 1,1/4,1/4 samples per pixel */
Vincent Seguin's avatar
Vincent Seguin committed
564 565
            i_chroma_width = i_width / 2;            
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
566
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
567 568
            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;
569 570
            break;
        case YUV_422_PICTURE:          /* YUV 422: 1,1/2,1/2 samples per pixel */
571 572 573
            i_chroma_width = i_width / 2;            
            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
574 575
            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;
576 577
            break;
        case YUV_444_PICTURE:              /* YUV 444: 1,1,1 samples per pixel */
578 579 580
            i_chroma_width = i_width;            
            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
581 582
            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;
Vincent Seguin's avatar
Vincent Seguin committed
583 584 585
            break;                
#ifdef DEBUG
        default:
586
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
587 588
            p_free_picture->p_data   =  NULL;            
            break;            
Vincent Seguin's avatar
Vincent Seguin committed
589
#endif    
Michel Kaempf's avatar
Michel Kaempf committed
590 591
        }

Vincent Seguin's avatar
Vincent Seguin committed
592 593
        if( p_free_picture->p_data != NULL )
        {        
594
            /* Copy picture informations, set some default values */
595 596
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
597
            p_free_picture->i_matrix_coefficients       = 1; 
598 599
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
600 601 602 603 604 605
            p_free_picture->i_chroma_width              = i_chroma_width;            
            p_free_picture->i_display_horizontal_offset = 0;
            p_free_picture->i_display_vertical_offset   = 0;            
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;            
606
            p_free_picture->i_refcount                  = 0;            
Vincent Seguin's avatar
Vincent Seguin committed
607 608 609 610
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
611 612 613 614
            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
615 616
        }
        
617 618 619
#ifdef DEBUG_VIDEO
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
#endif
Vincent Seguin's avatar
Vincent Seguin committed
620
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
621
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
622
    }
Vincent Seguin's avatar
Vincent Seguin committed
623
    
624
    /* No free or destroyed picture could be found */
625
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
626
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
627
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
628 629
}

630
/******************************************************************************
631
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
632
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
633
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
634 635
 * 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 !
636 637
 * This function does not need locking since reserved pictures are ignored by
 * the output thread.
638
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
639
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
640
{
641
#ifdef DEBUG
642
   /* Check if picture status is valid */
643 644 645
   if( (p_pic->i_status != RESERVED_PICTURE) && 
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
646
   {
Vincent Seguin's avatar
Vincent Seguin committed
647
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );       
648 649 650
   }   
#endif

Vincent Seguin's avatar
Vincent Seguin committed
651
    p_pic->i_status = DESTROYED_PICTURE;
652 653 654 655

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
656 657
}

658
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
659
 * vout_LinkPicture: increment reference counter of a picture
660
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
661
 * This function increment the reference counter of a picture in the video
662
 * heap. It needs a lock since several producer threads can access the picture.
663
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
664 665
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
666
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
667
    p_pic->i_refcount++;
668 669

#ifdef DEBUG_VIDEO
670
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
671
#endif
672 673

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

676
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
677
 * vout_UnlinkPicture: decrement reference counter of a picture
678
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
679
 * This function decrement the reference counter of a picture in the video heap.
680
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
681 682
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
683
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
684
    p_pic->i_refcount--;
685 686 687 688 689 690 691 692 693

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

Vincent Seguin's avatar
Vincent Seguin committed
694
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
695
    {
Vincent Seguin's avatar
Vincent Seguin committed
696
	p_pic->i_status = DESTROYED_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
697
    }
698 699

#ifdef DEBUG_VIDEO
700
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
701
#endif
702 703

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

706
/******************************************************************************
707
 * vout_SetBuffers: set buffers adresses
708
 ******************************************************************************
709 710
 * This function is called by system drivers to set buffers video memory 
 * adresses.
711
 ******************************************************************************/
712
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
713 714
{
    /* No picture previously */
715 716 717 718 719 720 721 722
    p_vout->p_buffer[0].i_pic_x =         0;
    p_vout->p_buffer[0].i_pic_y =         0;
    p_vout->p_buffer[0].i_pic_width =     0;
    p_vout->p_buffer[0].i_pic_height =    0;
    p_vout->p_buffer[1].i_pic_x =         0;
    p_vout->p_buffer[1].i_pic_y =         0;
    p_vout->p_buffer[1].i_pic_width =     0;
    p_vout->p_buffer[1].i_pic_height =    0;
723 724

    /* The first area covers all the screen */
725 726 727 728 729 730 731 732 733 734
    p_vout->p_buffer[0].i_areas =                 1;
    p_vout->p_buffer[0].pi_area_begin[0] =        0;
    p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
    p_vout->p_buffer[1].i_areas =                 1;
    p_vout->p_buffer[1].pi_area_begin[0] =        0;
    p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;

    /* Set adresses */
    p_vout->p_buffer[0].p_data = p_buf1;    
    p_vout->p_buffer[1].p_data = p_buf2;    
735 736
}

Vincent Seguin's avatar
Vincent Seguin committed
737 738 739 740 741 742 743 744 745 746 747 748 749
/*****************************************************************************
 * vout_Pixel2RGB: return red, green and blue from pixel value
 *****************************************************************************
 * Return color values, in 0-255 range, of the decomposition of a pixel. This
 * is a slow routine and should only be used for initialization phase.
 *****************************************************************************/
void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
{
    *pi_red =   i_pixel & p_vout->i_red_mask;
    *pi_green = i_pixel & p_vout->i_green_mask;
    *pi_blue =  i_pixel & p_vout->i_blue_mask;
}

Michel Kaempf's avatar
Michel Kaempf committed
750 751
/* following functions are local */

Vincent Seguin's avatar
Vincent Seguin committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
/*****************************************************************************
 * BinaryLog: computes the base 2 log of a binary value
 *****************************************************************************
 * This functions is used by MaskToShift, to get a bit index from a binary 
 * value.
 *****************************************************************************/
static int BinaryLog(u32 i)
{
    int i_log;

    i_log = 0;
    if (i & 0xffff0000) 
    {        
        i_log = 16;
    }    
    if (i & 0xff00ff00) 
    {        
        i_log += 8;
    }    
    if (i & 0xf0f0f0f0) 
    {        
        i_log += 4;
    }    
    if (i & 0xcccccccc) 
    {        
        i_log += 2;
    }    
    if (i & 0xaaaaaaaa) 
    {        
        i_log++;
    }    
    if (i != ((u32)1 << i_log))
    {        
	intf_ErrMsg("internal error: binary log overflow\n");        
    }    

    return( i_log );
}

/*****************************************************************************
 * MaskToShift: transform a color mask into right and left shifts
 *****************************************************************************
 * This function is used for obtaining color shifts from masks.
 *****************************************************************************/
static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
{
    u32 i_low, i_high;                 /* lower hand higher bits of the mask */

    /* Get bits */
    i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
    i_high = i_mask + i_low;                       /* higher bit of the mask */

    /* Transform bits into an index */
    i_low =  BinaryLog (i_low);
    i_high = BinaryLog (i_high);

    /* Update pointers and return */
    *pi_left =   i_low;
    *pi_right = (8 - i_high + i_low);
}

813
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
814
 * InitThread: initialize video output thread
815
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
816 817 818
 * This function is called from RunThread and performs the second step of the
 * initialization. It returns 0 on success. Note that the thread's flag are not
 * modified inside this function.
819
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
820 821 822
static int InitThread( vout_thread_t *p_vout )
{
    /* Update status */
823
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
824 825
    *p_vout->pi_status = THREAD_START;    

826 827
    /* Initialize output method - this function issues its own error messages */
    if( vout_SysInit( p_vout ) )
Michel Kaempf's avatar
Michel Kaempf committed
828 829 830 831
    {
        return( 1 );
    } 

832
    /* Initialize convertion tables and functions */
833
    if( vout_InitYUV( p_vout ) )
834
    {
835
        intf_ErrMsg("error: can't allocate YUV translation tables\n");
836
        return( 1 );                
Vincent Seguin's avatar
Vincent Seguin committed
837 838
    }
    
Michel Kaempf's avatar
Michel Kaempf committed
839
    /* Mark thread as running and return */
840 841
    p_vout->b_active =          1;    
    *p_vout->pi_status =        THREAD_READY;    
842
    intf_DbgMsg("thread ready\n");    
843
    return( 0 );    
Michel Kaempf's avatar
Michel Kaempf committed
844 845
}

846
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
847
 * RunThread: video output thread
848
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
849 850 851
 * Video output thread. This function does only returns when the thread is
 * terminated. It handles the pictures arriving in the video heap and the
 * display device events.
852
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
853 854
static void RunThread( vout_thread_t *p_vout)
{
855
    int             i_index;                                 /* index in heap */
856
    mtime_t         current_date;                             /* current date */
857
    mtime_t         display_date;                             /* display date */    
858 859
    boolean_t       b_display;                                /* display flag */    
    picture_t *     p_pic;                                 /* picture pointer */
860
    subpicture_t *  p_subpic;                           /* subpicture pointer */    
861
     
Michel Kaempf's avatar
Michel Kaempf committed
862
    /* 
863
     * Initialize thread
Michel Kaempf's avatar
Michel Kaempf committed
864 865 866 867
     */
    p_vout->b_error = InitThread( p_vout );
    if( p_vout->b_error )
    {
868
        DestroyThread( p_vout, THREAD_ERROR );
Michel Kaempf's avatar
Michel Kaempf committed
869 870
        return;        
    }    
871
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
872 873 874 875 876 877

    /*
     * Main loop - it is not executed if an error occured during
     * initialization
     */
    while( (!p_vout->b_die) && (!p_vout->b_error) )
878 879 880
    {
        /* Initialize loop variables */
        p_pic =         NULL;
881
        p_subpic =      NULL;
882 883 884
        display_date =  0;        
        current_date =  mdate();

Vincent Seguin's avatar
Vincent Seguin committed
885
        /* 
886
	 * Find the picture to display - this operation does not need lock,
887
         * since only READY_PICTUREs are handled 
888
         */
889
        for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
Vincent Seguin's avatar
Vincent Seguin committed
890
	{
891
	    if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
Vincent Seguin's avatar
Vincent Seguin committed
892
		( (p_pic == NULL) || 
893
		  (p_vout->p_picture[i_index].date < display_date) ) )
Vincent Seguin's avatar
Vincent Seguin committed
894
	    {
895 896
                p_pic = &p_vout->p_picture[i_index];
                display_date = p_pic->date;                
Vincent Seguin's avatar
Vincent Seguin committed
897 898
	    }
	}
Vincent Seguin's avatar
Vincent Seguin committed
899
 
Vincent Seguin's avatar
Vincent Seguin committed
900
        if( p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
901
        {
902 903
#ifdef STATS
            /* Computes FPS rate */
904
            p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
905
#endif	    
906
	    if( display_date < current_date )
Vincent Seguin's avatar
Vincent Seguin committed
907
	    {
908 909
		/* Picture is late: it will be destroyed and the thread will sleep and
                 * go to next picture */
Vincent Seguin's avatar
Vincent Seguin committed
910
                vlc_mutex_lock( &p_vout->picture_lock );
911
                p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
912 913
		intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
                vlc_mutex_unlock( &p_vout->picture_lock );
914
                p_pic =         NULL;                
915
                display_date =  0;                
Vincent Seguin's avatar
Vincent Seguin committed
916
	    }
917
	    else if( display_date > current_date + VOUT_DISPLAY_DELAY )
Vincent Seguin's avatar
Vincent Seguin committed
918 919 920 921
	    {
		/* A picture is ready to be rendered, but its rendering date is
		 * far from the current one so the thread will perform an empty loop
		 * as if no picture were found. The picture state is unchanged */
922
                p_pic =         NULL;                
923
                display_date =  0;                
Vincent Seguin's avatar
Vincent Seguin committed
924
	    }
Michel Kaempf's avatar
Michel Kaempf committed
925
        }
926 927

        /*
928
         * Find the subpicture to display - this operation does not need lock, since
929 930
         * only READY_SUBPICTURES are handled. If no picture has been selected,
         * display_date will depend on the subpicture
931 932 933
         */
        //??

Vincent Seguin's avatar
Vincent Seguin committed
934 935
        /*
         * Perform rendering, sleep and display rendered picture
936
         */
937
        if( p_pic )                          /* picture and perhaps subpicture */
938
        {
939
            b_display = p_vout->b_active;            
940 941
            p_vout->last_display_date = display_date;
            
942 943 944 945 946 947 948
            if( b_display )
            {                
                /* Set picture dimensions and clear buffer */
                SetBufferPicture( p_vout, p_pic );

                /* Render picture and informations */
                RenderPicture( p_vout, p_pic );             
Vincent Seguin's avatar
Vincent Seguin committed
949 950
                if( p_vout->b_info )
                {
951 952 953
                    RenderPictureInfo( p_vout, p_pic );
                    RenderInfo( p_vout );                
                }
Vincent Seguin's avatar
Vincent Seguin committed
954
            }
955
            
Vincent Seguin's avatar
Vincent Seguin committed
956 957 958 959
            /* Remove picture from heap */
            vlc_mutex_lock( &p_vout->picture_lock );
            p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
            vlc_mutex_unlock( &p_vout->picture_lock );                          
960

961
            /* Render interface and subpicture */
962 963 964 965
            if( b_display && p_vout->b_interface )
            {
                RenderInterface( p_vout );                
            }
966
            if( p_subpic )
967 968 969
            {
                if( b_display )
                {                    
970
                    RenderSubPicture( p_vout, p_subpic );
971 972
                }                

973 974 975 976
                /* Remove subpicture from heap */
                vlc_mutex_lock( &p_vout->subpicture_lock );
                p_subpic->i_status = DESTROYED_SUBPICTURE;
                vlc_mutex_unlock( &p_vout->subpicture_lock );                          
977 978 979
            }

        }
980
        else if( p_subpic )                                /* subpicture alone */
Vincent Seguin's avatar
Vincent Seguin committed
981
        {
982
            b_display = p_vout->b_active;
983
            p_vout->last_display_date = display_date;            
984 985

            if( b_display )
Vincent Seguin's avatar
Vincent Seguin committed
986
            {                
987 988 989
                /* Clear buffer */
                SetBufferPicture( p_vout, NULL );

Vincent Seguin's avatar