]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Added netlist support.
[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
1074
1075
1076             /* FIXME: if b_need_render == 0 we need to do something with
1077              * the subpictures one day. */
1078             if( p_vout->b_need_render && b_display )
1079             {
1080                 /* Set picture dimensions and clear buffer */
1081                 SetBufferPicture( p_vout, p_pic );
1082
1083                 /* Render picture and information */
1084                 RenderPicture( p_vout, p_pic );
1085                 if( p_vout->b_info )
1086                 {
1087                     RenderPictureInfo( p_vout, p_pic );
1088                     RenderInfo( p_vout );
1089                 }
1090                 if( p_subpic )
1091                 {
1092                     RenderSubPicture( p_vout, p_subpic );
1093                 }
1094             }
1095
1096             /* Render interface and subpicture */
1097             if( b_display && p_vout->b_interface && p_vout->b_need_render )
1098             {
1099                 RenderInterface( p_vout );
1100             }
1101
1102         }
1103         else if( p_vout->b_active && p_vout->b_need_render 
1104                   && p_vout->init_display_date == 0)
1105         {
1106             /* Idle or interface screen alone */
1107
1108             if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1109             {
1110                 /* Interface has changed, so a new rendering is required - force
1111                  * it by setting last idle date to 0 */
1112                 p_vout->last_idle_date = 0;
1113             }
1114
1115             /* Render idle screen and update idle date, then render interface if
1116              * required */
1117             b_display = RenderIdle( p_vout );
1118             if( b_display )
1119             {
1120                 p_vout->last_idle_date = current_date;
1121                 if( p_vout->b_interface )
1122                 {
1123                     RenderInterface( p_vout );
1124                 }
1125             }
1126
1127         }
1128         else
1129         {
1130             b_display = 0;
1131         }
1132
1133
1134         /*
1135          * Check for the current time and
1136          * display splash screen if everything is on time
1137          */
1138         if( p_vout->init_display_date > 0 && p_vout->b_need_render ) 
1139         {
1140             if( p_vout->b_active && 
1141                     mdate()-p_vout->init_display_date < 5000000)
1142             {
1143                 /* there is something to display ! */
1144                     b_display = 1;
1145                     RenderSplash( p_vout );
1146                 
1147             } else {
1148                 /* no splash screen ! */
1149                 intf_ErrMsgImm("End of splash screen\n");  
1150                 p_vout->init_display_date=0;
1151             }
1152         }
1153             
1154
1155         /*
1156          * Sleep, wake up and display rendered picture
1157          */
1158
1159         if( display_date != 0 )
1160         {
1161             /* Store render time using Bresenham algorithm */
1162             p_vout->render_time += mdate() - current_date;
1163             p_vout->render_time >>= 1;
1164         }
1165
1166         /* Give back change lock */
1167         vlc_mutex_unlock( &p_vout->change_lock );
1168
1169         /* Sleep a while or until a given date */
1170         if( display_date != 0 )
1171         {
1172             mwait( display_date - VOUT_MWAIT_TOLERANCE );
1173         }
1174         else
1175         {
1176             msleep( VOUT_IDLE_SLEEP );
1177         }
1178
1179         /* On awakening, take back lock and send immediately picture to display,
1180          * then swap buffers */
1181         vlc_mutex_lock( &p_vout->change_lock );
1182 #ifdef DEBUG_VOUT
1183         intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1184                      p_vout->i_buffer_index, b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ );
1185 #endif
1186         if( b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ )
1187         {
1188             p_vout->p_sys_display( p_vout );
1189 #ifndef SYS_BEOS
1190             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1191 #endif
1192         }
1193
1194         if( p_pic )
1195         {
1196             /* Remove picture from heap */
1197             vlc_mutex_lock( &p_vout->picture_lock );
1198             if( p_pic->i_refcount )
1199             {
1200                 p_pic->i_status = DISPLAYED_PICTURE;
1201             }
1202             else
1203             {
1204                 p_pic->i_status = DESTROYED_PICTURE;
1205                 p_vout->i_pictures--;
1206             }
1207             vlc_mutex_unlock( &p_vout->picture_lock );
1208         }
1209
1210
1211         /*
1212          * Check events and manage thread
1213          */
1214         if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1215         {
1216             /* A fatal error occured, and the thread must terminate immediately,
1217              * without displaying anything - setting b_error to 1 cause the
1218              * immediate end of the main while() loop. */
1219             p_vout->b_error = 1;
1220         }
1221     }
1222
1223     /*
1224      * Error loop - wait until the thread destruction is requested
1225      */
1226     if( p_vout->b_error )
1227     {
1228         ErrorThread( p_vout );
1229     }
1230
1231     /* End of thread */
1232     EndThread( p_vout );
1233     DestroyThread( p_vout, THREAD_OVER );
1234     intf_DbgMsg( "thread end\n" );
1235 }
1236
1237 /*****************************************************************************
1238  * ErrorThread: RunThread() error loop
1239  *****************************************************************************
1240  * This function is called when an error occured during thread main's loop. The
1241  * thread can still receive feed, but must be ready to terminate as soon as
1242  * possible.
1243  *****************************************************************************/
1244 static void ErrorThread( vout_thread_t *p_vout )
1245 {
1246     /* Wait until a `die' order */
1247     intf_DbgMsg("\n");
1248     while( !p_vout->b_die )
1249     {
1250         /* Sleep a while */
1251         msleep( VOUT_IDLE_SLEEP );
1252     }
1253 }
1254
1255 /*****************************************************************************
1256  * EndThread: thread destruction
1257  *****************************************************************************
1258  * This function is called when the thread ends after a sucessful
1259  * initialization. It frees all ressources allocated by InitThread.
1260  *****************************************************************************/
1261 static void EndThread( vout_thread_t *p_vout )
1262 {
1263     int     i_index;                                        /* index in heap */
1264
1265     /* Store status */
1266     intf_DbgMsg("\n");
1267     *p_vout->pi_status = THREAD_END;
1268
1269 #ifdef STATS
1270     {
1271         struct tms cpu_usage;
1272         times( &cpu_usage );
1273
1274         intf_Msg("vout stats: cpu usage (user: %d, system: %d)\n",
1275                  cpu_usage.tms_utime, cpu_usage.tms_stime);
1276     }
1277 #endif
1278
1279     /* Destroy all remaining pictures and subpictures */
1280     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1281     {
1282         if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1283         {
1284             free( p_vout->p_picture[i_index].p_data );
1285         }
1286     }
1287     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1288     {
1289         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1290         {
1291             free( p_vout->p_subpicture[i_index].p_data );
1292         }
1293     }
1294
1295     /* Destroy translation tables */
1296     vout_EndYUV( p_vout );
1297     p_vout->p_sys_end( p_vout );
1298 }
1299
1300 /*****************************************************************************
1301  * DestroyThread: thread destruction
1302  *****************************************************************************
1303  * This function is called when the thread ends. It frees all ressources
1304  * allocated by CreateThread. Status is available at this stage.
1305  *****************************************************************************/
1306 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1307 {
1308     int *pi_status;                                         /* status adress */
1309
1310     /* Store status adress */
1311     intf_DbgMsg("\n");
1312     pi_status = p_vout->pi_status;
1313
1314     /* Destroy thread structures allocated by Create and InitThread */
1315     vout_UnloadFont( p_vout->p_default_font );
1316     vout_UnloadFont( p_vout->p_large_font );
1317     p_vout->p_sys_destroy( p_vout );
1318
1319     /* Free structure */
1320     free( p_vout );
1321     *pi_status = i_status;
1322 }
1323
1324 /*****************************************************************************
1325  * Print: print simple text on a picture
1326  *****************************************************************************
1327  * This function will print a simple text on the picture. It is designed to
1328  * print debugging or general information.
1329  *****************************************************************************/
1330 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 )
1331 {
1332     int                 i_text_height;                  /* total text height */
1333     int                 i_text_width;                    /* total text width */
1334
1335     /* Update upper left coordinates according to alignment */
1336     vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1337     if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1338     {
1339         /* Set area and print text */
1340         SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1341         vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1342                     i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1343                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1344                     p_vout->i_white_pixel, 0, 0,
1345                     0, psz_text, 100 );
1346     }
1347 }
1348
1349 /*****************************************************************************
1350  * SetBufferArea: activate an area in current buffer
1351  *****************************************************************************
1352  * This function is called when something is rendered on the current buffer.
1353  * It set the area as active and prepare it to be cleared on next rendering.
1354  * Pay attention to the fact that in this functions, i_h is in fact the end y
1355  * coordinate of the new area.
1356  *****************************************************************************/
1357 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1358 {
1359     vout_buffer_t *     p_buffer;                          /* current buffer */
1360     int                 i_area_begin, i_area_end; /* area vertical extension */
1361     int                 i_area, i_area_copy;                   /* area index */
1362     int                 i_area_shift;            /* shift distance for areas */
1363
1364     /* Choose buffer and modify h to end of area position */
1365     p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];
1366     i_h +=      i_y - 1;
1367
1368     /*
1369      * Remove part of the area which is inside the picture - this is done
1370      * by calling again SetBufferArea with the correct areas dimensions.
1371      */
1372     if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1373     {
1374         i_area_begin =  p_buffer->i_pic_y;
1375         i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
1376
1377         if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1378             ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1379             ((i_y <  i_area_begin) && (i_h > i_area_end)) )
1380         {
1381             /* Keep the stripe above the picture, if any */
1382             if( i_y < i_area_begin )
1383             {
1384                 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1385             }
1386             /* Keep the stripe below the picture, if any */
1387             if( i_h > i_area_end )
1388             {
1389                 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1390             }
1391             return;
1392         }
1393     }
1394
1395     /* Skip some extensions until interesting areas */
1396     for( i_area = 0;
1397          (i_area < p_buffer->i_areas) &&
1398              (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1399          i_area++ )
1400     {
1401         ;
1402     }
1403
1404     if( i_area == p_buffer->i_areas )
1405     {
1406         /* New area is below all existing ones: just add it at the end of the
1407          * array, if possible - otherwise, append it to the last one */
1408         if( i_area < VOUT_MAX_AREAS )
1409         {
1410             p_buffer->pi_area_begin[i_area] = i_y;
1411             p_buffer->pi_area_end[i_area] = i_h;
1412             p_buffer->i_areas++;
1413         }
1414         else
1415         {
1416 #ifdef DEBUG_VOUT
1417             intf_DbgMsg("area overflow\n");
1418 #endif
1419             p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1420         }
1421     }
1422     else
1423     {
1424         i_area_begin =  p_buffer->pi_area_begin[i_area];
1425         i_area_end =    p_buffer->pi_area_end[i_area];
1426
1427         if( i_y < i_area_begin )
1428         {
1429             if( i_h >= i_area_begin - 1 )
1430             {
1431                 /* Extend area above */
1432                 p_buffer->pi_area_begin[i_area] = i_y;
1433             }
1434             else
1435             {
1436                 /* Create a new area above : merge last area if overflow, then
1437                  * move all old areas down */
1438                 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1439                 {
1440 #ifdef DEBUG_VOUT
1441                     intf_DbgMsg("areas overflow\n");
1442 #endif
1443                     p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1444                 }
1445                 else
1446                 {
1447                     p_buffer->i_areas++;
1448                 }
1449                 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1450                 {
1451                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1452                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
1453                 }
1454                 p_buffer->pi_area_begin[i_area] = i_y;
1455                 p_buffer->pi_area_end[i_area] = i_h;
1456                 return;
1457             }
1458         }
1459         if( i_h > i_area_end )
1460         {
1461             /* Find further areas which can be merged with the new one */
1462             for( i_area_copy = i_area + 1;
1463                  (i_area_copy < p_buffer->i_areas) &&
1464                      (p_buffer->pi_area_begin[i_area] <= i_h);
1465                  i_area_copy++ )
1466             {
1467                 ;
1468             }
1469             i_area_copy--;
1470
1471             if( i_area_copy != i_area )
1472             {
1473                 /* Merge with last possible areas */
1474                 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1475
1476                 /* Shift lower areas upward */
1477                 i_area_shift = i_area_copy - i_area;
1478                 p_buffer->i_areas -= i_area_shift;
1479                 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1480                 {
1481                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1482                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
1483                 }
1484             }
1485             else
1486             {
1487                 /* Extend area below */
1488                 p_buffer->pi_area_end[i_area] = i_h;
1489             }
1490         }
1491     }
1492 }
1493
1494 /*****************************************************************************
1495  * SetBufferPicture: clear buffer and set picture area
1496  *****************************************************************************
1497  * This function is called before any rendering. It clears the current
1498  * rendering buffer and set the new picture area. If the picture pointer is
1499  * NULL, then no picture area is defined. Floating operations are avoided since
1500  * some MMX calculations may follow.
1501  *****************************************************************************/
1502 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1503 {
1504     vout_buffer_t *     p_buffer;                          /* current buffer */
1505     int                 i_pic_x, i_pic_y;                /* picture position */
1506     int                 i_pic_width, i_pic_height;     /* picture dimensions */
1507     int                 i_old_pic_y, i_old_pic_height;   /* old picture area */
1508     int                 i_vout_width, i_vout_height;   /* display dimensions */
1509     int                 i_area;                                /* area index */
1510     int                 i_data_index;                     /* area data index */
1511     int                 i_data_size;   /* area data size, in 256 bytes blocs */
1512     u64 *               p_data;                   /* area data, for clearing */
1513     byte_t *            p_data8;           /* area data, for clearing (slow) */
1514
1515     /* Choose buffer and set display dimensions */
1516     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
1517     i_vout_width =      p_vout->i_width;
1518     i_vout_height =     p_vout->i_height;
1519
1520     /*
1521      * Computes new picture size
1522      */
1523     if( p_pic != NULL )
1524     {
1525         /* Try horizontal scaling first - width must be a mutiple of 16 */
1526         i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1527                        i_vout_width : p_pic->i_width) & ~0xf;
1528         switch( p_pic->i_aspect_ratio )
1529         {
1530         case AR_3_4_PICTURE:
1531             i_pic_height = i_pic_width * 3 / 4;
1532             break;
1533         case AR_16_9_PICTURE:
1534             i_pic_height = i_pic_width * 9 / 16;
1535             break;
1536         case AR_221_1_PICTURE:
1537             i_pic_height = i_pic_width * 100 / 221;
1538             break;
1539         case AR_SQUARE_PICTURE:
1540         default:
1541             i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1542             break;
1543         }
1544
1545         /* If picture dimensions using horizontal scaling are too large, use
1546          * vertical scaling. Since width must be a multiple of 16, height is
1547          * adjusted again after. */
1548         if( i_pic_height > i_vout_height )
1549         {
1550             i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1551                 i_vout_height : p_pic->i_height;
1552             switch( p_pic->i_aspect_ratio )
1553             {
1554             case AR_3_4_PICTURE:
1555                 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1556                 i_pic_height = i_pic_width * 3 / 4;
1557                 break;
1558             case AR_16_9_PICTURE:
1559                 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1560                 i_pic_height = i_pic_width * 9 / 16;
1561                 break;
1562             case AR_221_1_PICTURE:
1563                 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1564                 i_pic_height = i_pic_width * 100 / 221;
1565                 break;
1566             case AR_SQUARE_PICTURE:
1567             default:
1568                 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1569                 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1570                 break;
1571             }
1572         }
1573
1574         /* Set picture position */
1575         i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1576         i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1577
1578     }
1579     else
1580     {
1581         /* No picture: size is 0 */
1582         i_pic_x =       0;
1583         i_pic_y =       0;
1584         i_pic_width =   0;
1585         i_pic_height =  0;
1586     }
1587
1588     /*
1589      * Set new picture size - if it is smaller than the previous one, clear
1590      * around it. Since picture are centered, only their size is tested.
1591      */
1592     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1593     {
1594         i_old_pic_y =            p_buffer->i_pic_y;
1595         i_old_pic_height =       p_buffer->i_pic_height;
1596         p_buffer->i_pic_x =      i_pic_x;
1597         p_buffer->i_pic_y =      i_pic_y;
1598         p_buffer->i_pic_width =  i_pic_width;
1599         p_buffer->i_pic_height = i_pic_height;
1600         SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1601     }
1602     else
1603     {
1604         p_buffer->i_pic_x =      i_pic_x;
1605         p_buffer->i_pic_y =      i_pic_y;
1606         p_buffer->i_pic_width =  i_pic_width;
1607         p_buffer->i_pic_height = i_pic_height;
1608     }
1609
1610     /*
1611      * Clear areas
1612      */
1613     for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1614     {
1615 #ifdef DEBUG_VOUT
1616         intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1617                     p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1618 #endif
1619         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1620         p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1621         for( i_data_index = i_data_size / 256; i_data_index-- ; )
1622         {
1623             /* Clear 256 bytes block */
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             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1629             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1630             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1631             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1632         }
1633         for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1634         {
1635             /* Clear remaining 16 bytes blocks */
1636             *p_data++ = 0;  *p_data++ = 0;
1637         }
1638         p_data8 = (byte_t *)p_data;
1639         for( i_data_index = i_data_size % 16; i_data_index--; )
1640         {
1641             /* Clear remaining bytes */
1642             *p_data8++ = 0;
1643         }
1644     }
1645
1646     /*
1647      * Clear areas array
1648      */
1649     p_buffer->i_areas = 0;
1650 }
1651
1652 /*****************************************************************************
1653  * RenderPicture: render a picture
1654  *****************************************************************************
1655  * This function converts a picture from a video heap to a pixel-encoded image
1656  * and copies it to the current rendering buffer. No lock is required, since
1657  * the * rendered picture has been determined as existant, and will only be
1658  * destroyed by the vout thread later.
1659  *****************************************************************************/
1660 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1661 {
1662 #ifdef DEBUG_VOUT
1663     char                psz_date[MSTRTIME_MAX_SIZE];         /* picture date */
1664     mtime_t             render_time;               /* picture rendering time */
1665 #endif
1666     vout_buffer_t *     p_buffer;                        /* rendering buffer */
1667     byte_t *            p_pic_data;                /* convertion destination */
1668
1669     /* Get and set rendering information */
1670     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
1671     p_pic_data =        p_buffer->p_data +
1672                         p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1673                         p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1674 #ifdef DEBUG_VOUT
1675     render_time = mdate();
1676 #endif
1677  
1678
1679     
1680     /*
1681      * Choose appropriate rendering function and render picture
1682      */
1683     switch( p_pic->i_type )
1684     {
1685     case YUV_420_PICTURE:
1686         p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1687                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1688                                   p_pic->i_width, p_pic->i_height,
1689                                   p_buffer->i_pic_width, p_buffer->i_pic_height,
1690                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1691                                   p_pic->i_matrix_coefficients );
1692         break;
1693     case YUV_422_PICTURE:
1694         p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1695                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1696                                   p_pic->i_width, p_pic->i_height,
1697                                   p_buffer->i_pic_width, p_buffer->i_pic_height,
1698                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1699                                   p_pic->i_matrix_coefficients );
1700         break;
1701     case YUV_444_PICTURE:
1702         p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1703                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1704                                   p_pic->i_width, p_pic->i_height,
1705                                   p_buffer->i_pic_width, p_buffer->i_pic_height,
1706                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1707                                   p_pic->i_matrix_coefficients );
1708         break;
1709 #ifdef DEBUG
1710     default:
1711         intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1712         break;
1713 #endif
1714     }
1715
1716 #ifdef DEBUG_VOUT
1717     /* Print picture date and rendering time */
1718     intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1719                 p_vout->i_buffer_index, (long) (mdate() - render_time),
1720                 mstrtime( psz_date, p_pic->date ));
1721 #endif
1722 }
1723
1724 /*****************************************************************************
1725  * RenderPictureInfo: print additionnal information on a picture
1726  *****************************************************************************
1727  * This function will print information such as fps and other picture
1728  * dependant information.
1729  *****************************************************************************/
1730 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1731 {
1732     char        psz_buffer[256];                            /* string buffer */
1733
1734     /*
1735      * Print FPS rate in upper right corner
1736      */
1737     if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1738     {
1739         long i_fps = VOUT_FPS_SAMPLES * 1000000 * 10 /
1740                            ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1)
1741                                                    % VOUT_FPS_SAMPLES ] -
1742                              p_vout->p_fps_sample[ p_vout->c_fps_samples
1743                                                    % VOUT_FPS_SAMPLES ] );
1744         sprintf( psz_buffer, "%li.%i fps", i_fps / 10, (int)i_fps % 10 );
1745         Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1746     }
1747
1748     /*
1749      * Print frames count and loop time in upper left corner
1750      */
1751     sprintf( psz_buffer, "%ld frames, render: %ldus",
1752              (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1753     Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1754
1755 #ifdef STATS
1756     /*
1757      * Print picture information in lower right corner
1758      */
1759     sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1760              (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1761              ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1762               ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1763              p_pic->i_width, p_pic->i_height,
1764              p_pic->i_display_width, p_pic->i_display_height,
1765              p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1766              (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1767              ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1768               ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1769                ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1770              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1771              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1772              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1773              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1774     Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1775 #endif
1776 }
1777
1778 /*****************************************************************************
1779  * RenderSplash: render splash picture
1780  *****************************************************************************
1781  * This function will print something on the screen. It will return 0 if
1782  * nothing has been rendered, or 1 if something has been changed on the screen.
1783  * Note that if you absolutely want something to be printed, you will have
1784  * to force it by setting the last idle date to 0.
1785  * Unlike other rendering functions, this one calls the SetBufferPicture
1786  * function when needed.
1787  *****************************************************************************/
1788 int RenderSplash( vout_thread_t *p_vout )
1789 {
1790     int         i_x = 0, i_y = 0;                           /* text position */
1791     int         i_width, i_height;                              /* text size */
1792     char *psz_text =    "VideoLAN Client (" VERSION ")";  /* text to display */
1793
1794     memset( p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1795                   p_vout->i_bytes_per_line * p_vout->i_height, 12);
1796
1797  //   SetBufferPicture( p_vout, NULL );
1798     vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1799                    &i_width, &i_height );
1800     if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1801     {
1802         vout_Print( p_vout->p_large_font,
1803                     p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1804                     i_x * p_vout->i_bytes_per_pixel + (i_y - 16 ) * p_vout->i_bytes_per_line,
1805                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1806                     p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1807                     WIDE_TEXT | OUTLINED_TEXT, psz_text, 100);
1808         SetBufferArea( p_vout, i_x, i_y, i_width, i_height);
1809     }
1810     return( 1 );
1811 }
1812
1813
1814 /*****************************************************************************
1815  * RenderIdle: render idle picture
1816  *****************************************************************************
1817  * This function will print something on the screen. It will return 0 if
1818  * nothing has been rendered, or 1 if something has been changed on the screen.
1819  * Note that if you absolutely want something to be printed, you will have
1820  * to force it by setting the last idle date to 0.
1821  * Unlike other rendering functions, this one calls the SetBufferPicture
1822  * function when needed.
1823  *****************************************************************************/
1824 int RenderIdle( vout_thread_t *p_vout )
1825 {
1826     int         i_x = 0, i_y = 0;                           /* text position */
1827     int         i_width, i_height;                              /* text size */
1828     mtime_t     current_date;                                /* current date */
1829     int         i_amount = 0;                             /*  amount to draw */
1830     char *psz_text =    "Waiting for stream";            /* text to display */
1831     char *psz_wtext =   "[................]";
1832
1833
1834     memset( p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1835                     p_vout->i_bytes_per_line * p_vout->i_height, 12);
1836
1837
1838     current_date = mdate();
1839     if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY 
1840 //            && (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY 
1841     )
1842     {
1843         SetBufferPicture( p_vout, NULL );
1844         vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1845                        &i_width, &i_height );
1846         if( !Align( p_vout, &i_x, &i_y, i_width, i_height * 2, CENTER_RALIGN, CENTER_RALIGN ) )
1847         {
1848             i_amount = (int) ((current_date - p_vout->last_display_date ) / 5000LL);            
1849             vout_Print( p_vout->p_large_font,
1850                         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1851                         i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1852                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1853                         p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1854                         WIDE_TEXT | OUTLINED_TEXT, psz_text,  (i_amount / 3 ) %110);
1855
1856             vout_Print( p_vout->p_large_font,
1857                     p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1858                     i_x * p_vout->i_bytes_per_pixel + (i_y + 16) * p_vout->i_bytes_per_line,
1859                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1860                     p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1861                     WIDE_TEXT | OUTLINED_TEXT, psz_wtext,  (i_amount/5)%110 );
1862             
1863
1864             SetBufferArea( p_vout, i_x, i_y, i_width, i_height * 2 );
1865         }
1866         return( 1 );
1867     }
1868     return( 0 );
1869 }
1870
1871 /*****************************************************************************
1872  * RenderInfo: render additionnal information
1873  *****************************************************************************
1874  * This function renders information which do not depend on the current
1875  * picture rendered.
1876  *****************************************************************************/
1877 static void RenderInfo( vout_thread_t *p_vout )
1878 {
1879 #ifdef DEBUG
1880     char        psz_buffer[256];                            /* string buffer */
1881     int         i_ready_pic = 0;                           /* ready pictures */
1882     int         i_reserved_pic = 0;                     /* reserved pictures */
1883     int         i_picture;                                  /* picture index */
1884 #endif
1885
1886 #ifdef DEBUG
1887     /*
1888      * Print thread state in lower left corner
1889      */
1890     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1891     {
1892         switch( p_vout->p_picture[i_picture].i_status )
1893         {
1894         case RESERVED_PICTURE:
1895         case RESERVED_DATED_PICTURE:
1896         case RESERVED_DISP_PICTURE:
1897             i_reserved_pic++;
1898             break;
1899         case READY_PICTURE:
1900             i_ready_pic++;
1901             break;
1902         }
1903     }
1904     sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1905              p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1906     Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1907 #endif
1908 }
1909
1910 /*****************************************************************************
1911  * RenderSubPicture: render a subpicture
1912  *****************************************************************************
1913  * This function renders a sub picture unit.
1914  *****************************************************************************/
1915 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1916 {
1917     p_vout_font_t       p_font;                                 /* text font */
1918     int                 i_width, i_height;          /* subpicture dimensions */
1919
1920     while( p_subpic != NULL )
1921     {
1922         switch( p_subpic->i_type )
1923         {
1924         case DVD_SUBPICTURE:                          /* DVD subpicture unit */
1925             /* test if the picture really has to be displayed */
1926             if( mdate() < p_subpic->begin_date )
1927             {
1928                 /* not yet, see you later */
1929                 break;
1930             }
1931             if( mdate() > p_subpic->end_date )
1932             {
1933                 /* too late, destroying the subpic */
1934                 vout_DestroySubPicture( p_vout, p_subpic );
1935                 break;
1936             }
1937             vout_RenderSPU( &p_vout->p_buffer[ p_vout->i_buffer_index ],
1938                             p_subpic, p_vout->i_bytes_per_pixel,
1939                             p_vout->i_bytes_per_line );
1940             break;
1941         case TEXT_SUBPICTURE:                            /* single line text */
1942             /* Select default font if not specified */
1943             p_font = p_subpic->type.text.p_font;
1944             if( p_font == NULL )
1945             {
1946                 p_font = p_vout->p_default_font;
1947             }
1948
1949             /* Compute text size (width and height fields are ignored)
1950              * and print it */
1951             vout_TextSize( p_font, p_subpic->type.text.i_style,
1952                            p_subpic->p_data, &i_width, &i_height );
1953             if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1954                         i_width, i_height, p_subpic->i_horizontal_align,
1955                         p_subpic->i_vertical_align ) )
1956             {
1957                 vout_Print( p_font,
1958                             p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1959                             p_subpic->i_x * p_vout->i_bytes_per_pixel +
1960                             p_subpic->i_y * p_vout->i_bytes_per_line,
1961                             p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1962                             p_subpic->type.text.i_char_color,
1963                             p_subpic->type.text.i_border_color,
1964                             p_subpic->type.text.i_bg_color,
1965                             p_subpic->type.text.i_style, p_subpic->p_data, 100 );
1966                 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1967                                i_width, i_height );
1968             }
1969             break;
1970
1971 #ifdef DEBUG
1972         default:
1973             intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1974                          p_subpic, p_subpic->i_type );
1975 #endif
1976         }
1977
1978         p_subpic = p_subpic->p_next;
1979     }
1980 }
1981
1982 /*****************************************************************************
1983  * RenderInterface: render the interface
1984  *****************************************************************************
1985  * This function renders the interface, if any.
1986  *****************************************************************************/
1987 static void RenderInterface( vout_thread_t *p_vout )
1988 {
1989     int         i_height, i_text_height;            /* total and text height */
1990     int         i_width_1, i_width_2;                          /* text width */
1991     int         i_byte;                                        /* byte index */
1992     const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
1993     const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";
1994
1995     /* Get text size */
1996     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1997     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1998     i_height += i_text_height;
1999
2000     /* Render background */
2001     for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
2002          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
2003          i_byte++ )
2004     {
2005         /* XXX?? noooo ! */
2006         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
2007     }
2008
2009     /* Render text, if not larger than screen */
2010     if( i_width_1 < p_vout->i_width )
2011     {
2012         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
2013                     (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
2014                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
2015                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
2016                     OUTLINED_TEXT, psz_text_1, 100 );
2017     }
2018     if( i_width_2 < p_vout->i_width )
2019     {
2020         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
2021                     (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
2022                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
2023                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
2024                     OUTLINED_TEXT, psz_text_2, 100 );
2025     }
2026
2027     /* Activate modified area */
2028     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
2029 }
2030
2031 /*****************************************************************************
2032  * Manage: manage thread
2033  *****************************************************************************
2034  * This function will handle changes in thread configuration.
2035  *****************************************************************************/
2036 static int Manage( vout_thread_t *p_vout )
2037 {
2038 #ifdef DEBUG_VOUT
2039     if( p_vout->i_changes )
2040     {
2041         intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2042                     0 /* p_vout->i_changes & VOUT_NODISPLAY_CHANGE */ );
2043     }
2044 #endif
2045
2046     /* On gamma or grayscale change, rebuild tables */
2047     if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2048                              VOUT_YUV_CHANGE) )
2049     {
2050         /* Change vdec_DecodeMacroblock when switching between BW and C */
2051         if( !p_vout->b_grayscale ) 
2052         {
2053             p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockC;
2054         }
2055         else if( p_vout->b_grayscale)
2056         {
2057             p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockBW;
2058         }  
2059         if( vout_ResetYUV( p_vout ) )
2060         {
2061             intf_ErrMsg("error: can't rebuild convertion tables\n");
2062             return( 1 );
2063         }
2064     }
2065
2066     /* Clear changes flags which does not need management or have been
2067      * handled */
2068     p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2069                            VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE |
2070                            VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
2071
2072     /* Detect unauthorized changes */
2073     if( p_vout->i_changes )
2074     {
2075         /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2076          * function, it means they should not be authorized */
2077         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2078         return( 1 );
2079     }
2080
2081     return( 0 );
2082 }
2083
2084 /*****************************************************************************
2085  * Align: align a subpicture in the screen
2086  *****************************************************************************
2087  * This function is used for rendering text or subpictures. It returns non 0
2088  * it the final aera is not fully included in display area. Return coordinates
2089  * are absolute.
2090  *****************************************************************************/
2091 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2092                    int i_width, int i_height, int i_h_align, int i_v_align )
2093 {
2094     /* Align horizontally */
2095     switch( i_h_align )
2096     {
2097     case CENTER_ALIGN:
2098         *pi_x -= i_width / 2;
2099         break;
2100     case CENTER_RALIGN:
2101         *pi_x += (p_vout->i_width - i_width) / 2;
2102         break;
2103     case RIGHT_ALIGN:
2104         *pi_x -= i_width;
2105         break;
2106     case RIGHT_RALIGN:
2107         *pi_x += p_vout->i_width - i_width;
2108         break;
2109     }
2110
2111     /* Align vertically */
2112     switch( i_v_align )
2113     {
2114     case CENTER_ALIGN:
2115         *pi_y -= i_height / 2;
2116         break;
2117     case CENTER_RALIGN:
2118         *pi_y += (p_vout->i_height - i_height) / 2;
2119         break;
2120     case BOTTOM_ALIGN:
2121         *pi_y -= i_height;
2122         break;
2123     case BOTTOM_RALIGN:
2124         *pi_y += p_vout->i_height - i_height;
2125         break;
2126     case SUBTITLE_RALIGN:
2127         *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2128                   p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2129         break;
2130     }
2131
2132     /* Return non 0 if clipping failed */
2133     return( (*pi_x < 0) || (*pi_y < 0) ||
2134             (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2135 }
2136
2137 /*****************************************************************************
2138  * SetPalette: sets an 8 bpp palette
2139  *****************************************************************************
2140  * This function is just a prototype that does nothing. Architectures that
2141  * support palette allocation should override it.
2142  *****************************************************************************/
2143 static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
2144                                     u16 *green, u16 *blue, u16 *transp )
2145 {
2146     intf_ErrMsg( "SetPalette: method does not support palette changing\n" );
2147 }
2148