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