video_output.c 40.3 KB
Newer Older
Michel Kaempf's avatar
Michel Kaempf committed
1 2
/*******************************************************************************
 * video_output.c : video output thread
3
 * (c)2000 VideoLAN
Michel Kaempf's avatar
Michel Kaempf committed
4 5 6 7 8 9 10 11 12
 *******************************************************************************
 * 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.
 *******************************************************************************/

/*******************************************************************************
 * Preamble
 *******************************************************************************/
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_sys.h"
25
#include "video_yuv.h"
Vincent Seguin's avatar
Vincent Seguin committed
26
#include "intf_msg.h"
27
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
28 29

/*******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
30
 * Local prototypes
Vincent Seguin's avatar
Vincent Seguin committed
31
 *******************************************************************************/
32 33 34 35
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 );
Vincent Seguin's avatar
Vincent Seguin committed
36 37 38 39 40
static void     RenderBlank             ( vout_thread_t *p_vout );
static int      RenderPicture           ( vout_thread_t *p_vout, picture_t *p_pic, boolean_t b_blank );
static int      RenderPictureInfo       ( vout_thread_t *p_vout, picture_t *p_pic, boolean_t b_blank );
static int      RenderIdle              ( vout_thread_t *p_vout, boolean_t b_blank );
static int      RenderInfo              ( vout_thread_t *p_vout, boolean_t b_balnk );
Vincent Seguin's avatar
Vincent Seguin committed
41
static int      Manage                  ( vout_thread_t *p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
42 43 44 45 46 47 48

/*******************************************************************************
 * vout_CreateThread: creates a new video output thread
 *******************************************************************************
 * 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.
49
 * If not, it will be updated using one of the THREAD_* constants.
Michel Kaempf's avatar
Michel Kaempf committed
50
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
51 52
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
53 54 55 56
{
    vout_thread_t * p_vout;                               /* thread descriptor */
    int             i_status;                                 /* thread status */

57
    /* Allocate descriptor */
Michel Kaempf's avatar
Michel Kaempf committed
58
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
59
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
60
    {
61
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));        
Michel Kaempf's avatar
Michel Kaempf committed
62 63
        return( NULL );
    }
64

65 66 67 68 69 70 71
    /* Initialize thread properties */
    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;    

72
    /* Initialize some fields used by the system-dependant method - these fields will
73
     * probably be modified by the method, and are only preferences */
Vincent Seguin's avatar
Vincent Seguin committed
74 75 76
#ifdef DEBUG
    p_vout->b_info              = 1;    
#else
77
    p_vout->b_info              = 0;    
Vincent Seguin's avatar
Vincent Seguin committed
78
#endif
79 80
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, 
                                                       VOUT_GRAYSCALE_DEFAULT );
81 82
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
83
    p_vout->i_bytes_per_line    = i_width * 2;    
84 85 86 87
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
    p_vout->f_x_ratio           = 1;
    p_vout->f_y_ratio           = 1;
88
    p_vout->f_gamma             = VOUT_GAMMA;    
89
    intf_DbgMsg("wished configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line), ratio %.2f:%.2f, gray=%d\n",
90
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
91 92
                p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
                p_vout->f_x_ratio, p_vout->f_y_ratio, p_vout->b_grayscale );
93 94 95
   
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
Vincent Seguin's avatar
Vincent Seguin committed
96
    if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
97
    {
Vincent Seguin's avatar
Vincent Seguin committed
98 99
      free( p_vout );
      return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
100
    }
101
    intf_DbgMsg("actual configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line), ratio %.2f:%.2f, gray=%d\n",
102
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
103 104
                p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
                p_vout->f_x_ratio, p_vout->f_y_ratio, p_vout->b_grayscale );
105

106
#ifdef STATS
107
    /* Initialize statistics fields */
Vincent Seguin's avatar
Vincent Seguin committed
108
    p_vout->render_time           = 0;    
Vincent Seguin's avatar
Vincent Seguin committed
109
    p_vout->c_fps_samples       = 0;    
110
#endif      
Michel Kaempf's avatar
Michel Kaempf committed
111

Vincent Seguin's avatar
Vincent Seguin committed
112 113 114 115 116
    /* Initialize running properties */
    p_vout->i_changes           = 0;
    p_vout->last_picture_date   = 0;
    p_vout->last_display_date   = 0;

Michel Kaempf's avatar
Michel Kaempf committed
117
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
118 119
    vlc_mutex_init( &p_vout->picture_lock );
    vlc_mutex_init( &p_vout->subtitle_lock );    
Vincent Seguin's avatar
Vincent Seguin committed
120 121 122
    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
123
    {
124
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
125
	vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
126 127 128 129
        free( p_vout );
        return( NULL );
    }   

130 131 132
    intf_Msg("Video: display initialized (%dx%d, %d bpp)\n", 
             p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth );    

Michel Kaempf's avatar
Michel Kaempf committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    /* 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 );
}

/*******************************************************************************
 * vout_DestroyThread: destroys a previously created thread
 *******************************************************************************
 * 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.
 *******************************************************************************/
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
{  
    int     i_status;                                         /* thread status */

    /* Set status */
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
Vincent Seguin's avatar
Vincent Seguin committed
163
    *p_vout->pi_status = THREAD_DESTROY;    
Michel Kaempf's avatar
Michel Kaempf committed
164 165 166 167 168
     
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
169
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
170 171 172 173 174 175 176 177 178
    {
        do
        {
            msleep( THREAD_SLEEP );
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
                && (i_status != THREAD_FATAL) );   
    }
}

Vincent Seguin's avatar
Vincent Seguin committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/*******************************************************************************
 * vout_DisplaySubtitle: display a subtitle
 *******************************************************************************
 * Remove the reservation flag of a subtitle, which will cause it to be ready for
 * display. The picture does not need to be locked, since it is ignored by
 * the output thread if is reserved.
 *******************************************************************************/
void  vout_DisplaySubtitle( vout_thread_t *p_vout, subtitle_t *p_sub )
{
#ifdef DEBUG_VIDEO
    char        psz_begin_date[MSTRTIME_MAX_SIZE];   /* buffer for date string */
    char        psz_end_date[MSTRTIME_MAX_SIZE];     /* buffer for date string */
#endif

#ifdef DEBUG
    /* Check if status is valid */
    if( p_sub->i_status != RESERVED_SUBTITLE )
    {
        intf_DbgMsg("error: subtitle %p has invalid status %d\n", p_sub, p_sub->i_status );       
    }   
#endif

    /* Remove reservation flag */
    p_sub->i_status = READY_SUBTITLE;

#ifdef DEBUG_VIDEO
    /* Send subtitle informations */
    intf_DbgMsg("subtitle %p: type=%d, begin date=%s, end date=%s\n", p_sub, p_sub->i_type, 
                mstrtime( psz_begin_date, p_sub->begin_date ), 
                mstrtime( psz_end_date, p_sub->end_date ) );    
#endif
}

/*******************************************************************************
 * vout_CreateSubtitle: allocate a subtitle in the video output heap.
 *******************************************************************************
 * This function create a reserved subtitle in the video output heap. 
 * A null pointer is returned if the function fails. This method provides an
 * already allocated zone of memory in the subtitle data fields. It needs locking
 * since several pictures can be created by several producers threads. 
 *******************************************************************************/
subtitle_t *vout_CreateSubtitle( vout_thread_t *p_vout, int i_type, 
                                 int i_size )
{
    //???
}

/*******************************************************************************
 * vout_DestroySubtitle: remove a permanent or reserved subtitle from the heap
 *******************************************************************************
 * This function frees a previously reserved subtitle.
 * It is meant to be used when the construction of a picture aborted.
 * This function does not need locking since reserved subtitles are ignored by
 * the output thread.
 *******************************************************************************/
void vout_DestroySubtitle( vout_thread_t *p_vout, subtitle_t *p_sub )
{
#ifdef DEBUG
   /* Check if subtitle status is valid */
   if( p_sub->i_status != RESERVED_SUBTITLE )
   {
       intf_DbgMsg("error: subtitle %p has invalid status %d\n", p_sub, p_sub->i_status );       
   }   
#endif

    p_sub->i_status = DESTROYED_SUBTITLE;

#ifdef DEBUG_VIDEO
    intf_DbgMsg("subtitle %p\n", p_sub);    
#endif
}

Michel Kaempf's avatar
Michel Kaempf committed
251
/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
252
 * vout_DisplayPicture: display a picture
Michel Kaempf's avatar
Michel Kaempf committed
253
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
254
 * Remove the reservation flag of a picture, which will cause it to be ready for
255 256
 * display. The picture does not need to be locked, since it is ignored by
 * the output thread if is reserved.
Michel Kaempf's avatar
Michel Kaempf committed
257
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
258
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
259
{
260 261 262 263
#ifdef DEBUG_VIDEO
    char        psz_date[MSTRTIME_MAX_SIZE];         /* buffer for date string */
#endif

264
#ifdef DEBUG
265 266 267
    /* Check if picture status is valid */
    if( p_pic->i_status != RESERVED_PICTURE )
    {
Vincent Seguin's avatar
Vincent Seguin committed
268
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );       
269
    }   
270
#endif
Michel Kaempf's avatar
Michel Kaempf committed
271 272

    /* Remove reservation flag */
Vincent Seguin's avatar
Vincent Seguin committed
273
    p_pic->i_status = READY_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
274

275 276 277 278 279
#ifdef DEBUG_VIDEO
    /* Send picture informations */
    intf_DbgMsg("picture %p: type=%d, %dx%d, date=%s\n", p_pic, p_pic->i_type, 
                p_pic->i_width,p_pic->i_height, mstrtime( psz_date, p_pic->date ) );    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
280 281 282
}

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
283
 * vout_CreatePicture: allocate a picture in the video output heap.
Michel Kaempf's avatar
Michel Kaempf committed
284 285
 *******************************************************************************
 * This function create a reserved image in the video output heap. 
Vincent Seguin's avatar
Vincent Seguin committed
286
 * A null pointer is returned if the function fails. This method provides an
287
 * already allocated zone of memory in the picture data fields. It needs locking
288
 * since several pictures can be created by several producers threads. 
Michel Kaempf's avatar
Michel Kaempf committed
289
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
290
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type, 
291
			       int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
292
{
Vincent Seguin's avatar
Vincent Seguin committed
293
    int         i_picture;                                    /* picture index */
Vincent Seguin's avatar
Vincent Seguin committed
294
    int         i_chroma_width = 0;                            /* chroma width */    
Vincent Seguin's avatar
Vincent Seguin committed
295 296
    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
297

Vincent Seguin's avatar
Vincent Seguin committed
298
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
299
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
300

Vincent Seguin's avatar
Vincent Seguin committed
301 302 303 304 305 306 307 308 309
    /* 
     * Look for an empty place 
     */
    for( i_picture = 0; 
         i_picture < VOUT_MAX_PICTURES; 
         i_picture++ )
    {
	if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
	{
310 311 312
	    /* 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
313 314
	    if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
		(p_vout->p_picture[i_picture].i_height         == i_height) &&
315
		(p_vout->p_picture[i_picture].i_width          == i_width) )
Vincent Seguin's avatar
Vincent Seguin committed
316 317 318 319 320
	    {
		/* 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;
321 322 323 324
#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
325
		vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		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
341 342
    }

Vincent Seguin's avatar
Vincent Seguin committed
343 344 345 346 347 348 349
    /* 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
350 351
    }

Vincent Seguin's avatar
Vincent Seguin committed
352 353 354 355 356 357 358
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
359
        {
360
        case YUV_420_PICTURE:          /* YUV 420: 1,1/4,1/4 samples per pixel */
Vincent Seguin's avatar
Vincent Seguin committed
361 362
            i_chroma_width = i_width / 2;            
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
363
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
364 365
            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;
366 367
            break;
        case YUV_422_PICTURE:          /* YUV 422: 1,1/2,1/2 samples per pixel */
368 369 370
            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
371 372
            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;
373 374
            break;
        case YUV_444_PICTURE:              /* YUV 444: 1,1,1 samples per pixel */
375 376 377
            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
378 379
            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
380 381 382
            break;                
#ifdef DEBUG
        default:
383
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
384 385
            p_free_picture->p_data   =  NULL;            
            break;            
Vincent Seguin's avatar
Vincent Seguin committed
386
#endif    
Michel Kaempf's avatar
Michel Kaempf committed
387 388
        }

Vincent Seguin's avatar
Vincent Seguin committed
389 390
        if( p_free_picture->p_data != NULL )
        {        
391
            /* Copy picture informations, set some default values */
392 393
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
394
            p_free_picture->i_matrix_coefficients       = 1; 
395 396
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
397 398 399 400 401 402
            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;            
403
            p_free_picture->i_refcount                  = 0;            
Vincent Seguin's avatar
Vincent Seguin committed
404 405 406 407
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
408 409 410 411
            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
412 413
        }
        
414 415 416
#ifdef DEBUG_VIDEO
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
#endif
Vincent Seguin's avatar
Vincent Seguin committed
417
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
418
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
419
    }
Vincent Seguin's avatar
Vincent Seguin committed
420 421
    
    // No free or destroyed picture could be found
422
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
423
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
424
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
425 426 427
}

/*******************************************************************************
428
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
Michel Kaempf's avatar
Michel Kaempf committed
429 430
 *******************************************************************************
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
431 432
 * 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 !
433 434
 * This function does not need locking since reserved pictures are ignored by
 * the output thread.
Michel Kaempf's avatar
Michel Kaempf committed
435
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
436
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
437
{
438
#ifdef DEBUG
439 440 441
   /* Check if picture status is valid */
   if( p_pic->i_status != RESERVED_PICTURE )
   {
Vincent Seguin's avatar
Vincent Seguin committed
442
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );       
443 444 445
   }   
#endif

Vincent Seguin's avatar
Vincent Seguin committed
446
    p_pic->i_status = DESTROYED_PICTURE;
447 448 449 450

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
451 452 453 454 455 456
}

/*******************************************************************************
 * vout_LinkPicture: increment reference counter of a picture
 *******************************************************************************
 * This function increment the reference counter of a picture in the video
457
 * heap. It needs a lock since several producer threads can access the picture.
Michel Kaempf's avatar
Michel Kaempf committed
458 459 460
 *******************************************************************************/
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
461
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
462
    p_pic->i_refcount++;
Vincent Seguin's avatar
Vincent Seguin committed
463
    vlc_mutex_unlock( &p_vout->picture_lock );
464 465 466 467

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
468 469 470 471 472 473 474 475 476
}

/*******************************************************************************
 * vout_UnlinkPicture: decrement reference counter of a picture
 *******************************************************************************
 * This function decrement the reference counter of a picture in the video heap.
 *******************************************************************************/
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
477
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
478
    p_pic->i_refcount--;
479 480 481 482 483 484 485 486 487

#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
488
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
489
    {
Vincent Seguin's avatar
Vincent Seguin committed
490
	p_pic->i_status = DESTROYED_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
491
    }
Vincent Seguin's avatar
Vincent Seguin committed
492
    vlc_mutex_unlock( &p_vout->picture_lock );
493 494 495 496

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
}

/* following functions are local */

/*******************************************************************************
 * InitThread: initialize video output thread
 *******************************************************************************
 * 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.
 *******************************************************************************/
static int InitThread( vout_thread_t *p_vout )
{
    int     i_index;                                          /* generic index */    

    /* Update status */
    *p_vout->pi_status = THREAD_START;    

515 516
    /* Initialize output method - this function issues its own error messages */
    if( vout_SysInit( p_vout ) )
Michel Kaempf's avatar
Michel Kaempf committed
517 518 519 520 521
    {
        *p_vout->pi_status = THREAD_ERROR;        
        return( 1 );
    } 

Vincent Seguin's avatar
Vincent Seguin committed
522
    /* Initialize pictures and subtitles */    
523
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
Vincent Seguin's avatar
Vincent Seguin committed
524
    {
Vincent Seguin's avatar
Vincent Seguin committed
525 526 527 528
        p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status = FREE_PICTURE;
        p_vout->p_subtitle[i_index].i_type  = EMPTY_SUBTITLE;
        p_vout->p_subtitle[i_index].i_status= FREE_SUBTITLE;
529 530 531 532 533 534
    }

    /* Initialize convertion tables and functions */
    if( vout_InitTables( p_vout ) )
    {
        intf_ErrMsg("error: can't allocate translation tables\n");
535
        return( 1 );                
Vincent Seguin's avatar
Vincent Seguin committed
536 537
    }
    
Michel Kaempf's avatar
Michel Kaempf committed
538
    /* Mark thread as running and return */
539 540
    p_vout->b_active =          1;    
    *p_vout->pi_status =        THREAD_READY;    
541
    intf_DbgMsg("thread ready\n");    
542
    return( 0 );    
Michel Kaempf's avatar
Michel Kaempf committed
543 544 545 546 547 548 549 550 551 552 553 554 555
}

/*******************************************************************************
 * RunThread: video output thread
 *******************************************************************************
 * 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.
 *******************************************************************************/
static void RunThread( vout_thread_t *p_vout)
{
    int             i_picture;                                /* picture index */
    mtime_t         current_date;                              /* current date */
556
    mtime_t         pic_date = 0;                              /* picture date */    
557 558 559
    boolean_t       b_display;                                 /* display flag */    
    picture_t *     p_pic;                                  /* picture pointer */
     
Michel Kaempf's avatar
Michel Kaempf committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    /* 
     * Initialize thread and free configuration 
     */
    p_vout->b_error = InitThread( p_vout );
    if( p_vout->b_error )
    {
        free( p_vout );                                  /* destroy descriptor */
        return;        
    }    

    /*
     * Main loop - it is not executed if an error occured during
     * initialization
     */
    while( (!p_vout->b_die) && (!p_vout->b_error) )
575
    {            
Vincent Seguin's avatar
Vincent Seguin committed
576
        /* 
577 578 579
	 * Find the picture to display - this operation does not need lock,
         * since only READY_PICTURES are handled 
         */
580
        p_pic = NULL;         
Vincent Seguin's avatar
Vincent Seguin committed
581
        current_date = mdate();
582
        for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
583 584 585
	{
	    if( (p_vout->p_picture[i_picture].i_status == READY_PICTURE) &&
		( (p_pic == NULL) || 
586
		  (p_vout->p_picture[i_picture].date < pic_date) ) )
Vincent Seguin's avatar
Vincent Seguin committed
587
	    {
588
                p_pic = &p_vout->p_picture[i_picture];
589
                pic_date = p_pic->date;                
Vincent Seguin's avatar
Vincent Seguin committed
590 591
	    }
	}
Vincent Seguin's avatar
Vincent Seguin committed
592
 
Vincent Seguin's avatar
Vincent Seguin committed
593 594 595 596
        /* 
	 * Render picture if any
	 */
        if( p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
597
        {
598 599
#ifdef STATS
            /* Computes FPS rate */
600 601
            p_vout->fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = pic_date;
#endif	    
602
	    if( pic_date < current_date )
Vincent Seguin's avatar
Vincent Seguin committed
603
	    {
604 605
		/* Picture is late: it will be destroyed and the thread will sleep and
                 * go to next picture */
Vincent Seguin's avatar
Vincent Seguin committed
606
                vlc_mutex_lock( &p_vout->picture_lock );
607
                p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
608
                vlc_mutex_unlock( &p_vout->picture_lock );
609 610 611
#ifdef DEBUG_VIDEO
		intf_DbgMsg( "warning: late picture %p skipped\n", p_pic );
#endif
612
                p_pic =         NULL;                
Vincent Seguin's avatar
Vincent Seguin committed
613
	    }
614
	    else if( pic_date > current_date + VOUT_DISPLAY_DELAY )
Vincent Seguin's avatar
Vincent Seguin committed
615 616 617 618
	    {
		/* 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 */
619
                p_pic =         NULL;                
Vincent Seguin's avatar
Vincent Seguin committed
620
	    }
Michel Kaempf's avatar
Michel Kaempf committed
621
        }
Vincent Seguin's avatar
Vincent Seguin committed
622 623 624
              
        /*
         * Perform rendering, sleep and display rendered picture
625
         */
Vincent Seguin's avatar
Vincent Seguin committed
626
        if( p_pic )
627
        {
Vincent Seguin's avatar
Vincent Seguin committed
628 629 630
            /* A picture is ready to be displayed : render it */
            if( p_vout->b_active )
            {                    
Vincent Seguin's avatar
Vincent Seguin committed
631
                b_display = RenderPicture( p_vout, p_pic, 1 );
Vincent Seguin's avatar
Vincent Seguin committed
632 633
                if( p_vout->b_info )
                {
Vincent Seguin's avatar
Vincent Seguin committed
634 635
                    b_display |= RenderPictureInfo( p_vout, p_pic, b_display );
                    b_display |= RenderInfo( p_vout, b_display );                    
Vincent Seguin's avatar
Vincent Seguin committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
                }                    
            }
            else
            {
                b_display = 0;                
            } 

            /* 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 );                          
        }            
        else
        {
            /* No picture. However, an idle screen may be ready to display */
Vincent Seguin's avatar
Vincent Seguin committed
651 652 653 654 655 656 657 658 659 660 661 662
            if( p_vout->b_active )
            {                
                b_display = RenderIdle( p_vout, 1 );
                if( p_vout->b_info )
                {                    
                    b_display |= RenderInfo( p_vout, b_display );
                }        
            }
            else
            {
                b_display = 0;                
            }            
Vincent Seguin's avatar
Vincent Seguin committed
663 664
        }

Vincent Seguin's avatar
Vincent Seguin committed
665 666 667
        /* Give back change lock */
        vlc_mutex_unlock( &p_vout->change_lock );        

Vincent Seguin's avatar
Vincent Seguin committed
668 669 670 671 672 673 674 675 676 677
        /* Sleep a while or until a given date */
        if( p_pic )
        {
            mwait( pic_date );
        }
        else
        {
            msleep( VOUT_IDLE_SLEEP );                
        }            

Vincent Seguin's avatar
Vincent Seguin committed
678 679 680 681
        /* On awakening, take back lock and send immediately picture to display */
        vlc_mutex_lock( &p_vout->change_lock );        
        if( b_display && p_vout->b_active && 
            !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
Vincent Seguin's avatar
Vincent Seguin committed
682 683 684
        {
            vout_SysDisplay( p_vout );
        }
685

Vincent Seguin's avatar
Vincent Seguin committed
686
        /*
Vincent Seguin's avatar
Vincent Seguin committed
687
         * Check events and manage thread
Vincent Seguin's avatar
Vincent Seguin committed
688
	 */
Vincent Seguin's avatar
Vincent Seguin committed
689
        if( vout_SysManage( p_vout ) | Manage( p_vout ) )
Vincent Seguin's avatar
Vincent Seguin committed
690 691 692 693 694
	{
	    /* A fatal error occured, and the thread must terminate immediately,
	     * without displaying anything - setting b_error to 1 cause the
	     * immediate end of the main while() loop. */
	    p_vout->b_error = 1;
Vincent Seguin's avatar
Vincent Seguin committed
695
	}  
Michel Kaempf's avatar
Michel Kaempf committed
696 697 698 699 700 701 702 703 704 705 706 707
    } 

    /*
     * Error loop
     */
    if( p_vout->b_error )
    {
        ErrorThread( p_vout );        
    }

    /* End of thread */
    EndThread( p_vout );
708
    intf_DbgMsg( "thread end\n" );
Michel Kaempf's avatar
Michel Kaempf committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
}

/*******************************************************************************
 * ErrorThread: RunThread() error loop
 *******************************************************************************
 * This function is called when an error occured during thread main's loop. The
 * thread can still receive feed, but must be ready to terminate as soon as
 * possible.
 *******************************************************************************/
static void ErrorThread( vout_thread_t *p_vout )
{
    /* Wait until a `die' order */
    while( !p_vout->b_die )
    {
        /* Sleep a while */
        msleep( VOUT_IDLE_SLEEP );                
    }
}

/*******************************************************************************
 * EndThread: thread destruction
 *******************************************************************************
 * This function is called when the thread ends after a sucessfull 
 * initialization.
 *******************************************************************************/
static void EndThread( vout_thread_t *p_vout )
{
    int *   pi_status;                                        /* thread status */
Vincent Seguin's avatar
Vincent Seguin committed
737
    int     i_picture;
Michel Kaempf's avatar
Michel Kaempf committed
738 739 740 741 742
        
    /* Store status */
    pi_status = p_vout->pi_status;    
    *pi_status = THREAD_END;    

Vincent Seguin's avatar
Vincent Seguin committed
743 744 745 746 747 748 749 750
    /* Destroy all remaining pictures */
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
    {
	if( p_vout->p_picture[i_picture].i_status != FREE_PICTURE )
	{
            free( p_vout->p_picture[i_picture].p_data );
        }
    }
Vincent Seguin's avatar
Vincent Seguin committed
751

752
    /* Destroy translation tables */
753
    vout_EndTables( p_vout );
Vincent Seguin's avatar
Vincent Seguin committed
754
    
Michel Kaempf's avatar
Michel Kaempf committed
755
    /* Destroy thread structures allocated by InitThread */
Vincent Seguin's avatar
Vincent Seguin committed
756
    vout_SysEnd( p_vout );
757
    vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
758 759 760 761 762 763
    free( p_vout );

    /* Update status */
    *pi_status = THREAD_OVER;    
}

Vincent Seguin's avatar
Vincent Seguin committed
764 765 766 767 768 769 770 771
/*******************************************************************************
 * RenderBlank: render a blank screen
 *******************************************************************************
 * This function is called by all other rendering functions when they arrive on
 * a non blanked screen.
 *******************************************************************************/
static void RenderBlank( vout_thread_t *p_vout )
{
772
    //?? toooooo slow
Vincent Seguin's avatar
Vincent Seguin committed
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
    int  i_index;                                    /* current 32 bits sample */    
    int  i_width;                                 /* number of 32 bits samples */    
    u32 *p_pic;                                  /* pointer to 32 bits samples */
    
    /* Initialize variables */
    p_pic =     vout_SysGetPicture( p_vout );
    i_width =   p_vout->i_bytes_per_line * p_vout->i_height / 128;

    /* Clear beginning of screen by 128 bytes blocks */
    for( i_index = 0; i_index < i_width; i_index++ )
    {
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
        *p_pic++ = 0;   *p_pic++ = 0;
    }

    /* Clear last pixels */
    //??
}


Michel Kaempf's avatar
Michel Kaempf committed
807 808 809 810 811 812
/*******************************************************************************
 * RenderPicture: render a picture
 *******************************************************************************
 * This function convert a picture from a video heap to a pixel-encoded image
 * and copy it to the current rendering buffer. No lock is required, since the
 * rendered picture has been determined as existant, and will only be destroyed
Vincent Seguin's avatar
Vincent Seguin committed
813
 * by the vout thread later.
Michel Kaempf's avatar
Michel Kaempf committed
814
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
815
static int RenderPicture( vout_thread_t *p_vout, picture_t *p_pic, boolean_t b_blank )
Michel Kaempf's avatar
Michel Kaempf committed
816
{
817 818 819 820 821
#ifdef STATS
    /* Start recording render time */
    p_vout->render_time = mdate();
#endif

Vincent Seguin's avatar
Vincent Seguin committed
822 823 824 825 826 827
    /* Mark last picture date */
    p_vout->last_picture_date = p_pic->date;    

    /* Blank screen if required */
    if( b_blank )
    {
828
// ?????       RenderBlank( p_vout );
Vincent Seguin's avatar
Vincent Seguin committed
829 830
    }

831 832 833
    /* 
     * Prepare scaling 
     */
834 835 836 837 838 839
    if( (p_pic->i_width > p_vout->i_width) || (p_pic->i_height > p_vout->i_height) )
    {
#ifdef VIDEO_X11
        /* X11: window can be resized, so resize it - the picture won't be 
         * rendered since any alteration of the window size means recreating the
         * XImages */
Vincent Seguin's avatar
Vincent Seguin committed
840 841
/*        p_vout->i_new_width =   p_pic->i_width;
        p_vout->i_new_height =  p_pic->i_height;*/
842 843 844 845 846 847 848
#else
        /* Other drivers: the video output thread can't change its size, so
         * we need to change the aspect ratio */
        //????
#endif
    }    

849 850 851
    /*
     * Choose appropriate rendering function and render picture
     */
852 853
    switch( p_pic->i_type )
    {
854 855 856 857
    case YUV_420_PICTURE:
        p_vout->p_ConvertYUV420( p_vout, vout_SysGetPicture( p_vout ),
                                 p_pic->p_y, p_pic->p_u, p_pic->p_v,
                                 p_pic->i_width, p_pic->i_height, 0, 0,
Vincent Seguin's avatar
Vincent Seguin committed
858
                                 4, p_pic->i_matrix_coefficients );
859
        break;        
860
    case YUV_422_PICTURE:
Vincent Seguin's avatar
Vincent Seguin committed
861
 /* ??? p_vout->p_convert_yuv_420( p_vout, 
862 863
                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
                                   i_chroma_width, i_chroma_height,
864
                                    p_vout->i_width / 2, p_vout->i_height,
865 866 867
                                   p_vout->i_bytes_per_line,
                                   0, 0, 0 );
  */      break;        
868
    case YUV_444_PICTURE:
869 870 871 872 873 874 875
/*  ???      p_vout->p_convert_yuv_420( p_vout, 
                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
                                   i_chroma_width, i_chroma_height,
                                   p_vout->i_width, p_vout->i_height,
                                   p_vout->i_bytes_per_line,
                                   0, 0, 0 );
  */      break;                
876 877
#ifdef DEBUG
    default:        
878
        intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
879 880 881
        break;        
#endif
    }
882

883 884 885 886
    /* 
     * Terminate scaling 
     */
    //??
Vincent Seguin's avatar
Vincent Seguin committed
887

888 889 890 891
#ifdef STATS
    /* End recording render time */
    p_vout->render_time = mdate() - p_vout->render_time;
#endif
Vincent Seguin's avatar
Vincent Seguin committed
892
    return( 1 );    
Michel Kaempf's avatar
Michel Kaempf committed
893 894
}

895
/*******************************************************************************
896
 * RenderPictureInfo: print additionnal informations on a picture
897
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
898 899
 * This function will print informations such as fps and other picture
 * dependant informations.
900
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
901
static int RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic, boolean_t b_blank )
902 903 904 905
{
    char        psz_buffer[256];                              /* string buffer */

#ifdef STATS
906 907 908
    /* 
     * Print FPS rate in upper right corner 
     */
909
    if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
910 911
    {        
        sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
912 913
                 ( p_vout->fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
                   p_vout->fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );        
914 915 916
        vout_SysPrint( p_vout, p_vout->i_width, 0, 1, -1, psz_buffer );
    }

917
    /* 
Vincent Seguin's avatar
Vincent Seguin committed
918
     * Print frames count and loop time in upper left corner 
919
     */
Vincent Seguin's avatar
Vincent Seguin committed
920 921
    sprintf( psz_buffer, "%ld frames   render time: %lu us", 
             p_vout->c_fps_samples, (long unsigned) p_vout->render_time );
922 923
    vout_SysPrint( p_vout, 0, 0, -1, -1, psz_buffer );    
#endif
Vincent Seguin's avatar
Vincent Seguin committed
924

925
#ifdef DEBUG
Vincent Seguin's avatar
Vincent Seguin committed
926 927
    /*
     * Print picture information in lower right corner
928
     */
929
    sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s)",
Vincent Seguin's avatar
Vincent Seguin committed
930 931
             (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
             ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
932 933
              ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
             p_pic->i_width, p_pic->i_height,
Vincent Seguin's avatar
Vincent Seguin committed
934 935
             p_pic->i_display_width, p_pic->i_display_height,
             p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
936
             (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
Vincent Seguin's avatar
Vincent Seguin committed
937 938
             ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
              ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
939
               ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))));    
Vincent Seguin's avatar
Vincent Seguin committed
940
    vout_SysPrint( p_vout, p_vout->i_width, p_vout->i_height, 1, 1, psz_buffer );
941
#endif
Vincent Seguin's avatar
Vincent Seguin committed
942 943
    
    return( 0 );    
944 945
}

946 947 948
/*******************************************************************************
 * RenderIdle: render idle picture
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
949
 * This function will clear the display or print a logo.
950
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
951
static int RenderIdle( vout_thread_t *p_vout, boolean_t b_blank )
952
{
Vincent Seguin's avatar
Vincent Seguin committed
953 954 955 956 957 958 959 960 961 962 963 964
    /* Blank screen if required */
    if( (mdate() - p_vout->last_picture_date > VOUT_IDLE_DELAY) &&
        (p_vout->last_picture_date > p_vout->last_display_date) &&
        b_blank )
    {        
        RenderBlank( p_vout );
        p_vout->last_display_date = mdate();        
        vout_SysPrint( p_vout, p_vout->i_width / 2, p_vout->i_height / 2, 0, 0,
                       "no stream" );        
        return( 1 );        
    }

Vincent Seguin's avatar
Vincent Seguin committed
965 966
    return( 0 );    
}
967

Vincent Seguin's avatar
Vincent Seguin committed
968 969 970
/*******************************************************************************
 * RenderInfo: render additionnal informations
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
971 972
 * This function render informations which do not depend of the current picture
 * rendered.
Vincent Seguin's avatar
Vincent Seguin committed
973
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
974
static int RenderInfo( vout_thread_t *p_vout, boolean_t b_blank )
Vincent Seguin's avatar
Vincent Seguin committed
975
{
Vincent Seguin's avatar
Vincent Seguin committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    char        psz_buffer[256];                              /* string buffer */
#ifdef DEBUG
    int         i_ready_pic = 0;                             /* ready pictures */
    int         i_reserved_pic = 0;                       /* reserved pictures */
    int         i_picture;                                    /* picture index */
#endif

#ifdef DEBUG
    /* 
     * Print thread state in lower left corner  
     */
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
    {
        switch( p_vout->p_picture[i_picture].i_status )
        {
        case RESERVED_PICTURE:
            i_reserved_pic++;            
            break;            
        case READY_PICTURE:
            i_ready_pic++;            
            break;            
        }        
    }
    sprintf( psz_buffer, "%s %dx%d:%d %.2f:%.2f g%+.2f   pic: %d/%d/%d", 
             p_vout->b_grayscale ? "gray" : "rgb", 
             p_vout->i_width, p_vout->i_height,
             p_vout->i_screen_depth, p_vout->f_x_ratio, p_vout->f_y_ratio, p_vout->f_gamma,
             i_reserved_pic, i_ready_pic,
             VOUT_MAX_PICTURES );
    vout_SysPrint( p_vout, 0, p_vout->i_height, -1, 1, psz_buffer );    
#endif
Vincent Seguin's avatar
Vincent Seguin committed
1007
    return( 0 );    
1008
}
1009

Vincent Seguin's avatar
Vincent Seguin committed
1010 1011 1012
/*******************************************************************************
 * Manage: manage thread
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
1013
 * This function will handle changes in thread configuration.
Vincent Seguin's avatar
Vincent Seguin committed
1014 1015 1016
 *******************************************************************************/
static int Manage( vout_thread_t *p_vout )
{
Vincent Seguin's avatar
Vincent Seguin committed
1017 1018 1019 1020 1021 1022 1023 1024
    /* On gamma or grayscale change, rebuild tables */
    if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE) )
    {
        vout_ResetTables( p_vout );        
    }    

    /* Clear changes flags which does not need management or have been handled */
    p_vout->i_changes &= ~(VOUT_INFO_CHANGE | VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE);
Vincent Seguin's avatar
Vincent Seguin committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

    /* Detect unauthorized changes */
    if( p_vout->i_changes )
    {
        /* Some changes were not acknowledged by vout_SysManage or this function,
         * it means they should not be authorized */
        intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );        
        return( 1 );        
    }
    
    return( 0 );    
}