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