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