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