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