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