]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
IMPORTANT:
[vlc] / src / video_output / video_output.c
1 /******************************************************************************
2  * video_output.c : video output thread
3  * (c)2000 VideoLAN
4  ******************************************************************************
5  * This module describes the programming interface for video output threads.
6  * It includes functions allowing to open a new thread, send pictures to a
7  * thread, and destroy a previously oppenned video output thread.
8  ******************************************************************************/
9
10 /******************************************************************************
11  * Preamble
12  ******************************************************************************/
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "common.h"
19 #include "config.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22 #include "video.h"
23 #include "video_output.h"
24 #include "video_text.h"
25 #include "video_sys.h"
26 #include "video_yuv.h"
27 #include "intf_msg.h"
28 #include "main.h"
29
30 /******************************************************************************
31  * Local prototypes
32  ******************************************************************************/
33 static int      BinaryLog         ( u32 i );
34 static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
35 static int      InitThread        ( vout_thread_t *p_vout );
36 static void     RunThread         ( vout_thread_t *p_vout );
37 static void     ErrorThread       ( vout_thread_t *p_vout );
38 static void     EndThread         ( vout_thread_t *p_vout );
39 static void     DestroyThread     ( vout_thread_t *p_vout, int i_status );
40 static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y, 
41                                     int i_h_align, int i_v_align,
42                                     unsigned char *psz_text );
43 static void     SetBufferArea     ( vout_thread_t *p_vout, int i_x, int i_y,
44                                     int i_w, int i_h );
45 static void     SetBufferPicture  ( vout_thread_t *p_vout, picture_t *p_pic );
46 static void     RenderPicture     ( vout_thread_t *p_vout, picture_t *p_pic );
47 static void     RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
48 static void     RenderSubPicture  ( vout_thread_t *p_vout,
49                                     subpicture_t *p_subpic );
50 static void     RenderInterface   ( vout_thread_t *p_vout );
51 static int      RenderIdle        ( vout_thread_t *p_vout );
52 static void     RenderInfo        ( vout_thread_t *p_vout );
53 static int      Manage            ( vout_thread_t *p_vout );
54 static int      Align             ( vout_thread_t *p_vout, int *pi_x,
55                                     int *pi_y, int i_width, int i_height,
56                                     int i_h_align, int i_v_align );
57
58 /******************************************************************************
59  * vout_CreateThread: creates a new video output thread
60  ******************************************************************************
61  * This function creates a new video output thread, and returns a pointer
62  * to its description. On error, it returns NULL.
63  * If pi_status is NULL, then the function will block until the thread is ready.
64  * If not, it will be updated using one of the THREAD_* constants.
65  ******************************************************************************/
66 vout_thread_t * vout_CreateThread               ( char *psz_display, int i_root_window, 
67                                                   int i_width, int i_height, int *pi_status )
68 {
69     vout_thread_t * p_vout;                              /* thread descriptor */
70     int             i_status;                                /* thread status */
71     int             i_index;                /* index for array initialization */
72
73     /* Allocate descriptor */
74     intf_DbgMsg("\n");    
75     p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
76     if( p_vout == NULL )
77     {
78         intf_ErrMsg("error: %s\n", strerror(ENOMEM));        
79         return( NULL );
80     }
81
82     /* Initialize thread properties - thread id and locks will be initialized 
83      * later */
84     p_vout->b_die               = 0;
85     p_vout->b_error             = 0;    
86     p_vout->b_active            = 0;
87     p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
88     *p_vout->pi_status          = THREAD_CREATE;    
89
90     /* Initialize some fields used by the system-dependant method - these fields will
91      * probably be modified by the method, and are only preferences */
92     p_vout->i_changes           = 0;
93     p_vout->i_width             = i_width;
94     p_vout->i_height            = i_height;
95     p_vout->i_bytes_per_line    = i_width * 2;    
96     p_vout->i_screen_depth      = 15;
97     p_vout->i_bytes_per_pixel   = 2;
98     p_vout->f_gamma             = VOUT_GAMMA;    
99
100     p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
101     p_vout->b_info              = 0;    
102     p_vout->b_interface         = 0;
103     p_vout->b_scale             = 0;
104     
105     intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
106                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
107                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
108
109     /* Initialize idle screen */
110     p_vout->last_display_date   = mdate();
111     p_vout->last_idle_date      = 0;
112
113 #ifdef STATS
114     /* Initialize statistics fields */
115     p_vout->render_time         = 0;    
116     p_vout->c_fps_samples       = 0;    
117 #endif      
118
119     /* Initialize buffer index */
120     p_vout->i_buffer_index      = 0;
121
122     /* Initialize pictures and subpictures - translation tables and functions
123      * will be initialized later in InitThread */    
124     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
125     {
126         p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
127         p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
128         p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
129         p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
130     }
131    
132     /* Create and initialize system-dependant method - this function issues its
133      * own error messages */
134     if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
135     {
136       free( p_vout );
137       return( NULL );
138     }
139     intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
140                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
141                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
142                 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
143
144     /* Calculate shifts from system-updated masks */
145     MaskToShift( &p_vout->i_red_lshift,   &p_vout->i_red_rshift,   p_vout->i_red_mask );
146     MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
147     MaskToShift( &p_vout->i_blue_lshift,  &p_vout->i_blue_rshift,  p_vout->i_blue_mask );
148
149     /* Set some usefull colors */
150     p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
151     p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
152     p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
153     p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );    
154
155     /* Load fonts - fonts must be initialized after the systme method since
156      * they may be dependant of screen depth and other thread properties */
157     p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );    
158     if( p_vout->p_default_font == NULL )
159     {
160         vout_SysDestroy( p_vout );        
161         free( p_vout );        
162         return( NULL );        
163     }    
164     p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );        
165     if( p_vout->p_large_font == NULL )
166     {
167         vout_UnloadFont( p_vout->p_default_font );        
168         vout_SysDestroy( p_vout );        
169         free( p_vout );        
170         return( NULL );        
171     }     
172
173     /* Create thread and set locks */
174     vlc_mutex_init( &p_vout->picture_lock );
175     vlc_mutex_init( &p_vout->subpicture_lock );    
176     vlc_mutex_init( &p_vout->change_lock );    
177     vlc_mutex_lock( &p_vout->change_lock );    
178     if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
179     {
180         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
181         vout_UnloadFont( p_vout->p_default_font );
182         vout_UnloadFont( p_vout->p_large_font );        
183         vout_SysDestroy( p_vout );
184         free( p_vout );
185         return( NULL );
186     }   
187
188     intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width, 
189              p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
190
191     /* If status is NULL, wait until the thread is created */
192     if( pi_status == NULL )
193     {
194         do
195         {            
196             msleep( THREAD_SLEEP );
197         }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR) 
198                 && (i_status != THREAD_FATAL) );
199         if( i_status != THREAD_READY )
200         {
201             return( NULL );            
202         }        
203     }
204     return( p_vout );
205 }
206
207 /******************************************************************************
208  * vout_DestroyThread: destroys a previously created thread
209  ******************************************************************************
210  * Destroy a terminated thread. 
211  * The function will request a destruction of the specified thread. If pi_error
212  * is NULL, it will return once the thread is destroyed. Else, it will be 
213  * update using one of the THREAD_* constants.
214  ******************************************************************************/
215 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
216 {  
217     int     i_status;                                        /* thread status */
218
219     /* Set status */
220     intf_DbgMsg("\n");
221     p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
222     *p_vout->pi_status = THREAD_DESTROY;    
223      
224     /* Request thread destruction */
225     p_vout->b_die = 1;
226
227     /* If status is NULL, wait until thread has been destroyed */
228     if( pi_status == NULL )
229     {
230         do
231         {
232             msleep( THREAD_SLEEP );
233         }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
234                 && (i_status != THREAD_FATAL) );   
235     }
236 }
237
238 /******************************************************************************
239  * vout_DisplaySubPicture: display a subpicture unit
240  ******************************************************************************
241  * Remove the reservation flag of an subpicture, which will cause it to be ready 
242  * for display. The picture does not need to be locked, since it is ignored by
243  * the output thread if is reserved.
244  ******************************************************************************/
245 void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
246 {
247 #ifdef DEBUG_VIDEO
248     char        psz_begin_date[MSTRTIME_MAX_SIZE];  /* buffer for date string */
249     char        psz_end_date[MSTRTIME_MAX_SIZE];    /* buffer for date string */
250 #endif
251
252 #ifdef DEBUG
253     /* Check if status is valid */
254     if( p_subpic->i_status != RESERVED_SUBPICTURE )
255     {
256         intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic, 
257                     p_subpic->i_status );       
258     }   
259 #endif
260
261     /* Remove reservation flag */
262     p_subpic->i_status = READY_SUBPICTURE;
263
264 #ifdef DEBUG_VIDEO
265     /* Send subpicture informations */
266     intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n", 
267                 p_subpic, p_subpic->i_type, 
268                 mstrtime( psz_begin_date, p_subpic->begin_date ), 
269                 mstrtime( psz_end_date, p_subpic->end_date ) );    
270 #endif
271 }
272
273 /******************************************************************************
274  * vout_CreateSubPicture: allocate an subpicture in the video output heap.
275  ******************************************************************************
276  * This function create a reserved subpicture in the video output heap. 
277  * A null pointer is returned if the function fails. This method provides an
278  * already allocated zone of memory in the spu data fields. It needs locking
279  * since several pictures can be created by several producers threads. 
280  ******************************************************************************/
281 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type, 
282                                      int i_size )
283 {
284     int                 i_subpic;                          /* subpicture index */
285     subpicture_t *      p_free_subpic = NULL;         /* first free subpicture */    
286     subpicture_t *      p_destroyed_subpic = NULL;   /* first destroyed subpic */
287
288     /* Get lock */
289     vlc_mutex_lock( &p_vout->subpicture_lock );
290
291     /* 
292      * Look for an empty place 
293      */
294     for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
295     {
296         if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
297         {
298             /* Subpicture is marked for destruction, but is still allocated */
299             if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
300                 (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
301             {
302                 /* Memory size do match or is smaller : memory will not be reallocated, 
303                  * and function can end immediately - this is the best possible case, 
304                  * since no memory allocation needs to be done */
305                 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
306 #ifdef DEBUG_VIDEO
307                 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n", 
308                             &p_vout->p_subpicture[i_subpic] );                
309 #endif
310                 vlc_mutex_unlock( &p_vout->subpicture_lock );
311                 return( &p_vout->p_subpicture[i_subpic] );
312             }
313             else if( p_destroyed_subpic == NULL )
314             {
315                 /* Memory size do not match, but subpicture index will be kept in
316                  * case no other place are left */
317                 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];                
318             }       
319         }
320         else if( (p_free_subpic == NULL) && 
321                  (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
322         {
323             /* Subpicture is empty and ready for allocation */
324             p_free_subpic = &p_vout->p_subpicture[i_subpic];
325         }
326     }
327
328     /* If no free subpicture is available, use a destroyed subpicture */
329     if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
330     { 
331         /* No free subpicture or matching destroyed subpicture has been found, but
332          * a destroyed subpicture is still avalaible */
333         free( p_destroyed_subpic->p_data );        
334         p_free_subpic = p_destroyed_subpic;
335     }
336
337     /*
338      * Prepare subpicture
339      */
340     if( p_free_subpic != NULL )
341     {
342         /* Allocate memory */
343         switch( i_type )
344         {
345         case TEXT_SUBPICTURE:                               /* text subpicture */
346             p_free_subpic->p_data = malloc( i_size + 1 );             
347             break;
348 #ifdef DEBUG
349         default:
350             intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
351             p_free_subpic->p_data   =  NULL;            
352             break;            
353 #endif    
354         }
355
356         if( p_free_subpic->p_data != NULL )
357         {                    /* Copy subpicture informations, set some default values */
358             p_free_subpic->i_type                      = i_type;
359             p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
360             p_free_subpic->i_size                      = i_size;            
361             p_free_subpic->i_x                         = 0;
362             p_free_subpic->i_y                         = 0;             
363             p_free_subpic->i_width                     = 0;
364             p_free_subpic->i_height                    = 0;
365             p_free_subpic->i_horizontal_align          = CENTER_RALIGN;             
366             p_free_subpic->i_vertical_align            = CENTER_RALIGN;             
367         }
368         else
369         {
370             /* Memory allocation failed : set subpicture as empty */
371             p_free_subpic->i_type   =  EMPTY_SUBPICTURE;            
372             p_free_subpic->i_status =  FREE_SUBPICTURE;            
373             p_free_subpic =            NULL;            
374             intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );            
375         }
376         
377 #ifdef DEBUG_VIDEO
378         intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );        
379 #endif
380         vlc_mutex_unlock( &p_vout->subpicture_lock );
381         return( p_free_subpic );
382     }
383     
384     /* No free or destroyed subpicture could be found */
385     intf_DbgMsg( "warning: heap is full\n" );
386     vlc_mutex_unlock( &p_vout->subpicture_lock );
387     return( NULL );
388 }
389
390 /******************************************************************************
391  * vout_DestroySubPicture: remove a subpicture from the heap
392  ******************************************************************************
393  * This function frees a previously reserved subpicture.
394  * It is meant to be used when the construction of a picture aborted.
395  * This function does not need locking since reserved subpictures are ignored 
396  * by the output thread.
397  ******************************************************************************/
398 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
399 {
400 #ifdef DEBUG
401    /* Check if status is valid */
402    if( p_subpic->i_status != RESERVED_SUBPICTURE )
403    {
404        intf_DbgMsg("error: subpicture %p has invalid status %d\n", 
405                    p_subpic, p_subpic->i_status );       
406    }   
407 #endif
408
409     p_subpic->i_status = DESTROYED_SUBPICTURE;
410
411 #ifdef DEBUG_VIDEO
412     intf_DbgMsg("subpicture %p\n", p_subpic);    
413 #endif
414 }
415
416 /*******************************************************************************
417  * vout_DisplayPicture: display a picture
418  *******************************************************************************
419  * Remove the reservation flag of a picture, which will cause it to be ready for
420  * display. The picture won't be displayed until vout_DatePicture has been 
421  * called.
422  *******************************************************************************/
423 void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
424 {
425     vlc_mutex_lock( &p_vout->picture_lock );
426     switch( p_pic->i_status )
427     {
428     case RESERVED_PICTURE:        
429         p_pic->i_status = RESERVED_DISP_PICTURE;
430         break;        
431     case RESERVED_DATED_PICTURE:
432         p_pic->i_status = READY_PICTURE;
433         break;        
434 #ifdef DEBUG
435     default:        
436         intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );    
437         break;        
438 #endif
439     }
440
441 #ifdef DEBUG_VIDEO
442     intf_DbgMsg("picture %p\n", p_pic);
443 #endif
444     vlc_mutex_unlock( &p_vout->picture_lock );
445 }
446
447 /*******************************************************************************
448  * vout_DatePicture: date a picture
449  *******************************************************************************
450  * Remove the reservation flag of a picture, which will cause it to be ready for
451  * display. The picture won't be displayed until vout_DisplayPicture has been 
452  * called.
453  *******************************************************************************/
454 void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
455 {
456 #ifdef DEBUG_VIDEO
457     char        psz_date[MSTRTIME_MAX_SIZE];                           /* date */
458 #endif
459
460     vlc_mutex_lock( &p_vout->picture_lock );
461     p_pic->date = date;    
462     switch( p_pic->i_status )
463     {
464     case RESERVED_PICTURE:        
465         p_pic->i_status = RESERVED_DATED_PICTURE;
466         break;        
467     case RESERVED_DISP_PICTURE:
468         p_pic->i_status = READY_PICTURE;
469         break;        
470 #ifdef DEBUG
471     default:        
472         intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );    
473         break;        
474 #endif
475     }
476
477 #ifdef DEBUG_VIDEO
478     intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
479 #endif
480     vlc_mutex_unlock( &p_vout->picture_lock );
481 }
482
483 /******************************************************************************
484  * vout_CreatePicture: allocate a picture in the video output heap.
485  ******************************************************************************
486  * This function create a reserved image in the video output heap. 
487  * A null pointer is returned if the function fails. This method provides an
488  * already allocated zone of memory in the picture data fields. It needs locking
489  * since several pictures can be created by several producers threads. 
490  ******************************************************************************/
491 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type, 
492                                int i_width, int i_height )
493 {
494     int         i_picture;                                   /* picture index */
495     int         i_chroma_width = 0;                           /* chroma width */    
496     picture_t * p_free_picture = NULL;                  /* first free picture */    
497     picture_t * p_destroyed_picture = NULL;        /* first destroyed picture */    
498
499     /* Get lock */
500     vlc_mutex_lock( &p_vout->picture_lock );
501
502     /* 
503      * Look for an empty place 
504      */
505     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
506     {
507         if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
508         {
509             /* Picture is marked for destruction, but is still allocated - note
510              * that if width and type are the same for two pictures, chroma_width 
511              * should also be the same */
512             if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
513                 (p_vout->p_picture[i_picture].i_height         == i_height) &&
514                 (p_vout->p_picture[i_picture].i_width          == i_width) )
515             {
516                 /* Memory size do match : memory will not be reallocated, and function
517                  * can end immediately - this is the best possible case, since no
518                  * memory allocation needs to be done */
519                 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
520 #ifdef DEBUG_VIDEO
521                 intf_DbgMsg("picture %p (in destroyed picture slot)\n", 
522                             &p_vout->p_picture[i_picture] );                
523 #endif
524                 vlc_mutex_unlock( &p_vout->picture_lock );
525                 return( &p_vout->p_picture[i_picture] );
526             }
527             else if( p_destroyed_picture == NULL )
528             {
529                 /* Memory size do not match, but picture index will be kept in
530                  * case no other place are left */
531                 p_destroyed_picture = &p_vout->p_picture[i_picture];                
532             }       
533         }
534         else if( (p_free_picture == NULL) && 
535                  (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
536         {
537             /* Picture is empty and ready for allocation */
538             p_free_picture = &p_vout->p_picture[i_picture];            
539         }
540     }
541
542     /* If no free picture is available, use a destroyed picture */
543     if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
544     { 
545         /* No free picture or matching destroyed picture has been found, but
546          * a destroyed picture is still avalaible */
547         free( p_destroyed_picture->p_data );        
548         p_free_picture = p_destroyed_picture;        
549     }
550
551     /*
552      * Prepare picture
553      */
554     if( p_free_picture != NULL )
555     {
556         /* Allocate memory */
557         switch( i_type )
558         {
559         case YUV_420_PICTURE:          /* YUV 420: 1,1/4,1/4 samples per pixel */
560             i_chroma_width = i_width / 2;            
561             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
562             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
563             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
564             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
565             break;
566         case YUV_422_PICTURE:          /* YUV 422: 1,1/2,1/2 samples per pixel */
567             i_chroma_width = i_width / 2;            
568             p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
569             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
570             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
571             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
572             break;
573         case YUV_444_PICTURE:              /* YUV 444: 1,1,1 samples per pixel */
574             i_chroma_width = i_width;            
575             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
576             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
577             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
578             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
579             break;                
580 #ifdef DEBUG
581         default:
582             intf_DbgMsg("error: unknown picture type %d\n", i_type );
583             p_free_picture->p_data   =  NULL;            
584             break;            
585 #endif    
586         }
587
588         if( p_free_picture->p_data != NULL )
589         {        
590             /* Copy picture informations, set some default values */
591             p_free_picture->i_type                      = i_type;
592             p_free_picture->i_status                    = RESERVED_PICTURE;
593             p_free_picture->i_matrix_coefficients       = 1; 
594             p_free_picture->i_width                     = i_width;
595             p_free_picture->i_height                    = i_height;
596             p_free_picture->i_chroma_width              = i_chroma_width;            
597             p_free_picture->i_display_horizontal_offset = 0;
598             p_free_picture->i_display_vertical_offset   = 0;            
599             p_free_picture->i_display_width             = i_width;
600             p_free_picture->i_display_height            = i_height;
601             p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;            
602             p_free_picture->i_refcount                  = 0;            
603         }
604         else
605         {
606             /* Memory allocation failed : set picture as empty */
607             p_free_picture->i_type   =  EMPTY_PICTURE;            
608             p_free_picture->i_status =  FREE_PICTURE;            
609             p_free_picture =            NULL;            
610             intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );            
611         }
612         
613 #ifdef DEBUG_VIDEO
614         intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
615 #endif
616         vlc_mutex_unlock( &p_vout->picture_lock );
617         return( p_free_picture );
618     }
619     
620     /* No free or destroyed picture could be found */
621     intf_DbgMsg( "warning: heap is full\n" );
622     vlc_mutex_unlock( &p_vout->picture_lock );
623     return( NULL );
624 }
625
626 /******************************************************************************
627  * vout_DestroyPicture: remove a permanent or reserved picture from the heap
628  ******************************************************************************
629  * This function frees a previously reserved picture or a permanent
630  * picture. It is meant to be used when the construction of a picture aborted.
631  * Note that the picture will be destroyed even if it is linked !
632  * This function does not need locking since reserved pictures are ignored by
633  * the output thread.
634  ******************************************************************************/
635 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
636 {
637 #ifdef DEBUG
638    /* Check if picture status is valid */
639    if( (p_pic->i_status != RESERVED_PICTURE) && 
640        (p_pic->i_status != RESERVED_DATED_PICTURE) &&
641        (p_pic->i_status != RESERVED_DISP_PICTURE) )
642    {
643        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );       
644    }   
645 #endif
646
647     p_pic->i_status = DESTROYED_PICTURE;
648
649 #ifdef DEBUG_VIDEO
650     intf_DbgMsg("picture %p\n", p_pic);    
651 #endif
652 }
653
654 /******************************************************************************
655  * vout_LinkPicture: increment reference counter of a picture
656  ******************************************************************************
657  * This function increment the reference counter of a picture in the video
658  * heap. It needs a lock since several producer threads can access the picture.
659  ******************************************************************************/
660 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
661 {
662     vlc_mutex_lock( &p_vout->picture_lock );
663     p_pic->i_refcount++;
664
665 #ifdef DEBUG_VIDEO
666     intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
667 #endif
668
669     vlc_mutex_unlock( &p_vout->picture_lock );
670 }
671
672 /******************************************************************************
673  * vout_UnlinkPicture: decrement reference counter of a picture
674  ******************************************************************************
675  * This function decrement the reference counter of a picture in the video heap.
676  ******************************************************************************/
677 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
678 {
679     vlc_mutex_lock( &p_vout->picture_lock );
680     p_pic->i_refcount--;
681
682 #ifdef DEBUG_VIDEO
683     if( p_pic->i_refcount < 0 )
684     {
685         intf_DbgMsg("error: refcount < 0\n");
686         p_pic->i_refcount = 0;        
687     }    
688 #endif
689
690     if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
691     {
692         p_pic->i_status = DESTROYED_PICTURE;
693     }
694
695 #ifdef DEBUG_VIDEO
696     intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
697 #endif
698
699     vlc_mutex_unlock( &p_vout->picture_lock );
700 }
701
702 /******************************************************************************
703  * vout_SetBuffers: set buffers adresses
704  ******************************************************************************
705  * This function is called by system drivers to set buffers video memory 
706  * adresses.
707  ******************************************************************************/
708 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
709 {
710     /* No picture previously */
711     p_vout->p_buffer[0].i_pic_x =         0;
712     p_vout->p_buffer[0].i_pic_y =         0;
713     p_vout->p_buffer[0].i_pic_width =     0;
714     p_vout->p_buffer[0].i_pic_height =    0;
715     p_vout->p_buffer[1].i_pic_x =         0;
716     p_vout->p_buffer[1].i_pic_y =         0;
717     p_vout->p_buffer[1].i_pic_width =     0;
718     p_vout->p_buffer[1].i_pic_height =    0;
719
720     /* The first area covers all the screen */
721     p_vout->p_buffer[0].i_areas =                 1;
722     p_vout->p_buffer[0].pi_area_begin[0] =        0;
723     p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
724     p_vout->p_buffer[1].i_areas =                 1;
725     p_vout->p_buffer[1].pi_area_begin[0] =        0;
726     p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;
727
728     /* Set adresses */
729     p_vout->p_buffer[0].p_data = p_buf1;    
730     p_vout->p_buffer[1].p_data = p_buf2;    
731 }
732
733 /*****************************************************************************
734  * vout_Pixel2RGB: return red, green and blue from pixel value
735  *****************************************************************************
736  * Return color values, in 0-255 range, of the decomposition of a pixel. This
737  * is a slow routine and should only be used for initialization phase.
738  *****************************************************************************/
739 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
740 {
741     *pi_red =   i_pixel & p_vout->i_red_mask;
742     *pi_green = i_pixel & p_vout->i_green_mask;
743     *pi_blue =  i_pixel & p_vout->i_blue_mask;
744 }
745
746 /* following functions are local */
747
748 /*****************************************************************************
749  * BinaryLog: computes the base 2 log of a binary value
750  *****************************************************************************
751  * This functions is used by MaskToShift, to get a bit index from a binary 
752  * value.
753  *****************************************************************************/
754 static int BinaryLog(u32 i)
755 {
756     int i_log;
757
758     i_log = 0;
759     if (i & 0xffff0000) 
760     {        
761         i_log = 16;
762     }    
763     if (i & 0xff00ff00) 
764     {        
765         i_log += 8;
766     }    
767     if (i & 0xf0f0f0f0) 
768     {        
769         i_log += 4;
770     }    
771     if (i & 0xcccccccc) 
772     {        
773         i_log += 2;
774     }    
775     if (i & 0xaaaaaaaa) 
776     {        
777         i_log++;
778     }    
779     if (i != ((u32)1 << i_log))
780     {        
781         intf_ErrMsg("internal error: binary log overflow\n");        
782     }    
783
784     return( i_log );
785 }
786
787 /*****************************************************************************
788  * MaskToShift: transform a color mask into right and left shifts
789  *****************************************************************************
790  * This function is used for obtaining color shifts from masks.
791  *****************************************************************************/
792 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
793 {
794     u32 i_low, i_high;                 /* lower hand higher bits of the mask */
795
796     /* Get bits */
797     i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
798     i_high = i_mask + i_low;                       /* higher bit of the mask */
799
800     /* Transform bits into an index */
801     i_low =  BinaryLog (i_low);
802     i_high = BinaryLog (i_high);
803
804     /* Update pointers and return */
805     *pi_left =   i_low;
806     *pi_right = (8 - i_high + i_low);
807 }
808
809 /******************************************************************************
810  * InitThread: initialize video output thread
811  ******************************************************************************
812  * This function is called from RunThread and performs the second step of the
813  * initialization. It returns 0 on success. Note that the thread's flag are not
814  * modified inside this function.
815  ******************************************************************************/
816 static int InitThread( vout_thread_t *p_vout )
817 {
818     /* Update status */
819     intf_DbgMsg("\n");
820     *p_vout->pi_status = THREAD_START;    
821
822     /* Initialize output method - this function issues its own error messages */
823     if( vout_SysInit( p_vout ) )
824     {
825         return( 1 );
826     } 
827
828     /* Initialize convertion tables and functions */
829     if( vout_InitYUV( p_vout ) )
830     {
831         intf_ErrMsg("error: can't allocate YUV translation tables\n");
832         return( 1 );                
833     }
834     
835     /* Mark thread as running and return */
836     p_vout->b_active =          1;    
837     *p_vout->pi_status =        THREAD_READY;    
838     intf_DbgMsg("thread ready\n");    
839     return( 0 );    
840 }
841
842 /******************************************************************************
843  * RunThread: video output thread
844  ******************************************************************************
845  * Video output thread. This function does only returns when the thread is
846  * terminated. It handles the pictures arriving in the video heap and the
847  * display device events.
848  ******************************************************************************/
849 static void RunThread( vout_thread_t *p_vout)
850 {
851     int             i_index;                                 /* index in heap */
852     mtime_t         current_date;                             /* current date */
853     mtime_t         display_date;                             /* display date */    
854     boolean_t       b_display;                                /* display flag */    
855     picture_t *     p_pic;                                 /* picture pointer */
856     subpicture_t *  p_subpic;                           /* subpicture pointer */    
857      
858     /* 
859      * Initialize thread
860      */
861     p_vout->b_error = InitThread( p_vout );
862     if( p_vout->b_error )
863     {
864         DestroyThread( p_vout, THREAD_ERROR );
865         return;        
866     }    
867     intf_DbgMsg("\n");
868
869     /*
870      * Main loop - it is not executed if an error occured during
871      * initialization
872      */
873     while( (!p_vout->b_die) && (!p_vout->b_error) )
874     {
875         /* Initialize loop variables */
876         p_pic =         NULL;
877         p_subpic =      NULL;
878         display_date =  0;        
879         current_date =  mdate();
880
881         /* 
882          * Find the picture to display - this operation does not need lock,
883          * since only READY_PICTUREs are handled 
884          */
885         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
886         {
887             if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
888                 ( (p_pic == NULL) || 
889                   (p_vout->p_picture[i_index].date < display_date) ) )
890             {
891                 p_pic = &p_vout->p_picture[i_index];
892                 display_date = p_pic->date;                
893             }
894         }
895  
896         if( p_pic )
897         {
898 #ifdef STATS
899             /* Computes FPS rate */
900             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
901 #endif      
902             if( display_date < current_date )
903             {
904                 /* Picture is late: it will be destroyed and the thread will sleep and
905                  * go to next picture */
906                 vlc_mutex_lock( &p_vout->picture_lock );
907                 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
908                 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
909                 vlc_mutex_unlock( &p_vout->picture_lock );
910                 p_pic =         NULL;                
911                 display_date =  0;                
912             }
913             else if( display_date > current_date + VOUT_DISPLAY_DELAY )
914             {
915                 /* A picture is ready to be rendered, but its rendering date is
916                  * far from the current one so the thread will perform an empty loop
917                  * as if no picture were found. The picture state is unchanged */
918                 p_pic =         NULL;                
919                 display_date =  0;                
920             }
921         }
922
923         /*
924          * Find the subpicture to display - this operation does not need lock, since
925          * only READY_SUBPICTURES are handled. If no picture has been selected,
926          * display_date will depend on the subpicture
927          */
928         //??
929
930         /*
931          * Perform rendering, sleep and display rendered picture
932          */
933         if( p_pic )                          /* picture and perhaps subpicture */
934         {
935             b_display = p_vout->b_active;            
936             p_vout->last_display_date = display_date;
937             
938             if( b_display )
939             {                
940                 /* Set picture dimensions and clear buffer */
941                 SetBufferPicture( p_vout, p_pic );
942
943                 /* Render picture and informations */
944                 RenderPicture( p_vout, p_pic );             
945                 if( p_vout->b_info )
946                 {
947                     RenderPictureInfo( p_vout, p_pic );
948                     RenderInfo( p_vout );                
949                 }
950             }
951             
952             /* Remove picture from heap */
953             vlc_mutex_lock( &p_vout->picture_lock );
954             p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
955             vlc_mutex_unlock( &p_vout->picture_lock );                          
956
957             /* Render interface and subpicture */
958             if( b_display && p_vout->b_interface )
959             {
960                 RenderInterface( p_vout );                
961             }
962             if( p_subpic )
963             {
964                 if( b_display )
965                 {                    
966                     RenderSubPicture( p_vout, p_subpic );
967                 }                
968
969                 /* Remove subpicture from heap */
970                 vlc_mutex_lock( &p_vout->subpicture_lock );
971                 p_subpic->i_status = DESTROYED_SUBPICTURE;
972                 vlc_mutex_unlock( &p_vout->subpicture_lock );                          
973             }
974
975         }
976         else if( p_subpic )                                /* subpicture alone */
977         {
978             b_display = p_vout->b_active;
979             p_vout->last_display_date = display_date;            
980
981             if( b_display )
982             {                
983                 /* Clear buffer */
984                 SetBufferPicture( p_vout, NULL );
985
986                 /* Render informations, interface and subpicture */
987                 if( p_vout->b_info )
988                 {
989                     RenderInfo( p_vout );
990                 }
991                 if( p_vout->b_interface )
992                 {
993                     RenderInterface( p_vout );
994                 }
995                 RenderSubPicture( p_vout, p_subpic );            
996             }            
997
998             /* Remove subpicture from heap */
999             vlc_mutex_lock( &p_vout->subpicture_lock );
1000             p_subpic->i_status = DESTROYED_SUBPICTURE;
1001             vlc_mutex_unlock( &p_vout->subpicture_lock );                          
1002         }
1003         else if( p_vout->b_active )          /* idle or interface screen alone */
1004         {
1005             if( p_vout->b_interface && 0 /* && ?? intf_change */ )
1006             {
1007                 /* Interface has changed, so a new rendering is required - force
1008                  * it by setting last idle date to 0 */
1009                 p_vout->last_idle_date = 0;                
1010             }
1011
1012             /* Render idle screen and update idle date, then render interface if
1013              * required */
1014             b_display = RenderIdle( p_vout );
1015             if( b_display )
1016             {                
1017                 p_vout->last_idle_date = current_date;
1018                 if( p_vout->b_interface )
1019                 {
1020                     RenderInterface( p_vout );                
1021                 }
1022             }
1023             
1024         }       
1025         else
1026         {
1027             b_display = 0;            
1028         }        
1029
1030         /*
1031          * Sleep, wake up and display rendered picture
1032          */
1033
1034 #ifdef STATS
1035         /* Store render time */
1036         p_vout->render_time = mdate() - current_date;
1037 #endif
1038
1039         /* Give back change lock */
1040         vlc_mutex_unlock( &p_vout->change_lock );        
1041
1042         /* Sleep a while or until a given date */
1043         if( display_date != 0 )
1044         {
1045             mwait( display_date );
1046         }
1047         else
1048         {
1049             msleep( VOUT_IDLE_SLEEP );                
1050         }            
1051
1052         /* On awakening, take back lock and send immediately picture to display, 
1053          * then swap buffers */
1054         vlc_mutex_lock( &p_vout->change_lock );        
1055 #ifdef DEBUG_VIDEO
1056         intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1057                      p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );        
1058 #endif            
1059         if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1060         {
1061             vout_SysDisplay( p_vout );
1062             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1063         }
1064
1065         /*
1066          * Check events and manage thread
1067          */
1068         if( vout_SysManage( p_vout ) | Manage( p_vout ) )
1069         {
1070             /* A fatal error occured, and the thread must terminate immediately,
1071              * without displaying anything - setting b_error to 1 cause the
1072              * immediate end of the main while() loop. */
1073             p_vout->b_error = 1;
1074         }  
1075     } 
1076
1077     /*
1078      * Error loop - wait until the thread destruction is requested
1079      */
1080     if( p_vout->b_error )
1081     {
1082         ErrorThread( p_vout );        
1083     }
1084
1085     /* End of thread */
1086     EndThread( p_vout );
1087     DestroyThread( p_vout, THREAD_OVER ); 
1088     intf_DbgMsg( "thread end\n" );
1089 }
1090
1091 /******************************************************************************
1092  * ErrorThread: RunThread() error loop
1093  ******************************************************************************
1094  * This function is called when an error occured during thread main's loop. The
1095  * thread can still receive feed, but must be ready to terminate as soon as
1096  * possible.
1097  ******************************************************************************/
1098 static void ErrorThread( vout_thread_t *p_vout )
1099 {
1100     /* Wait until a `die' order */
1101     intf_DbgMsg("\n");
1102     while( !p_vout->b_die )
1103     {
1104         /* Sleep a while */
1105         msleep( VOUT_IDLE_SLEEP );                
1106     }
1107 }
1108
1109 /*******************************************************************************
1110  * EndThread: thread destruction
1111  *******************************************************************************
1112  * This function is called when the thread ends after a sucessfull 
1113  * initialization. It frees all ressources allocated by InitThread.
1114  *******************************************************************************/
1115 static void EndThread( vout_thread_t *p_vout )
1116 {
1117     int     i_index;                                          /* index in heap */
1118             
1119     /* Store status */
1120     intf_DbgMsg("\n");
1121     *p_vout->pi_status = THREAD_END;    
1122
1123     /* Destroy all remaining pictures and subpictures */
1124     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1125     {
1126         if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1127         {
1128             free( p_vout->p_picture[i_index].p_data );
1129         }
1130         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1131         {
1132             free( p_vout->p_subpicture[i_index].p_data );            
1133         }        
1134     }
1135
1136     /* Destroy translation tables */
1137     vout_EndYUV( p_vout );  
1138     vout_SysEnd( p_vout );    
1139 }
1140
1141 /*******************************************************************************
1142  * DestroyThread: thread destruction
1143  *******************************************************************************
1144  * This function is called when the thread ends. It frees all ressources
1145  * allocated by CreateThread. Status is available at this stage.
1146  *******************************************************************************/
1147 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1148 {  
1149     int *pi_status;                                           /* status adress */
1150     
1151     /* Store status adress */
1152     intf_DbgMsg("\n");
1153     pi_status = p_vout->pi_status;    
1154     
1155     /* Destroy thread structures allocated by Create and InitThread */
1156     vout_UnloadFont( p_vout->p_default_font );
1157     vout_UnloadFont( p_vout->p_large_font ); 
1158     vout_SysDestroy( p_vout );
1159     free( p_vout );
1160     *pi_status = i_status;    
1161 }
1162
1163 /*******************************************************************************
1164  * Print: print simple text on a picture
1165  *******************************************************************************
1166  * This function will print a simple text on the picture. It is designed to
1167  * print debugging or general informations.
1168  *******************************************************************************/
1169 void Print( vout_thread_t *p_vout, int i_x, int i_y, int i_h_align, int i_v_align, unsigned char *psz_text )
1170 {
1171     int                 i_text_height;                    /* total text height */
1172     int                 i_text_width;                      /* total text width */
1173
1174     /* Update upper left coordinates according to alignment */
1175     vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1176     if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1177     {
1178         /* Set area and print text */
1179         SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );    
1180         vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data + 
1181                     i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1182                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line, 
1183                     p_vout->i_white_pixel, 0, 0, 
1184                     0, psz_text );
1185     }    
1186 }
1187
1188 /*******************************************************************************
1189  * SetBufferArea: activate an area in current buffer
1190  *******************************************************************************
1191  * This function is called when something is rendered on the current buffer.
1192  * It set the area as active and prepare it to be cleared on next rendering.
1193  * Pay attention to the fact that in this functions, i_h is in fact the end y
1194  * coordinate of the new area.
1195  *******************************************************************************/
1196 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1197 {
1198     vout_buffer_t *     p_buffer;                            /* current buffer */
1199     int                 i_area_begin, i_area_end;   /* area vertical extension */
1200     int                 i_area, i_area_copy;                     /* area index */    
1201     int                 i_area_shift;              /* shift distance for areas */    
1202     
1203     /* Choose buffer and modify h to end of area position */
1204     p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1205     i_h +=      i_y - 1;
1206  
1207     /* 
1208      * Remove part of the area which is inside the picture - this is done
1209      * by calling again SetBufferArea with the correct areas dimensions.
1210      */
1211     if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1212     {
1213         i_area_begin =  p_buffer->i_pic_y;
1214         i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
1215
1216         if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1217             ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1218             ((i_y <  i_area_begin) && (i_h > i_area_end)) )
1219         {                    
1220             /* Keep the stripe above the picture, if any */
1221             if( i_y < i_area_begin )
1222             {
1223                 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );            
1224             }
1225             /* Keep the stripe below the picture, if any */
1226             if( i_h > i_area_end )
1227             {
1228                 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1229             }        
1230             return;
1231         }        
1232     }   
1233
1234     /* Skip some extensions until interesting areas */
1235     for( i_area = 0; 
1236          (i_area < p_buffer->i_areas) &&
1237              (p_buffer->pi_area_end[i_area] + 1 <= i_y); 
1238          i_area++ )
1239     {
1240         ;        
1241     }
1242     
1243     if( i_area == p_buffer->i_areas )
1244     {
1245         /* New area is below all existing ones: just add it at the end of the 
1246          * array, if possible - else, append it to the last one */
1247         if( i_area < VOUT_MAX_AREAS )
1248         {
1249             p_buffer->pi_area_begin[i_area] = i_y;
1250             p_buffer->pi_area_end[i_area] = i_h;            
1251             p_buffer->i_areas++;            
1252         }
1253         else
1254         {
1255 #ifdef DEBUG_VIDEO
1256             intf_DbgMsg("areas overflow\n");            
1257 #endif
1258             p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;  
1259         }        
1260     }
1261     else 
1262     {
1263         i_area_begin =  p_buffer->pi_area_begin[i_area];
1264         i_area_end =    p_buffer->pi_area_end[i_area];
1265         
1266         if( i_y < i_area_begin ) 
1267         {
1268             if( i_h >= i_area_begin - 1 )
1269             {                
1270                 /* Extend area above */
1271                 p_buffer->pi_area_begin[i_area] = i_y;
1272             }
1273             else
1274             {
1275                 /* Create a new area above : merge last area if overflow, then 
1276                  * move all old areas down */
1277                 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1278                 {                    
1279 #ifdef DEBUG_VIDEO
1280                     intf_DbgMsg("areas overflow\n");       
1281 #endif
1282                     p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];                    
1283                 }
1284                 else
1285                 {
1286                     p_buffer->i_areas++;                    
1287                 }
1288                 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1289                 {
1290                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1291                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
1292                 }
1293                 p_buffer->pi_area_begin[i_area] = i_y;
1294                 p_buffer->pi_area_end[i_area] = i_h;
1295                 return;
1296             }              
1297         }
1298         if( i_h > i_area_end )
1299         {
1300             /* Find further areas which can be merged with the new one */
1301             for( i_area_copy = i_area + 1; 
1302                  (i_area_copy < p_buffer->i_areas) &&
1303                      (p_buffer->pi_area_begin[i_area] <= i_h);
1304                  i_area_copy++ )
1305             {
1306                 ;                
1307             }
1308             i_area_copy--;            
1309
1310             if( i_area_copy != i_area )
1311             {
1312                 /* Merge with last possible areas */
1313                 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1314
1315                 /* Shift lower areas upward */
1316                 i_area_shift = i_area_copy - i_area;                
1317                 p_buffer->i_areas -= i_area_shift;
1318                 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1319                 {
1320                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1321                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
1322                 }
1323             }
1324             else
1325             {
1326                 /* Extend area below */
1327                 p_buffer->pi_area_end[i_area] = i_h;
1328             }            
1329         }
1330     }
1331 }
1332
1333 /*******************************************************************************
1334  * SetBufferPicture: clear buffer and set picture area
1335  *******************************************************************************
1336  * This function is called before any rendering. It clears the current 
1337  * rendering buffer and set the new picture area. If the picture pointer is
1338  * NULL, then no picture area is defined. Floating operations are avoided since
1339  * some MMX calculations may follow.
1340  *******************************************************************************/
1341 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1342 {
1343     vout_buffer_t *     p_buffer;                            /* current buffer */
1344     int                 i_pic_x, i_pic_y;                  /* picture position */
1345     int                 i_pic_width, i_pic_height;       /* picture dimensions */    
1346     int                 i_old_pic_y, i_old_pic_height;     /* old picture area */    
1347     int                 i_vout_width, i_vout_height;     /* display dimensions */
1348     int                 i_area;                                  /* area index */    
1349     int                 i_data_index;                       /* area data index */    
1350     int                 i_data_size;     /* area data size, in 256 bytes blocs */
1351     u64 *               p_data;                     /* area data, for clearing */    
1352     byte_t *            p_data8;             /* area data, for clearing (slow) */
1353     
1354     /* Choose buffer and set display dimensions */
1355     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1356     i_vout_width =      p_vout->i_width;
1357     i_vout_height =     p_vout->i_height;    
1358
1359     /*
1360      * Computes new picture size 
1361      */
1362     if( p_pic != NULL )
1363     {
1364         /* Try horizontal scaling first - width must be a mutiple of 16 */
1365         i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ? 
1366                        i_vout_width : p_pic->i_width) & ~0xf;
1367         switch( p_pic->i_aspect_ratio )
1368         {
1369         case AR_3_4_PICTURE:
1370             i_pic_height = i_pic_width * 3 / 4;
1371             break;                
1372         case AR_16_9_PICTURE:
1373             i_pic_height = i_pic_width * 9 / 16;
1374             break;
1375         case AR_221_1_PICTURE:        
1376             i_pic_height = i_pic_width * 100 / 221;
1377             break;               
1378         case AR_SQUARE_PICTURE:
1379         default:
1380             i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;            
1381             break;
1382         }
1383
1384         /* If picture dimensions using horizontal scaling are too large, use 
1385          * vertical scaling. Since width must be a multiple of 16, height is
1386          * adjusted again after. */
1387         if( i_pic_height > i_vout_height )
1388         {
1389             i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ? 
1390                 i_vout_height : p_pic->i_height;
1391             switch( p_pic->i_aspect_ratio )
1392             {
1393             case AR_3_4_PICTURE:
1394                 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1395                 i_pic_height = i_pic_width * 3 / 4;
1396                 break;                
1397             case AR_16_9_PICTURE:
1398                 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1399                 i_pic_height = i_pic_width * 9 / 16;
1400                 break;
1401             case AR_221_1_PICTURE:        
1402                 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1403                 i_pic_height = i_pic_width * 100 / 221;
1404                 break;               
1405             case AR_SQUARE_PICTURE:
1406             default:
1407                 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1408                 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1409                 break;
1410             }        
1411         }        
1412
1413         /* Set picture position */
1414         i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1415         i_pic_y = (p_vout->i_height - i_pic_height) / 2;                
1416     }    
1417     else
1418     {
1419         /* No picture: size is 0 */
1420         i_pic_x =       0;
1421         i_pic_y =       0;
1422         i_pic_width =   0;
1423         i_pic_height =  0;
1424     }
1425
1426     /*
1427      * Set new picture size - if is is smaller than the previous one, clear 
1428      * around it. Since picture are centered, only their size is tested.
1429      */                                          
1430     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1431     {
1432         i_old_pic_y =            p_buffer->i_pic_y;
1433         i_old_pic_height =       p_buffer->i_pic_height;
1434         p_buffer->i_pic_x =      i_pic_x;
1435         p_buffer->i_pic_y =      i_pic_y;
1436         p_buffer->i_pic_width =  i_pic_width;
1437         p_buffer->i_pic_height = i_pic_height;                        
1438         SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1439     }
1440     else
1441     {
1442         p_buffer->i_pic_x =      i_pic_x;
1443         p_buffer->i_pic_y =      i_pic_y;
1444         p_buffer->i_pic_width =  i_pic_width;
1445         p_buffer->i_pic_height = i_pic_height;    
1446     }
1447
1448     /*
1449      * Clear areas
1450      */
1451     for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1452     {
1453 #ifdef DEBUG_VIDEO    
1454         intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic, 
1455                     p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1456 #endif
1457         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1458         p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1459         for( i_data_index = i_data_size / 256; i_data_index-- ; )
1460         {
1461             /* Clear 256 bytes block */
1462             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1463             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1464             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1465             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1466             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1467             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1468             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1469             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1470         }
1471         for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1472         {
1473             /* Clear remaining 16 bytes blocks */
1474             *p_data++ = 0;  *p_data++ = 0;            
1475         }
1476         p_data8 = (byte_t *)p_data;        
1477         for( i_data_index = i_data_size % 16; i_data_index--; )
1478         {
1479             /* Clear remaining bytes */
1480             *p_data8++ = 0;            
1481         }
1482     }    
1483
1484     /*
1485      * Clear areas array
1486      */
1487     p_buffer->i_areas = 0;
1488 }
1489
1490 /******************************************************************************
1491  * RenderPicture: render a picture
1492  ******************************************************************************
1493  * This function convert a picture from a video heap to a pixel-encoded image
1494  * and copy it to the current rendering buffer. No lock is required, since the
1495  * rendered picture has been determined as existant, and will only be destroyed
1496  * by the vout thread later.
1497  ******************************************************************************/
1498 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1499 {
1500 #ifdef DEBUG_VIDEO
1501     char                psz_date[MSTRTIME_MAX_SIZE];          /* picture date */    
1502     mtime_t             render_time;                /* picture rendering time */
1503 #endif
1504     vout_buffer_t *     p_buffer;                         /* rendering buffer */    
1505     byte_t *            p_pic_data;                 /* convertion destination */
1506     
1507     /* Get and set rendering informations */
1508     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1509     p_pic_data =        p_buffer->p_data + 
1510         p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1511         p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1512 #ifdef DEBUG_VIDEO
1513     render_time = mdate();    
1514 #endif
1515
1516     /*
1517      * Choose appropriate rendering function and render picture 
1518      */
1519     switch( p_pic->i_type )
1520     {
1521     case YUV_420_PICTURE:
1522         p_vout->yuv.p_Convert420( p_vout, p_pic_data, 
1523                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1524                                   p_pic->i_width, p_pic->i_height,
1525                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1526                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1527                                   p_pic->i_matrix_coefficients );
1528         break;        
1529     case YUV_422_PICTURE:
1530         p_vout->yuv.p_Convert422( p_vout, p_pic_data, 
1531                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1532                                   p_pic->i_width, p_pic->i_height,
1533                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1534                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1535                                   p_pic->i_matrix_coefficients );
1536         break;        
1537     case YUV_444_PICTURE:
1538         p_vout->yuv.p_Convert444( p_vout, p_pic_data, 
1539                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1540                                   p_pic->i_width, p_pic->i_height,
1541                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1542                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1543                                   p_pic->i_matrix_coefficients );
1544         break;        
1545 #ifdef DEBUG
1546     default:        
1547         intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1548         break;        
1549 #endif
1550     }
1551
1552 #ifdef DEBUG_VIDEO
1553     /* Print picture date and rendering time */
1554     intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1555                 p_vout->i_buffer_index, (long) (mdate() - render_time), 
1556                 mstrtime( psz_date, p_pic->date ));
1557 #endif
1558 }
1559
1560 /******************************************************************************
1561  * RenderPictureInfo: print additionnal informations on a picture
1562  ******************************************************************************
1563  * This function will print informations such as fps and other picture
1564  * dependant informations.
1565  ******************************************************************************/
1566 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1567 {
1568 #if defined(STATS) || defined(DEBUG)
1569     char        psz_buffer[256];                             /* string buffer */
1570 #endif
1571
1572 #ifdef STATS
1573     /* 
1574      * Print FPS rate in upper right corner 
1575      */
1576     if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1577     {        
1578         sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1579                  ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1580                    p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );        
1581         Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1582     }
1583
1584     /* 
1585      * Print frames count and loop time in upper left corner 
1586      */
1587     sprintf( psz_buffer, "%ld frames   rendering: %ld us", 
1588              (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1589     Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1590 #endif
1591
1592 #ifdef DEBUG
1593     /*
1594      * Print picture information in lower right corner
1595      */
1596     sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1597              (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1598              ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1599               ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1600              p_pic->i_width, p_pic->i_height,
1601              p_pic->i_display_width, p_pic->i_display_height,
1602              p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1603              (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1604              ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1605               ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1606                ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1607              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1608              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1609              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1610              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );    
1611     Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1612 #endif
1613 }
1614
1615 /******************************************************************************
1616  * RenderIdle: render idle picture
1617  ******************************************************************************
1618  * This function will print something on the screen. It will return 0 if 
1619  * nothing has been rendered, or 1 if something has been changed on the screen.
1620  * Note that if you absolutely want something to be printed, you will have
1621  * to force it by setting the last idle date to 0.
1622  * Unlike other rendering functions, this one calls the SetBufferPicture 
1623  * function when needed.
1624  ******************************************************************************/
1625 static int RenderIdle( vout_thread_t *p_vout )
1626 {
1627     int         i_x = 0, i_y = 0;                            /* text position */    
1628     int         i_width, i_height;                               /* text size */    
1629     mtime_t     current_date;                                 /* current date */
1630     const char *psz_text = "no stream";                    /* text to display */
1631     
1632     
1633     current_date = mdate();    
1634     if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1635         (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1636     {
1637         SetBufferPicture( p_vout, NULL );            
1638         vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1639                        &i_width, &i_height );
1640         if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1641         {  
1642             vout_Print( p_vout->p_large_font, 
1643                         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1644                         i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1645                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1646                         p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1647                         WIDE_TEXT | OUTLINED_TEXT, psz_text );        
1648             SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1649         }        
1650         return( 1 );        
1651     }
1652     return( 0 );    
1653 }
1654
1655 /******************************************************************************
1656  * RenderInfo: render additionnal informations
1657  ******************************************************************************
1658  * This function render informations which do not depend of the current picture
1659  * rendered.
1660  ******************************************************************************/
1661 static void RenderInfo( vout_thread_t *p_vout )
1662 {
1663 #ifdef DEBUG
1664     char        psz_buffer[256];                             /* string buffer */
1665     int         i_ready_pic = 0;                            /* ready pictures */
1666     int         i_reserved_pic = 0;                      /* reserved pictures */
1667     int         i_picture;                                   /* picture index */
1668 #endif
1669
1670 #ifdef DEBUG
1671     /* 
1672      * Print thread state in lower left corner  
1673      */
1674     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1675     {
1676         switch( p_vout->p_picture[i_picture].i_status )
1677         {
1678         case RESERVED_PICTURE:
1679         case RESERVED_DATED_PICTURE:
1680         case RESERVED_DISP_PICTURE:
1681             i_reserved_pic++;            
1682             break;            
1683         case READY_PICTURE:
1684             i_ready_pic++;            
1685             break;            
1686         }        
1687     }
1688     sprintf( psz_buffer, "pic: %d/%d/%d", 
1689              i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1690     Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );    
1691 #endif
1692 }
1693
1694 /*******************************************************************************
1695  * RenderSubPicture: render a subpicture
1696  *******************************************************************************
1697  * This function render a sub picture unit.
1698  *******************************************************************************/
1699 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1700 {
1701     p_vout_font_t       p_font;                                   /* text font */    
1702     int                 i_width, i_height;            /* subpicture dimensions */    
1703     
1704     switch( p_subpic->i_type )
1705     {
1706     case TEXT_SUBPICTURE:                                  /* single line text */
1707         /* Select default font if not specified */
1708         p_font = p_subpic->type.text.p_font;
1709         if( p_font == NULL )
1710         {
1711             p_font = p_vout->p_default_font;            
1712         }
1713
1714         /* Computes text size (width and height fields are ignored) and print it */
1715         vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );        
1716         if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1717                     p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1718         {
1719             vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1720                         p_subpic->i_x * p_vout->i_bytes_per_pixel +
1721                         p_subpic->i_y * p_vout->i_bytes_per_line, 
1722                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1723                         p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1724                         p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1725                         p_subpic->p_data );            
1726             SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );            
1727         }        
1728         break;        
1729         
1730 #ifdef DEBUG
1731     default:
1732         intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );        
1733 #endif
1734     }
1735 }
1736
1737 /*******************************************************************************
1738  * RenderInterface: render the interface
1739  *******************************************************************************
1740  * This function render the interface, if any.
1741  *******************************************************************************/
1742 static void RenderInterface( vout_thread_t *p_vout )
1743 {
1744     int         i_height, i_text_height;              /* total and text height */
1745     int         i_width_1, i_width_2;                            /* text width */
1746     int         i_byte;                                          /* byte index */
1747     const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
1748     const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";    
1749
1750     /* Get text size */
1751     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1752     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1753     i_height += i_text_height;
1754
1755     /* Render background */
1756     for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1757          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1758          i_byte++ )
1759     {
1760         //?? noooo !
1761         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1762     }    
1763
1764     /* Render text, if not larger than screen */
1765     if( i_width_1 < p_vout->i_width )
1766     {        
1767         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1768                     (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1769                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1770                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1771                     OUTLINED_TEXT, psz_text_1 );
1772     }
1773     if( i_width_2 < p_vout->i_width )
1774     {        
1775         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1776                     (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1777                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1778                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1779                     OUTLINED_TEXT, psz_text_2 );
1780     }    
1781
1782     /* Activate modified area */
1783     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1784 }
1785
1786 /******************************************************************************
1787  * Manage: manage thread
1788  ******************************************************************************
1789  * This function will handle changes in thread configuration.
1790  ******************************************************************************/
1791 static int Manage( vout_thread_t *p_vout )
1792 {
1793 #ifdef DEBUG_VIDEO
1794     if( p_vout->i_changes )
1795     {
1796         intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes, 
1797                     p_vout->i_changes & VOUT_NODISPLAY_CHANGE );        
1798     }    
1799 #endif
1800
1801     /* On gamma or grayscale change, rebuild tables */
1802     if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE | 
1803                              VOUT_YUV_CHANGE) )
1804     {
1805         if( vout_ResetYUV( p_vout ) )
1806         {
1807             intf_ErrMsg("error: can't rebuild convertion tables\n");            
1808             return( 1 );            
1809         }        
1810     }
1811
1812     /* Clear changes flags which does not need management or have been
1813      * handled */
1814     p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE | 
1815                            VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE | 
1816                            VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
1817
1818     /* Detect unauthorized changes */
1819     if( p_vout->i_changes )
1820     {
1821         /* Some changes were not acknowledged by vout_SysManage or this function,
1822          * it means they should not be authorized */
1823         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );        
1824         return( 1 );        
1825     }
1826     
1827     return( 0 );    
1828 }
1829
1830 /******************************************************************************
1831  * Align: align a subpicture in the screen
1832  ******************************************************************************
1833  * This function is used for rendering text or subpictures. It returns non 0
1834  * it the final aera is not fully included in display area. Return coordinates
1835  * are absolute.
1836  ******************************************************************************/
1837 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y, 
1838                    int i_width, int i_height, int i_h_align, int i_v_align )
1839 {
1840     /* Align horizontally */
1841     switch( i_h_align )
1842     {
1843     case CENTER_ALIGN:
1844         *pi_x -= i_width / 2;        
1845         break;        
1846     case CENTER_RALIGN:
1847         *pi_x += (p_vout->i_width - i_width) / 2;        
1848         break;        
1849     case RIGHT_ALIGN:   
1850         *pi_x -= i_width;        
1851         break;        
1852     case RIGHT_RALIGN:
1853         *pi_x += p_vout->i_width - i_width;        
1854         break;        
1855     }
1856
1857     /* Align vertically */
1858     switch( i_v_align )
1859     {
1860     case CENTER_ALIGN:
1861         *pi_y -= i_height / 2;        
1862         break;
1863     case CENTER_RALIGN:
1864         *pi_y += (p_vout->i_height - i_height) / 2;        
1865         break;        
1866     case BOTTOM_ALIGN:
1867         *pi_y -= i_height;        
1868         break;        
1869     case BOTTOM_RALIGN:
1870         *pi_y += p_vout->i_height - i_height;        
1871         break;        
1872     case SUBTITLE_RALIGN:
1873         *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y + 
1874                   p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;        
1875         break;        
1876     }
1877
1878     /* Return non 0 if clipping failed */
1879     return( (*pi_x < 0) || (*pi_y < 0) || 
1880             (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );    
1881 }
1882