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