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