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