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