]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Temporaire (�a segfaulte si on le chatouille un peu).
[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
428     vlc_mutex_unlock( &p_vout->picture_lock );
429 }
430
431 /******************************************************************************
432  * vout_DatePicture: date a picture
433  ******************************************************************************
434  * Remove the reservation flag of a picture, which will cause it to be ready for
435  * display. The picture won't be displayed until vout_DisplayPicture has been 
436  * called.
437  ******************************************************************************/
438 void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
439 {
440     vlc_mutex_lock( &p_vout->picture_lock );
441     p_pic->date = date;    
442     switch( p_pic->i_status )
443     {
444     case RESERVED_PICTURE:        
445         p_pic->i_status = RESERVED_DATED_PICTURE;
446         break;        
447     case RESERVED_DISP_PICTURE:
448         p_pic->i_status = READY_PICTURE;
449         break;        
450 #ifdef DEBUG
451     default:        
452         intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );    
453         break;        
454 #endif
455     }
456
457 #ifdef DEBUG_VIDEO
458     intf_DbgMsg("picture %p\n", p_pic);
459 #endif
460
461     vlc_mutex_unlock( &p_vout->picture_lock );
462 }
463
464 /******************************************************************************
465  * vout_CreatePicture: allocate a picture in the video output heap.
466  ******************************************************************************
467  * This function create a reserved image in the video output heap. 
468  * A null pointer is returned if the function fails. This method provides an
469  * already allocated zone of memory in the picture data fields. It needs locking
470  * since several pictures can be created by several producers threads. 
471  ******************************************************************************/
472 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type, 
473                                int i_width, int i_height )
474 {
475     int         i_picture;                                   /* picture index */
476     int         i_chroma_width = 0;                           /* chroma width */    
477     picture_t * p_free_picture = NULL;                  /* first free picture */    
478     picture_t * p_destroyed_picture = NULL;        /* first destroyed picture */    
479
480     /* Get lock */
481     vlc_mutex_lock( &p_vout->picture_lock );
482
483     /* 
484      * Look for an empty place 
485      */
486     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
487     {
488         if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
489         {
490             /* Picture is marked for destruction, but is still allocated - note
491              * that if width and type are the same for two pictures, chroma_width 
492              * should also be the same */
493             if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
494                 (p_vout->p_picture[i_picture].i_height         == i_height) &&
495                 (p_vout->p_picture[i_picture].i_width          == i_width) )
496             {
497                 /* Memory size do match : memory will not be reallocated, and function
498                  * can end immediately - this is the best possible case, since no
499                  * memory allocation needs to be done */
500                 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
501 #ifdef DEBUG_VIDEO
502                 intf_DbgMsg("picture %p (in destroyed picture slot)\n", 
503                             &p_vout->p_picture[i_picture] );                
504 #endif
505                 vlc_mutex_unlock( &p_vout->picture_lock );
506                 return( &p_vout->p_picture[i_picture] );
507             }
508             else if( p_destroyed_picture == NULL )
509             {
510                 /* Memory size do not match, but picture index will be kept in
511                  * case no other place are left */
512                 p_destroyed_picture = &p_vout->p_picture[i_picture];                
513             }       
514         }
515         else if( (p_free_picture == NULL) && 
516                  (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
517         {
518             /* Picture is empty and ready for allocation */
519             p_free_picture = &p_vout->p_picture[i_picture];            
520         }
521     }
522
523     /* If no free picture is available, use a destroyed picture */
524     if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
525     { 
526         /* No free picture or matching destroyed picture has been found, but
527          * a destroyed picture is still avalaible */
528         free( p_destroyed_picture->p_data );        
529         p_free_picture = p_destroyed_picture;        
530     }
531
532     /*
533      * Prepare picture
534      */
535     if( p_free_picture != NULL )
536     {
537         /* Allocate memory */
538         switch( i_type )
539         {
540         case YUV_420_PICTURE:          /* YUV 420: 1,1/4,1/4 samples per pixel */
541             i_chroma_width = i_width / 2;            
542             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
543             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
544             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
545             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
546             break;
547         case YUV_422_PICTURE:          /* YUV 422: 1,1/2,1/2 samples per pixel */
548             i_chroma_width = i_width / 2;            
549             p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
550             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
551             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
552             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
553             break;
554         case YUV_444_PICTURE:              /* YUV 444: 1,1,1 samples per pixel */
555             i_chroma_width = i_width;            
556             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
557             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
558             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
559             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
560             break;                
561 #ifdef DEBUG
562         default:
563             intf_DbgMsg("error: unknown picture type %d\n", i_type );
564             p_free_picture->p_data   =  NULL;            
565             break;            
566 #endif    
567         }
568
569         if( p_free_picture->p_data != NULL )
570         {        
571             /* Copy picture informations, set some default values */
572             p_free_picture->i_type                      = i_type;
573             p_free_picture->i_status                    = RESERVED_PICTURE;
574             p_free_picture->i_matrix_coefficients       = 1; 
575             p_free_picture->i_width                     = i_width;
576             p_free_picture->i_height                    = i_height;
577             p_free_picture->i_chroma_width              = i_chroma_width;            
578             p_free_picture->i_display_horizontal_offset = 0;
579             p_free_picture->i_display_vertical_offset   = 0;            
580             p_free_picture->i_display_width             = i_width;
581             p_free_picture->i_display_height            = i_height;
582             p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;            
583             p_free_picture->i_refcount                  = 0;            
584         }
585         else
586         {
587             /* Memory allocation failed : set picture as empty */
588             p_free_picture->i_type   =  EMPTY_PICTURE;            
589             p_free_picture->i_status =  FREE_PICTURE;            
590             p_free_picture =            NULL;            
591             intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );            
592         }
593         
594 #ifdef DEBUG_VIDEO
595         intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );        
596 #endif
597         vlc_mutex_unlock( &p_vout->picture_lock );
598         return( p_free_picture );
599     }
600     
601     /* No free or destroyed picture could be found */
602     intf_DbgMsg( "warning: heap is full\n" );
603     vlc_mutex_unlock( &p_vout->picture_lock );
604     return( NULL );
605 }
606
607 /******************************************************************************
608  * vout_DestroyPicture: remove a permanent or reserved picture from the heap
609  ******************************************************************************
610  * This function frees a previously reserved picture or a permanent
611  * picture. It is meant to be used when the construction of a picture aborted.
612  * Note that the picture will be destroyed even if it is linked !
613  * This function does not need locking since reserved pictures are ignored by
614  * the output thread.
615  ******************************************************************************/
616 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
617 {
618 #ifdef DEBUG
619    /* Check if picture status is valid */
620    if( (p_pic->i_status != RESERVED_PICTURE) && 
621        (p_pic->i_status != RESERVED_DATED_PICTURE) &&
622        (p_pic->i_status != RESERVED_DISP_PICTURE) )
623    {
624        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );       
625    }   
626 #endif
627
628     p_pic->i_status = DESTROYED_PICTURE;
629
630 #ifdef DEBUG_VIDEO
631     intf_DbgMsg("picture %p\n", p_pic);    
632 #endif
633 }
634
635 /******************************************************************************
636  * vout_LinkPicture: increment reference counter of a picture
637  ******************************************************************************
638  * This function increment the reference counter of a picture in the video
639  * heap. It needs a lock since several producer threads can access the picture.
640  ******************************************************************************/
641 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
642 {
643     vlc_mutex_lock( &p_vout->picture_lock );
644     p_pic->i_refcount++;
645
646 #ifdef DEBUG_VIDEO
647     intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
648 #endif
649
650     vlc_mutex_unlock( &p_vout->picture_lock );
651 }
652
653 /******************************************************************************
654  * vout_UnlinkPicture: decrement reference counter of a picture
655  ******************************************************************************
656  * This function decrement the reference counter of a picture in the video heap.
657  ******************************************************************************/
658 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
659 {
660     vlc_mutex_lock( &p_vout->picture_lock );
661     p_pic->i_refcount--;
662
663 #ifdef DEBUG_VIDEO
664     if( p_pic->i_refcount < 0 )
665     {
666         intf_DbgMsg("error: refcount < 0\n");
667         p_pic->i_refcount = 0;        
668     }    
669 #endif
670
671     if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
672     {
673         p_pic->i_status = DESTROYED_PICTURE;
674     }
675
676 #ifdef DEBUG_VIDEO
677     intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );    
678 #endif
679
680     vlc_mutex_unlock( &p_vout->picture_lock );
681 }
682
683 /******************************************************************************
684  * vout_SetBuffers: set buffers adresses
685  ******************************************************************************
686  * This function is called by system drivers to set buffers video memory 
687  * adresses.
688  ******************************************************************************/
689 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
690 {
691     /* No picture previously */
692     p_vout->p_buffer[0].i_pic_x =         0;
693     p_vout->p_buffer[0].i_pic_y =         0;
694     p_vout->p_buffer[0].i_pic_width =     0;
695     p_vout->p_buffer[0].i_pic_height =    0;
696     p_vout->p_buffer[1].i_pic_x =         0;
697     p_vout->p_buffer[1].i_pic_y =         0;
698     p_vout->p_buffer[1].i_pic_width =     0;
699     p_vout->p_buffer[1].i_pic_height =    0;
700
701     /* The first area covers all the screen */
702     p_vout->p_buffer[0].i_areas =                 1;
703     p_vout->p_buffer[0].pi_area_begin[0] =        0;
704     p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
705     p_vout->p_buffer[1].i_areas =                 1;
706     p_vout->p_buffer[1].pi_area_begin[0] =        0;
707     p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;
708
709     /* Set adresses */
710     p_vout->p_buffer[0].p_data = p_buf1;    
711     p_vout->p_buffer[1].p_data = p_buf2;    
712 }
713
714 /* following functions are local */
715
716 /******************************************************************************
717  * InitThread: initialize video output thread
718  ******************************************************************************
719  * This function is called from RunThread and performs the second step of the
720  * initialization. It returns 0 on success. Note that the thread's flag are not
721  * modified inside this function.
722  ******************************************************************************/
723 static int InitThread( vout_thread_t *p_vout )
724 {
725     /* Update status */
726     intf_DbgMsg("\n");
727     *p_vout->pi_status = THREAD_START;    
728
729     /* Initialize output method - this function issues its own error messages */
730     if( vout_SysInit( p_vout ) )
731     {
732         return( 1 );
733     } 
734
735     /* Initialize convertion tables and functions */
736     if( vout_InitYUV( p_vout ) )
737     {
738         intf_ErrMsg("error: can't allocate YUV translation tables\n");
739         return( 1 );                
740     }
741     
742     /* Mark thread as running and return */
743     p_vout->b_active =          1;    
744     *p_vout->pi_status =        THREAD_READY;    
745     intf_DbgMsg("thread ready\n");    
746     return( 0 );    
747 }
748
749 /******************************************************************************
750  * RunThread: video output thread
751  ******************************************************************************
752  * Video output thread. This function does only returns when the thread is
753  * terminated. It handles the pictures arriving in the video heap and the
754  * display device events.
755  ******************************************************************************/
756 static void RunThread( vout_thread_t *p_vout)
757 {
758     int             i_index;                                 /* index in heap */
759     mtime_t         current_date;                             /* current date */
760     mtime_t         display_date;                             /* display date */    
761     boolean_t       b_display;                                /* display flag */    
762     picture_t *     p_pic;                                 /* picture pointer */
763     subpicture_t *  p_subpic;                           /* subpicture pointer */    
764      
765     /* 
766      * Initialize thread
767      */
768     p_vout->b_error = InitThread( p_vout );
769     if( p_vout->b_error )
770     {
771         DestroyThread( p_vout, THREAD_ERROR );
772         return;        
773     }    
774     intf_DbgMsg("\n");
775
776     /*
777      * Main loop - it is not executed if an error occured during
778      * initialization
779      */
780     while( (!p_vout->b_die) && (!p_vout->b_error) )
781     {
782         /* Initialize loop variables */
783         p_pic =         NULL;
784         p_subpic =      NULL;
785         display_date =  0;        
786         current_date =  mdate();
787
788         /* 
789          * Find the picture to display - this operation does not need lock,
790          * since only READY_PICTUREs are handled 
791          */
792         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
793         {
794             if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
795                 ( (p_pic == NULL) || 
796                   (p_vout->p_picture[i_index].date < display_date) ) )
797             {
798                 p_pic = &p_vout->p_picture[i_index];
799                 display_date = p_pic->date;                
800             }
801         }
802  
803         if( p_pic )
804         {
805 #ifdef STATS
806             /* Computes FPS rate */
807             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
808 #endif      
809             if( display_date < current_date )
810             {
811                 /* Picture is late: it will be destroyed and the thread will sleep and
812                  * go to next picture */
813                 vlc_mutex_lock( &p_vout->picture_lock );
814                 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
815                 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
816                 vlc_mutex_unlock( &p_vout->picture_lock );
817                 p_pic =         NULL;                
818                 display_date =  0;                
819             }
820             else if( display_date > current_date + VOUT_DISPLAY_DELAY )
821             {
822                 /* A picture is ready to be rendered, but its rendering date is
823                  * far from the current one so the thread will perform an empty loop
824                  * as if no picture were found. The picture state is unchanged */
825                 p_pic =         NULL;                
826                 display_date =  0;                
827             }
828         }
829
830         /*
831          * Find the subpicture to display - this operation does not need lock, since
832          * only READY_SUBPICTURES are handled. If no picture has been selected,
833          * display_date will depend on the subpicture
834          */
835         //??
836
837         /*
838          * Perform rendering, sleep and display rendered picture
839          */
840         if( p_pic )                          /* picture and perhaps subpicture */
841         {
842             b_display = p_vout->b_active;            
843             p_vout->last_display_date = display_date;
844             
845             if( b_display )
846             {                
847                 /* Set picture dimensions and clear buffer */
848                 SetBufferPicture( p_vout, p_pic );
849
850                 /* Render picture and informations */
851                 RenderPicture( p_vout, p_pic );             
852                 if( p_vout->b_info )
853                 {
854                     RenderPictureInfo( p_vout, p_pic );
855                     RenderInfo( p_vout );                
856                 }
857             }
858             
859             /* Remove picture from heap */
860             vlc_mutex_lock( &p_vout->picture_lock );
861             p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
862             vlc_mutex_unlock( &p_vout->picture_lock );                          
863
864             /* Render interface and subpicture */
865             if( b_display && p_vout->b_interface )
866             {
867                 RenderInterface( p_vout );                
868             }
869             if( p_subpic )
870             {
871                 if( b_display )
872                 {                    
873                     RenderSubPicture( p_vout, p_subpic );
874                 }                
875
876                 /* Remove subpicture from heap */
877                 vlc_mutex_lock( &p_vout->subpicture_lock );
878                 p_subpic->i_status = DESTROYED_SUBPICTURE;
879                 vlc_mutex_unlock( &p_vout->subpicture_lock );                          
880             }
881
882         }
883         else if( p_subpic )                                /* subpicture alone */
884         {
885             b_display = p_vout->b_active;
886             p_vout->last_display_date = display_date;            
887
888             if( b_display )
889             {                
890                 /* Clear buffer */
891                 SetBufferPicture( p_vout, NULL );
892
893                 /* Render informations, interface and subpicture */
894                 if( p_vout->b_info )
895                 {
896                     RenderInfo( p_vout );
897                 }
898                 if( p_vout->b_interface )
899                 {
900                     RenderInterface( p_vout );
901                 }
902                 RenderSubPicture( p_vout, p_subpic );            
903             }            
904
905             /* Remove subpicture from heap */
906             vlc_mutex_lock( &p_vout->subpicture_lock );
907             p_subpic->i_status = DESTROYED_SUBPICTURE;
908             vlc_mutex_unlock( &p_vout->subpicture_lock );                          
909         }
910         else if( p_vout->b_active )          /* idle or interface screen alone */
911         {
912             if( p_vout->b_interface && 0 /* && ?? intf_change */ )
913             {
914                 /* Interface has changed, so a new rendering is required - force
915                  * it by setting last idle date to 0 */
916                 p_vout->last_idle_date = 0;                
917             }
918
919             /* Render idle screen and update idle date, then render interface if
920              * required */
921             b_display = RenderIdle( p_vout );
922             if( b_display )
923             {                
924                 p_vout->last_idle_date = current_date;
925                 if( p_vout->b_interface )
926                 {
927                     RenderInterface( p_vout );                
928                 }
929             }
930             
931         }       
932         else
933         {
934             b_display = 0;            
935         }        
936
937         /*
938          * Sleep, wake up and display rendered picture
939          */
940
941 #ifdef STATS
942         /* Store render time */
943         p_vout->render_time = mdate() - current_date;
944 #endif
945
946         /* Give back change lock */
947         vlc_mutex_unlock( &p_vout->change_lock );        
948
949         /* Sleep a while or until a given date */
950         if( display_date != 0 )
951         {
952             mwait( display_date );
953         }
954         else
955         {
956             msleep( VOUT_IDLE_SLEEP );                
957         }            
958
959         /* On awakening, take back lock and send immediately picture to display, 
960          * then swap buffers */
961         vlc_mutex_lock( &p_vout->change_lock );        
962 #ifdef DEBUG_VIDEO
963         intf_DbgMsg( "picture %p, subpicture %p\n", p_pic, p_subpic );        
964 #endif            
965         if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
966         {
967             vout_SysDisplay( p_vout );
968             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
969         }
970
971         /*
972          * Check events and manage thread
973          */
974         if( vout_SysManage( p_vout ) | Manage( p_vout ) )
975         {
976             /* A fatal error occured, and the thread must terminate immediately,
977              * without displaying anything - setting b_error to 1 cause the
978              * immediate end of the main while() loop. */
979             p_vout->b_error = 1;
980         }  
981     } 
982
983     /*
984      * Error loop - wait until the thread destruction is requested
985      */
986     if( p_vout->b_error )
987     {
988         ErrorThread( p_vout );        
989     }
990
991     /* End of thread */
992     EndThread( p_vout );
993     DestroyThread( p_vout, THREAD_OVER ); 
994     intf_DbgMsg( "thread end\n" );
995 }
996
997 /******************************************************************************
998  * ErrorThread: RunThread() error loop
999  ******************************************************************************
1000  * This function is called when an error occured during thread main's loop. The
1001  * thread can still receive feed, but must be ready to terminate as soon as
1002  * possible.
1003  ******************************************************************************/
1004 static void ErrorThread( vout_thread_t *p_vout )
1005 {
1006     /* Wait until a `die' order */
1007     intf_DbgMsg("\n");
1008     while( !p_vout->b_die )
1009     {
1010         /* Sleep a while */
1011         msleep( VOUT_IDLE_SLEEP );                
1012     }
1013 }
1014
1015 /*******************************************************************************
1016  * EndThread: thread destruction
1017  *******************************************************************************
1018  * This function is called when the thread ends after a sucessfull 
1019  * initialization. It frees all ressources allocated by InitThread.
1020  *******************************************************************************/
1021 static void EndThread( vout_thread_t *p_vout )
1022 {
1023     int     i_index;                                          /* index in heap */
1024             
1025     /* Store status */
1026     intf_DbgMsg("\n");
1027     *p_vout->pi_status = THREAD_END;    
1028
1029     /* Destroy all remaining pictures and subpictures */
1030     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1031     {
1032         if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1033         {
1034             free( p_vout->p_picture[i_index].p_data );
1035         }
1036         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1037         {
1038             free( p_vout->p_subpicture[i_index].p_data );            
1039         }        
1040     }
1041
1042     /* Destroy translation tables */
1043     vout_EndYUV( p_vout );  
1044     vout_SysEnd( p_vout );    
1045 }
1046
1047 /*******************************************************************************
1048  * DestroyThread: thread destruction
1049  *******************************************************************************
1050  * This function is called when the thread ends. It frees all ressources
1051  * allocated by CreateThread. Status is available at this stage.
1052  *******************************************************************************/
1053 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1054 {  
1055     int *pi_status;                                           /* status adress */
1056     
1057     /* Store status adress */
1058     intf_DbgMsg("\n");
1059     pi_status = p_vout->pi_status;    
1060     
1061     /* Destroy thread structures allocated by Create and InitThread */
1062     vout_UnloadFont( p_vout->p_default_font );
1063     vout_UnloadFont( p_vout->p_large_font ); 
1064     vout_SysDestroy( p_vout );
1065     free( p_vout );
1066     *pi_status = i_status;    
1067 }
1068
1069 /*******************************************************************************
1070  * Print: print simple text on a picture
1071  *******************************************************************************
1072  * This function will print a simple text on the picture. It is designed to
1073  * print debugging or general informations.
1074  *******************************************************************************/
1075 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 )
1076 {
1077     int                 i_text_height;                    /* total text height */
1078     int                 i_text_width;                      /* total text width */
1079
1080     /* Update upper left coordinates according to alignment */
1081     vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1082     if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1083     {
1084         /* Set area and print text */
1085         SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );    
1086         vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data + 
1087                     i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1088                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line, 
1089                     0xffffffff, 0x00000000, 0x00000000, 0, psz_text );
1090     }    
1091 }
1092
1093 /*******************************************************************************
1094  * SetBufferArea: activate an area in current buffer
1095  *******************************************************************************
1096  * This function is called when something is rendered on the current buffer.
1097  * It set the area as active and prepare it to be cleared on next rendering.
1098  * Pay attention to the fact that in this functions, i_h is in fact the end y
1099  * coordinate of the new area.
1100  *******************************************************************************/
1101 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1102 {
1103     vout_buffer_t *     p_buffer;                            /* current buffer */
1104     int                 i_area_begin, i_area_end;   /* area vertical extension */
1105     int                 i_area, i_area_copy;                     /* area index */    
1106     int                 i_area_shift;              /* shift distance for areas */    
1107     
1108     /* Choose buffer and modify h to end of area position */
1109     p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1110     i_h +=      i_y - 1;
1111  
1112     /* 
1113      * Remove part of the area which is inside the picture - this is done
1114      * by calling again SetBufferArea with the correct areas dimensions.
1115      */
1116     if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1117     {
1118         i_area_begin =  p_buffer->i_pic_y;
1119         i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
1120
1121         if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1122             ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1123             ((i_y <  i_area_begin) && (i_h > i_area_end)) )
1124         {                    
1125             /* Keep the stripe above the picture, if any */
1126             if( i_y < i_area_begin )
1127             {
1128                 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );            
1129             }
1130             /* Keep the stripe below the picture, if any */
1131             if( i_h > i_area_end )
1132             {
1133                 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1134             }        
1135             return;
1136         }        
1137     }   
1138
1139     /* Skip some extensions until interesting areas */
1140     for( i_area = 0; 
1141          (i_area < p_buffer->i_areas) &&
1142              (p_buffer->pi_area_end[i_area] + 1 <= i_y); 
1143          i_area++ )
1144     {
1145         ;        
1146     }
1147     
1148     if( i_area == p_buffer->i_areas )
1149     {
1150         /* New area is below all existing ones: just add it at the end of the 
1151          * array, if possible - else, append it to the last one */
1152         if( i_area < VOUT_MAX_AREAS )
1153         {
1154             p_buffer->pi_area_begin[i_area] = i_y;
1155             p_buffer->pi_area_end[i_area] = i_h;            
1156             p_buffer->i_areas++;            
1157         }
1158         else
1159         {
1160 #ifdef DEBUG_VIDEO
1161             intf_DbgMsg("areas overflow\n");            
1162 #endif
1163             p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;  
1164         }        
1165     }
1166     else 
1167     {
1168         i_area_begin =  p_buffer->pi_area_begin[i_area];
1169         i_area_end =    p_buffer->pi_area_end[i_area];
1170         
1171         if( i_y < i_area_begin ) 
1172         {
1173             if( i_h >= i_area_begin - 1 )
1174             {                
1175                 /* Extend area above */
1176                 p_buffer->pi_area_begin[i_area] = i_y;
1177             }
1178             else
1179             {
1180                 /* Create a new area above : merge last area if overflow, then 
1181                  * move all old areas down */
1182                 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1183                 {                    
1184 #ifdef DEBUG_VIDEO
1185                     intf_DbgMsg("areas overflow\n");       
1186 #endif
1187                     p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];                    
1188                 }
1189                 else
1190                 {
1191                     p_buffer->i_areas++;                    
1192                 }
1193                 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1194                 {
1195                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1196                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
1197                 }
1198                 p_buffer->pi_area_begin[i_area] = i_y;
1199                 p_buffer->pi_area_end[i_area] = i_h;
1200                 return;
1201             }              
1202         }
1203         if( i_h > i_area_end )
1204         {
1205             /* Find further areas which can be merged with the new one */
1206             for( i_area_copy = i_area + 1; 
1207                  (i_area_copy < p_buffer->i_areas) &&
1208                      (p_buffer->pi_area_begin[i_area] <= i_h);
1209                  i_area_copy++ )
1210             {
1211                 ;                
1212             }
1213             i_area_copy--;            
1214
1215             if( i_area_copy != i_area )
1216             {
1217                 /* Merge with last possible areas */
1218                 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1219
1220                 /* Shift lower areas upward */
1221                 i_area_shift = i_area_copy - i_area;                
1222                 p_buffer->i_areas -= i_area_shift;
1223                 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1224                 {
1225                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1226                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
1227                 }
1228             }
1229             else
1230             {
1231                 /* Extend area below */
1232                 p_buffer->pi_area_end[i_area] = i_h;
1233             }            
1234         }
1235     }
1236 }
1237
1238 /*******************************************************************************
1239  * SetBufferPicture: clear buffer and set picture area
1240  *******************************************************************************
1241  * This function is called before any rendering. It clears the current 
1242  * rendering buffer and set the new picture area. If the picture pointer is
1243  * NULL, then no picture area is defined. Floating operations are avoided since
1244  * some MMX calculations may follow.
1245  *******************************************************************************/
1246 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1247 {
1248     vout_buffer_t *     p_buffer;                            /* current buffer */
1249     int                 i_pic_x, i_pic_y;                  /* picture position */
1250     int                 i_pic_width, i_pic_height;       /* picture dimensions */    
1251     int                 i_old_pic_y, i_old_pic_height;     /* old picture area */    
1252     int                 i_vout_width, i_vout_height;     /* display dimensions */
1253     int                 i_area;                                  /* area index */    
1254     int                 i_data_index;                       /* area data index */    
1255     int                 i_data_size;     /* area data size, in 256 bytes blocs */
1256     u64 *               p_data;                                   /* area data */    
1257     
1258     /* Choose buffer and set display dimensions */
1259     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1260     i_vout_width =      p_vout->i_width;
1261     i_vout_height =     p_vout->i_height;    
1262
1263     /*
1264      * Computes new picture size 
1265      */
1266     if( p_pic != NULL )
1267     {
1268         /* Try horizontal scaling first */
1269         i_pic_width = ( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ? 
1270             i_vout_width : p_pic->i_width;
1271         i_pic_width = i_pic_width;
1272         switch( p_pic->i_aspect_ratio )
1273         {
1274         case AR_3_4_PICTURE:
1275             i_pic_height = i_pic_width * 3 / 4;
1276             break;                
1277         case AR_16_9_PICTURE:
1278             i_pic_height = i_pic_width * 9 / 16;
1279             break;
1280         case AR_221_1_PICTURE:        
1281             i_pic_height = i_pic_width * 100 / 221;
1282             break;               
1283         case AR_SQUARE_PICTURE:
1284         default:
1285             i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;            
1286             break;
1287         }
1288
1289         /* If picture dimensions using horizontal scaling are too large, use 
1290          * vertical scaling */
1291         if( i_pic_height > i_vout_height )
1292         {
1293             i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ? 
1294                 i_vout_height : p_pic->i_height;
1295             switch( p_pic->i_aspect_ratio )
1296             {
1297             case AR_3_4_PICTURE:
1298                 i_pic_width = i_pic_height * 4 / 3;
1299                 break;                
1300             case AR_16_9_PICTURE:
1301                 i_pic_width = i_pic_height * 16 / 9;
1302                 break;
1303             case AR_221_1_PICTURE:        
1304                 i_pic_width = i_pic_height * 221 / 100;
1305                 break;               
1306             case AR_SQUARE_PICTURE:
1307             default:
1308                 i_pic_width = p_pic->i_width * i_pic_height / p_pic->i_height;
1309                 break;
1310             }        
1311             i_pic_width = i_pic_width;
1312         }        
1313
1314         /* Set picture position */
1315         i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1316         i_pic_y = (p_vout->i_height - i_pic_height) / 2;                
1317     }    
1318     else
1319     {
1320         /* No picture: size is 0 */
1321         i_pic_x =       0;
1322         i_pic_y =       0;
1323         i_pic_width =   0;
1324         i_pic_height =  0;
1325     }
1326
1327     /*
1328      * Set new picture size - if is is smaller than the previous one, clear 
1329      * around it. Since picture are centered, only their size is tested.
1330      */                                          
1331     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1332     {
1333         i_old_pic_y =            p_buffer->i_pic_y;
1334         i_old_pic_height =       p_buffer->i_pic_height;
1335         p_buffer->i_pic_x =      i_pic_x;
1336         p_buffer->i_pic_y =      i_pic_y;
1337         p_buffer->i_pic_width =  i_pic_width;
1338         p_buffer->i_pic_height = i_pic_height;                        
1339         SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1340     }
1341     else
1342     {
1343         p_buffer->i_pic_x =      i_pic_x;
1344         p_buffer->i_pic_y =      i_pic_y;
1345         p_buffer->i_pic_width =  i_pic_width;
1346         p_buffer->i_pic_height = i_pic_height;    
1347     }
1348
1349     /*
1350      * Clear areas
1351      */
1352     for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1353     {
1354 #ifdef DEBUG_VIDEO    
1355         intf_DbgMsg("clearing picture %p area: %d-%d\n", p_pic, 
1356                     p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area]);    
1357 #endif
1358         p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1359         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * 
1360             p_vout->i_bytes_per_line / 256;
1361         for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1362         {
1363             /* Clear 256 bytes block */
1364             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1365             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1366             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1367             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1368             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
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         }
1373         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) *
1374             p_vout->i_bytes_per_line % 256 / 4;
1375         for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1376         {
1377             /* Clear remaining 4 bytes blocks */
1378             *p_data++ = 0;
1379         }
1380     }    
1381
1382     /*
1383      * Clear areas array
1384      */
1385     p_buffer->i_areas = 0;
1386 }
1387
1388 /******************************************************************************
1389  * RenderPicture: render a picture
1390  ******************************************************************************
1391  * This function convert a picture from a video heap to a pixel-encoded image
1392  * and copy it to the current rendering buffer. No lock is required, since the
1393  * rendered picture has been determined as existant, and will only be destroyed
1394  * by the vout thread later.
1395  ******************************************************************************/
1396 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1397 {
1398     vout_buffer_t *     p_buffer;                         /* rendering buffer */    
1399     byte_t *            p_pic_data;                 /* convertion destination */
1400     
1401     /* Get and set rendering informations */
1402     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1403     p_pic_data =        p_buffer->p_data + 
1404         p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1405         p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1406
1407     /*
1408      * Choose appropriate rendering function and render picture 
1409      */
1410     switch( p_pic->i_type )
1411     {
1412     case YUV_420_PICTURE:
1413         p_vout->yuv.p_Convert420( p_vout, p_pic_data, 
1414                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1415                                   p_pic->i_width, p_pic->i_height,
1416                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1417                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1418                                   p_pic->i_matrix_coefficients );
1419         break;        
1420     case YUV_422_PICTURE:
1421         p_vout->yuv.p_Convert422( p_vout, p_pic_data, 
1422                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1423                                   p_pic->i_width, p_pic->i_height,
1424                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1425                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1426                                   p_pic->i_matrix_coefficients );
1427         break;        
1428     case YUV_444_PICTURE:
1429         p_vout->yuv.p_Convert444( p_vout, p_pic_data, 
1430                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1431                                   p_pic->i_width, p_pic->i_height,
1432                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1433                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1434                                   p_pic->i_matrix_coefficients );
1435         break;        
1436 #ifdef DEBUG
1437     default:        
1438         intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1439         break;        
1440 #endif
1441     }
1442 }
1443
1444 /******************************************************************************
1445  * RenderPictureInfo: print additionnal informations on a picture
1446  ******************************************************************************
1447  * This function will print informations such as fps and other picture
1448  * dependant informations.
1449  ******************************************************************************/
1450 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1451 {
1452 #if defined(STATS) || defined(DEBUG)
1453     char        psz_buffer[256];                             /* string buffer */
1454 #endif
1455
1456 #ifdef STATS
1457     /* 
1458      * Print FPS rate in upper right corner 
1459      */
1460     if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1461     {        
1462         sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1463                  ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1464                    p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );        
1465         Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1466     }
1467
1468     /* 
1469      * Print frames count and loop time in upper left corner 
1470      */
1471     sprintf( psz_buffer, "%ld frames   rendering: %ld us", 
1472              (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1473     Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1474 #endif
1475
1476 #ifdef DEBUG
1477     /*
1478      * Print picture information in lower right corner
1479      */
1480     sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1481              (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1482              ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1483               ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1484              p_pic->i_width, p_pic->i_height,
1485              p_pic->i_display_width, p_pic->i_display_height,
1486              p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1487              (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1488              ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1489               ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1490                ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1491              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1492              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1493              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1494              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );    
1495     Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1496 #endif
1497 }
1498
1499 /******************************************************************************
1500  * RenderIdle: render idle picture
1501  ******************************************************************************
1502  * This function will print something on the screen. It will return 0 if 
1503  * nothing has been rendered, or 1 if something has been changed on the screen.
1504  * Note that if you absolutely want something to be printed, you will have
1505  * to force it by setting the last idle date to 0.
1506  * Unlike other rendering functions, this one calls the SetBufferPicture 
1507  * function when needed.
1508  ******************************************************************************/
1509 static int RenderIdle( vout_thread_t *p_vout )
1510 {
1511     int         i_x = 0, i_y = 0;                            /* text position */    
1512     int         i_width, i_height;                               /* text size */    
1513     mtime_t     current_date;                                 /* current date */
1514     const char *psz_text = "no stream";                    /* text to display */
1515     
1516     
1517     current_date = mdate();    
1518     if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1519         (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1520     {
1521         SetBufferPicture( p_vout, NULL );            
1522         vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1523                        &i_width, &i_height );
1524         if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1525         {  
1526             vout_Print( p_vout->p_large_font, 
1527                         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1528                         i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1529                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1530                         0xffffffff, 0x33333333, 0,
1531                         WIDE_TEXT | OUTLINED_TEXT, psz_text );        
1532             SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1533         }        
1534         return( 1 );        
1535     }
1536     return( 0 );    
1537 }
1538
1539 /******************************************************************************
1540  * RenderInfo: render additionnal informations
1541  ******************************************************************************
1542  * This function render informations which do not depend of the current picture
1543  * rendered.
1544  ******************************************************************************/
1545 static void RenderInfo( vout_thread_t *p_vout )
1546 {
1547 #ifdef DEBUG
1548     char        psz_buffer[256];                             /* string buffer */
1549     int         i_ready_pic = 0;                            /* ready pictures */
1550     int         i_reserved_pic = 0;                      /* reserved pictures */
1551     int         i_picture;                                   /* picture index */
1552 #endif
1553
1554 #ifdef DEBUG
1555     /* 
1556      * Print thread state in lower left corner  
1557      */
1558     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1559     {
1560         switch( p_vout->p_picture[i_picture].i_status )
1561         {
1562         case RESERVED_PICTURE:
1563         case RESERVED_DATED_PICTURE:
1564         case RESERVED_DISP_PICTURE:
1565             i_reserved_pic++;            
1566             break;            
1567         case READY_PICTURE:
1568             i_ready_pic++;            
1569             break;            
1570         }        
1571     }
1572     sprintf( psz_buffer, "pic: %d/%d/%d", 
1573              i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1574     Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );    
1575 #endif
1576 }
1577
1578 /*******************************************************************************
1579  * RenderSubPicture: render a subpicture
1580  *******************************************************************************
1581  * This function render a sub picture unit.
1582  *******************************************************************************/
1583 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1584 {
1585     p_vout_font_t       p_font;                                   /* text font */    
1586     int                 i_width, i_height;            /* subpicture dimensions */    
1587     
1588     switch( p_subpic->i_type )
1589     {
1590     case TEXT_SUBPICTURE:                                  /* single line text */
1591         /* Select default font if not specified */
1592         p_font = p_subpic->type.text.p_font;
1593         if( p_font == NULL )
1594         {
1595             p_font = p_vout->p_default_font;            
1596         }
1597
1598         /* Computes text size (width and height fields are ignored) and print it */
1599         vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );        
1600         if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1601                     p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1602         {
1603             vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1604                         p_subpic->i_x * p_vout->i_bytes_per_pixel +
1605                         p_subpic->i_y * p_vout->i_bytes_per_line, 
1606                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1607                         p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1608                         p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1609                         p_subpic->p_data );            
1610             SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );            
1611         }        
1612         break;        
1613         
1614 #ifdef DEBUG
1615     default:
1616         intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );        
1617 #endif
1618     }
1619 }
1620
1621 /*******************************************************************************
1622  * RenderInterface: render the interface
1623  *******************************************************************************
1624  * This function render the interface, if any.
1625  *******************************************************************************/
1626 static void RenderInterface( vout_thread_t *p_vout )
1627 {
1628     int         i_height, i_text_height;              /* total and text height */
1629     int         i_width_1, i_width_2;                            /* text width */
1630     int         i_byte;                                          /* byte index */    
1631     const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
1632     const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";    
1633
1634     /* Get text size */
1635     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1636     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1637     i_height += i_text_height;
1638
1639     /* Render background - effective background color will depend of the screen
1640      * depth */
1641     for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1642          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1643          i_byte++ )
1644     {
1645         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = 0x33;        
1646     }    
1647
1648     /* Render text, if not larger than screen */
1649     if( i_width_1 < p_vout->i_width )
1650     {        
1651         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1652                     (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1653                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1654                     0xffffffff, 0x00000000, 0x00000000,
1655                     OUTLINED_TEXT, psz_text_1 );
1656     }
1657     if( i_width_2 < p_vout->i_width )
1658     {        
1659         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1660                     (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1661                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1662                     0xffffffff, 0x00000000, 0x00000000,
1663                     OUTLINED_TEXT, psz_text_2 );
1664     }    
1665
1666     /* Activate modified area */
1667     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1668 }
1669
1670 /******************************************************************************
1671  * Manage: manage thread
1672  ******************************************************************************
1673  * This function will handle changes in thread configuration.
1674  ******************************************************************************/
1675 static int Manage( vout_thread_t *p_vout )
1676 {
1677 #ifdef DEBUG_VIDEO
1678     if( p_vout->i_changes )
1679     {
1680         intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes, 
1681                     p_vout->i_changes & VOUT_NODISPLAY_CHANGE );        
1682     }    
1683 #endif
1684
1685     /* On gamma or grayscale change, rebuild tables */
1686     if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE | 
1687                              VOUT_YUV_CHANGE) )
1688     {
1689         if( vout_ResetYUV( p_vout ) )
1690         {
1691             intf_ErrMsg("error: can't rebuild convertion tables\n");            
1692             return( 1 );            
1693         }        
1694     }
1695
1696     /* Clear changes flags which does not need management or have been
1697      * handled */
1698     p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE | 
1699                            VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE | 
1700                            VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
1701
1702     /* Detect unauthorized changes */
1703     if( p_vout->i_changes )
1704     {
1705         /* Some changes were not acknowledged by vout_SysManage or this function,
1706          * it means they should not be authorized */
1707         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );        
1708         return( 1 );        
1709     }
1710     
1711     return( 0 );    
1712 }
1713
1714 /******************************************************************************
1715  * Align: align a subpicture in the screen
1716  ******************************************************************************
1717  * This function is used for rendering text or subpictures. It returns non 0
1718  * it the final aera is not fully included in display area. Return coordinates
1719  * are absolute.
1720  ******************************************************************************/
1721 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y, 
1722                    int i_width, int i_height, int i_h_align, int i_v_align )
1723 {
1724     /* Align horizontally */
1725     switch( i_h_align )
1726     {
1727     case CENTER_ALIGN:
1728         *pi_x -= i_width / 2;        
1729         break;        
1730     case CENTER_RALIGN:
1731         *pi_x += (p_vout->i_width - i_width) / 2;        
1732         break;        
1733     case RIGHT_ALIGN:   
1734         *pi_x -= i_width;        
1735         break;        
1736     case RIGHT_RALIGN:
1737         *pi_x += p_vout->i_width - i_width;        
1738         break;        
1739     }
1740
1741     /* Align vertically */
1742     switch( i_v_align )
1743     {
1744     case CENTER_ALIGN:
1745         *pi_y -= i_height / 2;        
1746         break;
1747     case CENTER_RALIGN:
1748         *pi_y += (p_vout->i_height - i_height) / 2;        
1749         break;        
1750     case BOTTOM_ALIGN:
1751         *pi_y -= i_height;        
1752         break;        
1753     case BOTTOM_RALIGN:
1754         *pi_y += p_vout->i_height - i_height;        
1755         break;        
1756     case SUBTITLE_RALIGN:
1757         *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y + 
1758                   p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;        
1759         break;        
1760     }
1761
1762     /* Return non 0 if clipping failed */
1763     return( (*pi_x < 0) || (*pi_y < 0) || 
1764             (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );    
1765 }
1766