]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
* Updated the TODO list.
[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.118 2001/04/25 09:31:14 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( p_main->p_bank,
113                                     MODULE_CAPABILITY_VOUT, NULL );
114
115     if( p_vout->p_module == NULL )
116     {
117         intf_ErrMsg( "vout error: no suitable vout module" );
118         free( p_vout );
119         return( NULL );
120     }
121
122 #define f p_vout->p_module->p_functions->vout.functions.vout
123     p_vout->pf_create     = f.pf_create;
124     p_vout->pf_init       = f.pf_init;
125     p_vout->pf_end        = f.pf_end;
126     p_vout->pf_destroy    = f.pf_destroy;
127     p_vout->pf_manage     = f.pf_manage;
128     p_vout->pf_display    = f.pf_display;
129     p_vout->pf_setpalette = f.pf_setpalette;
130 #undef f
131
132     if( p_vout->pf_setpalette == NULL )
133     {
134         p_vout->pf_setpalette = SetPalette;
135     }
136
137     /* Initialize thread properties - thread id and locks will be initialized
138      * later */
139     p_vout->b_die               = 0;
140     p_vout->b_error             = 0;
141     p_vout->b_active            = 0;
142     p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
143     *p_vout->pi_status          = THREAD_CREATE;
144
145     /* Initialize some fields used by the system-dependant method - these
146      * fields will probably be modified by the method, and are only
147      * preferences */
148     p_vout->i_changes             = 0;
149     p_vout->i_width               = main_GetIntVariable( VOUT_WIDTH_VAR,
150                                                          VOUT_WIDTH_DEFAULT );
151     p_vout->i_height              = main_GetIntVariable( VOUT_HEIGHT_VAR,
152                                                          VOUT_HEIGHT_DEFAULT );
153     p_vout->i_bytes_per_line      = p_vout->i_width * 2;
154     p_vout->i_screen_depth        = 15;
155     p_vout->i_bytes_per_pixel     = 2;
156     p_vout->f_gamma               = VOUT_GAMMA;
157     p_vout->b_need_render         = 1;
158     p_vout->b_YCbr                = 0;
159     
160     p_vout->b_grayscale           = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
161                                                      VOUT_GRAYSCALE_DEFAULT );
162     p_vout->b_info                = 0;
163     p_vout->b_interface           = 0;
164     p_vout->b_scale               = 1;
165
166     intf_WarnMsg( 1, "wished configuration: %dx%d, %d/%d bpp (%d Bpl)",
167                   p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
168                   p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
169
170     /* Initialize idle screen */
171     p_vout->last_display_date   = 0;
172     p_vout->last_idle_date      = 0;
173     p_vout->init_display_date   = mdate();
174     p_vout->render_time         = 10000;
175
176     /* Initialize statistics fields */
177     p_vout->c_fps_samples       = 0;
178
179     /* Initialize buffer index */
180     p_vout->i_buffer_index      = 0;
181
182     /* Initialize pictures and subpictures - translation tables and functions
183      * will be initialized later in InitThread */
184     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
185     {
186         p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
187         p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
188     }
189     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
190     {
191         p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
192         p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
193     }
194     p_vout->i_pictures = 0;
195
196     /* Create and initialize system-dependant method - this function issues its
197      * own error messages */
198     if( p_vout->pf_create( p_vout ) )
199     {
200         module_Unneed( p_main->p_bank, p_vout->p_module );
201         free( p_vout );
202         return( NULL );
203     }
204     intf_WarnMsg( 1, "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
205                   "masks: 0x%x/0x%x/0x%x",
206                   p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
207                   p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
208                   p_vout->i_red_mask, p_vout->i_green_mask,
209                   p_vout->i_blue_mask );
210
211     /* Calculate shifts from system-updated masks */
212     MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift,
213                  p_vout->i_red_mask );
214     MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift,
215                  p_vout->i_green_mask );
216     MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift,
217                  p_vout->i_blue_mask );
218
219     /* Set some useful colors */
220     p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
221     p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
222     p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
223     p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
224
225     /* Load fonts - fonts must be initialized after the system method since
226      * they may be dependant on screen depth and other thread properties */
227     p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
228     if( p_vout->p_default_font == NULL )
229     {
230         intf_ErrMsg( "vout error: could not load default font" );
231     }
232
233     p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
234     if( p_vout->p_large_font == NULL )
235     {
236         intf_ErrMsg( "vout error: could not load large font" );
237     }
238
239     /* Create thread and set locks */
240     vlc_mutex_init( &p_vout->picture_lock );
241     vlc_mutex_init( &p_vout->subpicture_lock );
242     vlc_mutex_init( &p_vout->change_lock );
243
244     if( vlc_thread_create( &p_vout->thread_id, "video output",
245                            (void *) RunThread, (void *) p_vout) )
246     {
247         intf_ErrMsg("vout error: %s", strerror(ENOMEM));
248         vout_UnloadFont( p_vout->p_default_font );
249         vout_UnloadFont( p_vout->p_large_font );
250         p_vout->pf_destroy( p_vout );
251         free( p_vout );
252         return( NULL );
253     }
254
255     intf_Msg( "vout: video display initialized (%dx%d, %d/%d bpp)",
256               p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
257               p_vout->i_bytes_per_pixel * 8 );
258
259     /* If status is NULL, wait until the thread is created */
260     if( pi_status == NULL )
261     {
262         do
263         {
264             msleep( THREAD_SLEEP );
265         }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
266                 && (i_status != THREAD_FATAL) );
267         if( i_status != THREAD_READY )
268         {
269             return( NULL );
270         }
271     }
272     return( p_vout );
273 }
274
275 /*****************************************************************************
276  * vout_DestroyThread: destroys a previously created thread
277  *****************************************************************************
278  * Destroy a terminated thread.
279  * The function will request a destruction of the specified thread. If pi_error
280  * is NULL, it will return once the thread is destroyed. Else, it will be
281  * update using one of the THREAD_* constants.
282  *****************************************************************************/
283 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
284 {
285     int     i_status;                                       /* thread status */
286
287     /* Set status */
288     intf_DbgMsg("");
289     p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
290     *p_vout->pi_status = THREAD_DESTROY;
291
292     /* Request thread destruction */
293     p_vout->b_die = 1;
294
295     /* If status is NULL, wait until thread has been destroyed */
296     if( pi_status == NULL )
297     {
298         do
299         {
300             msleep( THREAD_SLEEP );
301         }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
302                 && (i_status != THREAD_FATAL) );
303     }
304 }
305
306 /*****************************************************************************
307  * vout_DisplaySubPicture: display a subpicture unit
308  *****************************************************************************
309  * Remove the reservation flag of an subpicture, which will cause it to be ready
310  * for display. The picture does not need to be locked, since it is ignored by
311  * the output thread if is reserved.
312  *****************************************************************************/
313 void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
314 {
315 #ifdef DEBUG_VOUT
316     char        psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
317     char        psz_end_date[MSTRTIME_MAX_SIZE];   /* buffer for date string */
318 #endif
319
320 #ifdef DEBUG
321     /* Check if status is valid */
322     if( p_subpic->i_status != RESERVED_SUBPICTURE )
323     {
324         intf_DbgMsg("error: subpicture %p has invalid status %d", p_subpic,
325                     p_subpic->i_status );
326     }
327 #endif
328
329     /* Remove reservation flag */
330     p_subpic->i_status = READY_SUBPICTURE;
331
332 #ifdef DEBUG_VOUT
333     /* Send subpicture information */
334     intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s",
335                 p_subpic, p_subpic->i_type,
336                 mstrtime( psz_begin_date, p_subpic->begin_date ),
337                 mstrtime( psz_end_date, p_subpic->end_date ) );
338 #endif
339 }
340
341 /*****************************************************************************
342  * vout_CreateSubPicture: allocate a subpicture in the video output heap.
343  *****************************************************************************
344  * This function create a reserved subpicture in the video output heap.
345  * A null pointer is returned if the function fails. This method provides an
346  * already allocated zone of memory in the spu data fields. It needs locking
347  * since several pictures can be created by several producers threads.
348  *****************************************************************************/
349 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
350                                      int i_size )
351 {
352     int                 i_subpic;                        /* subpicture index */
353     subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
354     subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
355
356     /* Get lock */
357     vlc_mutex_lock( &p_vout->subpicture_lock );
358
359     /*
360      * Look for an empty place
361      */
362     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
363     {
364         if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
365         {
366             /* Subpicture is marked for destruction, but is still allocated */
367             if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
368                 (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
369             {
370                 /* Memory size do match or is smaller : memory will not be
371                  * reallocated, and function can end immediately - this is
372                  * the best possible case, since no memory allocation needs
373                  * to be done */
374                 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
375 #ifdef DEBUG_VOUT
376                 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)",
377                             &p_vout->p_subpicture[i_subpic] );
378 #endif
379                 vlc_mutex_unlock( &p_vout->subpicture_lock );
380                 return( &p_vout->p_subpicture[i_subpic] );
381             }
382             else if( p_destroyed_subpic == NULL )
383             {
384                 /* Memory size do not match, but subpicture index will be kept in
385                  * case no other place are left */
386                 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
387             }
388         }
389         else if( (p_free_subpic == NULL) &&
390                  (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
391         {
392             /* Subpicture is empty and ready for allocation */
393             p_free_subpic = &p_vout->p_subpicture[i_subpic];
394         }
395     }
396
397     /* If no free subpictures are available, use a destroyed subpicture */
398     if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
399     {
400         /* No free subpicture or matching destroyed subpictures have been
401          * found, but a destroyed subpicture is still avalaible */
402         free( p_destroyed_subpic->p_data );
403         p_free_subpic = p_destroyed_subpic;
404     }
405
406     /*
407      * Prepare subpicture
408      */
409     if( p_free_subpic != NULL )
410     {
411         /* Allocate memory */
412         switch( i_type )
413         {
414         case TEXT_SUBPICTURE:                             /* text subpicture */
415             p_free_subpic->p_data = malloc( i_size + 1 );
416             break;
417         case DVD_SUBPICTURE:                          /* DVD subpicture unit */
418             p_free_subpic->p_data = malloc( i_size );
419             break;
420 #ifdef DEBUG
421         default:
422             intf_DbgMsg("error: unknown subpicture type %d", i_type );
423             p_free_subpic->p_data   =  NULL;
424             break;
425 #endif
426         }
427
428         if( p_free_subpic->p_data != NULL )
429         {
430             /* Copy subpicture information, set some default values */
431             p_free_subpic->i_type                      = i_type;
432             p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
433             p_free_subpic->i_size                      = i_size;
434             p_free_subpic->i_x                         = 0;
435             p_free_subpic->i_y                         = 0;
436             p_free_subpic->i_width                     = 0;
437             p_free_subpic->i_height                    = 0;
438             p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
439             p_free_subpic->i_vertical_align            = CENTER_RALIGN;
440         }
441         else
442         {
443             /* Memory allocation failed : set subpicture as empty */
444             p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
445             p_free_subpic->i_status =  FREE_SUBPICTURE;
446             p_free_subpic =            NULL;
447             intf_ErrMsg( "vout error: spu allocation returned %s",
448                          strerror( ENOMEM ) );
449         }
450
451 #ifdef DEBUG_VOUT
452         intf_DbgMsg("subpicture %p (in free subpicture slot)", p_free_subpic );
453 #endif
454         vlc_mutex_unlock( &p_vout->subpicture_lock );
455         return( p_free_subpic );
456     }
457
458     /* No free or destroyed subpicture could be found */
459     intf_DbgMsg( "warning: subpicture heap is full" );
460     vlc_mutex_unlock( &p_vout->subpicture_lock );
461     return( NULL );
462 }
463
464 /*****************************************************************************
465  * vout_DestroySubPicture: remove a subpicture from the heap
466  *****************************************************************************
467  * This function frees a previously reserved subpicture.
468  * It is meant to be used when the construction of a picture aborted.
469  * This function does not need locking since reserved subpictures are ignored
470  * by the output thread.
471  *****************************************************************************/
472 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
473 {
474 #ifdef DEBUG
475    /* Check if status is valid */
476    if( p_subpic->i_status != RESERVED_SUBPICTURE )
477    {
478        intf_DbgMsg("error: subpicture %p has invalid status %d",
479                    p_subpic, p_subpic->i_status );
480    }
481 #endif
482
483     p_subpic->i_status = DESTROYED_SUBPICTURE;
484
485 #ifdef DEBUG_VOUT
486     intf_DbgMsg("subpicture %p", p_subpic);
487 #endif
488 }
489
490 /*****************************************************************************
491  * vout_DisplayPicture: display a picture
492  *****************************************************************************
493  * Remove the reservation flag of a picture, which will cause it to be ready for
494  * display. The picture won't be displayed until vout_DatePicture has been
495  * called.
496  *****************************************************************************/
497 void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
498 {
499     vlc_mutex_lock( &p_vout->picture_lock );
500     switch( p_pic->i_status )
501     {
502     case RESERVED_PICTURE:
503         p_pic->i_status = RESERVED_DISP_PICTURE;
504         break;
505     case RESERVED_DATED_PICTURE:
506         p_pic->i_status = READY_PICTURE;
507         break;
508 #ifdef DEBUG
509     default:
510         intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
511         break;
512 #endif
513     }
514
515 #ifdef DEBUG_VOUT
516     intf_DbgMsg("picture %p", p_pic);
517 #endif
518     vlc_mutex_unlock( &p_vout->picture_lock );
519 }
520
521 /*****************************************************************************
522  * vout_DatePicture: date a picture
523  *****************************************************************************
524  * Remove the reservation flag of a picture, which will cause it to be ready for
525  * display. The picture won't be displayed until vout_DisplayPicture has been
526  * called.
527  *****************************************************************************/
528 void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
529 {
530 #ifdef DEBUG_VOUT
531     char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
532 #endif
533
534     vlc_mutex_lock( &p_vout->picture_lock );
535     p_pic->date = date;
536     switch( p_pic->i_status )
537     {
538     case RESERVED_PICTURE:
539         p_pic->i_status = RESERVED_DATED_PICTURE;
540         break;
541     case RESERVED_DISP_PICTURE:
542         p_pic->i_status = READY_PICTURE;
543         break;
544 #ifdef DEBUG
545     default:
546         intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
547         break;
548 #endif
549     }
550
551 #ifdef DEBUG_VOUT
552     intf_DbgMsg("picture %p, display date: %s", p_pic, mstrtime( psz_date, p_pic->date) );
553 #endif
554     vlc_mutex_unlock( &p_vout->picture_lock );
555 }
556
557 /*****************************************************************************
558  * vout_CreatePicture: allocate a picture in the video output heap.
559  *****************************************************************************
560  * This function create a reserved image in the video output heap.
561  * A null pointer is returned if the function fails. This method provides an
562  * already allocated zone of memory in the picture data fields. It needs locking
563  * since several pictures can be created by several producers threads.
564  *****************************************************************************/
565 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
566                                int i_width, int i_height )
567 {
568     int         i_picture;                                  /* picture index */
569     int         i_chroma_width = 0;                          /* chroma width */
570     picture_t * p_free_picture = NULL;                 /* first free picture */
571     picture_t * p_destroyed_picture = NULL;       /* first destroyed picture */
572
573     /* Get lock */
574     vlc_mutex_lock( &p_vout->picture_lock );
575
576     /*
577      * Look for an empty place
578      */
579     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
580     {
581         if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
582         {
583             /* Picture is marked for destruction, but is still allocated - note
584              * that if width and type are the same for two pictures, chroma_width
585              * should also be the same */
586             if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
587                 (p_vout->p_picture[i_picture].i_height         == i_height) &&
588                 (p_vout->p_picture[i_picture].i_width          == i_width) )
589             {
590                 /* Memory size do match : memory will not be reallocated, and function
591                  * can end immediately - this is the best possible case, since no
592                  * memory allocation needs to be done */
593                 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
594                 p_vout->i_pictures++;
595 #ifdef DEBUG_VOUT
596                 intf_DbgMsg("picture %p (in destroyed picture slot)",
597                             &p_vout->p_picture[i_picture] );
598 #endif
599                 vlc_mutex_unlock( &p_vout->picture_lock );
600                 return( &p_vout->p_picture[i_picture] );
601             }
602             else if( p_destroyed_picture == NULL )
603             {
604                 /* Memory size do not match, but picture index will be kept in
605                  * case no other place are left */
606                 p_destroyed_picture = &p_vout->p_picture[i_picture];
607             }
608         }
609         else if( (p_free_picture == NULL) &&
610                  (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
611         {
612             /* Picture is empty and ready for allocation */
613             p_free_picture = &p_vout->p_picture[i_picture];
614         }
615     }
616
617     /* If no free picture is available, use a destroyed picture */
618     if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
619     {
620         /* No free picture or matching destroyed picture has been found, but
621          * a destroyed picture is still avalaible */
622         free( p_destroyed_picture->p_data );
623         p_free_picture = p_destroyed_picture;
624     }
625
626     /*
627      * Prepare picture
628      */
629     if( p_free_picture != NULL )
630     {
631         /* Allocate memory */
632         switch( i_type )
633         {
634         case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
635             i_chroma_width = i_width / 2;
636             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
637             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
638             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
639             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
640             break;
641         case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
642             i_chroma_width = i_width / 2;
643             p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
644             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
645             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
646             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
647             break;
648         case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
649             i_chroma_width = i_width;
650             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
651             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
652             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
653             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
654             break;
655 #ifdef DEBUG
656         default:
657             intf_DbgMsg("error: unknown picture type %d", i_type );
658             p_free_picture->p_data   =  NULL;
659             break;
660 #endif
661         }
662
663         if( p_free_picture->p_data != NULL )
664         {
665             /* Copy picture information, set some default values */
666             p_free_picture->i_type                      = i_type;
667             p_free_picture->i_status                    = RESERVED_PICTURE;
668             p_free_picture->i_matrix_coefficients       = 1;
669             p_free_picture->i_width                     = i_width;
670             p_free_picture->i_height                    = i_height;
671             p_free_picture->i_chroma_width              = i_chroma_width;
672             p_free_picture->i_size                      = i_width * i_height;
673             p_free_picture->i_chroma_size               = i_chroma_width
674                                                             * i_height;
675             p_free_picture->i_display_horizontal_offset = 0;
676             p_free_picture->i_display_vertical_offset   = 0;
677             p_free_picture->i_display_width             = i_width;
678             p_free_picture->i_display_height            = i_height;
679             p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
680             p_free_picture->i_refcount                  = 0;
681             p_vout->i_pictures++;
682         }
683         else
684         {
685             /* Memory allocation failed : set picture as empty */
686             p_free_picture->i_type   =  EMPTY_PICTURE;
687             p_free_picture->i_status =  FREE_PICTURE;
688             p_free_picture =            NULL;
689             intf_ErrMsg( "vout error: picture allocation returned %s",
690                          strerror( ENOMEM ) );
691         }
692
693 #ifdef DEBUG_VOUT
694         intf_DbgMsg("picture %p (in free picture slot)", p_free_picture );
695 #endif
696         vlc_mutex_unlock( &p_vout->picture_lock );
697
698         /* Initialize mutex */
699         vlc_mutex_init( &(p_free_picture->lock_deccount) );
700         
701         return( p_free_picture );
702     }
703
704     /* No free or destroyed picture could be found */
705     intf_DbgMsg( "warning: picture heap is full" );
706     vlc_mutex_unlock( &p_vout->picture_lock );
707     return( NULL );
708 }
709
710 /*****************************************************************************
711  * vout_DestroyPicture: remove a permanent or reserved picture from the heap
712  *****************************************************************************
713  * This function frees a previously reserved picture or a permanent
714  * picture. It is meant to be used when the construction of a picture aborted.
715  * Note that the picture will be destroyed even if it is linked !
716  *****************************************************************************/
717 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
718 {
719     vlc_mutex_lock( &p_vout->picture_lock );
720
721 #ifdef DEBUG
722     /* Check if picture status is valid */
723     if( (p_pic->i_status != RESERVED_PICTURE) &&
724         (p_pic->i_status != RESERVED_DATED_PICTURE) &&
725         (p_pic->i_status != RESERVED_DISP_PICTURE) )
726     {
727         intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
728     }
729 #endif
730
731     p_pic->i_status = DESTROYED_PICTURE;
732     p_vout->i_pictures--;
733
734 #ifdef DEBUG_VOUT
735     intf_DbgMsg("picture %p", p_pic);
736 #endif
737
738     /* destroy the lock that had been initialized in CreatePicture */
739     vlc_mutex_destroy( &(p_pic->lock_deccount) );
740    
741     vlc_mutex_unlock( &p_vout->picture_lock );
742 }
743
744 /*****************************************************************************
745  * vout_LinkPicture: increment reference counter of a picture
746  *****************************************************************************
747  * This function increment the reference counter of a picture in the video
748  * heap. It needs a lock since several producer threads can access the picture.
749  *****************************************************************************/
750 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
751 {
752     vlc_mutex_lock( &p_vout->picture_lock );
753     p_pic->i_refcount++;
754
755 #ifdef DEBUG_VOUT
756     intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
757 #endif
758
759     vlc_mutex_unlock( &p_vout->picture_lock );
760 }
761
762 /*****************************************************************************
763  * vout_UnlinkPicture: decrement reference counter of a picture
764  *****************************************************************************
765  * This function decrement the reference counter of a picture in the video heap.
766  *****************************************************************************/
767 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
768 {
769     vlc_mutex_lock( &p_vout->picture_lock );
770     p_pic->i_refcount--;
771
772 #ifdef DEBUG_VOUT
773     if( p_pic->i_refcount < 0 )
774     {
775         intf_DbgMsg("error: refcount < 0");
776         p_pic->i_refcount = 0;
777     }
778 #endif
779
780     if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
781     {
782         p_pic->i_status = DESTROYED_PICTURE;
783         p_vout->i_pictures--;
784     }
785
786 #ifdef DEBUG_VOUT
787     intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
788 #endif
789
790     vlc_mutex_unlock( &p_vout->picture_lock );
791 }
792
793 /*****************************************************************************
794  * vout_SetBuffers: set buffers adresses
795  *****************************************************************************
796  * This function is called by system drivers to set buffers video memory
797  * adresses.
798  *****************************************************************************/
799 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
800 {
801     /* No picture previously */
802     p_vout->p_buffer[0].i_pic_x =         0;
803     p_vout->p_buffer[0].i_pic_y =         0;
804     p_vout->p_buffer[0].i_pic_width =     0;
805     p_vout->p_buffer[0].i_pic_height =    0;
806     p_vout->p_buffer[1].i_pic_x =         0;
807     p_vout->p_buffer[1].i_pic_y =         0;
808     p_vout->p_buffer[1].i_pic_width =     0;
809     p_vout->p_buffer[1].i_pic_height =    0;
810
811     /* The first area covers all the screen */
812     p_vout->p_buffer[0].i_areas =                 1;
813     p_vout->p_buffer[0].pi_area_begin[0] =        0;
814     p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
815     p_vout->p_buffer[1].i_areas =                 1;
816     p_vout->p_buffer[1].pi_area_begin[0] =        0;
817     p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;
818
819     /* Set adresses */
820     p_vout->p_buffer[0].p_data = p_buf1;
821     p_vout->p_buffer[1].p_data = p_buf2;
822 }
823
824 /*****************************************************************************
825  * vout_Pixel2RGB: return red, green and blue from pixel value
826  *****************************************************************************
827  * Return color values, in 0-255 range, of the decomposition of a pixel. This
828  * is a slow routine and should only be used for initialization phase.
829  *****************************************************************************/
830 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
831 {
832     *pi_red =   i_pixel & p_vout->i_red_mask;
833     *pi_green = i_pixel & p_vout->i_green_mask;
834     *pi_blue =  i_pixel & p_vout->i_blue_mask;
835 }
836
837 /* following functions are local */
838
839 /*****************************************************************************
840  * BinaryLog: computes the base 2 log of a binary value
841  *****************************************************************************
842  * This functions is used by MaskToShift, to get a bit index from a binary
843  * value.
844  *****************************************************************************/
845 static int BinaryLog(u32 i)
846 {
847     int i_log = 0;
848
849     if(i & 0xffff0000)
850     {
851         i_log += 16;
852     }
853     if(i & 0xff00ff00)
854     {
855         i_log += 8;
856     }
857     if(i & 0xf0f0f0f0)
858     {
859         i_log += 4;
860     }
861     if(i & 0xcccccccc)
862     {
863         i_log += 2;
864     }
865     if(i & 0xaaaaaaaa)
866     {
867         i_log += 1;
868     }
869
870     if (i != ((u32)1 << i_log))
871     {
872         intf_DbgMsg("internal error: binary log overflow");
873     }
874
875     return( i_log );
876 }
877
878 /*****************************************************************************
879  * MaskToShift: transform a color mask into right and left shifts
880  *****************************************************************************
881  * This function is used for obtaining color shifts from masks.
882  *****************************************************************************/
883 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
884 {
885     u32 i_low, i_high;                 /* lower hand higher bits of the mask */
886
887     /* Get bits */
888     i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
889     i_high = i_mask + i_low;                       /* higher bit of the mask */
890
891     /* Transform bits into an index */
892     i_low =  BinaryLog (i_low);
893     i_high = BinaryLog (i_high);
894
895     /* Update pointers and return */
896     *pi_left =   i_low;
897     *pi_right = (8 - i_high + i_low);
898 }
899
900 /*****************************************************************************
901  * InitThread: initialize video output thread
902  *****************************************************************************
903  * This function is called from RunThread and performs the second step of the
904  * initialization. It returns 0 on success. Note that the thread's flag are not
905  * modified inside this function.
906  *****************************************************************************/
907 static int InitThread( vout_thread_t *p_vout )
908 {
909     /* Update status */
910     *p_vout->pi_status = THREAD_START;
911
912     vlc_mutex_lock( &p_vout->change_lock );
913
914 #ifdef STATS
915     p_vout->c_loops = 0;
916 #endif
917
918    /* Initialize output method - this function issues its own error messages */
919     if( p_vout->pf_init( p_vout ) )
920     {
921         return( 1 );
922     }
923
924     /* Initialize convertion tables and functions */
925     if( vout_InitYUV( p_vout ) )
926     {
927         intf_ErrMsg("vout error: can't allocate YUV translation tables");
928         return( 1 );
929     }
930
931     /* Mark thread as running and return */
932     p_vout->b_active =          1;
933     *p_vout->pi_status =        THREAD_READY;
934
935     
936     intf_DbgMsg("thread ready");
937     return( 0 );
938 }
939
940 /*****************************************************************************
941  * RunThread: video output thread
942  *****************************************************************************
943  * Video output thread. This function does only returns when the thread is
944  * terminated. It handles the pictures arriving in the video heap and the
945  * display device events.
946  *****************************************************************************/
947 static void RunThread( vout_thread_t *p_vout)
948 {
949     int             i_index;                                /* index in heap */
950     mtime_t         current_date;                            /* current date */
951     mtime_t         display_date;                            /* display date */
952     boolean_t       b_display;                               /* display flag */
953     picture_t *     p_pic;                                /* picture pointer */
954     subpicture_t *  p_subpic;                          /* subpicture pointer */
955
956     /*
957      * Initialize thread
958      */
959     p_vout->b_error = InitThread( p_vout );
960     if( p_vout->b_error )
961     {
962         DestroyThread( p_vout, THREAD_ERROR );
963         return;
964     }
965
966     /*
967      * Main loop - it is not executed if an error occured during
968      * initialization
969      */
970     while( (!p_vout->b_die) && (!p_vout->b_error) )
971     {
972         /* Initialize loop variables */
973         p_pic =         NULL;
974         p_subpic =      NULL;
975         display_date =  0;
976         current_date =  mdate();
977 #ifdef STATS
978         p_vout->c_loops++;
979         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
980         {
981             intf_Msg("vout stats: picture heap: %d/%d",
982                      p_vout->i_pictures, VOUT_MAX_PICTURES);
983         }
984 #endif
985
986         /*
987          * Find the picture to display - this operation does not need lock,
988          * since only READY_PICTUREs are handled
989          */
990         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
991         {
992             if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
993             ( (p_pic == NULL) ||
994               (p_vout->p_picture[i_index].date < display_date) ) )
995             {
996                 p_pic = &p_vout->p_picture[i_index];
997                 display_date = p_pic->date;
998             }
999         }
1000
1001         if( p_pic )
1002         {
1003             /* Computes FPS rate */
1004             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1005
1006             if( display_date < current_date - p_vout->render_time )
1007             {
1008                 /* Picture is late: it will be destroyed and the thread
1009                  * will sleep and go to next picture */
1010
1011                 vlc_mutex_lock( &p_vout->picture_lock );
1012                 if( p_pic->i_refcount )
1013                 {
1014                     p_pic->i_status = DISPLAYED_PICTURE;
1015                 }
1016                 else
1017                 {
1018                     p_pic->i_status = DESTROYED_PICTURE;
1019                     p_vout->i_pictures--;
1020                 }
1021                 intf_WarnMsg( 3,
1022                         "warning: late picture skipped (%p)", p_pic );
1023                 vlc_mutex_unlock( &p_vout->picture_lock );
1024
1025                 continue;
1026             }
1027             else if( display_date > current_date + VOUT_DISPLAY_DELAY )
1028             {
1029                 /* A picture is ready to be rendered, but its rendering date
1030                  * is far from the current one so the thread will perform an
1031                  * empty loop as if no picture were found. The picture state
1032                  * is unchanged */
1033                 p_pic =         NULL;
1034                 display_date =  0;
1035             }
1036         }
1037         /*
1038          * Find the subpictures to display - this operation does not need
1039          * lock, since only READY_SUBPICTURE are handled. If no picture
1040          * has been selected, display_date will depend on the subpicture.
1041          * We get an easily parsable chained list of subpictures which
1042          * ends with NULL since p_subpic was initialized to NULL.
1043          */
1044         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1045         {
1046             if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1047             {
1048                 p_vout->p_subpicture[i_index].p_next = p_subpic;
1049                 p_subpic = &p_vout->p_subpicture[i_index];
1050             }
1051         }
1052
1053         /*
1054          * Perform rendering, sleep and display rendered picture
1055          */
1056         if( p_pic )                        /* picture and perhaps subpicture */
1057         {
1058             b_display = p_vout->b_active;
1059             p_vout->last_display_date = display_date;
1060             p_vout->p_rendered_pic = p_pic;
1061
1062
1063
1064
1065             /* Set picture dimensions and clear buffer */
1066             SetBufferPicture( p_vout, p_pic );
1067
1068             /* FIXME: if b_need_render == 0 we need to do something with
1069              * the subpictures one day. */
1070
1071             if( p_vout->b_need_render && b_display )
1072             {
1073                 /* Render picture and information */
1074                 RenderPicture( p_vout, p_pic );
1075                 if( p_vout->b_info )
1076                 {
1077                     RenderPictureInfo( p_vout, p_pic );
1078                     RenderInfo( p_vout );
1079                 }
1080                 if( p_subpic )
1081                 {
1082                     RenderSubPicture( p_vout, p_subpic );
1083                 }
1084             }
1085
1086             /* Render interface and subpicture */
1087             if( b_display && p_vout->b_interface && p_vout->b_need_render )
1088             {
1089                 RenderInterface( p_vout );
1090             }
1091
1092         }
1093         else if( p_vout->b_active && p_vout->b_need_render 
1094                   && p_vout->init_display_date == 0)
1095         {
1096             /* Idle or interface screen alone */
1097
1098             if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1099             {
1100                 /* Interface has changed, so a new rendering is required - force
1101                  * it by setting last idle date to 0 */
1102                 p_vout->last_idle_date = 0;
1103             }
1104
1105             /* Render idle screen and update idle date, then render interface if
1106              * required */
1107             b_display = RenderIdle( p_vout );
1108             if( b_display )
1109             {
1110                 p_vout->last_idle_date = current_date;
1111                 if( p_vout->b_interface )
1112                 {
1113                     RenderInterface( p_vout );
1114                 }
1115             }
1116
1117         }
1118         else
1119         {
1120             b_display = 0;
1121         }
1122
1123
1124         /*
1125          * Check for the current time and
1126          * display splash screen if everything is on time
1127          */
1128         if( p_vout->init_display_date > 0 && p_vout->b_need_render ) 
1129         {
1130             if( p_vout->b_active && 
1131                     mdate()-p_vout->init_display_date < 5000000)
1132             {
1133                 /* there is something to display ! */
1134                     b_display = 1;
1135                     RenderSplash( p_vout );
1136                 
1137             } else {
1138                 /* no splash screen ! */
1139                 p_vout->init_display_date=0;
1140             }
1141         }
1142             
1143
1144         /*
1145          * Sleep, wake up and display rendered picture
1146          */
1147
1148         if( display_date != 0 )
1149         {
1150             /* Store render time using Bresenham algorithm */
1151             p_vout->render_time += mdate() - current_date;
1152             p_vout->render_time >>= 1;
1153         }
1154
1155         /* Give back change lock */
1156         vlc_mutex_unlock( &p_vout->change_lock );
1157
1158         /* Sleep a while or until a given date */
1159         if( display_date != 0 )
1160         {
1161             mwait( display_date - VOUT_MWAIT_TOLERANCE );
1162         }
1163         else
1164         {
1165             msleep( VOUT_IDLE_SLEEP );
1166         }
1167
1168         /* On awakening, take back lock and send immediately picture to display,
1169          * then swap buffers */
1170         vlc_mutex_lock( &p_vout->change_lock );
1171 #ifdef DEBUG_VOUT
1172         intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d", p_pic, p_subpic,
1173                      p_vout->i_buffer_index, b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ );
1174 #endif
1175         if( b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ )
1176         {
1177             p_vout->pf_display( p_vout );
1178 #ifndef SYS_BEOS
1179             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1180 #endif
1181         }
1182
1183         if( p_pic )
1184         {
1185             /* Remove picture from heap */
1186             vlc_mutex_lock( &p_vout->picture_lock );
1187             if( p_pic->i_refcount )
1188             {
1189                 p_pic->i_status = DISPLAYED_PICTURE;
1190             }
1191             else
1192             {
1193                 p_pic->i_status = DESTROYED_PICTURE;
1194                 p_vout->i_pictures--;
1195             }
1196             vlc_mutex_unlock( &p_vout->picture_lock );
1197         }
1198
1199
1200         /*
1201          * Check events and manage thread
1202          */
1203         if( p_vout->pf_manage( p_vout ) | Manage( p_vout ) )
1204         {
1205             /* A fatal error occured, and the thread must terminate immediately,
1206              * without displaying anything - setting b_error to 1 cause the
1207              * immediate end of the main while() loop. */
1208             p_vout->b_error = 1;
1209         }
1210     }
1211
1212     /*
1213      * Error loop - wait until the thread destruction is requested
1214      */
1215     if( p_vout->b_error )
1216     {
1217         ErrorThread( p_vout );
1218     }
1219
1220     /* End of thread */
1221     EndThread( p_vout );
1222     DestroyThread( p_vout, THREAD_OVER );
1223     intf_DbgMsg( "thread end" );
1224 }
1225
1226 /*****************************************************************************
1227  * ErrorThread: RunThread() error loop
1228  *****************************************************************************
1229  * This function is called when an error occured during thread main's loop. The
1230  * thread can still receive feed, but must be ready to terminate as soon as
1231  * possible.
1232  *****************************************************************************/
1233 static void ErrorThread( vout_thread_t *p_vout )
1234 {
1235     /* Wait until a `die' order */
1236     while( !p_vout->b_die )
1237     {
1238         /* Sleep a while */
1239         msleep( VOUT_IDLE_SLEEP );
1240     }
1241 }
1242
1243 /*****************************************************************************
1244  * EndThread: thread destruction
1245  *****************************************************************************
1246  * This function is called when the thread ends after a sucessful
1247  * initialization. It frees all ressources allocated by InitThread.
1248  *****************************************************************************/
1249 static void EndThread( vout_thread_t *p_vout )
1250 {
1251     int     i_index;                                        /* index in heap */
1252
1253     /* Store status */
1254     *p_vout->pi_status = THREAD_END;
1255
1256 #ifdef STATS
1257     {
1258         struct tms cpu_usage;
1259         times( &cpu_usage );
1260
1261         intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
1262                   cpu_usage.tms_utime, cpu_usage.tms_stime );
1263     }
1264 #endif
1265
1266     /* Destroy all remaining pictures and subpictures */
1267     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1268     {
1269         if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1270         {
1271             free( p_vout->p_picture[i_index].p_data );
1272         }
1273     }
1274
1275     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1276     {
1277         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1278         {
1279             free( p_vout->p_subpicture[i_index].p_data );
1280         }
1281     }
1282
1283     /* Destroy translation tables */
1284     vout_EndYUV( p_vout );
1285     p_vout->pf_end( p_vout );
1286
1287     /* Release the change lock */
1288     vlc_mutex_unlock( &p_vout->change_lock );
1289 }
1290
1291 /*****************************************************************************
1292  * DestroyThread: thread destruction
1293  *****************************************************************************
1294  * This function is called when the thread ends. It frees all ressources
1295  * allocated by CreateThread. Status is available at this stage.
1296  *****************************************************************************/
1297 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1298 {
1299     int *pi_status;                                         /* status adress */
1300
1301     /* Store status adress */
1302     pi_status = p_vout->pi_status;
1303
1304     /* Destroy thread structures allocated by Create and InitThread */
1305     vout_UnloadFont( p_vout->p_default_font );
1306     vout_UnloadFont( p_vout->p_large_font );
1307     p_vout->pf_destroy( p_vout );
1308
1309     /* Destroy the locks */
1310     vlc_mutex_destroy( &p_vout->picture_lock );
1311     vlc_mutex_destroy( &p_vout->subpicture_lock );
1312     vlc_mutex_destroy( &p_vout->change_lock );
1313                 
1314     /* Release the module */
1315     module_Unneed( p_main->p_bank, p_vout->p_module );
1316
1317     /* Free structure */
1318     free( p_vout );
1319     *pi_status = i_status;
1320 }
1321
1322 /*****************************************************************************
1323  * Print: print simple text on a picture
1324  *****************************************************************************
1325  * This function will print a simple text on the picture. It is designed to
1326  * print debugging or general information.
1327  *****************************************************************************/
1328 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 )
1329 {
1330     int                 i_text_height;                  /* total text height */
1331     int                 i_text_width;                    /* total text width */
1332
1333     /* Update upper left coordinates according to alignment */
1334     vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1335     if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1336     {
1337         /* Set area and print text */
1338         SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1339         vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1340                     i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1341                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1342                     p_vout->i_white_pixel, 0, 0,
1343                     0, psz_text, 100 );
1344     }
1345 }
1346
1347 /*****************************************************************************
1348  * SetBufferArea: activate an area in current buffer
1349  *****************************************************************************
1350  * This function is called when something is rendered on the current buffer.
1351  * It set the area as active and prepare it to be cleared on next rendering.
1352  * Pay attention to the fact that in this functions, i_h is in fact the end y
1353  * coordinate of the new area.
1354  *****************************************************************************/
1355 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1356 {
1357     vout_buffer_t *     p_buffer;                          /* current buffer */
1358     int                 i_area_begin, i_area_end; /* area vertical extension */
1359     int                 i_area, i_area_copy;                   /* area index */
1360     int                 i_area_shift;            /* shift distance for areas */
1361
1362     /* Choose buffer and modify h to end of area position */
1363     p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];
1364     i_h +=      i_y - 1;
1365
1366     /*
1367      * Remove part of the area which is inside the picture - this is done
1368      * by calling again SetBufferArea with the correct areas dimensions.
1369      */
1370     if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1371     {
1372         i_area_begin =  p_buffer->i_pic_y;
1373         i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
1374
1375         if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1376             ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1377             ((i_y <  i_area_begin) && (i_h > i_area_end)) )
1378         {
1379             /* Keep the stripe above the picture, if any */
1380             if( i_y < i_area_begin )
1381             {
1382                 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1383             }
1384             /* Keep the stripe below the picture, if any */
1385             if( i_h > i_area_end )
1386             {
1387                 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1388             }
1389             return;
1390         }
1391     }
1392
1393     /* Skip some extensions until interesting areas */
1394     for( i_area = 0;
1395          (i_area < p_buffer->i_areas) &&
1396              (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1397          i_area++ )
1398     {
1399         ;
1400     }
1401
1402     if( i_area == p_buffer->i_areas )
1403     {
1404         /* New area is below all existing ones: just add it at the end of the
1405          * array, if possible - otherwise, append it to the last one */
1406         if( i_area < VOUT_MAX_AREAS )
1407         {
1408             p_buffer->pi_area_begin[i_area] = i_y;
1409             p_buffer->pi_area_end[i_area] = i_h;
1410             p_buffer->i_areas++;
1411         }
1412         else
1413         {
1414 #ifdef DEBUG_VOUT
1415             intf_DbgMsg("area overflow");
1416 #endif
1417             p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1418         }
1419     }
1420     else
1421     {
1422         i_area_begin =  p_buffer->pi_area_begin[i_area];
1423         i_area_end =    p_buffer->pi_area_end[i_area];
1424
1425         if( i_y < i_area_begin )
1426         {
1427             if( i_h >= i_area_begin - 1 )
1428             {
1429                 /* Extend area above */
1430                 p_buffer->pi_area_begin[i_area] = i_y;
1431             }
1432             else
1433             {
1434                 /* Create a new area above : merge last area if overflow, then
1435                  * move all old areas down */
1436                 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1437                 {
1438 #ifdef DEBUG_VOUT
1439                     intf_DbgMsg("areas overflow");
1440 #endif
1441                     p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1442                 }
1443                 else
1444                 {
1445                     p_buffer->i_areas++;
1446                 }
1447                 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1448                 {
1449                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1450                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
1451                 }
1452                 p_buffer->pi_area_begin[i_area] = i_y;
1453                 p_buffer->pi_area_end[i_area] = i_h;
1454                 return;
1455             }
1456         }
1457         if( i_h > i_area_end )
1458         {
1459             /* Find further areas which can be merged with the new one */
1460             for( i_area_copy = i_area + 1;
1461                  (i_area_copy < p_buffer->i_areas) &&
1462                      (p_buffer->pi_area_begin[i_area] <= i_h);
1463                  i_area_copy++ )
1464             {
1465                 ;
1466             }
1467             i_area_copy--;
1468
1469             if( i_area_copy != i_area )
1470             {
1471                 /* Merge with last possible areas */
1472                 //p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1473
1474                 /* Shift lower areas upward */
1475                 i_area_shift = i_area_copy - i_area;
1476                 p_buffer->i_areas -= i_area_shift;
1477                 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1478                 {
1479                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1480                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
1481                 }
1482             }
1483             else
1484             {
1485                 /* Extend area below */
1486                 p_buffer->pi_area_end[i_area] = i_h;
1487             }
1488         }
1489     }
1490 }
1491
1492 /*****************************************************************************
1493  * SetBufferPicture: clear buffer and set picture area
1494  *****************************************************************************
1495  * This function is called before any rendering. It clears the current
1496  * rendering buffer and set the new picture area. If the picture pointer is
1497  * NULL, then no picture area is defined. Floating operations are avoided since
1498  * some MMX calculations may follow.
1499  *****************************************************************************/
1500 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1501 {
1502     vout_buffer_t *     p_buffer;                          /* current buffer */
1503     int                 i_pic_x, i_pic_y;                /* picture position */
1504     int                 i_pic_width, i_pic_height;     /* picture dimensions */
1505     int                 i_old_pic_y, i_old_pic_height;   /* old picture area */
1506     int                 i_vout_width, i_vout_height;   /* display dimensions */
1507     int                 i_area;                                /* area index */
1508     int                 i_data_index;                     /* area data index */
1509     int                 i_data_size;   /* area data size, in 256 bytes blocs */
1510     u64 *               p_data;                   /* area data, for clearing */
1511     byte_t *            p_data8;           /* area data, for clearing (slow) */
1512
1513     /* Choose buffer and set display dimensions */
1514     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
1515     i_vout_width =      p_vout->i_width;
1516     i_vout_height =     p_vout->i_height;
1517
1518     /*
1519      * Computes new picture size
1520      */
1521     if( p_pic != NULL )
1522     {
1523         /* Try horizontal scaling first - width must be a mutiple of 16 */
1524         i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1525                        i_vout_width : p_pic->i_width) & ~0xf;
1526         switch( p_pic->i_aspect_ratio )
1527         {
1528         case AR_3_4_PICTURE:
1529             i_pic_height = i_pic_width * 3 / 4;
1530             break;
1531         case AR_16_9_PICTURE:
1532             i_pic_height = i_pic_width * 9 / 16;
1533             break;
1534         case AR_221_1_PICTURE:
1535             i_pic_height = i_pic_width * 100 / 221;
1536             break;
1537         case AR_SQUARE_PICTURE:
1538         default:
1539             i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1540             break;
1541         }
1542
1543         /* If picture dimensions using horizontal scaling are too large, use
1544          * vertical scaling. Since width must be a multiple of 16, height is
1545          * adjusted again after. */
1546         if( i_pic_height > i_vout_height )
1547         {
1548             i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1549                 i_vout_height : p_pic->i_height;
1550             switch( p_pic->i_aspect_ratio )
1551             {
1552             case AR_3_4_PICTURE:
1553                 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1554                 i_pic_height = i_pic_width * 3 / 4;
1555                 break;
1556             case AR_16_9_PICTURE:
1557                 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1558                 i_pic_height = i_pic_width * 9 / 16;
1559                 break;
1560             case AR_221_1_PICTURE:
1561                 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1562                 i_pic_height = i_pic_width * 100 / 221;
1563                 break;
1564             case AR_SQUARE_PICTURE:
1565             default:
1566                 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1567                 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1568                 break;
1569             }
1570         }
1571
1572         /* Set picture position */
1573         i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1574         i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1575
1576     }
1577     else
1578     {
1579         /* No picture: size is 0 */
1580         i_pic_x =       0;
1581         i_pic_y =       0;
1582         i_pic_width =   0;
1583         i_pic_height =  0;
1584     }
1585
1586     /*
1587      * Set new picture size - if it is smaller than the previous one, clear
1588      * around it. Since picture are centered, only their size is tested.
1589      */
1590     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1591     {
1592         i_old_pic_y =            p_buffer->i_pic_y;
1593         i_old_pic_height =       p_buffer->i_pic_height;
1594         p_buffer->i_pic_x =      i_pic_x;
1595         p_buffer->i_pic_y =      i_pic_y;
1596         p_buffer->i_pic_width =  i_pic_width;
1597         p_buffer->i_pic_height = i_pic_height;
1598         SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1599     }
1600     else
1601     {
1602         p_buffer->i_pic_x =      i_pic_x;
1603         p_buffer->i_pic_y =      i_pic_y;
1604         p_buffer->i_pic_width =  i_pic_width;
1605         p_buffer->i_pic_height = i_pic_height;
1606     }
1607
1608     /*
1609      * Clear areas
1610      */
1611     for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1612     {
1613 #ifdef DEBUG_VOUT
1614         intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d", p_pic,
1615                     p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1616 #endif
1617         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1618         p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
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 DEBUG_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 DEBUG_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_DbgMsg("error: unknown picture type %d", p_pic->i_type );
1710         break;
1711 #endif
1712     }
1713
1714 #ifdef DEBUG_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_DbgMsg( "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 DEBUG_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