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