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