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