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