video_output.c 47.1 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 13 14 15 16
 *******************************************************************************
 * 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
 *******************************************************************************/
#include <errno.h> 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Vincent Seguin's avatar
Vincent Seguin committed
17 18

#ifdef VIDEO_X11
Michel Kaempf's avatar
Michel Kaempf committed
19
#include <X11/Xlib.h>
Vincent Seguin's avatar
Vincent Seguin committed
20
#endif
Michel Kaempf's avatar
Michel Kaempf committed
21 22 23 24

#include "common.h"
#include "config.h"
#include "mtime.h"
25
#include "vlc_thread.h"
Michel Kaempf's avatar
Michel Kaempf committed
26 27
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
28 29
#include "video_sys.h"
#include "intf_msg.h"
30
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
31 32 33 34

/*******************************************************************************
 * Macros
 *******************************************************************************/
35 36 37

/* CLIP_BYTE: return value if between 0 and 255, else return nearest boundary 
 * (0 or 255) */
Vincent Seguin's avatar
Vincent Seguin committed
38 39
#define CLIP_BYTE( i_val ) ( (i_val < 0) ? 0 : ((i_val > 255) ? 255 : i_val) )

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* YUV_GRAYSCALE: parametric macro for YUV grayscale transformation.
 * Due to the high performance need of this loop, all possible conditions 
 * evaluations are made outside the transformation loop. However, the code does 
 * not change much for two different loops. This macro allows to change slightly
 * the content of the loop without having to copy and paste code. It is used in 
 * RenderYUVPicture function. */
#define YUV_GRAYSCALE( TRANS_RED, TRANS_GREEN, TRANS_BLUE, P_PIC )      \
/* Main loop */                                                         \
for (i_pic_y=0; i_pic_y < p_pic->i_height ; i_pic_y++)                  \
{                                                                       \
    for (i_pic_x=0; i_pic_x< p_pic->i_width; i_pic_x++)                 \
    {                                                                   \
        i_y = *p_y++;                                                   \
        *P_PIC++ = TRANS_RED[i_y] | TRANS_GREEN[i_y] | TRANS_BLUE[i_y]; \
    }                                                                   \
    /* Skip until beginning of next line */                             \
    P_PIC += i_eol_offset;                                              \
}                                                                       

59 60 61 62 63 64
/* YUV_TRANSFORM: parametric macro for YUV transformation.
 * Due to the high performance need of this loop, all possible conditions 
 * evaluations are made outside the transformation loop. However, the code does 
 * not change much for two different loops. This macro allows to change slightly
 * the content of the loop without having to copy and paste code. It is used in 
 * RenderYUVPicture function. */
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#define YUV_TRANSFORM( CHROMA, TRANS_RED, TRANS_GREEN, TRANS_BLUE, P_PIC ) \
/* Main loop */                                                         \
for (i_pic_y=0; i_pic_y < p_pic->i_height ; i_pic_y++)                  \
{                                                                       \
    for (i_pic_x=0; i_pic_x< p_pic->i_width; i_pic_x+=2)                \
    {                                                                   \
        /* First sample (complete) */                                   \
        i_y = 76309 * *p_y++ - 1188177;                                 \
        i_u = *p_u++ - 128;                                             \
        i_v = *p_v++ - 128;                                             \
        *P_PIC++ =                                                      \
            TRANS_RED   [(i_y+i_crv*i_v)                >>16] |         \
            TRANS_GREEN [(i_y-i_cgu*i_u-i_cgv*i_v)      >>16] |         \
            TRANS_BLUE  [(i_y+i_cbu*i_u)                >>16];          \
        i_y = 76309 * *p_y++ - 1188177;                                 \
        /* Second sample (partial) */                                   \
        if( CHROMA == 444 )                                             \
        {                                                               \
            i_u = *p_u++ - 128;                                         \
            i_v = *p_v++ - 128;                                         \
        }                                                               \
        *P_PIC++ =                                                      \
            TRANS_RED   [(i_y+i_crv*i_v)                >>16] |         \
            TRANS_GREEN [(i_y-i_cgu*i_u-i_cgv*i_v)      >>16] |         \
            TRANS_BLUE  [(i_y+i_cbu*i_u)                >>16];          \
    }                                                                   \
    if( (CHROMA == 420) && !(i_pic_y & 0x1) )                           \
    {                                                                   \
93 94
        p_u -= i_chroma_width;                                          \
        p_v -= i_chroma_width;                                          \
95 96
    }                                                                   \
    /* Skip until beginning of next line */                             \
97
    P_PIC += i_eol_offset;                                              \
98 99
}

Vincent Seguin's avatar
Vincent Seguin committed
100 101 102
/*******************************************************************************
 * Constants
 *******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
103

Vincent Seguin's avatar
Vincent Seguin committed
104
/* RGB/YUV inversion matrix (ISO/IEC 13818-2 section 6.3.6, table 6.9) */
105
const int MATRIX_COEFFICIENTS_TABLE[8][4] =
Vincent Seguin's avatar
Vincent Seguin committed
106 107 108 109 110 111 112 113 114 115 116
{
  {117504, 138453, 13954, 34903},       /* no sequence_display_extension */
  {117504, 138453, 13954, 34903},       /* ITU-R Rec. 709 (1990) */
  {104597, 132201, 25675, 53279},       /* unspecified */
  {104597, 132201, 25675, 53279},       /* reserved */
  {104448, 132798, 24759, 53109},       /* FCC */
  {104597, 132201, 25675, 53279},       /* ITU-R Rec. 624-4 System B, G */
  {104597, 132201, 25675, 53279},       /* SMPTE 170M */
  {117579, 136230, 16907, 35559}        /* SMPTE 240M (1987) */
};

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/*******************************************************************************
 * External prototypes
 *******************************************************************************/
#ifdef HAVE_MMX
/* YUV transformations for MMX - in yuv-mmx.S 
 *      p_y, p_u, p_v:          Y U and V planes
 *      i_width, i_height:      frames dimensions (pixels)
 *      i_ypitch, i_vpitch:     Y and V lines sizes (bytes)
 *      i_aspect:               vertical aspect factor
 *      pi_pic:                 RGB frame
 *      i_dci_offset:           ?? x offset for left image border
 *      i_offset_to_line_0:     ?? x offset for left image border
 *      i_pitch:                RGB line size (bytes)
 *      i_colortype:            0 for 565, 1 for 555 */
void vout_YUV420_16_MMX( u8* p_y, u8* p_u, u8 *p_v, 
                         unsigned int i_width, unsigned int i_height,
                         unsigned int i_ypitch, unsigned int i_vpitch,
                         unsigned int i_aspect, u8 *pi_pic, 
                         u32 i_dci_offset, u32 i_offset_to_line_0,
                         int CCOPitch, int i_colortype );
#endif

Vincent Seguin's avatar
Vincent Seguin committed
139
/*******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
140
 * Local prototypes
Vincent Seguin's avatar
Vincent Seguin committed
141
 *******************************************************************************/
142 143 144 145 146 147 148 149 150
static int      InitThread              ( vout_thread_t *p_vout );
static void     RunThread               ( vout_thread_t *p_vout );
static void     ErrorThread             ( vout_thread_t *p_vout );
static void     EndThread               ( vout_thread_t *p_vout );
static void     RenderPicture           ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderYUVGrayPicture    ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderYUV16Picture      ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderYUV32Picture      ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderInfo              ( vout_thread_t *p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
151 152 153 154 155 156 157

/*******************************************************************************
 * 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.
158
 * If not, it will be updated using one of the THREAD_* constants.
Michel Kaempf's avatar
Michel Kaempf committed
159
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
160
vout_thread_t * vout_CreateThread               ( 
161
#ifdef VIDEO_X11
162
                                                  char *psz_display, Window root_window, 
Vincent Seguin's avatar
Vincent Seguin committed
163 164 165
#endif
                                                  int i_width, int i_height, int *pi_status 
                                                )
Michel Kaempf's avatar
Michel Kaempf committed
166 167 168 169
{
    vout_thread_t * p_vout;                               /* thread descriptor */
    int             i_status;                                 /* thread status */

170
    /* Allocate descriptor */
Michel Kaempf's avatar
Michel Kaempf committed
171
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
172
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
173
    {
174
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));        
Michel Kaempf's avatar
Michel Kaempf committed
175 176
        return( NULL );
    }
177 178 179

    /* Initialize some fields used by the system-dependant method - these fields will
     * probably be modified by the method */
Vincent Seguin's avatar
Vincent Seguin committed
180 181 182
#ifdef DEBUG
    p_vout->b_info              = 1;    
#else
183
    p_vout->b_info              = 0;    
Vincent Seguin's avatar
Vincent Seguin committed
184
#endif
185 186
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, 
                                                       VOUT_GRAYSCALE_DEFAULT );
187 188
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
189
    p_vout->i_bytes_per_line    = i_width * 2;    
190 191 192 193
    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;
194
    intf_DbgMsg("wished configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line), ratio %.2f:%.2f, gray=%d\n",
195
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
196 197
                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 );
198 199 200
   
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
Vincent Seguin's avatar
Vincent Seguin committed
201 202
    if( vout_SysCreate( p_vout
#if defined(VIDEO_X11)
203
                        , psz_display, root_window 
Vincent Seguin's avatar
Vincent Seguin committed
204 205
#endif
        ) )
Michel Kaempf's avatar
Michel Kaempf committed
206
    {
Vincent Seguin's avatar
Vincent Seguin committed
207 208
      free( p_vout );
      return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
209
    }
210
    intf_DbgMsg("actual configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line), ratio %.2f:%.2f, gray=%d\n",
211
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
212 213
                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 );
Vincent Seguin's avatar
Vincent Seguin committed
214
  
215 216 217 218 219 220 221 222 223
    /* Terminate the initialization */
    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;    
#ifdef STATS
    p_vout->c_loops             = 0;
    p_vout->c_idle_loops        = 0;
224
    p_vout->c_pictures          = 0;
225
#endif      
Michel Kaempf's avatar
Michel Kaempf committed
226 227

    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
228 229 230
    vlc_mutex_init( &p_vout->lock );
    if( vlc_thread_create( &p_vout->thread_id, "video output", 
			   (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
231
    {
232
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
233
	vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
234 235 236 237
        free( p_vout );
        return( NULL );
    }   

238 239 240
    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
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    /* 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
271
    *p_vout->pi_status = THREAD_DESTROY;    
Michel Kaempf's avatar
Michel Kaempf committed
272 273 274 275 276
     
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
277
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
278 279 280 281 282 283 284 285 286 287
    {
        do
        {
            msleep( THREAD_SLEEP );
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
                && (i_status != THREAD_FATAL) );   
    }
}

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
288
 * vout_DisplayPicture: display a picture
Michel Kaempf's avatar
Michel Kaempf committed
289
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
290 291
 * Remove the reservation flag of a picture, which will cause it to be ready for
 * display.
Michel Kaempf's avatar
Michel Kaempf committed
292
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
293
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
294
{
295 296 297 298 299 300 301 302 303 304 305 306 307 308
#ifdef DEBUG_VIDEO
    char        psz_date[MSTRTIME_MAX_SIZE];         /* buffer for date string */
#endif
  
   vlc_mutex_lock( &p_vout->lock );

#ifdef DEBUG_VIDEO
   /* Check if picture status is valid */
   if( p_pic->i_status != RESERVED_PICTURE )
   {
       intf_DbgMsg("error: picture %d has invalid status %d\n", 
                   p_pic, p_pic->i_status );       
   }   
#endif
Michel Kaempf's avatar
Michel Kaempf committed
309 310

    /* Remove reservation flag */
Vincent Seguin's avatar
Vincent Seguin committed
311
    p_pic->i_status = READY_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
312 313 314 315 316 317

#ifdef STATS
    /* Update stats */
    p_vout->c_pictures++;
#endif

318 319 320 321 322 323
#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

Vincent Seguin's avatar
Vincent Seguin committed
324
    vlc_mutex_unlock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
325 326 327
}

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
328
 * vout_CreatePicture: allocate a picture in the video output heap.
Michel Kaempf's avatar
Michel Kaempf committed
329 330
 *******************************************************************************
 * This function create a reserved image in the video output heap. 
Vincent Seguin's avatar
Vincent Seguin committed
331 332
 * A null pointer is returned if the function fails. This method provides an
 * already allocated zone of memory in the picture data fields.
Michel Kaempf's avatar
Michel Kaempf committed
333
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
334 335
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type, 
			       int i_width, int i_height, int i_bytes_per_line )
Michel Kaempf's avatar
Michel Kaempf committed
336
{
Vincent Seguin's avatar
Vincent Seguin committed
337 338 339
    int         i_picture;                                    /* picture index */
    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
340

Vincent Seguin's avatar
Vincent Seguin committed
341 342
    /* Get lock */
    vlc_mutex_lock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
343

Vincent Seguin's avatar
Vincent Seguin committed
344 345 346 347 348 349 350 351 352 353 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 )
	{
	    /* Picture is marked for destruction, but is still allocated */
	    if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
		(p_vout->p_picture[i_picture].i_height         == i_height) &&
		(p_vout->p_picture[i_picture].i_bytes_per_line == i_bytes_per_line) )
	    {
		/* 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_width  = i_width;
		p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
363 364 365 366
#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
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		vlc_mutex_unlock( &p_vout->lock );
		return( &p_vout->p_picture[i_picture] );
	    }
	    else if( p_destroyed_picture == NULL )
	    {
		/* Memory size do not match, but picture index will be kept in
		 * case no other place are left */
		p_destroyed_picture = &p_vout->p_picture[i_picture];                
	    }	    
	}
        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
383 384
    }

Vincent Seguin's avatar
Vincent Seguin committed
385 386 387 388 389 390 391
    /* 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
392 393
    }

Vincent Seguin's avatar
Vincent Seguin committed
394 395 396 397 398 399 400
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
401
        {
402
        case YUV_420_PICTURE:                   /* YUV picture: bits per pixel */
403
        case YUV_422_PICTURE:
Vincent Seguin's avatar
Vincent Seguin committed
404 405
        case YUV_444_PICTURE:
            p_free_picture->p_data = malloc( 3 * i_height * i_bytes_per_line );                
406 407 408
            p_free_picture->p_y = (yuv_data_t *) p_free_picture->p_data;
            p_free_picture->p_u = (yuv_data_t *)(p_free_picture->p_data + i_height * i_bytes_per_line);
            p_free_picture->p_v = (yuv_data_t *)(p_free_picture->p_data + i_height * i_bytes_per_line * 2);
Vincent Seguin's avatar
Vincent Seguin committed
409 410 411
            break;                
#ifdef DEBUG
        default:
412
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
413 414
            p_free_picture->p_data   =  NULL;            
            break;            
Vincent Seguin's avatar
Vincent Seguin committed
415
#endif    
Michel Kaempf's avatar
Michel Kaempf committed
416 417
        }

Vincent Seguin's avatar
Vincent Seguin committed
418 419 420
        if( p_free_picture->p_data != NULL )
        {        
            /* Copy picture informations */
421 422 423 424 425 426 427
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
            p_free_picture->i_bytes_per_line            = i_bytes_per_line;
            p_free_picture->i_refcount                  = 0;            
            p_free_picture->i_matrix_coefficients       = 1; 
Vincent Seguin's avatar
Vincent Seguin committed
428 429 430 431
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
432 433 434 435
            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
436 437
        }
        
438 439 440
#ifdef DEBUG_VIDEO
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
#endif
Vincent Seguin's avatar
Vincent Seguin committed
441 442
        vlc_mutex_unlock( &p_vout->lock );
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
443
    }
Vincent Seguin's avatar
Vincent Seguin committed
444 445
    
    // No free or destroyed picture could be found
446
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
447 448
    vlc_mutex_unlock( &p_vout->lock );
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
449 450 451
}

/*******************************************************************************
452
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
Michel Kaempf's avatar
Michel Kaempf committed
453 454
 *******************************************************************************
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
455 456
 * 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 !
Michel Kaempf's avatar
Michel Kaempf committed
457
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
458
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
459
{
Vincent Seguin's avatar
Vincent Seguin committed
460
    vlc_mutex_lock( &p_vout->lock );
461 462 463 464 465 466 467 468 469 470

#ifdef DEBUG_VIDEO
   /* Check if picture status is valid */
   if( p_pic->i_status != RESERVED_PICTURE )
   {
       intf_DbgMsg("error: picture %d has invalid status %d\n", 
                   p_pic, p_pic->i_status );       
   }   
#endif

Vincent Seguin's avatar
Vincent Seguin committed
471
    p_pic->i_status = DESTROYED_PICTURE;
472 473 474 475 476

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif

Vincent Seguin's avatar
Vincent Seguin committed
477
    vlc_mutex_unlock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
478 479 480 481 482 483 484 485 486 487
}

/*******************************************************************************
 * vout_LinkPicture: increment reference counter of a picture
 *******************************************************************************
 * This function increment the reference counter of a picture in the video
 * heap.
 *******************************************************************************/
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
488
    vlc_mutex_lock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
489
    p_pic->i_refcount++;
490 491 492 493 494

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif

Vincent Seguin's avatar
Vincent Seguin committed
495
    vlc_mutex_unlock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
496 497 498 499 500 501 502 503 504
}

/*******************************************************************************
 * 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
505
    vlc_mutex_lock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
506
    p_pic->i_refcount--;
Vincent Seguin's avatar
Vincent Seguin committed
507
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
508
    {
Vincent Seguin's avatar
Vincent Seguin committed
509
	p_pic->i_status = DESTROYED_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
510
    }
511 512 513 514 515

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif

Vincent Seguin's avatar
Vincent Seguin committed
516
    vlc_mutex_unlock( &p_vout->lock );    
Michel Kaempf's avatar
Michel Kaempf committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530
}

/* 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 */    
Vincent Seguin's avatar
Vincent Seguin committed
531
    int     i_pixel_size;     /* pixel size, in bytes, for translations tables */    
Michel Kaempf's avatar
Michel Kaempf committed
532 533 534 535 536

    /* Update status */
    *p_vout->pi_status = THREAD_START;    
    
    /* Initialize pictures */    
537
    p_vout->i_pictures = 0;
Vincent Seguin's avatar
Vincent Seguin committed
538
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
Michel Kaempf's avatar
Michel Kaempf committed
539
    {
Vincent Seguin's avatar
Vincent Seguin committed
540 541
        p_vout->p_picture[i_index].i_type  = EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status= FREE_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
542 543
    }

544 545 546 547 548 549
#ifdef STATS
    /* Initialize FPS index - since samples won't be used until a minimum of
     * pictures, they don't need to be initialized */
    p_vout->i_fps_index = 0;    
#endif

550 551
    /* Initialize output method - this function issues its own error messages */
    if( vout_SysInit( p_vout ) )
Michel Kaempf's avatar
Michel Kaempf committed
552 553 554 555 556
    {
        *p_vout->pi_status = THREAD_ERROR;        
        return( 1 );
    } 

Vincent Seguin's avatar
Vincent Seguin committed
557 558 559 560 561 562 563 564
    /* Allocate translation tables */
    switch( p_vout->i_bytes_per_pixel )
    {
    case 2:                   /* 15 or 16 bpp, use 16 bits translations tables */        
        i_pixel_size = sizeof( u16 );        
        break;                
    case 3:                   /* 24 or 32 bpp, use 32 bits translations tables */        
    case 4:
565
#ifndef DEBUG
566
    default:        
567
#endif
Vincent Seguin's avatar
Vincent Seguin committed
568
        i_pixel_size = sizeof( u32 );
569 570 571
        break;
#ifdef DEBUG
    default:
572
        intf_DbgMsg("error: invalid bytes_per_pixel %d\n", p_vout->i_bytes_per_pixel );
573 574 575
        i_pixel_size = sizeof( u32 );        
        break;              
#endif
Vincent Seguin's avatar
Vincent Seguin committed
576
    }
577 578 579 580 581 582
    p_vout->pi_trans32_red =   (u32 *)p_vout->pi_trans16_red =   
        (u16 *)malloc( 1024 * i_pixel_size );
    p_vout->pi_trans32_green = (u32 *)p_vout->pi_trans16_green = 
        (u16 *)malloc( 1024 * i_pixel_size );
    p_vout->pi_trans32_blue =  (u32 *)p_vout->pi_trans16_blue =  
        (u16 *)malloc( 1024 * i_pixel_size );
Vincent Seguin's avatar
Vincent Seguin committed
583 584 585 586
    if( (p_vout->pi_trans16_red == NULL) || 
        (p_vout->pi_trans16_green == NULL ) ||
        (p_vout->pi_trans16_blue == NULL ) )
    {
587
        intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
588
        *p_vout->pi_status = THREAD_ERROR;   
Vincent Seguin's avatar
Vincent Seguin committed
589 590 591 592 593 594 595 596 597 598 599 600 601
        if( p_vout->pi_trans16_red != NULL )
        {
            free( p_vout->pi_trans16_red );
        }
        if( p_vout->pi_trans16_green != NULL )
        {
            free( p_vout->pi_trans16_green );
        }
        if( p_vout->pi_trans16_blue != NULL )
        {
            free( p_vout->pi_trans16_blue );
        }
        return( 1 );
602
    }              
Vincent Seguin's avatar
Vincent Seguin committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    
    /* Translate translation tables */
    p_vout->pi_trans16_red      += 384;
    p_vout->pi_trans16_green    += 384;
    p_vout->pi_trans16_blue     += 384;
    p_vout->pi_trans32_red      += 384;
    p_vout->pi_trans32_green    += 384;
    p_vout->pi_trans32_blue     += 384;

    /* Build translation tables */
    switch( p_vout->i_screen_depth )
    {
    case 15:
        for( i_index = -384; i_index < 640; i_index++) 
        {
            p_vout->pi_trans16_red[i_index]     = (CLIP_BYTE( i_index ) & 0xf8)<<7;
            p_vout->pi_trans16_green[i_index]   = (CLIP_BYTE( i_index ) & 0xf8)<<2;
            p_vout->pi_trans16_blue[i_index]    =  CLIP_BYTE( i_index ) >> 3;
        }
        break;        
    case 16:
        for( i_index = -384; i_index < 640; i_index++) 
        {
            p_vout->pi_trans16_red[i_index]     = (CLIP_BYTE( i_index ) & 0xf8)<<8;
627
            p_vout->pi_trans16_green[i_index]   = (CLIP_BYTE( i_index ) & 0xfc)<<3;
Vincent Seguin's avatar
Vincent Seguin committed
628 629
            p_vout->pi_trans16_blue[i_index]    =  CLIP_BYTE( i_index ) >> 3;
        }
630
        break;        
Vincent Seguin's avatar
Vincent Seguin committed
631 632 633 634 635 636 637 638 639
    case 24:
    case 32:        
        for( i_index = -384; i_index < 640; i_index++) 
        {
            p_vout->pi_trans32_red[i_index]     =  CLIP_BYTE( i_index ) <<16;
            p_vout->pi_trans32_green[i_index]   =  CLIP_BYTE( i_index ) <<8;
            p_vout->pi_trans32_blue[i_index]    =  CLIP_BYTE( i_index ) ;
        }
        break;        
640 641
#ifdef DEBUG
    default:
642
        intf_DbgMsg("error: invalid screen depth %d\n", p_vout->i_screen_depth );
643 644
        break;      
#endif
Vincent Seguin's avatar
Vincent Seguin committed
645 646
    }
    
Michel Kaempf's avatar
Michel Kaempf committed
647
    /* Mark thread as running and return */
648 649
    p_vout->b_active =          1;    
    *p_vout->pi_status =        THREAD_READY;    
650
    intf_DbgMsg("thread ready\n");    
Michel Kaempf's avatar
Michel Kaempf committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    return(0);    
}

/*******************************************************************************
 * 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 */
    int             i_err;                                       /* error code */
    mtime_t         current_date;                              /* current date */
666 667
    picture_t *     p_pic;                                  /* picture pointer */    
    mtime_t         pic_date = 0;                              /* picture date */    
Michel Kaempf's avatar
Michel Kaempf committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

    /* 
     * 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) )
    {
Vincent Seguin's avatar
Vincent Seguin committed
685 686 687 688 689 690
        /* 
	 * Find the picture to display - this is the only operation requiring
	 * the lock on the picture, since once a READY_PICTURE has been found,
	 * it can't be modified by the other threads (except if it is unliked,
	 * but its data remains)
	 */
691
        p_pic = NULL;         
Vincent Seguin's avatar
Vincent Seguin committed
692 693 694 695 696
        vlc_mutex_lock( &p_vout->lock );
	for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
	{
	    if( (p_vout->p_picture[i_picture].i_status == READY_PICTURE) &&
		( (p_pic == NULL) || 
697
		  (p_vout->p_picture[i_picture].date < pic_date) ) )
Vincent Seguin's avatar
Vincent Seguin committed
698 699
	    {
		p_pic = &p_vout->p_picture[i_picture];
700
                pic_date = p_pic->date;                
Vincent Seguin's avatar
Vincent Seguin committed
701 702 703 704 705 706 707 708
	    }
	}
	vlc_mutex_unlock( &p_vout->lock );

        /* 
	 * Render picture if any
	 */
        if( p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
709
        {
710 711 712 713 714 715 716 717
#ifdef STATS
            /* Computes FPS rate */
            p_vout->fps_sample[ p_vout->i_fps_index++ ] = pic_date;
            if( p_vout->i_fps_index == VOUT_FPS_SAMPLES )
            {
                p_vout->i_fps_index = 0;                
            }                            
#endif
Vincent Seguin's avatar
Vincent Seguin committed
718
	    current_date = mdate();
719
	    if( pic_date < current_date )
Vincent Seguin's avatar
Vincent Seguin committed
720 721 722 723 724 725 726 727 728 729 730 731
	    {
		/* Picture is late: it will be destroyed and the thread will go
		 * immediately to next picture */
		vlc_mutex_lock( &p_vout->lock );
		if( p_pic->i_refcount )
		{
		    p_pic->i_status = DISPLAYED_PICTURE;
		}
		else
		{
		    p_pic->i_status = DESTROYED_PICTURE;
		}
732 733 734 735

#ifdef DEBUG_VIDEO
		intf_DbgMsg( "warning: late picture %p skipped\n", p_pic );
#endif
Vincent Seguin's avatar
Vincent Seguin committed
736 737 738
		vlc_mutex_unlock( &p_vout->lock );
		p_pic = NULL;
	    }
739
	    else if( pic_date > current_date + VOUT_DISPLAY_DELAY )
Vincent Seguin's avatar
Vincent Seguin committed
740 741 742 743 744 745 746 747 748
	    {
		/* 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 */
		p_pic = NULL;
	    }
	    else
	    {
		/* Picture has not yet been displayed, and has a valid display
749
		 * date : render it, then forget it */
Vincent Seguin's avatar
Vincent Seguin committed
750
		RenderPicture( p_vout, p_pic );
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
                vlc_mutex_lock( &p_vout->lock );
                if( p_pic->i_refcount )
		{
		    p_pic->i_status = DISPLAYED_PICTURE;
		}
		else
		{
		    p_pic->i_status = DESTROYED_PICTURE;
		}
                vlc_mutex_unlock( &p_vout->lock );

                /* Print additional informations */
                if( p_vout->b_info )
                {                    
                    RenderInfo( p_vout );
                }                
Vincent Seguin's avatar
Vincent Seguin committed
767
	    }
Michel Kaempf's avatar
Michel Kaempf committed
768
        }
Vincent Seguin's avatar
Vincent Seguin committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    
        /*
         * Check events, sleep and display picture
	 */
        i_err = vout_SysManage( p_vout );
	if( i_err < 0 )
	{
	    /* 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;
	}
	else 
	{
	    if( p_pic )
	    {
		/* A picture is ready to be displayed : sleep until its display date */
786
		mwait( pic_date );
Vincent Seguin's avatar
Vincent Seguin committed
787 788 789 790 791 792 793 794 795 796

		if( !i_err )
		{
		    vout_SysDisplay( p_vout );
		}
	    }
	    else
	    {
		/* Sleep to wait for new pictures */
		msleep( VOUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
797
#ifdef STATS
Vincent Seguin's avatar
Vincent Seguin committed
798 799
		/* Update counters */
		p_vout->c_idle_loops++;
Michel Kaempf's avatar
Michel Kaempf committed
800
#endif
Vincent Seguin's avatar
Vincent Seguin committed
801 802
	    }
	}
Michel Kaempf's avatar
Michel Kaempf committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

#ifdef STATS
        /* Update counters */
        p_vout->c_loops++;
#endif
    } 

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

    /* End of thread */
    EndThread( p_vout );
820
    intf_DbgMsg( "thread end\n" );
Michel Kaempf's avatar
Michel Kaempf committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
}

/*******************************************************************************
 * 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
849
    int     i_picture;
Michel Kaempf's avatar
Michel Kaempf committed
850 851 852 853 854
        
    /* Store status */
    pi_status = p_vout->pi_status;    
    *pi_status = THREAD_END;    

Vincent Seguin's avatar
Vincent Seguin committed
855 856 857 858 859 860 861 862
    /* 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
863 864 865 866 867

    /* Destroy translation tables - remeber these tables are translated */    
    free( p_vout->pi_trans16_red - 384 );
    free( p_vout->pi_trans16_green - 384 );
    free( p_vout->pi_trans16_blue - 384 );
Vincent Seguin's avatar
Vincent Seguin committed
868
    
Michel Kaempf's avatar
Michel Kaempf committed
869
    /* Destroy thread structures allocated by InitThread */
Vincent Seguin's avatar
Vincent Seguin committed
870
    vout_SysEnd( p_vout );
871
    vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
872 873 874 875 876 877 878 879 880 881 882 883
    free( p_vout );

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

/*******************************************************************************
 * 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
884
 * by the vout thread later.
885
 * ???? 24 and 32 bpp should probably be separated
Michel Kaempf's avatar
Michel Kaempf committed
886 887 888
 *******************************************************************************/
static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
889 890 891 892 893 894 895 896
#ifdef DEBUG_VIDEO
    /* Send picture informations */
    intf_DbgMsg("picture %p\n", p_pic );

    /* Store rendering start date */
    p_vout->picture_render_time = mdate();    
#endif

897 898 899 900 901
    switch( p_pic->i_type )
    {
    case YUV_420_PICTURE:                   /* YUV picture: YUV transformation */        
    case YUV_422_PICTURE:
    case YUV_444_PICTURE:
902 903 904 905 906 907 908 909 910 911 912 913
        if( p_vout->b_grayscale )                                 /* grayscale */
        {
            RenderYUVGrayPicture( p_vout, p_pic );            
        }
        else if( p_vout->i_bytes_per_pixel == 2 )        /* color 15 or 16 bpp */
        {
            RenderYUV16Picture( p_vout, p_pic );        
        }
        else                                             /* color 24 or 32 bpp */
        {
            RenderYUV32Picture( p_vout, p_pic );            
        }
914
        break;        
915 916
#ifdef DEBUG
    default:        
917
        intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
918 919 920
        break;        
#endif
    }
921 922 923 924 925

#ifdef DEBUG_VIDEO
    /* Computes rendering time */
    p_vout->picture_render_time = mdate() - p_vout->picture_render_time;    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
926 927
}

928
/*******************************************************************************
929
 * RenderYUVGrayPicture: render a 15, 16, 24 or 32 bpp YUV picture in grayscale
930
 *******************************************************************************
931 932
 * Performs the YUV convertion. The picture sent to this function should only
 * have YUV_420, YUV_422 or YUV_444 types.
933
 *******************************************************************************/
934
static void RenderYUVGrayPicture( vout_thread_t *p_vout, picture_t *p_pic )
935
{
936 937 938
    int         i_pic_x, i_pic_y;                /* x,y coordinates in picture */
    int         i_width, i_height;                             /* picture size */
    int         i_eol_offset;          /* pixels from end of line to next line */   
939 940
    yuv_data_t *p_y;                                     /* Y data base adress */
    yuv_data_t  i_y;                                               /* Y sample */
941 942 943 944 945 946 947 948 949 950 951 952 953 954
    u16 *       pi_pic16;                 /* destination picture, 15 or 16 bpp */
    u32 *       pi_pic32;                 /* destination picture, 24 or 32 bpp */
    u16 *       pi_trans16_red;                    /* red transformation table */
    u16 *       pi_trans16_green;                /* green transformation table */
    u16 *       pi_trans16_blue;                  /* blue transformation table */
    u32 *       pi_trans32_red;                    /* red transformation table */
    u32 *       pi_trans32_green;                /* green transformation table */
    u32 *       pi_trans32_blue;                  /* blue transformation table */
 
    /* Set the base pointers and transformation parameters */
    p_y =               p_pic->p_y;
    i_width =           p_pic->i_width;
    i_height =          p_pic->i_height;
    i_eol_offset =      p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - i_width;
955

956 957
    /* Get base adress for destination image and translation tables, then
     * transform image */
958 959 960 961
    switch( p_vout->i_screen_depth )
    {
    case 15:
    case 16:
962 963 964 965 966 967 968
        pi_trans16_red =      p_vout->pi_trans16_red;
        pi_trans16_green =    p_vout->pi_trans16_green;
        pi_trans16_blue =     p_vout->pi_trans16_blue;        
        pi_pic16 = (u16 *) vout_SysGetPicture( p_vout );

        YUV_GRAYSCALE( pi_trans16_red, pi_trans16_green, pi_trans16_blue,
                       pi_pic16 );
969
        break;        
970
    case 24:        
971
    case 32:
972 973 974 975 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 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
        pi_trans32_red =      p_vout->pi_trans32_red;
        pi_trans32_green =    p_vout->pi_trans32_green;
        pi_trans32_blue =     p_vout->pi_trans32_blue;    
        pi_pic32 = (u32 *) vout_SysGetPicture( p_vout );

        YUV_GRAYSCALE( pi_trans32_red, pi_trans32_green, pi_trans32_blue,
                       pi_pic32 );
        break;        
#ifdef DEBUG
    default:
        intf_DbgMsg("error: invalid screen depth %d\n", p_vout->i_screen_depth );
        break;    
#endif      
    }
}


/*******************************************************************************
 * RenderYUV16Picture: render a 15 or 16 bpp YUV picture
 *******************************************************************************
 * Performs the YUV convertion. The picture sent to this function should only
 * have YUV_420, YUV_422 or YUV_444 types.
 *******************************************************************************/
static void RenderYUV16Picture( vout_thread_t *p_vout, picture_t *p_pic )
{
    int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
    int         i_pic_x, i_pic_y;                /* x,y coordinates in picture */
    int         i_y, i_u, i_v;                           /* Y, U and V samples */
    int         i_width, i_height;                             /* picture size */
    int         i_chroma_width;                                /* chroma width */    
    int         i_eol_offset;          /* pixels from end of line to next line */
    yuv_data_t *p_y;                                     /* Y data base adress */
    yuv_data_t *p_u;                                     /* U data base adress */
    yuv_data_t *p_v;                                     /* V data base adress */
    u16 *       pi_pic;                 /* base adress for destination picture */
    u16 *       pi_trans_red;                      /* red transformation table */
    u16 *       pi_trans_green;                  /* green transformation table */
    u16 *       pi_trans_blue;                    /* blue transformation table */
 
    /* Choose transformation matrix coefficients */
    i_crv = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][0];
    i_cbu = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][1];
    i_cgu = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][2];
    i_cgv = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][3];

    /* Choose the conversions tables */
    pi_trans_red =      p_vout->pi_trans16_red;
    pi_trans_green =    p_vout->pi_trans16_green;
    pi_trans_blue =     p_vout->pi_trans16_blue;    

    /* Set the base pointers and transformation parameters */
    p_y =               p_pic->p_y;
    p_u =               p_pic->p_u;
    p_v =               p_pic->p_v;
    i_width =           p_pic->i_width;
    i_height =          p_pic->i_height;
    i_chroma_width =    i_width / 2;
    i_eol_offset =      p_vout->i_bytes_per_line / 2 - i_width;    
        
    /* Get base adress for destination image */
    pi_pic = (u16 *)vout_SysGetPicture( p_vout );

    /* Do YUV transformation - the loops are repeated for optimization */
    switch( p_pic->i_type )
    {
    case YUV_420_PICTURE:                   /* 15 or 16 bpp 420 transformation */
#ifdef HAVE_MMX
        vout_YUV420_16_MMX( p_y, p_u, p_v, 
                            i_width, i_height, 
                            i_width, i_chroma_width,
                            0, (u8 *) pi_pic, 
                            0, 0, p_vout->i_bytes_per_line, 
                            p_vout->i_screen_depth == 15 );
#else
        YUV_TRANSFORM( 420,
                       pi_trans_red, 
                       pi_trans_green, 
                       pi_trans_blue,
                       pi_pic );            
#endif
        break;
    case YUV_422_PICTURE:                   /* 15 or 16 bpp 422 transformation */
        YUV_TRANSFORM( 422,
                       pi_trans_red, 
                       pi_trans_green, 
                       pi_trans_blue,
                       pi_pic );            
1059
        break;
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    case YUV_444_PICTURE:                   /* 15 or 16 bpp 444 transformation */
        YUV_TRANSFORM( 444,
                       pi_trans_red, 
                       pi_trans_green, 
                       pi_trans_blue,
                       pi_pic );            
        break;                 
    }
}

/*******************************************************************************
 * RenderYUV32Picture: render a 24 or 32 bpp YUV picture
 *******************************************************************************
 * Performs the YUV convertion. The picture sent to this function should only
 * have YUV_420, YUV_422 or YUV_444 types.
 *******************************************************************************/
static void RenderYUV32Picture( vout_thread_t *p_vout, picture_t *p_pic )
{
    int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
    int         i_pic_x, i_pic_y;                /* x,y coordinates in picture */
    int         i_y, i_u, i_v;                           /* Y, U and V samples */
    int         i_width, i_height;                             /* picture size */
    int         i_chroma_width;                                /* chroma width */    
    int         i_eol_offset;          /* pixels from end of line to next line */
    yuv_data_t *p_y;                                     /* Y data base adress */
    yuv_data_t *p_u;                                     /* U data base adress */
    yuv_data_t *p_v;                                     /* V data base adress */
    u32 *       pi_pic;                 /* base adress for destination picture */
    u32 *       pi_trans_red;                      /* red transformation table */
    u32 *       pi_trans_green;                  /* green transformation table */
    u32 *       pi_trans_blue;                    /* blue transformation table */
 
    /* Choose transformation matrix coefficients */
    i_crv = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][0];
    i_cbu = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][1];
    i_cgu = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][2];
    i_cgv = MATRIX_COEFFICIENTS_TABLE[p_pic->i_matrix_coefficients][3];

    /* Choose the conversions tables */
    pi_trans_red =      p_vout->pi_trans32_red;
    pi_trans_green =    p_vout->pi_trans32_green;
    pi_trans_blue =     p_vout->pi_trans32_blue;    

    /* Set the base pointers and transformation parameters */
    p_y =               p_pic->p_y;
    p_u =               p_pic->p_u;
    p_v =               p_pic->p_v;
    i_width =           p_pic->i_width;
    i_height =          p_pic->i_height;
    i_chroma_width =    i_width / 2;
    i_eol_offset =      p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - i_width;
        
    /* Get base adress for destination image */
    pi_pic = (u32 *)vout_SysGetPicture( p_vout );

    /* Do YUV transformation - the loops are repeated for optimization */
    switch( p_pic->i_type )
    {
    case YUV_420_PICTURE:                   /* 24 or 32 bpp 420 transformation */
        YUV_TRANSFORM( 420,
                       pi_trans_red, 
                       pi_trans_green, 
                       pi_trans_blue,
                       pi_pic );            
        break;
    case YUV_422_PICTURE:                   /* 24 or 32 bpp 422 transformation */
        YUV_TRANSFORM( 422,
                       pi_trans_red, 
                       pi_trans_green, 
                       pi_trans_blue,
                       pi_pic );            
        break;
    case YUV_444_PICTURE:                   /* 24 or 32 bpp 444 transformation */
        YUV_TRANSFORM( 444,
                       pi_trans_red, 
                       pi_trans_green, 
                       pi_trans_blue,
                       pi_pic );            
        break;                 
    }
}

/*******************************************************************************
 * RenderInfo: print additionnal informations on a picture
 *******************************************************************************
 * This function will add informations such as fps and buffer size on a picture
 *******************************************************************************/
static void RenderInfo( vout_thread_t *p_vout )
{
    char        psz_buffer[256];                              /* string buffer */

#ifdef STATS
    /* Print FPS rate */
    if( p_vout->c_pictures > VOUT_FPS_SAMPLES )
    {        
        sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
                 ( p_vout->fps_sample[ (p_vout->i_fps_index + (VOUT_FPS_SAMPLES - 1)) % 
                                     VOUT_FPS_SAMPLES ] -
                   p_vout->fps_sample[ p_vout->i_fps_index ] ) );        
        vout_SysPrint( p_vout, p_vout->i_width, 0, 1, -1, psz_buffer );
    }

    /* Print statistics */
    sprintf( psz_buffer, "%ld pictures, %.1f %% idle loops", p_vout->c_pictures,
             (double) p_vout->c_idle_loops * 100 / p_vout->c_loops );    
    vout_SysPrint( p_vout, 0, 0, -1, -1, psz_buffer );    
#endif
    
1168
#ifdef DEBUG
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    /* Print heap size  */
    sprintf( psz_buffer, "video heap size: %d (%.1f %%)", p_vout->i_pictures,
             (double) p_vout->i_pictures * 100 / VOUT_MAX_PICTURES );
    vout_SysPrint( p_vout, 0, p_vout->i_height, -1, 1, psz_buffer );    
#endif

#ifdef DEBUG_VIDEO
    /* Print rendering statistics */
    sprintf( psz_buffer, "picture rendering time: %lu us", 
             (unsigned long) p_vout->picture_render_time );    
    vout_SysPrint( p_vout, p_vout->i_width, p_vout->i_height, 1, 1, psz_buffer );    
1180
#endif
1181 1182
}

1183