]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Mise place du scaling, episode II
[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             //?? clear: SetBufferPicture( p_vout, NULL );
812             if( p_vout->b_interface /* && ?? intf_change -> cause use of 100% CPU ! */ )
813             {
814                 /* Interface has changed, so a new rendering is required - force
815                  * it by setting last idle date to 0 */
816                 p_vout->last_idle_date = 0;                
817             }
818
819             /* Render idle screen and update idle date, then render interface if
820              * required */
821             b_display = RenderIdle( p_vout );
822             if( b_display )
823             {                
824                 p_vout->last_idle_date = current_date;
825                 if( p_vout->b_interface )
826                 {
827                     RenderInterface( p_vout );                
828                 }
829             }
830             
831         }       
832         else
833         {
834             b_display = 0;            
835         }        
836
837         /*
838          * Sleep, wake up and display rendered picture
839          */
840
841 #ifdef STATS
842         /* Store render time */
843         p_vout->render_time = mdate() - current_date;
844 #endif
845
846         /* Give back change lock */
847         vlc_mutex_unlock( &p_vout->change_lock );        
848
849         /* Sleep a while or until a given date */
850         if( display_date != 0 )
851         {
852             mwait( display_date );
853         }
854         else
855         {
856             msleep( VOUT_IDLE_SLEEP );                
857         }            
858
859         /* On awakening, take back lock and send immediately picture to display, 
860          * then swap buffers */
861         vlc_mutex_lock( &p_vout->change_lock );        
862 #ifdef DEBUG_VIDEO
863         intf_DbgMsg( "picture %p, subpicture %p\n", p_pic, p_subpic );        
864 #endif            
865         if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
866         {
867             vout_SysDisplay( p_vout );
868             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
869         }
870
871         /*
872          * Check events and manage thread
873          */
874         if( vout_SysManage( p_vout ) | Manage( p_vout ) )
875         {
876             /* A fatal error occured, and the thread must terminate immediately,
877              * without displaying anything - setting b_error to 1 cause the
878              * immediate end of the main while() loop. */
879             p_vout->b_error = 1;
880         }  
881     } 
882
883     /*
884      * Error loop - wait until the thread destruction is requested
885      */
886     if( p_vout->b_error )
887     {
888         ErrorThread( p_vout );        
889     }
890
891     /* End of thread */
892     EndThread( p_vout );
893     DestroyThread( p_vout, THREAD_OVER ); 
894     intf_DbgMsg( "thread end\n" );
895 }
896
897 /******************************************************************************
898  * ErrorThread: RunThread() error loop
899  ******************************************************************************
900  * This function is called when an error occured during thread main's loop. The
901  * thread can still receive feed, but must be ready to terminate as soon as
902  * possible.
903  ******************************************************************************/
904 static void ErrorThread( vout_thread_t *p_vout )
905 {
906     /* Wait until a `die' order */
907     intf_DbgMsg("\n");
908     while( !p_vout->b_die )
909     {
910         /* Sleep a while */
911         msleep( VOUT_IDLE_SLEEP );                
912     }
913 }
914
915 /*******************************************************************************
916  * EndThread: thread destruction
917  *******************************************************************************
918  * This function is called when the thread ends after a sucessfull 
919  * initialization. It frees all ressources allocated by InitThread.
920  *******************************************************************************/
921 static void EndThread( vout_thread_t *p_vout )
922 {
923     int     i_index;                                          /* index in heap */
924             
925     /* Store status */
926     intf_DbgMsg("\n");
927     *p_vout->pi_status = THREAD_END;    
928
929     /* Destroy all remaining pictures and subpictures */
930     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
931     {
932         if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
933         {
934             free( p_vout->p_picture[i_index].p_data );
935         }
936         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
937         {
938             free( p_vout->p_subpicture[i_index].p_data );            
939         }        
940     }
941
942     /* Destroy translation tables */
943     vout_EndYUV( p_vout );  
944     vout_SysEnd( p_vout );    
945 }
946
947 /*******************************************************************************
948  * DestroyThread: thread destruction
949  *******************************************************************************
950  * This function is called when the thread ends. It frees all ressources
951  * allocated by CreateThread. Status is available at this stage.
952  *******************************************************************************/
953 static void DestroyThread( vout_thread_t *p_vout, int i_status )
954 {  
955     int *pi_status;                                           /* status adress */
956     
957     /* Store status adress */
958     intf_DbgMsg("\n");
959     pi_status = p_vout->pi_status;    
960     
961     /* Destroy thread structures allocated by Create and InitThread */
962     vout_UnloadFont( p_vout->p_default_font );
963     vout_UnloadFont( p_vout->p_large_font ); 
964     vout_SysDestroy( p_vout );
965     free( p_vout );
966     *pi_status = i_status;    
967 }
968
969 /*******************************************************************************
970  * Print: print simple text on a picture
971  *******************************************************************************
972  * This function will print a simple text on the picture. It is designed to
973  * print debugging or general informations.
974  *******************************************************************************/
975 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 )
976 {
977     int                 i_text_height;                    /* total text height */
978     int                 i_text_width;                      /* total text width */
979
980     /* Update upper left coordinates according to alignment */
981     vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
982     if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
983     {
984         /* Set area and print text */
985         SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );    
986         vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data + 
987                     i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
988                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line, 
989                     0xffffffff, 0x00000000, 0x00000000, 0, psz_text );
990     }    
991 }
992
993 /*******************************************************************************
994  * SetBufferArea: activate an area in current buffer
995  *******************************************************************************
996  * This function is called when something is rendered on the current buffer.
997  * It set the area as active and prepare it to be cleared on next rendering.
998  * Pay attention to the fact that in this functions, i_h is in fact the end y
999  * coordinate of the new area.
1000  *******************************************************************************/
1001 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1002 {
1003     vout_buffer_t *     p_buffer;                            /* current buffer */
1004     int                 i_area_begin, i_area_end;   /* area vertical extension */
1005     int                 i_area, i_area_copy;                     /* area index */    
1006     int                 i_area_shift;              /* shift distance for areas */    
1007     
1008     /* Choose buffer and modify h to end of area position */
1009     p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1010     i_h +=      i_y - 1;
1011  
1012     /* 
1013      * Remove part of the area which is inside the picture - this is done
1014      * by calling again SetBufferArea with the correct areas dimensions.
1015      */
1016     if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1017     {
1018         i_area_begin =  p_buffer->i_pic_y;
1019         i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
1020
1021         if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1022             ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1023             ((i_y <  i_area_begin) && (i_h > i_area_end)) )
1024         {                    
1025             /* Keep the stripe above the picture, if any */
1026             if( i_y < i_area_begin )
1027             {
1028                 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );            
1029             }
1030             /* Keep the stripe below the picture, if any */
1031             if( i_h > i_area_end )
1032             {
1033                 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1034             }        
1035             return;
1036         }        
1037     }   
1038
1039     /* Skip some extensions until interesting areas */
1040     for( i_area = 0; 
1041          (i_area < p_buffer->i_areas) &&
1042              (p_buffer->pi_area_end[i_area] + 1 <= i_y); 
1043          i_area++ )
1044     {
1045         ;        
1046     }
1047     
1048     if( i_area == p_buffer->i_areas )
1049     {
1050         /* New area is below all existing ones: just add it at the end of the 
1051          * array, if possible - else, append it to the last one */
1052         if( i_area < VOUT_MAX_AREAS )
1053         {
1054             p_buffer->pi_area_begin[i_area] = i_y;
1055             p_buffer->pi_area_end[i_area] = i_h;            
1056             p_buffer->i_areas++;            
1057         }
1058         else
1059         {
1060 #ifdef DEBUG_VIDEO
1061             intf_DbgMsg("areas overflow\n");            
1062 #endif
1063             p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;  
1064         }        
1065     }
1066     else 
1067     {
1068         i_area_begin =  p_buffer->pi_area_begin[i_area];
1069         i_area_end =    p_buffer->pi_area_end[i_area];
1070         
1071         if( i_y < i_area_begin ) 
1072         {
1073             if( i_h >= i_area_begin - 1 )
1074             {                
1075                 /* Extend area above */
1076                 p_buffer->pi_area_begin[i_area] = i_y;
1077             }
1078             else
1079             {
1080                 /* Create a new area above : merge last area if overflow, then 
1081                  * move all old areas down */
1082                 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1083                 {                    
1084 #ifdef DEBUG_VIDEO
1085                     intf_DbgMsg("areas overflow\n");       
1086 #endif
1087                     p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];                    
1088                 }
1089                 else
1090                 {
1091                     p_buffer->i_areas++;                    
1092                 }
1093                 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1094                 {
1095                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1096                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
1097                 }
1098                 p_buffer->pi_area_begin[i_area] = i_y;
1099                 p_buffer->pi_area_end[i_area] = i_h;
1100                 return;
1101             }              
1102         }
1103         if( i_h > i_area_end )
1104         {
1105             /* Find further areas which can be merged with the new one */
1106             for( i_area_copy = i_area + 1; 
1107                  (i_area_copy < p_buffer->i_areas) &&
1108                      (p_buffer->pi_area_begin[i_area] <= i_h);
1109                  i_area_copy++ )
1110             {
1111                 ;                
1112             }
1113             i_area_copy--;            
1114
1115             if( i_area_copy != i_area )
1116             {
1117                 /* Merge with last possible areas */
1118                 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1119
1120                 /* Shift lower areas upward */
1121                 i_area_shift = i_area_copy - i_area;                
1122                 p_buffer->i_areas -= i_area_shift;
1123                 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1124                 {
1125                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1126                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
1127                 }
1128             }
1129             else
1130             {
1131                 /* Extend area below */
1132                 p_buffer->pi_area_end[i_area] = i_h;
1133             }            
1134         }
1135     }
1136 }
1137
1138 /*******************************************************************************
1139  * SetBufferPicture: clear buffer and set picture area
1140  *******************************************************************************
1141  * This function is called before any rendering. It clears the current 
1142  * rendering buffer and set the new picture area. If the picture pointer is
1143  * NULL, then no picture area is defined. Floating operations are avoided since
1144  * some MMX calculations may follow.
1145  *******************************************************************************/
1146 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1147 {
1148     vout_buffer_t *     p_buffer;                            /* current buffer */
1149     int                 i_pic_x, i_pic_y;                  /* picture position */
1150     int                 i_pic_width, i_pic_height;       /* picture dimensions */    
1151     int                 i_old_pic_y, i_old_pic_height;     /* old picture area */    
1152     int                 i_vout_width, i_vout_height;     /* display dimensions */
1153     int                 i_area;                                  /* area index */    
1154     int                 i_data_index;                       /* area data index */    
1155     int                 i_data_size;     /* area data size, in 256 bytes blocs */
1156     u64 *               p_data;                                   /* area data */    
1157     
1158     /* Choose buffer and set display dimensions */
1159     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1160     i_vout_width =      p_vout->i_width;
1161     i_vout_height =     p_vout->i_height;    
1162
1163     /*
1164      * Computes new picture size 
1165      */
1166     if( p_pic != NULL )
1167     {
1168         /* Try horizontal scaling first */
1169         i_pic_width = ( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ? 
1170             i_vout_width : p_pic->i_width;
1171         i_pic_width = i_pic_width / 16 * 16; //?? currently, width must be multiple of 16        
1172         switch( p_pic->i_aspect_ratio )
1173         {
1174         case AR_3_4_PICTURE:
1175             i_pic_height = i_pic_width * 3 / 4;
1176             break;                
1177         case AR_16_9_PICTURE:
1178             i_pic_height = i_pic_width * 9 / 16;
1179             break;
1180         case AR_221_1_PICTURE:        
1181             i_pic_height = i_pic_width * 100 / 221;
1182             break;               
1183         case AR_SQUARE_PICTURE:
1184         default:
1185             i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;            
1186             break;
1187         }
1188
1189         /* If picture dimensions using horizontal scaling are too large, use 
1190          * vertical scaling */
1191         if( i_pic_height > i_vout_height )
1192         {
1193             i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ? 
1194                 i_vout_height : p_pic->i_height;
1195             switch( p_pic->i_aspect_ratio )
1196             {
1197             case AR_3_4_PICTURE:
1198                 i_pic_width = i_pic_height * 4 / 3;
1199                 break;                
1200             case AR_16_9_PICTURE:
1201                 i_pic_width = i_pic_height * 16 / 9;
1202                 break;
1203             case AR_221_1_PICTURE:        
1204                 i_pic_width = i_pic_height * 221 / 100;
1205                 break;               
1206             case AR_SQUARE_PICTURE:
1207             default:
1208                 i_pic_width = p_pic->i_width * i_pic_height / p_pic->i_height;
1209                 break;
1210             }        
1211             i_pic_width = i_pic_width / 16 * 16; //?? currently, width must be multiple of 16        
1212         }        
1213
1214         /* Set picture position */
1215         i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1216         i_pic_y = (p_vout->i_height - i_pic_height) / 2;                
1217     }    
1218     else
1219     {
1220         /* No picture: size is 0 */
1221         i_pic_x =       0;
1222         i_pic_y =       0;
1223         i_pic_width =   0;
1224         i_pic_height =  0;
1225     }
1226
1227     /*
1228      * Set new picture size - if is is smaller than the previous one, clear 
1229      * around it. Since picture are centered, only their size is tested.
1230      */                                          
1231     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1232     {
1233         i_old_pic_y =            p_buffer->i_pic_y;
1234         i_old_pic_height =       p_buffer->i_pic_height;
1235         p_buffer->i_pic_x =      i_pic_x;
1236         p_buffer->i_pic_y =      i_pic_y;
1237         p_buffer->i_pic_width =  i_pic_width;
1238         p_buffer->i_pic_height = i_pic_height;                        
1239         SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1240     }
1241     else
1242     {
1243         p_buffer->i_pic_x =      i_pic_x;
1244         p_buffer->i_pic_y =      i_pic_y;
1245         p_buffer->i_pic_width =  i_pic_width;
1246         p_buffer->i_pic_height = i_pic_height;    
1247     }
1248
1249     /*
1250      * Clear areas
1251      */
1252     for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1253     {
1254 #ifdef DEBUG_VIDEO    
1255         intf_DbgMsg("clearing picture %p area: %d-%d\n", p_pic, 
1256                     p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area]);    
1257 #endif
1258         p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1259         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * 
1260             p_vout->i_bytes_per_line / 256;
1261         for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1262         {
1263             /* Clear 256 bytes block */
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             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1272         }
1273         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) *
1274             p_vout->i_bytes_per_line % 256 / 4;
1275         for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1276         {
1277             /* Clear remaining 4 bytes blocks */
1278             *p_data++ = 0;
1279         }
1280     }    
1281
1282     /*
1283      * Clear areas array
1284      */
1285     p_buffer->i_areas = 0;
1286 }
1287
1288 /******************************************************************************
1289  * RenderPicture: render a picture
1290  ******************************************************************************
1291  * This function convert a picture from a video heap to a pixel-encoded image
1292  * and copy it to the current rendering buffer. No lock is required, since the
1293  * rendered picture has been determined as existant, and will only be destroyed
1294  * by the vout thread later.
1295  ******************************************************************************/
1296 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1297 {
1298     vout_buffer_t *     p_buffer;                         /* rendering buffer */    
1299     byte_t *            p_pic_data;                 /* convertion destination */
1300     
1301     /* Get and set rendering informations */
1302     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];    
1303     p_pic_data =        p_buffer->p_data + 
1304         p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1305         p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1306
1307     /*
1308      * Choose appropriate rendering function and render picture 
1309      */
1310     switch( p_pic->i_type )
1311     {
1312     case YUV_420_PICTURE:
1313         p_vout->yuv.p_Convert420( p_vout, p_pic_data, 
1314                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1315                                   p_pic->i_width, p_pic->i_height, 0,
1316                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1317                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - p_buffer->i_pic_width,
1318                                   p_pic->i_matrix_coefficients );
1319         break;        
1320     case YUV_422_PICTURE:
1321         p_vout->yuv.p_Convert422( p_vout, p_pic_data, 
1322                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1323                                   p_pic->i_width, p_pic->i_height, 0,
1324                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1325                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - p_buffer->i_pic_width,
1326                                   p_pic->i_matrix_coefficients );
1327         break;        
1328     case YUV_444_PICTURE:
1329         p_vout->yuv.p_Convert444( p_vout, p_pic_data, 
1330                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1331                                   p_pic->i_width, p_pic->i_height, 0,
1332                                   p_buffer->i_pic_width, p_buffer->i_pic_height, 
1333                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel - p_buffer->i_pic_width,
1334                                   p_pic->i_matrix_coefficients );
1335         break;        
1336 #ifdef DEBUG
1337     default:        
1338         intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1339         break;        
1340 #endif
1341     }
1342 }
1343
1344 /******************************************************************************
1345  * RenderPictureInfo: print additionnal informations on a picture
1346  ******************************************************************************
1347  * This function will print informations such as fps and other picture
1348  * dependant informations.
1349  ******************************************************************************/
1350 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1351 {
1352 #if defined(STATS) || defined(DEBUG)
1353     char        psz_buffer[256];                             /* string buffer */
1354 #endif
1355
1356 #ifdef STATS
1357     /* 
1358      * Print FPS rate in upper right corner 
1359      */
1360     if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1361     {        
1362         sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1363                  ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1364                    p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );        
1365         Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1366     }
1367
1368     /* 
1369      * Print frames count and loop time in upper left corner 
1370      */
1371     sprintf( psz_buffer, "%ld frames   rendering: %ld us", 
1372              (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1373     Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1374 #endif
1375
1376 #ifdef DEBUG
1377     /*
1378      * Print picture information in lower right corner
1379      */
1380     sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1381              (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1382              ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1383               ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1384              p_pic->i_width, p_pic->i_height,
1385              p_pic->i_display_width, p_pic->i_display_height,
1386              p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1387              (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1388              ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1389               ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1390                ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1391              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1392              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1393              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1394              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );    
1395     Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1396 #endif
1397 }
1398
1399 /******************************************************************************
1400  * RenderIdle: render idle picture
1401  ******************************************************************************
1402  * This function will print something on the screen. It will return 0 if 
1403  * nothing has been rendered, or 1 if something has been changed on the screen.
1404  * Note that if you absolutely want something to be printed, you will have
1405  * to force it by setting the last idle date to 0.
1406  ******************************************************************************/
1407 static int RenderIdle( vout_thread_t *p_vout )
1408 {
1409     int         i_x = 0, i_y = 0;                            /* text position */    
1410     int         i_width, i_height;                               /* text size */    
1411     mtime_t     current_date;                                 /* current date */
1412     const char *psz_text = "no stream";                    /* text to display */
1413     
1414     
1415     current_date = mdate();    
1416     if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1417         (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1418     {
1419         vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1420                        &i_width, &i_height );
1421         if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1422         {       
1423             vout_Print( p_vout->p_large_font, 
1424                         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1425                         i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1426                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1427                         0xffffffff, 0x33333333, 0,
1428                         WIDE_TEXT | OUTLINED_TEXT, psz_text );        
1429             SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1430         }        
1431         return( 1 );        
1432     }
1433     return( 0 );    
1434 }
1435
1436 /******************************************************************************
1437  * RenderInfo: render additionnal informations
1438  ******************************************************************************
1439  * This function render informations which do not depend of the current picture
1440  * rendered.
1441  ******************************************************************************/
1442 static void RenderInfo( vout_thread_t *p_vout )
1443 {
1444 #ifdef DEBUG
1445     char        psz_buffer[256];                             /* string buffer */
1446     int         i_ready_pic = 0;                            /* ready pictures */
1447     int         i_reserved_pic = 0;                      /* reserved pictures */
1448     int         i_picture;                                   /* picture index */
1449 #endif
1450
1451 #ifdef DEBUG
1452     /* 
1453      * Print thread state in lower left corner  
1454      */
1455     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1456     {
1457         switch( p_vout->p_picture[i_picture].i_status )
1458         {
1459         case RESERVED_PICTURE:
1460         case RESERVED_DATED_PICTURE:
1461         case RESERVED_DISP_PICTURE:
1462             i_reserved_pic++;            
1463             break;            
1464         case READY_PICTURE:
1465             i_ready_pic++;            
1466             break;            
1467         }        
1468     }
1469     sprintf( psz_buffer, "pic: %d/%d/%d", 
1470              i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1471     Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );    
1472 #endif
1473 }
1474
1475 /*******************************************************************************
1476  * RenderSubPicture: render a subpicture
1477  *******************************************************************************
1478  * This function render a sub picture unit.
1479  *******************************************************************************/
1480 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1481 {
1482     //??
1483 }
1484
1485 /*******************************************************************************
1486  * RenderInterface: render the interface
1487  *******************************************************************************
1488  * This function render the interface, if any.
1489  *******************************************************************************/
1490 static void RenderInterface( vout_thread_t *p_vout )
1491 {
1492     int         i_height, i_text_height;              /* total and text height */
1493     int         i_width_1, i_width_2;                            /* text width */
1494     int         i_byte;                                          /* byte index */    
1495     const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
1496     const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";    
1497
1498     /* Get text size */
1499     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1500     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1501     i_height += i_text_height;
1502
1503     /* Render background - effective background color will depend of the screen
1504      * depth */
1505     for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1506          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1507          i_byte++ )
1508     {
1509         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = 0x33;        
1510     }    
1511
1512     /* Render text, if not larger than screen */
1513     if( i_width_1 < p_vout->i_width )
1514     {        
1515         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1516                     (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1517                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1518                     0xffffffff, 0x00000000, 0x00000000,
1519                     OUTLINED_TEXT, psz_text_1 );
1520     }
1521     if( i_width_2 < p_vout->i_width )
1522     {        
1523         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1524                     (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1525                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1526                     0xffffffff, 0x00000000, 0x00000000,
1527                     OUTLINED_TEXT, psz_text_2 );
1528     }    
1529
1530     /* Activate modified area */
1531     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1532 }
1533
1534 /******************************************************************************
1535  * Manage: manage thread
1536  ******************************************************************************
1537  * This function will handle changes in thread configuration.
1538  ******************************************************************************/
1539 static int Manage( vout_thread_t *p_vout )
1540 {
1541 #ifdef DEBUG_VIDEO
1542     if( p_vout->i_changes )
1543     {
1544         intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes, 
1545                     p_vout->i_changes & VOUT_NODISPLAY_CHANGE );        
1546     }    
1547 #endif
1548
1549     /* On gamma or grayscale change, rebuild tables */
1550     if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE | 
1551                              VOUT_YUV_CHANGE) )
1552     {
1553         if( vout_ResetYUV( p_vout ) )
1554         {
1555             intf_ErrMsg("error: can't rebuild convertion tables\n");            
1556             return( 1 );            
1557         }        
1558     }
1559
1560     /* Clear changes flags which does not need management or have been
1561      * handled */
1562     p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE | 
1563                            VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE | 
1564                            VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
1565
1566     /* Detect unauthorized changes */
1567     if( p_vout->i_changes )
1568     {
1569         /* Some changes were not acknowledged by vout_SysManage or this function,
1570          * it means they should not be authorized */
1571         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );        
1572         return( 1 );        
1573     }
1574     
1575     return( 0 );    
1576 }
1577
1578 /******************************************************************************
1579  * Align: align a subpicture in the screen
1580  ******************************************************************************
1581  * This function is used for rendering text or subpictures. It returns non 0
1582  * it the final aera is not fully included in display area. Return coordinates
1583  * are absolute.
1584  ******************************************************************************/
1585 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y, 
1586                    int i_width, int i_height, int i_h_align, int i_v_align )
1587 {
1588     /* Align horizontally */
1589     switch( i_h_align )
1590     {
1591     case CENTER_ALIGN:
1592         *pi_x -= i_width / 2;        
1593         break;        
1594     case CENTER_RALIGN:
1595         *pi_x += (p_vout->i_width - i_width) / 2;        
1596         break;        
1597     case RIGHT_ALIGN:   
1598         *pi_x -= i_width;        
1599         break;        
1600     case RIGHT_RALIGN:
1601         *pi_x += p_vout->i_width - i_width;        
1602         break;        
1603     }
1604
1605     /* Align vertically */
1606     switch( i_v_align )
1607     {
1608     case CENTER_ALIGN:
1609         *pi_y -= i_height / 2;        
1610         break;
1611     case CENTER_RALIGN:
1612         *pi_y += (p_vout->i_height - i_height) / 2;        
1613         break;        
1614     case BOTTOM_ALIGN:
1615         *pi_y -= i_height;        
1616         break;        
1617     case BOTTOM_RALIGN:
1618         *pi_y += p_vout->i_height - i_height;        
1619         break;        
1620     case SUBTITLE_RALIGN:
1621         *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y + 
1622                   p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;        
1623         break;        
1624     }
1625
1626     /* Return non 0 if clipping failed */
1627     return( (*pi_x < 0) || (*pi_y < 0) || 
1628             (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );    
1629 }
1630