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

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

#ifdef VIDEO_X11
20
#include <X11/Xlib.h>                           /* for video_sys.h in X11 mode */
Vincent Seguin's avatar
Vincent Seguin committed
21
#endif
Michel Kaempf's avatar
Michel Kaempf committed
22 23 24 25

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

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

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

41 42 43 44 45 46
/* 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. */
47
#define YUV_GRAYSCALE( TRANS_GRAY, P_PIC )                              \
48 49 50
/* Main loop */                                                         \
for (i_pic_y=0; i_pic_y < p_pic->i_height ; i_pic_y++)                  \
{                                                                       \
51
    for (i_pic_x=0; i_pic_x< p_pic->i_width; i_pic_x+=16)               \
52
    {                                                                   \
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        /* Convert 16 pixels (width is always multiple of 16 */         \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
        *P_PIC++ = TRANS_GRAY[ *p_y++ ];                                \
70 71 72 73 74
    }                                                                   \
    /* Skip until beginning of next line */                             \
    P_PIC += i_eol_offset;                                              \
}                                                                       

75 76 77 78 79 80
/* 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. */
81 82 83 84
#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++)                  \
{                                                                       \
85
    for (i_pic_x=0; i_pic_x< p_pic->i_width; i_pic_x+=2 )               \
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    {                                                                   \
        /* 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) )                           \
    {                                                                   \
109 110
        p_u -= i_chroma_width;                                          \
        p_v -= i_chroma_width;                                          \
111 112
    }                                                                   \
    /* Skip until beginning of next line */                             \
113
    P_PIC += i_eol_offset;                                              \
114 115
}

Vincent Seguin's avatar
Vincent Seguin committed
116 117 118
/*******************************************************************************
 * Constants
 *******************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
119

Vincent Seguin's avatar
Vincent Seguin committed
120
/* RGB/YUV inversion matrix (ISO/IEC 13818-2 section 6.3.6, table 6.9) */
121
const int MATRIX_COEFFICIENTS_TABLE[8][4] =
Vincent Seguin's avatar
Vincent Seguin committed
122 123 124 125 126 127 128 129 130 131 132
{
  {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) */
};

133 134 135 136
/*******************************************************************************
 * External prototypes
 *******************************************************************************/
#ifdef HAVE_MMX
137
/* YUV transformations for MMX - in video_yuv_mmx.S 
138 139 140 141
 *      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
142
 *      p_pic:                  RGB frame
143 144 145 146 147 148 149
 *      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,
150
                         unsigned int i_aspect, u8 *p_pic, 
151 152 153 154
                         u32 i_dci_offset, u32 i_offset_to_line_0,
                         int CCOPitch, int i_colortype );
#endif

155 156 157 158 159 160 161 162 163 164
/* Optimized YUV functions: translations and tables building - in video_yuv_c.c
 * ??? looks efficient, but does not work well - ask walken */
void yuvToRgb16 ( unsigned char * Y,
			unsigned char * U, unsigned char * V,
                  short * dest, short table[1935], int width);
int rgbTable16 (short table [1935],
                int redMask, int greenMask, int blueMask,
                unsigned char gamma[256]);


Vincent Seguin's avatar
Vincent Seguin committed
165
/*******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
166
 * Local prototypes
Vincent Seguin's avatar
Vincent Seguin committed
167
 *******************************************************************************/
168 169 170 171
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 );
172
static void     BuildTables             ( vout_thread_t *p_vout );
173 174 175 176
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 );
177
static void     RenderPictureInfo       ( vout_thread_t *p_vout, picture_t *p_pic );
178
static int      RenderIdle              ( vout_thread_t *p_vout, int i_level );
Michel Kaempf's avatar
Michel Kaempf committed
179 180 181 182 183 184 185

/*******************************************************************************
 * 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.
186
 * If not, it will be updated using one of the THREAD_* constants.
Michel Kaempf's avatar
Michel Kaempf committed
187
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
188
vout_thread_t * vout_CreateThread               ( 
189
#ifdef VIDEO_X11
190
                                                  char *psz_display, Window root_window, 
Vincent Seguin's avatar
Vincent Seguin committed
191 192 193
#endif
                                                  int i_width, int i_height, int *pi_status 
                                                )
Michel Kaempf's avatar
Michel Kaempf committed
194 195 196 197
{
    vout_thread_t * p_vout;                               /* thread descriptor */
    int             i_status;                                 /* thread status */

198
    /* Allocate descriptor */
Michel Kaempf's avatar
Michel Kaempf committed
199
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
200
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
201
    {
202
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));        
Michel Kaempf's avatar
Michel Kaempf committed
203 204
        return( NULL );
    }
205 206 207

    /* 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
208 209 210
#ifdef DEBUG
    p_vout->b_info              = 1;    
#else
211
    p_vout->b_info              = 0;    
Vincent Seguin's avatar
Vincent Seguin committed
212
#endif
213 214
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, 
                                                       VOUT_GRAYSCALE_DEFAULT );
215 216
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
217
    p_vout->i_bytes_per_line    = i_width * 2;    
218 219 220 221
    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;
222
    p_vout->f_gamma             = VOUT_GAMMA;    
223
    intf_DbgMsg("wished configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line), ratio %.2f:%.2f, gray=%d\n",
224
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
225 226
                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 );
227 228 229
   
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
Vincent Seguin's avatar
Vincent Seguin committed
230 231
    if( vout_SysCreate( p_vout
#if defined(VIDEO_X11)
232
                        , psz_display, root_window 
Vincent Seguin's avatar
Vincent Seguin committed
233 234
#endif
        ) )
Michel Kaempf's avatar
Michel Kaempf committed
235
    {
Vincent Seguin's avatar
Vincent Seguin committed
236 237
      free( p_vout );
      return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
238
    }
239
    intf_DbgMsg("actual configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line), ratio %.2f:%.2f, gray=%d\n",
240
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
241 242
                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
243
  
244 245 246 247 248 249 250 251 252
    /* 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;
253
    p_vout->c_fps_samples       = 0;
254
#endif      
255 256 257
    p_vout->b_gamma_change      = 0;
    p_vout->i_new_width         = p_vout->i_width;
    p_vout->i_new_height        = p_vout->i_height;    
Michel Kaempf's avatar
Michel Kaempf committed
258 259

    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
260 261 262
    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
263
    {
264
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
265
	vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
266 267 268 269
        free( p_vout );
        return( NULL );
    }   

270 271 272
    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
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    /* 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
303
    *p_vout->pi_status = THREAD_DESTROY;    
Michel Kaempf's avatar
Michel Kaempf committed
304 305 306 307 308
     
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
309
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
310 311 312 313 314 315 316 317 318 319
    {
        do
        {
            msleep( THREAD_SLEEP );
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
                && (i_status != THREAD_FATAL) );   
    }
}

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
320
 * vout_DisplayPicture: display a picture
Michel Kaempf's avatar
Michel Kaempf committed
321
 *******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
322
 * Remove the reservation flag of a picture, which will cause it to be ready for
323 324
 * display. The picture does not need to be locked, since it is ignored by
 * the output thread if is reserved.
Michel Kaempf's avatar
Michel Kaempf committed
325
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
326
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
327
{
328 329 330 331
#ifdef DEBUG_VIDEO
    char        psz_date[MSTRTIME_MAX_SIZE];         /* buffer for date string */
#endif

332
#ifdef DEBUG
333 334 335
    /* Check if picture status is valid */
    if( p_pic->i_status != RESERVED_PICTURE )
    {
336
        intf_DbgMsg("error: picture %d has invalid status %d\n", p_pic, p_pic->i_status );       
337
    }   
338
#endif
Michel Kaempf's avatar
Michel Kaempf committed
339 340

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

343 344 345 346 347
#ifdef DEBUG_VIDEO
    /* Send picture informations */
    intf_DbgMsg("picture %p: type=%d, %dx%d, date=%s\n", p_pic, p_pic->i_type, 
                p_pic->i_width,p_pic->i_height, mstrtime( psz_date, p_pic->date ) );    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
348 349 350
}

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
351
 * vout_CreatePicture: allocate a picture in the video output heap.
Michel Kaempf's avatar
Michel Kaempf committed
352 353
 *******************************************************************************
 * This function create a reserved image in the video output heap. 
Vincent Seguin's avatar
Vincent Seguin committed
354
 * A null pointer is returned if the function fails. This method provides an
355 356
 * already allocated zone of memory in the picture data fields. It needs locking
 * since several pictures can be created by several producers threads.
Michel Kaempf's avatar
Michel Kaempf committed
357
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
358 359
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
360
{
Vincent Seguin's avatar
Vincent Seguin committed
361 362 363
    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
364

Vincent Seguin's avatar
Vincent Seguin committed
365 366
    /* Get lock */
    vlc_mutex_lock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
367

Vincent Seguin's avatar
Vincent Seguin committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    /* 
     * 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;
387 388 389 390
#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
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
		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
407 408
    }

Vincent Seguin's avatar
Vincent Seguin committed
409 410 411 412 413 414 415
    /* 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
416 417
    }

Vincent Seguin's avatar
Vincent Seguin committed
418 419 420 421 422 423 424
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
425
        {
426 427 428 429 430 431 432 433 434 435 436 437 438
        case YUV_420_PICTURE:          /* YUV 420: 1,1/4,1/4 samples per pixel */
            p_free_picture->p_data = malloc( i_height * i_bytes_per_line * 3 / 2 );
            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 * 5 / 4);
            break;
        case YUV_422_PICTURE:          /* YUV 422: 1,1/2,1/2 samples per pixel */
            p_free_picture->p_data = malloc( 2 * i_height * i_bytes_per_line );
            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 * 3 / 2);
            break;
        case YUV_444_PICTURE:              /* YUV 444: 1,1,1 samples per pixel */
Vincent Seguin's avatar
Vincent Seguin committed
439
            p_free_picture->p_data = malloc( 3 * i_height * i_bytes_per_line );                
440 441 442
            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
443 444 445
            break;                
#ifdef DEBUG
        default:
446
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
447 448
            p_free_picture->p_data   =  NULL;            
            break;            
Vincent Seguin's avatar
Vincent Seguin committed
449
#endif    
Michel Kaempf's avatar
Michel Kaempf committed
450 451
        }

Vincent Seguin's avatar
Vincent Seguin committed
452 453 454
        if( p_free_picture->p_data != NULL )
        {        
            /* Copy picture informations */
455 456 457 458 459 460 461
            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
462 463 464 465
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
466 467 468 469
            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
470 471
        }
        
472 473 474
#ifdef DEBUG_VIDEO
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
#endif
Vincent Seguin's avatar
Vincent Seguin committed
475 476
        vlc_mutex_unlock( &p_vout->lock );
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
477
    }
Vincent Seguin's avatar
Vincent Seguin committed
478 479
    
    // No free or destroyed picture could be found
480
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
481 482
    vlc_mutex_unlock( &p_vout->lock );
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
483 484 485
}

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

Vincent Seguin's avatar
Vincent Seguin committed
504
    p_pic->i_status = DESTROYED_PICTURE;
505 506 507 508

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
509 510 511 512 513 514
}

/*******************************************************************************
 * vout_LinkPicture: increment reference counter of a picture
 *******************************************************************************
 * This function increment the reference counter of a picture in the video
515
 * heap. It needs a lock since several producer threads can access the picture.
Michel Kaempf's avatar
Michel Kaempf committed
516 517 518
 *******************************************************************************/
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
519
    vlc_mutex_lock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
520
    p_pic->i_refcount++;
521
    vlc_mutex_unlock( &p_vout->lock );
522 523 524 525

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
526 527 528 529 530 531 532 533 534
}

/*******************************************************************************
 * 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
535
    vlc_mutex_lock( &p_vout->lock );
Michel Kaempf's avatar
Michel Kaempf committed
536
    p_pic->i_refcount--;
Vincent Seguin's avatar
Vincent Seguin committed
537
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
538
    {
Vincent Seguin's avatar
Vincent Seguin committed
539
	p_pic->i_status = DESTROYED_PICTURE;
Michel Kaempf's avatar
Michel Kaempf committed
540
    }
541
    vlc_mutex_unlock( &p_vout->lock );
542 543 544 545

#ifdef DEBUG_VIDEO
    intf_DbgMsg("picture %p\n", p_pic);    
#endif
Michel Kaempf's avatar
Michel Kaempf committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
}

/* following functions are local */

/*******************************************************************************
 * InitThread: initialize video output thread
 *******************************************************************************
 * This function is called from RunThread and performs the second step of the
 * initialization. It returns 0 on success. Note that the thread's flag are not
 * modified inside this function.
 *******************************************************************************/
static int InitThread( vout_thread_t *p_vout )
{
    int     i_index;                                          /* generic index */    

    /* Update status */
    *p_vout->pi_status = THREAD_START;    
    
    /* Initialize pictures */    
Vincent Seguin's avatar
Vincent Seguin committed
565
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
Michel Kaempf's avatar
Michel Kaempf committed
566
    {
Vincent Seguin's avatar
Vincent Seguin committed
567 568
        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
569 570
    }

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

Vincent Seguin's avatar
Vincent Seguin committed
578
    /* Allocate translation tables */
579
    p_vout->p_trans_base = malloc( ( 4 * 1024 + 1935 ) * p_vout->i_bytes_per_pixel );
580
    if( p_vout->p_trans_base == NULL )
Vincent Seguin's avatar
Vincent Seguin committed
581
    {
582 583
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
        return( 1 );                
Vincent Seguin's avatar
Vincent Seguin committed
584
    }
585 586 587 588 589
    p_vout->p_trans_red =       p_vout->p_trans_base +           384 *p_vout->i_bytes_per_pixel;
    p_vout->p_trans_green =     p_vout->p_trans_base + (  1024 + 384)*p_vout->i_bytes_per_pixel;
    p_vout->p_trans_blue =      p_vout->p_trans_base + (2*1024 + 384)*p_vout->i_bytes_per_pixel;
    p_vout->p_trans_gray =      p_vout->p_trans_base + (3*1024 + 384)*p_vout->i_bytes_per_pixel;
    p_vout->p_trans_optimized = p_vout->p_trans_base + (4*1024      )*p_vout->i_bytes_per_pixel;    
Vincent Seguin's avatar
Vincent Seguin committed
590 591
    
    /* Build translation tables */
592
    BuildTables( p_vout );
Vincent Seguin's avatar
Vincent Seguin committed
593
    
Michel Kaempf's avatar
Michel Kaempf committed
594
    /* Mark thread as running and return */
595 596
    p_vout->b_active =          1;    
    *p_vout->pi_status =        THREAD_READY;    
597
    intf_DbgMsg("thread ready\n");    
598
    return( 0 );    
Michel Kaempf's avatar
Michel Kaempf committed
599 600 601 602 603 604 605 606 607 608 609 610 611
}

/*******************************************************************************
 * 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 */
612
    int             i_idle_level = 0;                            /* idle level */
Michel Kaempf's avatar
Michel Kaempf committed
613
    mtime_t         current_date;                              /* current date */
614
    mtime_t         pic_date = 0;                              /* picture date */    
615 616 617 618
    mtime_t         last_date = 0;                        /* last picture date */    
    boolean_t       b_display;                                 /* display flag */    
    picture_t *     p_pic;                                  /* picture pointer */
     
Michel Kaempf's avatar
Michel Kaempf committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    /* 
     * 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
635
        /* 
636 637 638
	 * Find the picture to display - this operation does not need lock,
         * since only READY_PICTURES are handled 
         */
639
        p_pic = NULL;         
Vincent Seguin's avatar
Vincent Seguin committed
640 641 642 643
	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) || 
644
		  (p_vout->p_picture[i_picture].date < pic_date) ) )
Vincent Seguin's avatar
Vincent Seguin committed
645 646
	    {
		p_pic = &p_vout->p_picture[i_picture];
647
                pic_date = p_pic->date;                
Vincent Seguin's avatar
Vincent Seguin committed
648 649
	    }
	}
650
        current_date = mdate();
Vincent Seguin's avatar
Vincent Seguin committed
651 652 653 654 655

        /* 
	 * Render picture if any
	 */
        if( p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
656
        {
657 658
#ifdef STATS
            /* Computes FPS rate */
659 660
            p_vout->fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = pic_date;
#endif	    
661
	    if( pic_date < current_date )
Vincent Seguin's avatar
Vincent Seguin committed
662
	    {
663 664 665 666 667
		/* Picture is late: it will be destroyed and the thread will sleep and
                 * go to next picture */
                vlc_mutex_lock( &p_vout->lock );
                p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
                vlc_mutex_unlock( &p_vout->lock );
668 669 670
#ifdef DEBUG_VIDEO
		intf_DbgMsg( "warning: late picture %p skipped\n", p_pic );
#endif
671
                p_pic =         NULL;                
Vincent Seguin's avatar
Vincent Seguin committed
672
	    }
673
	    else if( pic_date > current_date + VOUT_DISPLAY_DELAY )
Vincent Seguin's avatar
Vincent Seguin committed
674 675 676 677
	    {
		/* 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 */
678
                p_pic =         NULL;                
Vincent Seguin's avatar
Vincent Seguin committed
679 680 681 682
	    }
	    else
	    {
		/* Picture has not yet been displayed, and has a valid display
683 684
		 * date : render it, then mark it as displayed */
                if( p_vout->b_active )
685
                {                    
686
                    RenderPicture( p_vout, p_pic );
687 688 689 690
                    if( p_vout->b_info )
                    {
                        RenderPictureInfo( p_vout, p_pic );                        
                    }                    
691
                }                
692 693 694
                vlc_mutex_lock( &p_vout->lock );
                p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
                vlc_mutex_unlock( &p_vout->lock );
Vincent Seguin's avatar
Vincent Seguin committed
695
	    }
Michel Kaempf's avatar
Michel Kaempf committed
696
        }
697

698 699 700 701 702 703 704 705 706
        /* 
         * Rebuild tables if gamma has changed
         */
        if( p_vout->b_gamma_change )
        {
            p_vout->b_gamma_change = 0;            
            BuildTables( p_vout );            
        }        

Vincent Seguin's avatar
Vincent Seguin committed
707 708 709 710 711 712 713 714 715 716 717 718
        /*
         * 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 
719
	{            
Vincent Seguin's avatar
Vincent Seguin committed
720 721
	    if( p_pic )
	    {
722 723 724 725 726 727
		/* A picture is ready to be displayed : remove blank screen flag */
                last_date =     pic_date;
                i_idle_level =  0;
                b_display =     1;                
                
                /* Sleep until its display date */
728
		mwait( pic_date );
Vincent Seguin's avatar
Vincent Seguin committed
729 730 731
	    }
	    else
	    {
732 733 734 735 736 737 738 739 740 741 742 743
                /* If last picture was a long time ago, increase idle level, reset
                 * date and render idle screen */
                if( !i_err && (current_date - last_date > VOUT_IDLE_DELAY) )
                {       
                    last_date = current_date;                    
                    b_display = p_vout->b_active && RenderIdle( p_vout, i_idle_level++ );
                }
                else
                {
                    b_display = 0;                    
                }
                
Michel Kaempf's avatar
Michel Kaempf committed
744
#ifdef STATS
Vincent Seguin's avatar
Vincent Seguin committed
745 746
		/* Update counters */
		p_vout->c_idle_loops++;
Michel Kaempf's avatar
Michel Kaempf committed
747
#endif
748 749 750

		/* Sleep to wait for new pictures */
		msleep( VOUT_IDLE_SLEEP );
Vincent Seguin's avatar
Vincent Seguin committed
751
	    }
752 753 754 755 756 757

            /* On awakening, send immediately picture to display */
            if( b_display && p_vout->b_active )
            {
                vout_SysDisplay( p_vout );
            }
Vincent Seguin's avatar
Vincent Seguin committed
758
	}
Michel Kaempf's avatar
Michel Kaempf committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

#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 );
776
    intf_DbgMsg( "thread end\n" );
Michel Kaempf's avatar
Michel Kaempf committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
}

/*******************************************************************************
 * 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
805
    int     i_picture;
Michel Kaempf's avatar
Michel Kaempf committed
806 807 808 809 810
        
    /* Store status */
    pi_status = p_vout->pi_status;    
    *pi_status = THREAD_END;    

Vincent Seguin's avatar
Vincent Seguin committed
811 812 813 814 815 816 817 818
    /* 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
819

820 821
    /* Destroy translation tables */
    free( p_vout->p_trans_base );
Vincent Seguin's avatar
Vincent Seguin committed
822
    
Michel Kaempf's avatar
Michel Kaempf committed
823
    /* Destroy thread structures allocated by InitThread */
Vincent Seguin's avatar
Vincent Seguin committed
824
    vout_SysEnd( p_vout );
825
    vout_SysDestroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
826 827 828 829 830 831
    free( p_vout );

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

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 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
/*******************************************************************************
 * BuildTables: build YUV translation tables
 *******************************************************************************
 * This function will build translations tables according to pixel width and
 * gamma.
 *******************************************************************************/  
static void BuildTables( vout_thread_t *p_vout )
{
    u16 *       p_trans16_red =         (u16 *) p_vout->p_trans_red;
    u16 *       p_trans16_green =       (u16 *) p_vout->p_trans_green;
    u16 *       p_trans16_blue =        (u16 *) p_vout->p_trans_blue;
    u16 *       p_trans16_gray =        (u16 *) p_vout->p_trans_gray;
    u32 *       p_trans32_red =         (u32 *) p_vout->p_trans_red;
    u32 *       p_trans32_green =       (u32 *) p_vout->p_trans_green;
    u32 *       p_trans32_blue =        (u32 *) p_vout->p_trans_blue;
    u32 *       p_trans32_gray =        (u32 *) p_vout->p_trans_gray;          
    u8          i_gamma[256];                                   /* gamma table */    
    int         i_index;                                    /* index in tables */
    
    /* Build gamma table */     
    for( i_index = 0; i_index < 256; i_index++ )
    {
        i_gamma[i_index] = 255. * pow( (double)i_index / 255., p_vout->f_gamma );        
    }
        
    /* Build red, green, blue and gray tables */
    switch( p_vout->i_screen_depth )
    {
    case 15:
        for( i_index = -384; i_index < 640; i_index++) 
        {
            p_trans16_red[i_index]     = (i_gamma[CLIP_BYTE( i_index )] & 0xf8)<<7;
            p_trans16_green[i_index]   = (i_gamma[CLIP_BYTE( i_index )] & 0xf8)<<2;
            p_trans16_blue[i_index]    =  i_gamma[CLIP_BYTE( i_index )] >> 3;
            p_trans16_gray[i_index]    = p_trans16_red[i_index] | 
                p_trans16_green[i_index] | p_trans16_blue[i_index];            
        }
        break;        
    case 16:
        for( i_index = -384; i_index < 640; i_index++) 
        {
            p_trans16_red[i_index]     = (i_gamma[CLIP_BYTE( i_index )] & 0xf8)<<8;
            p_trans16_green[i_index]   = (i_gamma[CLIP_BYTE( i_index )] & 0xfc)<<3;
            p_trans16_blue[i_index]    =  i_gamma[CLIP_BYTE( i_index )] >> 3;
            p_trans16_gray[i_index]    = p_trans16_red[i_index] |
                p_trans16_green[i_index] | p_trans16_blue[i_index];
        }        
        break;        
    case 32:        
        for( i_index = -384; i_index < 640; i_index++) 
        {
            p_trans32_red[i_index]     = i_gamma[CLIP_BYTE( i_index )] <<16;
            p_trans32_green[i_index]   = i_gamma[CLIP_BYTE( i_index )] <<8;
            p_trans32_blue[i_index]    = i_gamma[CLIP_BYTE( i_index )] ;
            p_trans32_gray[i_index]    = p_trans32_red[i_index] |
                p_trans32_green[i_index] | p_trans32_blue[i_index];
        }
        break;        
#ifdef DEBUG
    default:
        intf_DbgMsg("error: invalid screen depth %d\n", p_vout->i_screen_depth );
        break;      
#endif
    } 
896 897 898 899 900 901 902 903 904

    /* Build red, green and blue tables for optimized transformation */
    //????
    switch( p_vout->i_screen_depth )
    {
    case 16:
        rgbTable16( (short *) p_vout->p_trans_optimized, 0xf800, 0x07e0, 0x01f, i_gamma );
        break;        
    }    
905 906
}

Michel Kaempf's avatar
Michel Kaempf committed
907 908 909 910 911 912
/*******************************************************************************
 * 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
913
 * by the vout thread later.
914
 * ???? 24 and 32 bpp should probably be separated
Michel Kaempf's avatar
Michel Kaempf committed
915 916 917
 *******************************************************************************/
static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
918 919 920 921 922 923 924 925
#ifdef DEBUG_VIDEO
    /* Send picture informations */
    intf_DbgMsg("picture %p\n", p_pic );

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

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    /* Change aspect ratio or resize frame to fit frame */
    if( (p_pic->i_width > p_vout->i_width) || (p_pic->i_height > p_vout->i_height) )
    {
#ifdef VIDEO_X11
        /* X11: window can be resized, so resize it - the picture won't be 
         * rendered since any alteration of the window size means recreating the
         * XImages */
        p_vout->i_new_width =   p_pic->i_width;
        p_vout->i_new_height =  p_pic->i_height;
        return;        
#else
        /* Other drivers: the video output thread can't change its size, so
         * we need to change the aspect ratio */
        //????
#endif
    }    

    /* Choose appropriate rendering function */
944 945 946 947 948
    switch( p_pic->i_type )
    {
    case YUV_420_PICTURE:                   /* YUV picture: YUV transformation */        
    case YUV_422_PICTURE:
    case YUV_444_PICTURE:
949 950 951 952 953 954 955 956
        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 );        
        }
957
        else if( p_vout->i_bytes_per_pixel == 4 )              /* color 32 bpp */
958 959 960
        {
            RenderYUV32Picture( p_vout, p_pic );            
        }
961
        break;        
962 963
#ifdef DEBUG
    default:        
964
        intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
965 966 967
        break;        
#endif
    }
968 969 970 971 972

#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
973 974
}

975
/*******************************************************************************
976
 * RenderYUVGrayPicture: render YUV picture in grayscale
977
 *******************************************************************************
978 979
 * Performs the YUV convertion. The picture sent to this function should only
 * have YUV_420, YUV_422 or YUV_444 types.
980
 *******************************************************************************/
981
static void RenderYUVGrayPicture( vout_thread_t *p_vout, picture_t *p_pic )
982
{
983 984 985
    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 */   
986
    yuv_data_t *p_y;                                     /* Y data base adress */
987 988 989 990
    u16 *       p_pic16;                  /* destination picture, 15 or 16 bpp */
    u32 *       p_pic32;                        /* destination picture, 32 bpp */
    u16 *       p_trans16_gray;          /* transformation table, 15 or 16 bpp */
    u32 *       p_trans32_gray;                /* transformation table, 32 bpp */
991 992 993 994 995 996
 
    /* 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;
997

998 999
    /* Get base adress for destination image and translation tables, then
     * transform image */
1000 1001 1002 1003
    switch( p_vout->i_screen_depth )
    {
    case 15:
    case 16:
1004 1005 1006
        p_trans16_gray =       (u16 *) p_vout->p_trans_gray;
        p_pic16 =              (u16 *) vout_SysGetPicture( p_vout );
        YUV_GRAYSCALE( p_trans16_gray, p_pic16 );
1007 1008
        break;        
    case 32:
1009 1010 1011
        p_trans32_gray =       (u32 *) p_vout->p_trans_gray;
        p_pic32 =              (u32 *) vout_SysGetPicture( p_vout );
        YUV_GRAYSCALE( p_trans32_gray, p_pic32 );
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
        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 */
1039 1040 1041 1042
    u16 *       p_data;                 /* base adress for destination picture */
    u16 *       p_trans_red;                       /* red transformation table */
    u16 *       p_trans_green;                   /* green transformation table */
    u16 *       p_trans_blue;                     /* blue transformation table */
1043 1044 1045 1046 1047 1048 1049
 
    /* 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];

1050 1051 1052 1053 1054
    /* Choose the conversions tables and picture address */
    p_trans_red =       (u16 *) p_vout->p_trans_red;
    p_trans_green =     (u16 *) p_vout->p_trans_green;
    p_trans_blue =      (u16 *) p_vout->p_trans_blue;    
    p_data =            (u16 *) vout_SysGetPicture( p_vout );
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

    /* 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;    
        
    /* 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,
1073
                            0, p_data, 
1074 1075 1076 1077
                            0, 0, p_vout->i_bytes_per_line, 
                            p_vout->i_screen_depth == 15 );
#else
        YUV_TRANSFORM( 420,
1078 1079 1080 1081
                       p_trans_red, 
                       p_trans_green, 
                       p_trans_blue,
                       p_data );            
1082
  //???      yuvToRgb16( p_y, p_u, p_v, p_data, p_vout->p_trans_optimized, i_width*i_height );
1083 1084 1085 1086
#endif
        break;
    case YUV_422_PICTURE:                   /* 15 or 16 bpp 422 transformation */
        YUV_TRANSFORM( 422,
1087 1088 1089 1090
                       p_trans_red, 
                       p_trans_green, 
                       p_trans_blue,
                       p_data );            
1091
        break;
1092 1093
    case YUV_444_PICTURE:                   /* 15 or 16 bpp 444 transformation */
        YUV_TRANSFORM( 444,
1094 1095 1096 1097
                       p_trans_red, 
                       p_trans_green, 
                       p_trans_blue,
                       p_data );            
1098 1099 1100 1101 1102
        break;                 
    }
}

/*******************************************************************************
1103
 * RenderYUV32Picture: render a 32 bpp YUV picture
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
 *******************************************************************************
 * 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 */
1119 1120 1121 1122
    u32 *       p_data;                 /* base adress for destination picture */
    u32 *       p_trans_red;                       /* red transformation table */
    u32 *       p_trans_green;                   /* green transformation table */
    u32 *       p_trans_blue;                     /* blue transformation table */
1123 1124 1125 1126 1127 1128 1129
 
    /* 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];

1130 1131 1132 1133 1134
    /* Choose the conversions tables and picture address */
    p_trans_red =       (u32 *) p_vout->p_trans_red;
    p_trans_green =     (u32 *) p_vout->p_trans_green;
    p_trans_blue =      (u32 *) p_vout->p_trans_blue;    
    p_data =            (u32 *) vout_SysGetPicture( p_vout );
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

    /* 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;
        
    /* Do YUV transformation - the loops are repeated for optimization */
    switch( p_pic->i_type )
    {
1148
    case YUV_420_PICTURE:                         /* 32 bpp 420 transformation */
1149
        YUV_TRANSFORM( 420,
1150 1151 1152 1153
                       p_trans_red, 
                       p_trans_green, 
                       p_trans_blue,
                       p_data );            
1154
        break;
1155
    case YUV_422_PICTURE:                         /* 32 bpp 422 transformation */
1156
        YUV_TRANSFORM( 422,
1157 1158 1159 1160
                       p_trans_red, 
                       p_trans_green, 
                       p_trans_blue,
                       p_data );            
1161
        break;
1162
    case YUV_444_PICTURE:                         /* 32 bpp 444 transformation */
1163
        YUV_TRANSFORM( 444,
1164 1165 1166 1167
                       p_trans_red, 
                       p_trans_green, 
                       p_trans_blue,
                       p_data );            
1168 1169 1170 1171 1172
        break;                 
    }
}

/*******************************************************************************
1173
 * RenderPictureInfo: print additionnal informations on a picture
1174 1175 1176
 *******************************************************************************
 * This function will add informations such as fps and buffer size on a picture
 *******************************************************************************/
1177
static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1178 1179
{
    char        psz_buffer[256];                              /* string buffer */
1180 1181 1182 1183 1184
#ifdef DEBUG
    int         i_ready_pic = 0;                             /* ready pictures */
    int         i_reserved_pic = 0;                       /* reserved pictures */
    int         i_picture;                                    /* picture index */
#endif
1185 1186

#ifdef STATS
1187 1188 1189
    /* 
     * Print FPS rate in upper right corner 
     */
1190
    if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1191 1192
    {        
        sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1193 1194
                 ( 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 ] ) );        
1195 1196 1197
        vout_SysPrint( p_vout, p_vout->i_width, 0, 1, -1, psz_buffer );
    }

1198 1199 1200
    /* 
     * Print statistics in upper left corner 
     */
1201 1202
    sprintf( psz_buffer, "gamma=%.2f   %ld frames (%.1f %% idle)", 
             p_vout->f_gamma, p_vout->c_fps_samples, p_vout->c_loops ? 
1203
             (double ) p_vout->c_idle_loops * 100 / p_vout->c_loops : 100. );    
1204 1205 1206
    vout_SysPrint( p_vout, 0, 0, -1, -1, psz_buffer );    
#endif
    
1207
#ifdef DEBUG
1208 1209 1210
    /* 
     * Print heap state in lower left corner  
     */
1211 1212 1213 1214 1215 1216