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