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