]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
. corrections de fautes d'orthographe je ne sais plus trop o�
[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 #ifndef SYS_BEOS
1195             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1196 #endif
1197         }
1198
1199         /*
1200          * Check events and manage thread
1201          */
1202         if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1203         {
1204             /* A fatal error occured, and the thread must terminate immediately,
1205              * without displaying anything - setting b_error to 1 cause the
1206              * immediate end of the main while() loop. */
1207             p_vout->b_error = 1;
1208         }
1209     }
1210
1211     /*
1212      * Error loop - wait until the thread destruction is requested
1213      */
1214     if( p_vout->b_error )
1215     {
1216         ErrorThread( p_vout );
1217     }
1218
1219     /* End of thread */
1220     EndThread( p_vout );
1221     DestroyThread( p_vout, THREAD_OVER );
1222     intf_DbgMsg( "thread end\n" );
1223 }
1224
1225 /*****************************************************************************
1226  * ErrorThread: RunThread() error loop
1227  *****************************************************************************
1228  * This function is called when an error occured during thread main's loop. The
1229  * thread can still receive feed, but must be ready to terminate as soon as
1230  * possible.
1231  *****************************************************************************/
1232 static void ErrorThread( vout_thread_t *p_vout )
1233 {
1234     /* Wait until a `die' order */
1235     intf_DbgMsg("\n");
1236     while( !p_vout->b_die )
1237     {
1238         /* Sleep a while */
1239         msleep( VOUT_IDLE_SLEEP );
1240     }
1241 }
1242
1243 /*****************************************************************************
1244  * EndThread: thread destruction
1245  *****************************************************************************
1246  * This function is called when the thread ends after a sucessful
1247  * initialization. It frees all ressources allocated by InitThread.
1248  *****************************************************************************/
1249 static void EndThread( vout_thread_t *p_vout )
1250 {
1251     int     i_index;                                        /* index in heap */
1252
1253     /* Store status */
1254     intf_DbgMsg("\n");
1255     *p_vout->pi_status = THREAD_END;
1256
1257     /* Destroy all remaining pictures and subpictures */
1258     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1259     {
1260         if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1261         {
1262             free( p_vout->p_picture[i_index].p_data );
1263         }
1264     }
1265     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1266     {
1267         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1268         {
1269             free( p_vout->p_subpicture[i_index].p_data );
1270         }
1271     }
1272
1273     /* Destroy translation tables */
1274     vout_EndYUV( p_vout );
1275     p_vout->p_sys_end( p_vout );
1276 }
1277
1278 /*****************************************************************************
1279  * DestroyThread: thread destruction
1280  *****************************************************************************
1281  * This function is called when the thread ends. It frees all ressources
1282  * allocated by CreateThread. Status is available at this stage.
1283  *****************************************************************************/
1284 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1285 {
1286     int *pi_status;                                         /* status adress */
1287
1288     /* Store status adress */
1289     intf_DbgMsg("\n");
1290     pi_status = p_vout->pi_status;
1291
1292     /* Destroy thread structures allocated by Create and InitThread */
1293     vout_UnloadFont( p_vout->p_default_font );
1294     vout_UnloadFont( p_vout->p_large_font );
1295     p_vout->p_sys_destroy( p_vout );
1296
1297     /* Close plugin */
1298     TrashPlugin( p_vout->vout_plugin );
1299
1300     /* Free structure */
1301     free( p_vout );
1302     *pi_status = i_status;
1303 }
1304
1305 /*****************************************************************************
1306  * Print: print simple text on a picture
1307  *****************************************************************************
1308  * This function will print a simple text on the picture. It is designed to
1309  * print debugging or general informations.
1310  *****************************************************************************/
1311 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 )
1312 {
1313     int                 i_text_height;                  /* total text height */
1314     int                 i_text_width;                    /* total text width */
1315
1316     /* Update upper left coordinates according to alignment */
1317     vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1318     if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1319     {
1320         /* Set area and print text */
1321         SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1322         vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1323                     i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1324                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1325                     p_vout->i_white_pixel, 0, 0,
1326                     0, psz_text );
1327     }
1328 }
1329
1330 /*****************************************************************************
1331  * SetBufferArea: activate an area in current buffer
1332  *****************************************************************************
1333  * This function is called when something is rendered on the current buffer.
1334  * It set the area as active and prepare it to be cleared on next rendering.
1335  * Pay attention to the fact that in this functions, i_h is in fact the end y
1336  * coordinate of the new area.
1337  *****************************************************************************/
1338 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1339 {
1340     vout_buffer_t *     p_buffer;                          /* current buffer */
1341     int                 i_area_begin, i_area_end; /* area vertical extension */
1342     int                 i_area, i_area_copy;                   /* area index */
1343     int                 i_area_shift;            /* shift distance for areas */
1344
1345     /* Choose buffer and modify h to end of area position */
1346     p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];
1347     i_h +=      i_y - 1;
1348
1349     /*
1350      * Remove part of the area which is inside the picture - this is done
1351      * by calling again SetBufferArea with the correct areas dimensions.
1352      */
1353     if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1354     {
1355         i_area_begin =  p_buffer->i_pic_y;
1356         i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
1357
1358         if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1359             ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1360             ((i_y <  i_area_begin) && (i_h > i_area_end)) )
1361         {
1362             /* Keep the stripe above the picture, if any */
1363             if( i_y < i_area_begin )
1364             {
1365                 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1366             }
1367             /* Keep the stripe below the picture, if any */
1368             if( i_h > i_area_end )
1369             {
1370                 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1371             }
1372             return;
1373         }
1374     }
1375
1376     /* Skip some extensions until interesting areas */
1377     for( i_area = 0;
1378          (i_area < p_buffer->i_areas) &&
1379              (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1380          i_area++ )
1381     {
1382         ;
1383     }
1384
1385     if( i_area == p_buffer->i_areas )
1386     {
1387         /* New area is below all existing ones: just add it at the end of the
1388          * array, if possible - else, append it to the last one */
1389         if( i_area < VOUT_MAX_AREAS )
1390         {
1391             p_buffer->pi_area_begin[i_area] = i_y;
1392             p_buffer->pi_area_end[i_area] = i_h;
1393             p_buffer->i_areas++;
1394         }
1395         else
1396         {
1397 #ifdef DEBUG_VIDEO
1398             intf_DbgMsg("areas overflow\n");
1399 #endif
1400             p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1401         }
1402     }
1403     else
1404     {
1405         i_area_begin =  p_buffer->pi_area_begin[i_area];
1406         i_area_end =    p_buffer->pi_area_end[i_area];
1407
1408         if( i_y < i_area_begin )
1409         {
1410             if( i_h >= i_area_begin - 1 )
1411             {
1412                 /* Extend area above */
1413                 p_buffer->pi_area_begin[i_area] = i_y;
1414             }
1415             else
1416             {
1417                 /* Create a new area above : merge last area if overflow, then
1418                  * move all old areas down */
1419                 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1420                 {
1421 #ifdef DEBUG_VIDEO
1422                     intf_DbgMsg("areas overflow\n");
1423 #endif
1424                     p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1425                 }
1426                 else
1427                 {
1428                     p_buffer->i_areas++;
1429                 }
1430                 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1431                 {
1432                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1433                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
1434                 }
1435                 p_buffer->pi_area_begin[i_area] = i_y;
1436                 p_buffer->pi_area_end[i_area] = i_h;
1437                 return;
1438             }
1439         }
1440         if( i_h > i_area_end )
1441         {
1442             /* Find further areas which can be merged with the new one */
1443             for( i_area_copy = i_area + 1;
1444                  (i_area_copy < p_buffer->i_areas) &&
1445                      (p_buffer->pi_area_begin[i_area] <= i_h);
1446                  i_area_copy++ )
1447             {
1448                 ;
1449             }
1450             i_area_copy--;
1451
1452             if( i_area_copy != i_area )
1453             {
1454                 /* Merge with last possible areas */
1455                 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1456
1457                 /* Shift lower areas upward */
1458                 i_area_shift = i_area_copy - i_area;
1459                 p_buffer->i_areas -= i_area_shift;
1460                 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1461                 {
1462                     p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1463                     p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
1464                 }
1465             }
1466             else
1467             {
1468                 /* Extend area below */
1469                 p_buffer->pi_area_end[i_area] = i_h;
1470             }
1471         }
1472     }
1473 }
1474
1475 /*****************************************************************************
1476  * SetBufferPicture: clear buffer and set picture area
1477  *****************************************************************************
1478  * This function is called before any rendering. It clears the current
1479  * rendering buffer and set the new picture area. If the picture pointer is
1480  * NULL, then no picture area is defined. Floating operations are avoided since
1481  * some MMX calculations may follow.
1482  *****************************************************************************/
1483 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1484 {
1485     vout_buffer_t *     p_buffer;                          /* current buffer */
1486     int                 i_pic_x, i_pic_y;                /* picture position */
1487     int                 i_pic_width, i_pic_height;     /* picture dimensions */
1488     int                 i_old_pic_y, i_old_pic_height;   /* old picture area */
1489     int                 i_vout_width, i_vout_height;   /* display dimensions */
1490     int                 i_area;                                /* area index */
1491     int                 i_data_index;                     /* area data index */
1492     int                 i_data_size;   /* area data size, in 256 bytes blocs */
1493     u64 *               p_data;                   /* area data, for clearing */
1494     byte_t *            p_data8;           /* area data, for clearing (slow) */
1495
1496     /* Choose buffer and set display dimensions */
1497     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
1498     i_vout_width =      p_vout->i_width;
1499     i_vout_height =     p_vout->i_height;
1500
1501     /*
1502      * Computes new picture size
1503      */
1504     if( p_pic != NULL )
1505     {
1506         /* Try horizontal scaling first - width must be a mutiple of 16 */
1507         i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1508                        i_vout_width : p_pic->i_width) & ~0xf;
1509         switch( p_pic->i_aspect_ratio )
1510         {
1511         case AR_3_4_PICTURE:
1512             i_pic_height = i_pic_width * 3 / 4;
1513             break;
1514         case AR_16_9_PICTURE:
1515             i_pic_height = i_pic_width * 9 / 16;
1516             break;
1517         case AR_221_1_PICTURE:
1518             i_pic_height = i_pic_width * 100 / 221;
1519             break;
1520         case AR_SQUARE_PICTURE:
1521         default:
1522             i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1523             break;
1524         }
1525
1526         /* If picture dimensions using horizontal scaling are too large, use
1527          * vertical scaling. Since width must be a multiple of 16, height is
1528          * adjusted again after. */
1529         if( i_pic_height > i_vout_height )
1530         {
1531             i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1532                 i_vout_height : p_pic->i_height;
1533             switch( p_pic->i_aspect_ratio )
1534             {
1535             case AR_3_4_PICTURE:
1536                 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1537                 i_pic_height = i_pic_width * 3 / 4;
1538                 break;
1539             case AR_16_9_PICTURE:
1540                 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1541                 i_pic_height = i_pic_width * 9 / 16;
1542                 break;
1543             case AR_221_1_PICTURE:
1544                 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1545                 i_pic_height = i_pic_width * 100 / 221;
1546                 break;
1547             case AR_SQUARE_PICTURE:
1548             default:
1549                 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1550                 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1551                 break;
1552             }
1553         }
1554
1555         /* Set picture position */
1556         i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1557         i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1558     }
1559     else
1560     {
1561         /* No picture: size is 0 */
1562         i_pic_x =       0;
1563         i_pic_y =       0;
1564         i_pic_width =   0;
1565         i_pic_height =  0;
1566     }
1567
1568     /*
1569      * Set new picture size - if is is smaller than the previous one, clear
1570      * around it. Since picture are centered, only their size is tested.
1571      */
1572     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1573     {
1574         i_old_pic_y =            p_buffer->i_pic_y;
1575         i_old_pic_height =       p_buffer->i_pic_height;
1576         p_buffer->i_pic_x =      i_pic_x;
1577         p_buffer->i_pic_y =      i_pic_y;
1578         p_buffer->i_pic_width =  i_pic_width;
1579         p_buffer->i_pic_height = i_pic_height;
1580         SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1581     }
1582     else
1583     {
1584         p_buffer->i_pic_x =      i_pic_x;
1585         p_buffer->i_pic_y =      i_pic_y;
1586         p_buffer->i_pic_width =  i_pic_width;
1587         p_buffer->i_pic_height = i_pic_height;
1588     }
1589
1590     /*
1591      * Clear areas
1592      */
1593     for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1594     {
1595 #ifdef DEBUG_VIDEO
1596         intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1597                     p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1598 #endif
1599         i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1600         p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1601         for( i_data_index = i_data_size / 256; i_data_index-- ; )
1602         {
1603             /* Clear 256 bytes block */
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             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1611             *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
1612         }
1613         for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1614         {
1615             /* Clear remaining 16 bytes blocks */
1616             *p_data++ = 0;  *p_data++ = 0;
1617         }
1618         p_data8 = (byte_t *)p_data;
1619         for( i_data_index = i_data_size % 16; i_data_index--; )
1620         {
1621             /* Clear remaining bytes */
1622             *p_data8++ = 0;
1623         }
1624     }
1625
1626     /*
1627      * Clear areas array
1628      */
1629     p_buffer->i_areas = 0;
1630 }
1631
1632 /*****************************************************************************
1633  * RenderPicture: render a picture
1634  *****************************************************************************
1635  * This function convert a picture from a video heap to a pixel-encoded image
1636  * and copy it to the current rendering buffer. No lock is required, since the
1637  * rendered picture has been determined as existant, and will only be destroyed
1638  * by the vout thread later.
1639  *****************************************************************************/
1640 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1641 {
1642 #ifdef DEBUG_VIDEO
1643     char                psz_date[MSTRTIME_MAX_SIZE];         /* picture date */
1644     mtime_t             render_time;               /* picture rendering time */
1645 #endif
1646     vout_buffer_t *     p_buffer;                        /* rendering buffer */
1647     byte_t *            p_pic_data;                /* convertion destination */
1648
1649     /* Get and set rendering informations */
1650     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
1651     p_pic_data =        p_buffer->p_data +
1652                         p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1653                         p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1654 #ifdef DEBUG_VIDEO
1655     render_time = mdate();
1656 #endif
1657
1658     /*
1659      * Choose appropriate rendering function and render picture
1660      */
1661     switch( p_pic->i_type )
1662     {
1663     case YUV_420_PICTURE:
1664         p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1665                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1666                                   p_pic->i_width, p_pic->i_height,
1667                                   p_buffer->i_pic_width, p_buffer->i_pic_height,
1668                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1669                                   p_pic->i_matrix_coefficients );
1670         break;
1671     case YUV_422_PICTURE:
1672         p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1673                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1674                                   p_pic->i_width, p_pic->i_height,
1675                                   p_buffer->i_pic_width, p_buffer->i_pic_height,
1676                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1677                                   p_pic->i_matrix_coefficients );
1678         break;
1679     case YUV_444_PICTURE:
1680         p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1681                                   p_pic->p_y, p_pic->p_u, p_pic->p_v,
1682                                   p_pic->i_width, p_pic->i_height,
1683                                   p_buffer->i_pic_width, p_buffer->i_pic_height,
1684                                   p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1685                                   p_pic->i_matrix_coefficients );
1686         break;
1687 #ifdef DEBUG
1688     default:
1689         intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1690         break;
1691 #endif
1692     }
1693
1694 #ifdef DEBUG_VIDEO
1695     /* Print picture date and rendering time */
1696     intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1697                 p_vout->i_buffer_index, (long) (mdate() - render_time),
1698                 mstrtime( psz_date, p_pic->date ));
1699 #endif
1700 }
1701
1702 /*****************************************************************************
1703  * RenderPictureInfo: print additionnal informations on a picture
1704  *****************************************************************************
1705  * This function will print informations such as fps and other picture
1706  * dependant informations.
1707  *****************************************************************************/
1708 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1709 {
1710 #if defined(STATS) || defined(DEBUG)
1711     char        psz_buffer[256];                            /* string buffer */
1712 #endif
1713
1714 #ifdef STATS
1715     /*
1716      * Print FPS rate in upper right corner
1717      */
1718     if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1719     {
1720         sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1721                  ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1722                    p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1723         Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1724     }
1725
1726     /*
1727      * Print frames count and loop time in upper left corner
1728      */
1729     sprintf( psz_buffer, "%ld frames   rendering: %ld us",
1730              (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1731     Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1732 #endif
1733
1734 #ifdef DEBUG
1735     /*
1736      * Print picture information in lower right corner
1737      */
1738     sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1739              (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1740              ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1741               ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1742              p_pic->i_width, p_pic->i_height,
1743              p_pic->i_display_width, p_pic->i_display_height,
1744              p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1745              (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1746              ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1747               ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1748                ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1749              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1750              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1751              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1752              p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1753     Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1754 #endif
1755 }
1756
1757 /*****************************************************************************
1758  * RenderIdle: render idle picture
1759  *****************************************************************************
1760  * This function will print something on the screen. It will return 0 if
1761  * nothing has been rendered, or 1 if something has been changed on the screen.
1762  * Note that if you absolutely want something to be printed, you will have
1763  * to force it by setting the last idle date to 0.
1764  * Unlike other rendering functions, this one calls the SetBufferPicture
1765  * function when needed.
1766  *****************************************************************************/
1767 static int RenderIdle( vout_thread_t *p_vout )
1768 {
1769     int         i_x = 0, i_y = 0;                           /* text position */
1770     int         i_width, i_height;                              /* text size */
1771     mtime_t     current_date;                                /* current date */
1772     const char *psz_text = "waiting for stream ...";      /* text to display */
1773
1774
1775     current_date = mdate();
1776     if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1777         (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1778     {
1779         SetBufferPicture( p_vout, NULL );
1780         vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1781                        &i_width, &i_height );
1782         if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1783         {
1784             vout_Print( p_vout->p_large_font,
1785                         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1786                         i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1787                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1788                         p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1789                         WIDE_TEXT | OUTLINED_TEXT, psz_text );
1790             SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1791         }
1792         return( 1 );
1793     }
1794     return( 0 );
1795 }
1796
1797 /*****************************************************************************
1798  * RenderInfo: render additionnal informations
1799  *****************************************************************************
1800  * This function render informations which do not depend of the current picture
1801  * rendered.
1802  *****************************************************************************/
1803 static void RenderInfo( vout_thread_t *p_vout )
1804 {
1805 #ifdef DEBUG
1806     char        psz_buffer[256];                            /* string buffer */
1807     int         i_ready_pic = 0;                           /* ready pictures */
1808     int         i_reserved_pic = 0;                     /* reserved pictures */
1809     int         i_picture;                                  /* picture index */
1810 #endif
1811
1812 #ifdef DEBUG
1813     /*
1814      * Print thread state in lower left corner
1815      */
1816     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1817     {
1818         switch( p_vout->p_picture[i_picture].i_status )
1819         {
1820         case RESERVED_PICTURE:
1821         case RESERVED_DATED_PICTURE:
1822         case RESERVED_DISP_PICTURE:
1823             i_reserved_pic++;
1824             break;
1825         case READY_PICTURE:
1826             i_ready_pic++;
1827             break;
1828         }
1829     }
1830     sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1831              p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1832     Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1833 #endif
1834 }
1835
1836 /*****************************************************************************
1837  * RenderSubPicture: render a subpicture
1838  *****************************************************************************
1839  * This function render a sub picture unit.
1840  *****************************************************************************/
1841 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1842 {
1843     p_vout_font_t       p_font;                                 /* text font */
1844     int                 i_width, i_height;          /* subpicture dimensions */
1845
1846     while( p_subpic != NULL )
1847     {
1848         switch( p_subpic->i_type )
1849         {
1850         case DVD_SUBPICTURE:                          /* DVD subpicture unit */
1851             /* test if the picture really has to be displayed */
1852             if( mdate() < p_subpic->begin_date )
1853             {
1854                 break;
1855             }
1856             if( mdate() > p_subpic->end_date )
1857             {
1858                 /* too late, destroying the subpic */
1859                 vout_DestroySubPicture( p_vout, p_subpic );
1860                 printf( "destroying subpicture\n" );
1861                 break;
1862             }
1863             vout_RenderSPU( p_subpic->p_data, p_subpic->type.spu.i_offset,
1864                             p_subpic,
1865                             p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1866                             p_vout->i_bytes_per_pixel,
1867                             p_vout->i_bytes_per_line );
1868             break;
1869         case TEXT_SUBPICTURE:                            /* single line text */
1870             /* Select default font if not specified */
1871             p_font = p_subpic->type.text.p_font;
1872             if( p_font == NULL )
1873             {
1874                 p_font = p_vout->p_default_font;
1875             }
1876
1877             /* Compute text size (width and height fields are ignored)
1878              * and print it */
1879             vout_TextSize( p_font, p_subpic->type.text.i_style,
1880                            p_subpic->p_data, &i_width, &i_height );
1881             if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1882                         i_width, i_height, p_subpic->i_horizontal_align,
1883                         p_subpic->i_vertical_align ) )
1884             {
1885                 vout_Print( p_font,
1886                             p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1887                             p_subpic->i_x * p_vout->i_bytes_per_pixel +
1888                             p_subpic->i_y * p_vout->i_bytes_per_line,
1889                             p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1890                             p_subpic->type.text.i_char_color,
1891                             p_subpic->type.text.i_border_color,
1892                             p_subpic->type.text.i_bg_color,
1893                             p_subpic->type.text.i_style, p_subpic->p_data );
1894                 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1895                                i_width, i_height );
1896             }
1897             break;
1898
1899 #ifdef DEBUG
1900         default:
1901             intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1902                          p_subpic, p_subpic->i_type );
1903 #endif
1904         }
1905
1906         p_subpic = p_subpic->p_next;
1907     }
1908 }
1909
1910 /*****************************************************************************
1911  * RenderInterface: render the interface
1912  *****************************************************************************
1913  * This function render the interface, if any.
1914  *****************************************************************************/
1915 static void RenderInterface( vout_thread_t *p_vout )
1916 {
1917     int         i_height, i_text_height;            /* total and text height */
1918     int         i_width_1, i_width_2;                          /* text width */
1919     int         i_byte;                                        /* byte index */
1920     const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
1921     const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";
1922
1923     /* Get text size */
1924     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1925     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1926     i_height += i_text_height;
1927
1928     /* Render background */
1929     for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1930          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1931          i_byte++ )
1932     {
1933         /* XXX?? noooo ! */
1934         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1935     }
1936
1937     /* Render text, if not larger than screen */
1938     if( i_width_1 < p_vout->i_width )
1939     {
1940         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1941                     (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1942                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1943                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1944                     OUTLINED_TEXT, psz_text_1 );
1945     }
1946     if( i_width_2 < p_vout->i_width )
1947     {
1948         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1949                     (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1950                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1951                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1952                     OUTLINED_TEXT, psz_text_2 );
1953     }
1954
1955     /* Activate modified area */
1956     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1957 }
1958
1959 /*****************************************************************************
1960  * Synchronize: update synchro level depending of heap state
1961  *****************************************************************************
1962  * This function is called during the main vout loop.
1963  *****************************************************************************/
1964 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1965 {
1966     int i_synchro_inc = 0;
1967     /* XXX?? gore following */
1968     static int i_panic_count = 0;
1969     static int i_last_synchro_inc = 0;
1970     static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1971     static int i_truc = 10;
1972
1973     if( i_delay < 0 )
1974     {
1975         //fprintf( stderr, "PANIC %d\n", i_panic_count );
1976         i_panic_count++;
1977     }
1978
1979     i_truc *= 2;
1980
1981     if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1982     {
1983         i_truc = 40;
1984         i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1985
1986     }
1987     else
1988     {
1989         if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1990         {
1991             i_truc = 32;
1992             i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1993         }
1994     }
1995
1996     if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1997         i_synchro_inc*i_last_synchro_inc < 0 )
1998     {
1999         i_truc = 32;
2000     }
2001
2002     if( i_delay < 6000 )
2003     {
2004         i_truc = 16;
2005         i_synchro_inc -= 2;
2006     }
2007     else if( i_delay < 70000 )
2008     {
2009         i_truc = 24+(24*i_delay)/70000;
2010         if( i_truc < 16 )
2011             i_truc = 16;
2012         i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
2013     }
2014     else if( i_delay > 100000 )
2015     {
2016         r_synchro_level += 1;
2017         if( i_delay > 130000 )
2018             r_synchro_level += 1;
2019     }
2020
2021     r_synchro_level += (float)i_synchro_inc / i_truc;
2022     p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
2023
2024     if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
2025     {
2026         r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
2027     }
2028
2029     //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
2030     //        p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
2031     i_last_synchro_inc = i_synchro_inc;
2032 }
2033
2034 /*****************************************************************************
2035  * Manage: manage thread
2036  *****************************************************************************
2037  * This function will handle changes in thread configuration.
2038  *****************************************************************************/
2039 static int Manage( vout_thread_t *p_vout )
2040 {
2041 #ifdef DEBUG_VIDEO
2042     if( p_vout->i_changes )
2043     {
2044         intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2045                     p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
2046     }
2047 #endif
2048
2049     /* On gamma or grayscale change, rebuild tables */
2050     if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2051                              VOUT_YUV_CHANGE) )
2052     {
2053         if( vout_ResetYUV( p_vout ) )
2054         {
2055             intf_ErrMsg("error: can't rebuild convertion tables\n");
2056             return( 1 );
2057         }
2058     }
2059
2060     /* Clear changes flags which does not need management or have been
2061      * handled */
2062     p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2063                            VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE |
2064                            VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
2065
2066     /* Detect unauthorized changes */
2067     if( p_vout->i_changes )
2068     {
2069         /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2070          * function, it means they should not be authorized */
2071         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2072         return( 1 );
2073     }
2074
2075     return( 0 );
2076 }
2077
2078 /*****************************************************************************
2079  * Align: align a subpicture in the screen
2080  *****************************************************************************
2081  * This function is used for rendering text or subpictures. It returns non 0
2082  * it the final aera is not fully included in display area. Return coordinates
2083  * are absolute.
2084  *****************************************************************************/
2085 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2086                    int i_width, int i_height, int i_h_align, int i_v_align )
2087 {
2088     /* Align horizontally */
2089     switch( i_h_align )
2090     {
2091     case CENTER_ALIGN:
2092         *pi_x -= i_width / 2;
2093         break;
2094     case CENTER_RALIGN:
2095         *pi_x += (p_vout->i_width - i_width) / 2;
2096         break;
2097     case RIGHT_ALIGN:
2098         *pi_x -= i_width;
2099         break;
2100     case RIGHT_RALIGN:
2101         *pi_x += p_vout->i_width - i_width;
2102         break;
2103     }
2104
2105     /* Align vertically */
2106     switch( i_v_align )
2107     {
2108     case CENTER_ALIGN:
2109         *pi_y -= i_height / 2;
2110         break;
2111     case CENTER_RALIGN:
2112         *pi_y += (p_vout->i_height - i_height) / 2;
2113         break;
2114     case BOTTOM_ALIGN:
2115         *pi_y -= i_height;
2116         break;
2117     case BOTTOM_RALIGN:
2118         *pi_y += p_vout->i_height - i_height;
2119         break;
2120     case SUBTITLE_RALIGN:
2121         *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2122                   p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2123         break;
2124     }
2125
2126     /* Return non 0 if clipping failed */
2127     return( (*pi_x < 0) || (*pi_y < 0) ||
2128             (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2129 }
2130
2131 /*****************************************************************************
2132  * SetPalette: sets an 8 bpp palette
2133  *****************************************************************************
2134  * This function is just a prototype that does nothing. Architectures that
2135  * support palette allocation should override it.
2136  *****************************************************************************/
2137 static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
2138                                     u16 *green, u16 *blue, u16 *transp )
2139 {
2140     intf_ErrMsg( "SetPalette: method does not support palette changing\n" );
2141 }
2142