video_output.c 45.6 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
 ******************************************************************************/
33 34 35 36
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
37
static void     Print                   ( vout_thread_t *p_vout, int i_x, int i_y, int i_halign, int i_valign, unsigned char *psz_text );
Vincent Seguin's avatar
Vincent Seguin committed
38 39 40 41 42
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
43
static int      Manage                  ( vout_thread_t *p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
44

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

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

68 69 70 71 72 73 74
    /* 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;    

75
    /* Initialize some fields used by the system-dependant method - these fields will
76
     * probably be modified by the method, and are only preferences */
77 78
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, 
                                                       VOUT_GRAYSCALE_DEFAULT );
79 80
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
81
    p_vout->i_bytes_per_line    = i_width * 2;    
82 83
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
84
    p_vout->f_gamma             = VOUT_GAMMA;    
Vincent Seguin's avatar
Vincent Seguin committed
85 86 87 88 89 90
#ifdef DEBUG
    p_vout->b_info              = 1;    
#else
    p_vout->b_info              = 0;    
#endif
    intf_DbgMsg("wished configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line)\n",
91
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
92
                p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line );
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
    }
Vincent Seguin's avatar
Vincent Seguin committed
101
    intf_DbgMsg("actual configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line)\n",
102
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
103
                p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line );
Vincent Seguin's avatar
Vincent Seguin committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    /* Load fonts */
    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 );        
    }    
121
 
122
#ifdef STATS
123
    /* Initialize statistics fields */
Vincent Seguin's avatar
Vincent Seguin committed
124
    p_vout->render_time           = 0;    
Vincent Seguin's avatar
Vincent Seguin committed
125
    p_vout->c_fps_samples       = 0;    
126
#endif      
Michel Kaempf's avatar
Michel Kaempf committed
127

Vincent Seguin's avatar
Vincent Seguin committed
128 129 130 131 132
    /* 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
133
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
134 135
    vlc_mutex_init( &p_vout->picture_lock );
    vlc_mutex_init( &p_vout->subtitle_lock );    
Vincent Seguin's avatar
Vincent Seguin committed
136 137 138
    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
139
    {
140
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
141 142
        vout_UnloadFont( p_vout->p_default_font );
        vout_UnloadFont( p_vout->p_large_font );        
Vincent Seguin's avatar
Vincent Seguin committed
143
	vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
144 145 146 147
        free( p_vout );
        return( NULL );
    }   

148
    intf_Msg("Video display initialized (%dx%d, %d bpp)\n", 
149 150
             p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth );    

Michel Kaempf's avatar
Michel Kaempf committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    /* 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 );
}

167
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
168
 * vout_DestroyThread: destroys a previously created thread
169
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
170 171 172 173
 * 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.
174
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
175 176
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
{  
177
    int     i_status;                                        /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
178 179

    /* Set status */
180
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
181
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
Vincent Seguin's avatar
Vincent Seguin committed
182
    *p_vout->pi_status = THREAD_DESTROY;    
Michel Kaempf's avatar
Michel Kaempf committed
183 184 185 186 187
     
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
188
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
189 190 191 192 193 194 195 196 197
    {
        do
        {
            msleep( THREAD_SLEEP );
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
                && (i_status != THREAD_FATAL) );   
    }
}

198
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
199
 * vout_DisplaySubtitle: display a subtitle
200
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
201 202 203
 * 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.
204
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
205 206 207
void  vout_DisplaySubtitle( vout_thread_t *p_vout, subtitle_t *p_sub )
{
#ifdef DEBUG_VIDEO
208 209
    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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#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
}

231
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
232
 * vout_CreateSubtitle: allocate a subtitle in the video output heap.
233
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
234 235 236 237
 * 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. 
238
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
239 240 241 242 243 244
subtitle_t *vout_CreateSubtitle( vout_thread_t *p_vout, int i_type, 
                                 int i_size )
{
    //???
}

245
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
246
 * vout_DestroySubtitle: remove a permanent or reserved subtitle from the heap
247
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
248 249 250 251
 * 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.
252
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
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
}

270
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
271
 * vout_DisplayPicture: display a picture
272
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
273
 * Remove the reservation flag of a picture, which will cause it to be ready for
274 275
 * display. The picture won't be displayed until vout_DatePicture has been 
 * called.
276
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
277
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
278
{
279 280
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
281
    {
282 283 284 285 286 287 288 289 290 291
    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;        
292
#endif
293
    }
Michel Kaempf's avatar
Michel Kaempf committed
294

295
#ifdef DEBUG_VIDEO
296
    intf_DbgMsg("picture %p\n", p_pic );
297
#endif
298 299

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

302
/******************************************************************************
303
 * vout_DatePicture: date a picture
304
 ******************************************************************************
305
 * Remove the reservation flag of a picture, which will cause it to be ready for
306 307
 * display. The picture won't be displayed until vout_DisplayPicture has been 
 * called.
308
 ******************************************************************************/
309 310
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    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
    intf_DbgMsg("picture %p\n", p_pic);
#endif
331 332

    vlc_mutex_unlock( &p_vout->picture_lock );
333 334
}

335
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
336
 * vout_CreatePicture: allocate a picture in the video output heap.
337
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
338
 * This function create a reserved image in the video output heap. 
Vincent Seguin's avatar
Vincent Seguin committed
339
 * A null pointer is returned if the function fails. This method provides an
340
 * already allocated zone of memory in the picture data fields. It needs locking
341
 * since several pictures can be created by several producers threads. 
342
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
343
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type, 
344
			       int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
345
{
346 347 348 349
    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
350

Vincent Seguin's avatar
Vincent Seguin committed
351
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
352
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
353

Vincent Seguin's avatar
Vincent Seguin committed
354 355 356 357 358 359 360 361 362
    /* 
     * 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 )
	{
363 364 365
	    /* 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
366 367
	    if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
		(p_vout->p_picture[i_picture].i_height         == i_height) &&
368
		(p_vout->p_picture[i_picture].i_width          == i_width) )
Vincent Seguin's avatar
Vincent Seguin committed
369 370 371 372 373
	    {
		/* 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;
374 375 376 377
#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
378
		vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		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
394 395
    }

Vincent Seguin's avatar
Vincent Seguin committed
396 397 398 399 400 401 402
    /* 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
403 404
    }

Vincent Seguin's avatar
Vincent Seguin committed
405 406 407 408 409 410 411
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
412
        {
413
        case YUV_420_PICTURE:          /* YUV 420: 1,1/4,1/4 samples per pixel */
Vincent Seguin's avatar
Vincent Seguin committed
414 415
            i_chroma_width = i_width / 2;            
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
416
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
417 418
            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;
419 420
            break;
        case YUV_422_PICTURE:          /* YUV 422: 1,1/2,1/2 samples per pixel */
421 422 423
            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
424 425
            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;
426 427
            break;
        case YUV_444_PICTURE:              /* YUV 444: 1,1,1 samples per pixel */
428 429 430
            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
431 432
            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
433 434 435
            break;                
#ifdef DEBUG
        default:
436
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
437 438
            p_free_picture->p_data   =  NULL;            
            break;            
Vincent Seguin's avatar
Vincent Seguin committed
439
#endif    
Michel Kaempf's avatar
Michel Kaempf committed
440 441
        }

Vincent Seguin's avatar
Vincent Seguin committed
442 443
        if( p_free_picture->p_data != NULL )
        {        
444
            /* Copy picture informations, set some default values */
445 446
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
447
            p_free_picture->i_matrix_coefficients       = 1; 
448 449
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
450 451 452 453 454 455
            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;            
456
            p_free_picture->i_refcount                  = 0;            
Vincent Seguin's avatar
Vincent Seguin committed
457 458 459 460
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
461 462 463 464
            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
465 466
        }
        
467 468 469
#ifdef DEBUG_VIDEO
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
#endif
Vincent Seguin's avatar
Vincent Seguin committed
470
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
471
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
472
    }
Vincent Seguin's avatar
Vincent Seguin committed
473 474
    
    // No free or destroyed picture could be found
475
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
476
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
477
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
478 479
}

480
/******************************************************************************
481
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
482
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
483
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
484 485
 * 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 !
486 487
 * This function does not need locking since reserved pictures are ignored by
 * the output thread.
488
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
489
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
490
{
491
#ifdef DEBUG
492
   /* Check if picture status is valid */
493 494 495
   if( (p_pic->i_status != RESERVED_PICTURE) && 
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
496
   {
Vincent Seguin's avatar
Vincent Seguin committed
497
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );       
498 499 500
   }   
#endif

Vincent Seguin's avatar
Vincent Seguin committed
501
    p_pic->i_status = DESTROYED_PICTURE;
502 503 504 505

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

508
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
509
 * vout_LinkPicture: increment reference counter of a picture
510
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
511
 * This function increment the reference counter of a picture in the video
512
 * heap. It needs a lock since several producer threads can access the picture.
513
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
514 515
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
516
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
517
    p_pic->i_refcount++;
518 519

#ifdef DEBUG_VIDEO
520
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
521
#endif
522 523

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

526
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
527
 * vout_UnlinkPicture: decrement reference counter of a picture
528
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
529
 * This function decrement the reference counter of a picture in the video heap.
530
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
531 532
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
533
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
534
    p_pic->i_refcount--;
535 536 537 538 539 540 541 542 543

#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
544
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
545
    {
Vincent Seguin's avatar
Vincent Seguin committed
546
	p_pic->i_status = DESTROYED_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
547
    }
548 549

#ifdef DEBUG_VIDEO
550
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
551
#endif
552 553

    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
554 555 556 557
}

/* following functions are local */

558
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
559
 * InitThread: initialize video output thread
560
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
561 562 563
 * 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.
564
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
565 566
static int InitThread( vout_thread_t *p_vout )
{
567
    int     i_index;                                         /* generic index */    
Michel Kaempf's avatar
Michel Kaempf committed
568 569

    /* Update status */
570
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
571 572
    *p_vout->pi_status = THREAD_START;    

573 574
    /* Initialize output method - this function issues its own error messages */
    if( vout_SysInit( p_vout ) )
Michel Kaempf's avatar
Michel Kaempf committed
575 576 577 578 579
    {
        *p_vout->pi_status = THREAD_ERROR;        
        return( 1 );
    } 

Vincent Seguin's avatar
Vincent Seguin committed
580
    /* Initialize pictures and subtitles */    
581
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
Vincent Seguin's avatar
Vincent Seguin committed
582
    {
Vincent Seguin's avatar
Vincent Seguin committed
583 584 585 586
        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;
587 588 589 590 591 592
    }

    /* Initialize convertion tables and functions */
    if( vout_InitTables( p_vout ) )
    {
        intf_ErrMsg("error: can't allocate translation tables\n");
593
        return( 1 );                
Vincent Seguin's avatar
Vincent Seguin committed
594 595
    }
    
Michel Kaempf's avatar
Michel Kaempf committed
596
    /* Mark thread as running and return */
597 598
    p_vout->b_active =          1;    
    *p_vout->pi_status =        THREAD_READY;    
599
    intf_DbgMsg("thread ready\n");    
600
    return( 0 );    
Michel Kaempf's avatar
Michel Kaempf committed
601 602
}

603
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
604
 * RunThread: video output thread
605
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
606 607 608
 * 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.
609
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
610 611
static void RunThread( vout_thread_t *p_vout)
{
612 613 614 615 616
    int             i_picture;                               /* picture index */
    mtime_t         current_date;                             /* current date */
    mtime_t         pic_date = 0;                             /* picture date */    
    boolean_t       b_display;                                /* display flag */    
    picture_t *     p_pic;                                 /* picture pointer */
617
     
Michel Kaempf's avatar
Michel Kaempf committed
618 619 620 621 622 623
    /* 
     * Initialize thread and free configuration 
     */
    p_vout->b_error = InitThread( p_vout );
    if( p_vout->b_error )
    {
Vincent Seguin's avatar
Vincent Seguin committed
624
        //??
625
        free( p_vout );                                 /* destroy descriptor */
Michel Kaempf's avatar
Michel Kaempf committed
626 627
        return;        
    }    
628
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
629 630 631 632 633 634

    /*
     * Main loop - it is not executed if an error occured during
     * initialization
     */
    while( (!p_vout->b_die) && (!p_vout->b_error) )
635
    {            
Vincent Seguin's avatar
Vincent Seguin committed
636
        /* 
637 638 639
	 * Find the picture to display - this operation does not need lock,
         * since only READY_PICTURES are handled 
         */
640
        p_pic = NULL;         
Vincent Seguin's avatar
Vincent Seguin committed
641
        current_date = mdate();
642
        for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
643 644 645
	{
	    if( (p_vout->p_picture[i_picture].i_status == READY_PICTURE) &&
		( (p_pic == NULL) || 
646
		  (p_vout->p_picture[i_picture].date < pic_date) ) )
Vincent Seguin's avatar
Vincent Seguin committed
647
	    {
648
                p_pic = &p_vout->p_picture[i_picture];
649
                pic_date = p_pic->date;                
Vincent Seguin's avatar
Vincent Seguin committed
650 651
	    }
	}
Vincent Seguin's avatar
Vincent Seguin committed
652
 
Vincent Seguin's avatar
Vincent Seguin committed
653 654 655 656
        /* 
	 * Render picture if any
	 */
        if( p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
657
        {
658 659
#ifdef STATS
            /* Computes FPS rate */
660 661
            p_vout->fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = pic_date;
#endif	    
662
	    if( pic_date < current_date )
Vincent Seguin's avatar
Vincent Seguin committed
663
	    {
664 665
		/* Picture is late: it will be destroyed and the thread will sleep and
                 * go to next picture */
Vincent Seguin's avatar
Vincent Seguin committed
666
                vlc_mutex_lock( &p_vout->picture_lock );
667
                p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
668
#ifdef DEBUG_VIDEO
669
		intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
670
#endif
671
                vlc_mutex_unlock( &p_vout->picture_lock );
672
                p_pic =         NULL;                
Vincent Seguin's avatar
Vincent Seguin committed
673
	    }
674
	    else if( pic_date > current_date + VOUT_DISPLAY_DELAY )
Vincent Seguin's avatar
Vincent Seguin committed
675 676 677 678
	    {
		/* 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 */
679
                p_pic =         NULL;                
Vincent Seguin's avatar
Vincent Seguin committed
680
	    }
Michel Kaempf's avatar
Michel Kaempf committed
681
        }
Vincent Seguin's avatar
Vincent Seguin committed
682 683 684
              
        /*
         * Perform rendering, sleep and display rendered picture
685
         */
Vincent Seguin's avatar
Vincent Seguin committed
686
        if( p_pic )
687
        {
Vincent Seguin's avatar
Vincent Seguin committed
688 689 690
            /* A picture is ready to be displayed : render it */
            if( p_vout->b_active )
            {                    
Vincent Seguin's avatar
Vincent Seguin committed
691
                b_display = RenderPicture( p_vout, p_pic, 1 );
Vincent Seguin's avatar
Vincent Seguin committed
692 693
                if( p_vout->b_info )
                {
Vincent Seguin's avatar
Vincent Seguin committed
694 695
                    b_display |= RenderPictureInfo( p_vout, p_pic, b_display );
                    b_display |= RenderInfo( p_vout, b_display );                    
Vincent Seguin's avatar
Vincent Seguin committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
                }                    
            }
            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
711 712 713 714 715 716 717 718 719 720 721 722
            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
723 724
        }

Vincent Seguin's avatar
Vincent Seguin committed
725 726 727
        /* Give back change lock */
        vlc_mutex_unlock( &p_vout->change_lock );        

Vincent Seguin's avatar
Vincent Seguin committed
728 729 730 731 732 733 734 735 736 737
        /* 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
738 739 740 741
        /* 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
742 743 744
        {
            vout_SysDisplay( p_vout );
        }
745

Vincent Seguin's avatar
Vincent Seguin committed
746
        /*
Vincent Seguin's avatar
Vincent Seguin committed
747
         * Check events and manage thread
Vincent Seguin's avatar
Vincent Seguin committed
748
	 */
Vincent Seguin's avatar
Vincent Seguin committed
749
        if( vout_SysManage( p_vout ) | Manage( p_vout ) )
Vincent Seguin's avatar
Vincent Seguin committed
750 751 752 753 754
	{
	    /* 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
755
	}  
Michel Kaempf's avatar
Michel Kaempf committed
756 757 758 759 760 761 762 763 764 765 766 767
    } 

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

    /* End of thread */
    EndThread( p_vout );
768
    intf_DbgMsg( "thread end\n" );
Michel Kaempf's avatar
Michel Kaempf committed
769 770
}

771
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
772
 * ErrorThread: RunThread() error loop
773
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
774 775 776
 * 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.
777
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
778 779 780
static void ErrorThread( vout_thread_t *p_vout )
{
    /* Wait until a `die' order */
781
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
782 783 784 785 786 787 788
    while( !p_vout->b_die )
    {
        /* Sleep a while */
        msleep( VOUT_IDLE_SLEEP );                
    }
}

789
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
790
 * EndThread: thread destruction
791
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
792 793
 * This function is called when the thread ends after a sucessfull 
 * initialization.
794
 ******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
795 796
static void EndThread( vout_thread_t *p_vout )
{
797
    int *   pi_status;                                       /* thread status */
Vincent Seguin's avatar
Vincent Seguin committed
798
    int     i_picture;
Michel Kaempf's avatar
Michel Kaempf committed
799 800
        
    /* Store status */
801
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
802 803 804
    pi_status = p_vout->pi_status;    
    *pi_status = THREAD_END;    

Vincent Seguin's avatar
Vincent Seguin committed
805 806 807 808 809 810 811 812
    /* 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
813

814
    /* Destroy translation tables */
815
    vout_EndTables( p_vout );
Vincent Seguin's avatar
Vincent Seguin committed
816
    
Vincent Seguin's avatar
Vincent Seguin committed
817
    /* Destroy thread structures allocated by Create and InitThread */
Vincent Seguin's avatar
Vincent Seguin committed
818
    vout_SysEnd( p_vout );
Vincent Seguin's avatar
Vincent Seguin committed
819 820
    vout_UnloadFont( p_vout->p_default_font );
    vout_UnloadFont( p_vout->p_large_font ); 
821
    vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
822 823 824 825 826 827
    free( p_vout );

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

Vincent Seguin's avatar
Vincent Seguin committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
/*******************************************************************************
 * Print: print simple text on a picture
 *******************************************************************************
 * This function will print a simple text on the picture. It is designed to
 * print debugging or general informations.
 *******************************************************************************/
void Print( vout_thread_t *p_vout, int i_x, int i_y, int i_halign, int i_valign, unsigned char *psz_text )
{
    int                 i_text_height;                    /* total text height */
    int                 i_text_width;                      /* total text width */

    /* Update upper left coordinates according to alignment */
    vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
    switch( i_halign )
    {
    case 0:                                                        /* centered */
        i_x -= i_text_width / 2;
        break;        
    case 1:                                                   /* right aligned */
        i_x -= i_text_width;
        break;                
    }
    switch( i_valign )
    {
    case 0:                                                        /* centered */
        i_y -= i_text_height / 2;
        break;        
    case 1:                                                   /* bottom aligned */
        i_y -= i_text_height;
        break;                
    }

    /* Check clipping */
    if( (i_y < 0) || (i_y + i_text_height > p_vout->i_height) || 
        (i_x < 0) || (i_x + i_text_width > p_vout->i_width) )
    {
        intf_DbgMsg("'%s' would print outside the screen\n", psz_text);        
        return;        
    }    

    /* Print text */
    vout_Print( p_vout->p_default_font, vout_SysGetPicture( p_vout ) + 
                i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
                p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line, 
                0xffffffff, 0x00000000, 0x00000000, 0, psz_text );
}

875
/******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
876
 * RenderBlank: render a blank screen
877
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
878 879
 * This function is called by all other rendering functions when they arrive on
 * a non blanked screen.
880
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
881 882
static void RenderBlank( vout_thread_t *p_vout )
{
883
    //?? toooooo slow
884 885 886
    int  i_index;                                   /* current 64 bits sample */    
    int  i_width;                                /* number of 64 bits samples */    
    u64 *p_pic;                                 /* pointer to 64 bits samples */
Vincent Seguin's avatar
Vincent Seguin committed
887 888 889
    
    /* Initialize variables */
    p_pic =     vout_SysGetPicture( p_vout );
890
    i_width =   p_vout->i_bytes_per_line * p_vout->i_height / 256;
Vincent Seguin's avatar
Vincent Seguin committed
891

892
    /* Clear beginning of screen by 256 bytes blocks */
Vincent Seguin's avatar
Vincent Seguin committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    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 */
    //??
}


918
/******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
919
 * RenderPicture: render a picture
920
 ******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
921 922 923
 * 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
924
 * by the vout thread later.
925
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
926
static int RenderPicture( vout_thread_t *p_vout, picture_t *p_pic, boolean_t b_blank )
Michel Kaempf's avatar
Michel Kaempf committed
927
{
928 929 930 931 932 933
    int         i_display_height, i_display_width;      /* display dimensions */
    int         i_height, i_width;               /* source picture dimensions */
    int         i_scaled_height;              /* scaled height of the picture */   
    int         i_aspect_scale;                /* aspect ratio vertical scale */
    int         i_eol;                       /* end of line offset for source */    
    byte_t *    p_convert_dst;                      /* convertion destination */        
Vincent Seguin's avatar
Vincent Seguin committed
934
    
935 936 937 938 939
#ifdef STATS
    /* Start recording render time */
    p_vout->render_time = mdate();
#endif

Vincent Seguin's avatar
Vincent Seguin committed
940
    /* Mark last picture date */
Vincent Seguin's avatar
Vincent Seguin committed
941 942 943 944 945 946 947 948
    p_vout->last_picture_date = p_pic->date;
    i_width =                   p_pic->i_width;    
    i_height =                  p_pic->i_height;
    i_display_width =           p_vout->i_width;    
    i_display_height =          p_vout->i_height;    

    /* Select scaling depending of aspect ratio */
    switch( p_pic->i_aspect_ratio )
Vincent Seguin's avatar
Vincent Seguin committed
949
    {
Vincent Seguin's avatar
Vincent Seguin committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
    case AR_3_4_PICTURE:
        i_aspect_scale = (4 * i_height - 3 * i_width) ? 
            1 + 3 * i_width / ( 4 * i_height - 3 * i_width ) : 0;
        break;
    case AR_16_9_PICTURE:
        i_aspect_scale = ( 16 * i_height - 9 * i_width ) ? 
            1 + 9 * i_width / ( 16 * i_height - 9 * i_width ) : 0;
        break;
    case AR_221_1_PICTURE:        
        i_aspect_scale = ( 221 * i_height - 100 * i_width ) ?
            1 + 100 * i_width / ( 221 * i_height - 100 * i_width ) : 0;
        break;               
    case AR_SQUARE_PICTURE:
    default:
        i_aspect_scale = 0;        
Vincent Seguin's avatar
Vincent Seguin committed
965
    }
Vincent Seguin's avatar
Vincent Seguin committed
966 967 968 969
    i_scaled_height = (i_aspect_scale ? i_height * (i_aspect_scale - 1) / i_aspect_scale : i_height);
    
    /* Crop picture if too large for the screen */
    if( i_width > i_display_width )
970
    {
Vincent Seguin's avatar
Vincent Seguin committed
971 972 973 974 975 976 977 978 979 980 981
        i_eol = i_width - i_display_width / 16 * 16;
        i_width = i_display_width / 16 * 16;        
    }
    else
    {
        i_eol = 0;        
    }
    if( i_scaled_height > i_display_height )
    {
        i_height = (i_aspect_scale * i_display_height / (i_aspect_scale - 1)) / 2 * 2;
        i_scaled_height = i_display_height;        
982
    }    
Vincent Seguin's avatar
Vincent Seguin committed
983 984 985
    p_convert_dst = vout_SysGetPicture( p_vout ) + 
        ( i_display_width - i_width ) / 2 * p_vout->i_bytes_per_pixel +
        ( i_display_height - i_scaled_height ) / 2 * p_vout->i_bytes_per_line;
986

987 988 989
    /*
     * Choose appropriate rendering function and render picture
     */
990 991
    switch( p_pic->i_type )
    {
992
    case YUV_420_PICTURE:
Vincent Seguin's avatar
Vincent Seguin committed
993
        p_vout->p_ConvertYUV420( p_vout, p_convert_dst, 
994
                                 p_pic->p_y, p_pic->p_u, p_pic->p_v,
Vincent Seguin's avatar
Vincent Seguin committed
995 996 997
                                 i_width, i_height, i_eol, 
                                 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - i_width,
                                 i_aspect_scale, p_pic->i_matrix_coefficients );
998
        break;        
999
    case YUV_422_PICTURE:
Vincent Seguin's avatar
Vincent Seguin committed
1000 1001 1002 1003 1004 1005
        p_vout->p_ConvertYUV422( p_vout, p_convert_dst, 
                                 p_pic->p_y, p_pic->p_u, p_pic->p_v,
                                 i_width, i_height, i_eol, 
                                 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - i_width,
                                 i_aspect_scale, p_pic->i_matrix_coefficients );
        break;        
1006
    case YUV_444_PICTURE:
Vincent Seguin's avatar
Vincent Seguin committed
1007 1008 1009 1010 1011 1012
        p_vout->p_ConvertYUV444( p_vout, p_convert_dst, 
                                 p_pic->p_y, p_pic->p_u, p_pic->p_v,
                                 i_width, i_height, i_eol, 
                                 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - i_width,
                                 i_aspect_scale, p_pic->i_matrix_coefficients );
        break;                
1013 1014
#ifdef DEBUG
    default:        
1015
        intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1016 1017 1018
        break;        
#endif
    }
1019

1020 1021 1022 1023
#ifdef STATS
    /* End recording render time */
    p_vout->render_time = mdate() - p_vout->render_time;
#endif
Vincent Seguin's avatar
Vincent Seguin committed
1024
    return( 1 );    
Michel Kaempf's avatar
Michel Kaempf committed
1025 1026
}

1027
/******************************************************************************
1028
 * RenderPictureInfo: print additionnal informations on a picture
1029
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
1030 1031
 * This function will print informations such as fps and other picture
 * dependant informations.
1032
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
1033
static int RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic, boolean_t b_blank )
1034
{
1035
    char        psz_buffer[256];                             /* string buffer */
1036 1037

#ifdef STATS
1038 1039 1040
    /* 
     * Print FPS rate in upper right corner 
     */
1041
    if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1042 1043
    {        
        sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1044 1045
                 ( 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 ] ) );        
Vincent Seguin's avatar
Vincent Seguin committed
1046
        Print( p_vout, p_vout->i_width, 0, 1, -1, psz_buffer );
1047 1048
    }

1049
    /* 
Vincent Seguin's avatar
Vincent Seguin committed
1050
     * Print frames count and loop time in upper left corner 
1051
     */
Vincent Seguin's avatar
Vincent Seguin committed
1052 1053
    sprintf( psz_buffer, "%ld frames   render time: %lu us", 
             p_vout->c_fps_samples, (long unsigned) p_vout->render_time );
Vincent Seguin's avatar
Vincent Seguin committed
1054
    Print( p_vout, 0, 0, -1, -1, psz_buffer );    
1055
#endif
Vincent Seguin's avatar
Vincent Seguin committed
1056

1057
#ifdef DEBUG
Vincent Seguin's avatar
Vincent Seguin committed
1058 1059
    /*
     * Print picture information in lower right corner
1060
     */
1061
    sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s)",
Vincent Seguin's avatar
Vincent Seguin committed
1062 1063
             (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
             ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1064 1065
              ((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
1066 1067
             p_pic->i_display_width, p_pic->i_display_height,
             p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1068
             (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
Vincent Seguin's avatar
Vincent Seguin committed
1069 1070
             ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
              ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1071
               ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))));    
Vincent Seguin's avatar
Vincent Seguin committed
1072
    Print( p_vout, p_vout->i_width, p_vout->i_height, 1, 1, psz_buffer );
1073
#endif
Vincent Seguin's avatar
Vincent Seguin committed
1074 1075
    
    return( 0 );    
1076 1077
}

1078
/******************************************************************************
1079
 * RenderIdle: render idle picture
1080
 ******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
1081
 * This function will clear the display or print a logo.
1082
 ******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
1083
static int RenderIdle( vout_thread_t *p_vout, boolean_t b_blank )
1084
{
Vincent Seguin's avatar
Vincent Seguin committed
1085 1086 1087 1088 1089 1090 1091
    /* 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();        
Vincent Seguin's avatar
Vincent Seguin committed
1092 1093
        Print( p_vout, p_vout->i_width / 2, p_vout->i_height / 2, 0, 0, 
               "no stream" );        //??
Vincent Seguin's avatar
Vincent Seguin committed
1094 1095 1096
        return( 1 );        
    }

Vincent Seguin's avatar
Vincent Seguin committed
1097 1098
    return( 0 );    
}