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