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