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