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