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