]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
D�but du portage BeOS. Beaucoup de fuchiers ont �t� modifi� car il a fallu
[vlc] / src / video_output / video_output.c
1 /*****************************************************************************
2  * video_output.c : video output thread
3  * This module describes the programming interface for video output threads.
4  * It includes functions allowing to open a new thread, send pictures to a
5  * thread, and destroy a previously oppened video output thread.
6  *****************************************************************************
7  * Copyright (C) 2000 VideoLAN
8  *
9  * Authors:
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <string.h>                                            /* strerror() */
34
35 #include "threads.h"
36 #include "common.h"
37 #include "config.h"
38 #include "mtime.h"
39 #include "plugins.h"
40 #include "video.h"
41 #include "video_output.h"
42 #include "video_text.h"
43 #include "video_yuv.h"
44
45 #include "intf_msg.h"
46 #include "main.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int      BinaryLog         ( u32 i );
52 static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
53 static int      InitThread        ( vout_thread_t *p_vout );
54 static void     RunThread         ( vout_thread_t *p_vout );
55 static void     ErrorThread       ( vout_thread_t *p_vout );
56 static void     EndThread         ( vout_thread_t *p_vout );
57 static void     DestroyThread     ( vout_thread_t *p_vout, int i_status );
58 static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
59                                     int i_h_align, int i_v_align,
60                                     unsigned char *psz_text );
61 static void     SetBufferArea     ( vout_thread_t *p_vout, int i_x, int i_y,
62                                     int i_w, int i_h );
63 static void     SetBufferPicture  ( vout_thread_t *p_vout, picture_t *p_pic );
64 static void     RenderPicture     ( vout_thread_t *p_vout, picture_t *p_pic );
65 static void     RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
66 static void     RenderSubPicture  ( vout_thread_t *p_vout,
67                                     subpicture_t *p_subpic );
68 static void     RenderInterface   ( vout_thread_t *p_vout );
69 static int      RenderIdle        ( vout_thread_t *p_vout );
70 static void     RenderInfo        ( vout_thread_t *p_vout );
71 static void     Synchronize       ( vout_thread_t *p_vout, s64 i_delay );
72 static int      Manage            ( vout_thread_t *p_vout );
73 static int      Align             ( vout_thread_t *p_vout, int *pi_x,
74                                     int *pi_y, int i_width, int i_height,
75                                     int i_h_align, int i_v_align );
76 static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
77                                     u16 *green, u16 *blue, u16 *transp );
78
79 /*****************************************************************************
80  * vout_CreateThread: creates a new video output thread
81  *****************************************************************************
82  * This function creates a new video output thread, and returns a pointer
83  * to its description. On error, it returns NULL.
84  * If pi_status is NULL, then the function will block until the thread is ready.
85  * If not, it will be updated using one of the THREAD_* constants.
86  *****************************************************************************/
87 vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
88                           int i_width, int i_height, int *pi_status, int i_method )
89 {
90     vout_thread_t * p_vout;                             /* thread descriptor */
91     int             i_status;                               /* thread status */
92     int             i_index;               /* index for array initialization */
93     char *          psz_method;
94
95     /* Allocate descriptor */
96     intf_DbgMsg("\n");
97     p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
98     if( p_vout == NULL )
99     {
100         intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
101         return( NULL );
102     }
103
104     /* Request an interface plugin */
105     psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
106
107     if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
108     {
109         intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
110         free( p_vout );
111         return( NULL );
112     }
113
114     /* Get plugins */
115     p_vout->p_sys_create =  GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
116     p_vout->p_sys_init =    GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
117     p_vout->p_sys_end =     GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
118     p_vout->p_sys_destroy = GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
119     p_vout->p_sys_manage =  GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
120     p_vout->p_sys_display = GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
121
122     /* Initialize thread properties - thread id and locks will be initialized
123      * later */
124     p_vout->b_die               = 0;
125     p_vout->b_error             = 0;
126     p_vout->b_active            = 0;
127     p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
128     *p_vout->pi_status          = THREAD_CREATE;
129
130     /* Initialize some fields used by the system-dependant method - these fields will
131      * probably be modified by the method, and are only preferences */
132     p_vout->i_changes           = 0;
133     p_vout->i_width             = i_width;
134     p_vout->i_height            = i_height;
135     p_vout->i_bytes_per_line    = i_width * 2;
136     p_vout->i_screen_depth      = 15;
137     p_vout->i_bytes_per_pixel   = 2;
138     p_vout->f_gamma             = VOUT_GAMMA;
139
140     p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
141     p_vout->b_info              = 0;
142     p_vout->b_interface         = 0;
143     p_vout->b_scale             = 0;
144
145     p_vout->p_set_palette       = SetPalette;
146
147     intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
148                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
149                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
150
151     /* Initialize idle screen */
152     p_vout->last_display_date   = mdate();
153     p_vout->last_display_date   = 0;
154     p_vout->last_idle_date      = 0;
155
156 #ifdef STATS
157     /* Initialize statistics fields */
158     p_vout->render_time         = 0;
159     p_vout->c_fps_samples       = 0;
160 #endif
161
162     /* Initialize buffer index */
163     p_vout->i_buffer_index      = 0;
164
165     /* Initialize pictures and subpictures - translation tables and functions
166      * will be initialized later in InitThread */
167     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
168     {
169         p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
170         p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
171         p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
172         p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
173     }
174     p_vout->i_pictures = 0;
175
176     /* Initialize synchronization informations */
177     p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
178
179     /* Create and initialize system-dependant method - this function issues its
180      * own error messages */
181     if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
182     {
183         TrashPlugin( p_vout->vout_plugin );
184         free( p_vout );
185         return( NULL );
186     }
187     intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
188                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
189                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
190                 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
191
192     /* Calculate shifts from system-updated masks */
193     MaskToShift( &p_vout->i_red_lshift,   &p_vout->i_red_rshift,   p_vout->i_red_mask );
194     MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
195     MaskToShift( &p_vout->i_blue_lshift,  &p_vout->i_blue_rshift,  p_vout->i_blue_mask );
196
197     /* Set some useful colors */
198     p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
199     p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
200     p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
201     p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
202
203     /* Load fonts - fonts must be initialized after the system method since
204      * they may be dependant on screen depth and other thread properties */
205     p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
206     if( p_vout->p_default_font == NULL )
207     {
208         p_vout->p_sys_destroy( p_vout );
209         TrashPlugin( p_vout->vout_plugin );
210         free( p_vout );
211         return( NULL );
212     }
213     p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
214     if( p_vout->p_large_font == NULL )
215     {
216         vout_UnloadFont( p_vout->p_default_font );
217         p_vout->p_sys_destroy( p_vout );
218         TrashPlugin( p_vout->vout_plugin );
219         free( p_vout );
220         return( NULL );
221     }
222
223     /* Create thread and set locks */
224     vlc_mutex_init( &p_vout->picture_lock );
225     vlc_mutex_init( &p_vout->subpicture_lock );
226     vlc_mutex_init( &p_vout->change_lock );
227     vlc_mutex_lock( &p_vout->change_lock );
228     if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
229     {
230         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
231         vout_UnloadFont( p_vout->p_default_font );
232         vout_UnloadFont( p_vout->p_large_font );
233         p_vout->p_sys_destroy( p_vout );
234         TrashPlugin( p_vout->vout_plugin );
235         free( p_vout );
236         return( NULL );
237     }
238
239     intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
240              p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
241
242     /* If status is NULL, wait until the thread is created */
243     if( pi_status == NULL )
244     {
245         do
246         {
247             msleep( THREAD_SLEEP );
248         }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
249                 && (i_status != THREAD_FATAL) );
250         if( i_status != THREAD_READY )
251         {
252             return( NULL );
253         }
254     }
255     return( p_vout );
256 }
257
258 /*****************************************************************************
259  * vout_DestroyThread: destroys a previously created thread
260  *****************************************************************************
261  * Destroy a terminated thread.
262  * The function will request a destruction of the specified thread. If pi_error
263  * is NULL, it will return once the thread is destroyed. Else, it will be
264  * update using one of the THREAD_* constants.
265  *****************************************************************************/
266 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
267 {
268     int     i_status;                                       /* thread status */
269
270     /* Set status */
271     intf_DbgMsg("\n");
272     p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
273     *p_vout->pi_status = THREAD_DESTROY;
274
275     /* Request thread destruction */
276     p_vout->b_die = 1;
277
278     /* If status is NULL, wait until thread has been destroyed */
279     if( pi_status == NULL )
280     {
281         do
282         {
283             msleep( THREAD_SLEEP );
284         }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
285                 && (i_status != THREAD_FATAL) );
286     }
287 }
288
289 /*****************************************************************************
290  * vout_DisplaySubPicture: display a subpicture unit
291  *****************************************************************************
292  * Remove the reservation flag of an subpicture, which will cause it to be ready
293  * for display. The picture does not need to be locked, since it is ignored by
294  * the output thread if is reserved.
295  *****************************************************************************/
296 void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
297 {
298 #ifdef DEBUG_VIDEO
299     char        psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
300     char        psz_end_date[MSTRTIME_MAX_SIZE];   /* buffer for date string */
301 #endif
302
303 #ifdef DEBUG
304     /* Check if status is valid */
305     if( p_subpic->i_status != RESERVED_SUBPICTURE )
306     {
307         intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
308                     p_subpic->i_status );
309     }
310 #endif
311
312     /* Remove reservation flag */
313     p_subpic->i_status = READY_SUBPICTURE;
314
315 #ifdef DEBUG_VIDEO
316     /* Send subpicture informations */
317     intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
318                 p_subpic, p_subpic->i_type,
319                 mstrtime( psz_begin_date, p_subpic->begin_date ),
320                 mstrtime( psz_end_date, p_subpic->end_date ) );
321 #endif
322 }
323
324 /*****************************************************************************
325  * vout_CreateSubPicture: allocate an subpicture in the video output heap.
326  *****************************************************************************
327  * This function create a reserved subpicture in the video output heap.
328  * A null pointer is returned if the function fails. This method provides an
329  * already allocated zone of memory in the spu data fields. It needs locking
330  * since several pictures can be created by several producers threads.
331  *****************************************************************************/
332 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
333                                      int i_size )
334 {
335     int                 i_subpic;                        /* subpicture index */
336     subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
337     subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
338
339     /* Get lock */
340     vlc_mutex_lock( &p_vout->subpicture_lock );
341
342     /*
343      * Look for an empty place
344      */
345     for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
346     {
347         if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
348         {
349             /* Subpicture is marked for destruction, but is still allocated */
350             if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
351                 (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
352             {
353                 /* Memory size do match or is smaller : memory will not be reallocated,
354                  * and function can end immediately - this is the best possible case,
355                  * since no memory allocation needs to be done */
356                 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
357 #ifdef DEBUG_VIDEO
358                 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
359                             &p_vout->p_subpicture[i_subpic] );
360 #endif
361                 vlc_mutex_unlock( &p_vout->subpicture_lock );
362                 return( &p_vout->p_subpicture[i_subpic] );
363             }
364             else if( p_destroyed_subpic == NULL )
365             {
366                 /* Memory size do not match, but subpicture index will be kept in
367                  * case no other place are left */
368                 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
369             }
370         }
371         else if( (p_free_subpic == NULL) &&
372                  (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
373         {
374             /* Subpicture is empty and ready for allocation */
375             p_free_subpic = &p_vout->p_subpicture[i_subpic];
376         }
377     }
378
379     /* If no free subpicture is available, use a destroyed subpicture */
380     if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
381     {
382         /* No free subpicture or matching destroyed subpicture has been found, but
383          * a destroyed subpicture is still avalaible */
384         free( p_destroyed_subpic->p_data );
385         p_free_subpic = p_destroyed_subpic;
386     }
387
388     /*
389      * Prepare subpicture
390      */
391     if( p_free_subpic != NULL )
392     {
393         /* Allocate memory */
394         switch( i_type )
395         {
396         case TEXT_SUBPICTURE:                             /* text subpicture */
397             p_free_subpic->p_data = malloc( i_size + 1 );
398             break;
399 #ifdef DEBUG
400         default:
401             intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
402             p_free_subpic->p_data   =  NULL;
403             break;
404 #endif
405         }
406
407         if( p_free_subpic->p_data != NULL )
408         {                    /* Copy subpicture informations, set some default values */
409             p_free_subpic->i_type                      = i_type;
410             p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
411             p_free_subpic->i_size                      = i_size;
412             p_free_subpic->i_x                         = 0;
413             p_free_subpic->i_y                         = 0;
414             p_free_subpic->i_width                     = 0;
415             p_free_subpic->i_height                    = 0;
416             p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
417             p_free_subpic->i_vertical_align            = CENTER_RALIGN;
418         }
419         else
420         {
421             /* Memory allocation failed : set subpicture as empty */
422             p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
423             p_free_subpic->i_status =  FREE_SUBPICTURE;
424             p_free_subpic =            NULL;
425             intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
426         }
427
428 #ifdef DEBUG_VIDEO
429         intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
430 #endif
431         vlc_mutex_unlock( &p_vout->subpicture_lock );
432         return( p_free_subpic );
433     }
434
435     /* No free or destroyed subpicture could be found */
436     intf_DbgMsg( "warning: heap is full\n" );
437     vlc_mutex_unlock( &p_vout->subpicture_lock );
438     return( NULL );
439 }
440
441 /*****************************************************************************
442  * vout_DestroySubPicture: remove a subpicture from the heap
443  *****************************************************************************
444  * This function frees a previously reserved subpicture.
445  * It is meant to be used when the construction of a picture aborted.
446  * This function does not need locking since reserved subpictures are ignored
447  * by the output thread.
448  *****************************************************************************/
449 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
450 {
451 #ifdef DEBUG
452    /* Check if status is valid */
453    if( p_subpic->i_status != RESERVED_SUBPICTURE )
454    {
455        intf_DbgMsg("error: subpicture %p has invalid status %d\n",
456                    p_subpic, p_subpic->i_status );
457    }
458 #endif
459
460     p_subpic->i_status = DESTROYED_SUBPICTURE;
461
462 #ifdef DEBUG_VIDEO
463     intf_DbgMsg("subpicture %p\n", p_subpic);
464 #endif
465 }
466
467 /*****************************************************************************
468  * vout_DisplayPicture: display a picture
469  *****************************************************************************
470  * Remove the reservation flag of a picture, which will cause it to be ready for
471  * display. The picture won't be displayed until vout_DatePicture has been
472  * called.
473  *****************************************************************************/
474 void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
475 {
476     vlc_mutex_lock( &p_vout->picture_lock );
477     switch( p_pic->i_status )
478     {
479     case RESERVED_PICTURE:
480         p_pic->i_status = RESERVED_DISP_PICTURE;
481         break;
482     case RESERVED_DATED_PICTURE:
483         p_pic->i_status = READY_PICTURE;
484         break;
485 #ifdef DEBUG
486     default:
487         intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
488         break;
489 #endif
490     }
491
492 #ifdef DEBUG_VIDEO
493     intf_DbgMsg("picture %p\n", p_pic);
494 #endif
495     vlc_mutex_unlock( &p_vout->picture_lock );
496 }
497
498 /*****************************************************************************
499  * vout_DatePicture: date a picture
500  *****************************************************************************
501  * Remove the reservation flag of a picture, which will cause it to be ready for
502  * display. The picture won't be displayed until vout_DisplayPicture has been
503  * called.
504  *****************************************************************************/
505 void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
506 {
507 #ifdef DEBUG_VIDEO
508     char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
509 #endif
510
511     vlc_mutex_lock( &p_vout->picture_lock );
512     p_pic->date = date;
513     switch( p_pic->i_status )
514     {
515     case RESERVED_PICTURE:
516         p_pic->i_status = RESERVED_DATED_PICTURE;
517         break;
518     case RESERVED_DISP_PICTURE:
519         p_pic->i_status = READY_PICTURE;
520         break;
521 #ifdef DEBUG
522     default:
523         intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
524         break;
525 #endif
526     }
527
528 #ifdef DEBUG_VIDEO
529     intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
530 #endif
531     vlc_mutex_unlock( &p_vout->picture_lock );
532 }
533
534 /*****************************************************************************
535  * vout_CreatePicture: allocate a picture in the video output heap.
536  *****************************************************************************
537  * This function create a reserved image in the video output heap.
538  * A null pointer is returned if the function fails. This method provides an
539  * already allocated zone of memory in the picture data fields. It needs locking
540  * since several pictures can be created by several producers threads.
541  *****************************************************************************/
542 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
543                                int i_width, int i_height )
544 {
545     int         i_picture;                                  /* picture index */
546     int         i_chroma_width = 0;                          /* chroma width */
547     picture_t * p_free_picture = NULL;                 /* first free picture */
548     picture_t * p_destroyed_picture = NULL;       /* first destroyed picture */
549
550     /* Get lock */
551     vlc_mutex_lock( &p_vout->picture_lock );
552
553     /*
554      * Look for an empty place
555      */
556     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
557     {
558         if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
559         {
560             /* Picture is marked for destruction, but is still allocated - note
561              * that if width and type are the same for two pictures, chroma_width
562              * should also be the same */
563             if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
564                 (p_vout->p_picture[i_picture].i_height         == i_height) &&
565                 (p_vout->p_picture[i_picture].i_width          == i_width) )
566             {
567                 /* Memory size do match : memory will not be reallocated, and function
568                  * can end immediately - this is the best possible case, since no
569                  * memory allocation needs to be done */
570                 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
571                 p_vout->i_pictures++;
572 #ifdef DEBUG_VIDEO
573                 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
574                             &p_vout->p_picture[i_picture] );
575 #endif
576                 vlc_mutex_unlock( &p_vout->picture_lock );
577                 return( &p_vout->p_picture[i_picture] );
578             }
579             else if( p_destroyed_picture == NULL )
580             {
581                 /* Memory size do not match, but picture index will be kept in
582                  * case no other place are left */
583                 p_destroyed_picture = &p_vout->p_picture[i_picture];
584             }
585         }
586         else if( (p_free_picture == NULL) &&
587                  (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
588         {
589             /* Picture is empty and ready for allocation */
590             p_free_picture = &p_vout->p_picture[i_picture];
591         }
592     }
593
594     /* If no free picture is available, use a destroyed picture */
595     if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
596     {
597         /* No free picture or matching destroyed picture has been found, but
598          * a destroyed picture is still avalaible */
599         free( p_destroyed_picture->p_data );
600         p_free_picture = p_destroyed_picture;
601     }
602
603     /*
604      * Prepare picture
605      */
606     if( p_free_picture != NULL )
607     {
608         /* Allocate memory */
609         switch( i_type )
610         {
611         case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
612             i_chroma_width = i_width / 2;
613             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
614             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
615             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
616             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
617             break;
618         case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
619             i_chroma_width = i_width / 2;
620             p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
621             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
622             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
623             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
624             break;
625         case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
626             i_chroma_width = i_width;
627             p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
628             p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
629             p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
630             p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
631             break;
632 #ifdef DEBUG
633         default:
634             intf_DbgMsg("error: unknown picture type %d\n", i_type );
635             p_free_picture->p_data   =  NULL;
636             break;
637 #endif
638         }
639
640         if( p_free_picture->p_data != NULL )
641         {
642             /* Copy picture informations, set some default values */
643             p_free_picture->i_type                      = i_type;
644             p_free_picture->i_status                    = RESERVED_PICTURE;
645             p_free_picture->i_matrix_coefficients       = 1;
646             p_free_picture->i_width                     = i_width;
647             p_free_picture->i_height                    = i_height;
648             p_free_picture->i_chroma_width              = i_chroma_width;
649             p_free_picture->i_display_horizontal_offset = 0;
650             p_free_picture->i_display_vertical_offset   = 0;
651             p_free_picture->i_display_width             = i_width;
652             p_free_picture->i_display_height            = i_height;
653             p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
654             p_free_picture->i_refcount                  = 0;
655             p_vout->i_pictures++;
656         }
657         else
658         {
659             /* Memory allocation failed : set picture as empty */
660             p_free_picture->i_type   =  EMPTY_PICTURE;
661             p_free_picture->i_status =  FREE_PICTURE;
662             p_free_picture =            NULL;
663             intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
664         }
665
666 #ifdef DEBUG_VIDEO
667         intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
668 #endif
669         vlc_mutex_unlock( &p_vout->picture_lock );
670         return( p_free_picture );
671     }
672
673     /* No free or destroyed picture could be found */
674     intf_DbgMsg( "warning: heap is full\n" );
675     vlc_mutex_unlock( &p_vout->picture_lock );
676     return( NULL );
677 }
678
679 /*****************************************************************************
680  * vout_DestroyPicture: remove a permanent or reserved picture from the heap
681  *****************************************************************************
682  * This function frees a previously reserved picture or a permanent
683  * picture. It is meant to be used when the construction of a picture aborted.
684  * Note that the picture will be destroyed even if it is linked !
685  *****************************************************************************/
686 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
687 {
688    vlc_mutex_lock( &p_vout->picture_lock );
689
690 #ifdef DEBUG
691    /* Check if picture status is valid */
692    if( (p_pic->i_status != RESERVED_PICTURE) &&
693        (p_pic->i_status != RESERVED_DATED_PICTURE) &&
694        (p_pic->i_status != RESERVED_DISP_PICTURE) )
695    {
696        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
697    }
698 #endif
699
700    p_pic->i_status = DESTROYED_PICTURE;
701    p_vout->i_pictures--;
702
703 #ifdef DEBUG_VIDEO
704    intf_DbgMsg("picture %p\n", p_pic);
705 #endif
706    vlc_mutex_unlock( &p_vout->picture_lock );
707 }
708
709 /*****************************************************************************
710  * vout_LinkPicture: increment reference counter of a picture
711  *****************************************************************************
712  * This function increment the reference counter of a picture in the video
713  * heap. It needs a lock since several producer threads can access the picture.
714  *****************************************************************************/
715 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
716 {
717     vlc_mutex_lock( &p_vout->picture_lock );
718     p_pic->i_refcount++;
719
720 #ifdef DEBUG_VIDEO
721     intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
722 #endif
723
724     vlc_mutex_unlock( &p_vout->picture_lock );
725 }
726
727 /*****************************************************************************
728  * vout_UnlinkPicture: decrement reference counter of a picture
729  *****************************************************************************
730  * This function decrement the reference counter of a picture in the video heap.
731  *****************************************************************************/
732 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
733 {
734     vlc_mutex_lock( &p_vout->picture_lock );
735     p_pic->i_refcount--;
736
737 #ifdef DEBUG_VIDEO
738     if( p_pic->i_refcount < 0 )
739     {
740         intf_DbgMsg("error: refcount < 0\n");
741         p_pic->i_refcount = 0;
742     }
743 #endif
744
745     if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
746     {
747         p_pic->i_status = DESTROYED_PICTURE;
748         p_vout->i_pictures--;
749     }
750
751 #ifdef DEBUG_VIDEO
752     intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
753 #endif
754
755     vlc_mutex_unlock( &p_vout->picture_lock );
756 }
757
758 /*****************************************************************************
759  * vout_SetBuffers: set buffers adresses
760  *****************************************************************************
761  * This function is called by system drivers to set buffers video memory
762  * adresses.
763  *****************************************************************************/
764 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
765 {
766     /* No picture previously */
767     p_vout->p_buffer[0].i_pic_x =         0;
768     p_vout->p_buffer[0].i_pic_y =         0;
769     p_vout->p_buffer[0].i_pic_width =     0;
770     p_vout->p_buffer[0].i_pic_height =    0;
771     p_vout->p_buffer[1].i_pic_x =         0;
772     p_vout->p_buffer[1].i_pic_y =         0;
773     p_vout->p_buffer[1].i_pic_width =     0;
774     p_vout->p_buffer[1].i_pic_height =    0;
775
776     /* The first area covers all the screen */
777     p_vout->p_buffer[0].i_areas =                 1;
778     p_vout->p_buffer[0].pi_area_begin[0] =        0;
779     p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
780     p_vout->p_buffer[1].i_areas =                 1;
781     p_vout->p_buffer[1].pi_area_begin[0] =        0;
782     p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;
783
784     /* Set adresses */
785     p_vout->p_buffer[0].p_data = p_buf1;
786     p_vout->p_buffer[1].p_data = p_buf2;
787 }
788
789 /*****************************************************************************
790  * vout_Pixel2RGB: return red, green and blue from pixel value
791  *****************************************************************************
792  * Return color values, in 0-255 range, of the decomposition of a pixel. This
793  * is a slow routine and should only be used for initialization phase.
794  *****************************************************************************/
795 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
796 {
797     *pi_red =   i_pixel & p_vout->i_red_mask;
798     *pi_green = i_pixel & p_vout->i_green_mask;
799     *pi_blue =  i_pixel & p_vout->i_blue_mask;
800 }
801
802 /* following functions are local */
803
804 /*****************************************************************************
805  * BinaryLog: computes the base 2 log of a binary value
806  *****************************************************************************
807  * This functions is used by MaskToShift, to get a bit index from a binary
808  * value.
809  *****************************************************************************/
810 static int BinaryLog(u32 i)
811 {
812     int i_log;
813
814     i_log = 0;
815     if (i & 0xffff0000)
816     {
817         i_log = 16;
818     }
819     if (i & 0xff00ff00)
820     {
821         i_log += 8;
822     }
823     if (i & 0xf0f0f0f0)
824     {
825         i_log += 4;
826     }
827     if (i & 0xcccccccc)
828     {
829         i_log += 2;
830     }
831     if (i & 0xaaaaaaaa)
832     {
833         i_log++;
834     }
835     if (i != ((u32)1 << i_log))
836     {
837         intf_ErrMsg("internal error: binary log overflow\n");
838     }
839
840     return( i_log );
841 }
842
843 /*****************************************************************************
844  * MaskToShift: transform a color mask into right and left shifts
845  *****************************************************************************
846  * This function is used for obtaining color shifts from masks.
847  *****************************************************************************/
848 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
849 {
850     u32 i_low, i_high;                 /* lower hand higher bits of the mask */
851
852     /* Get bits */
853     i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
854     i_high = i_mask + i_low;                       /* higher bit of the mask */
855
856     /* Transform bits into an index */
857     i_low =  BinaryLog (i_low);
858     i_high = BinaryLog (i_high);
859
860     /* Update pointers and return */
861     *pi_left =   i_low;
862     *pi_right = (8 - i_high + i_low);
863 }
864
865 /*****************************************************************************
866  * InitThread: initialize video output thread
867  *****************************************************************************
868  * This function is called from RunThread and performs the second step of the
869  * initialization. It returns 0 on success. Note that the thread's flag are not
870  * modified inside this function.
871  *****************************************************************************/
872 static int InitThread( vout_thread_t *p_vout )
873 {
874     /* Update status */
875     intf_DbgMsg("\n");
876     *p_vout->pi_status = THREAD_START;
877
878    /* Initialize output method - this function issues its own error messages */
879     if( p_vout->p_sys_init( p_vout ) )
880     {
881         return( 1 );
882     }
883
884     /* Initialize convertion tables and functions */
885     if( vout_InitYUV( p_vout ) )
886     {
887         intf_ErrMsg("error: can't allocate YUV translation tables\n");
888         return( 1 );
889     }
890
891     /* Mark thread as running and return */
892     p_vout->b_active =          1;
893     *p_vout->pi_status =        THREAD_READY;
894     intf_DbgMsg("thread ready\n");
895     return( 0 );
896 }
897
898 /*****************************************************************************
899  * RunThread: video output thread
900  *****************************************************************************
901  * Video output thread. This function does only returns when the thread is
902  * terminated. It handles the pictures arriving in the video heap and the
903  * display device events.
904  *****************************************************************************/
905 static void RunThread( vout_thread_t *p_vout)
906 {
907     /* XXX?? welcome to gore land */
908     static int i_trash_count = 0;
909     static mtime_t last_display_date = 0;
910
911     int             i_index;                                /* index in heap */
912     mtime_t         current_date;                            /* current date */
913     mtime_t         display_date;                            /* display date */
914     boolean_t       b_display;                               /* display flag */
915     picture_t *     p_pic;                                /* picture pointer */
916     subpicture_t *  p_subpic;                          /* subpicture pointer */
917
918     /*
919      * Initialize thread
920      */
921     p_vout->b_error = InitThread( p_vout );
922     if( p_vout->b_error )
923     {
924         DestroyThread( p_vout, THREAD_ERROR );
925         return;
926     }
927     intf_DbgMsg("\n");
928
929     /*
930      * Main loop - it is not executed if an error occured during
931      * initialization
932      */
933     while( (!p_vout->b_die) && (!p_vout->b_error) )
934     {
935         /* Initialize loop variables */
936         p_pic =         NULL;
937         p_subpic =      NULL;
938         display_date =  0;
939         current_date =  mdate();
940
941         /*
942          * Find the picture to display - this operation does not need lock,
943          * since only READY_PICTUREs are handled
944          */
945         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
946         {
947             if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
948             ( (p_pic == NULL) ||
949               (p_vout->p_picture[i_index].date < display_date) ) )
950             {
951                 p_pic = &p_vout->p_picture[i_index];
952                 display_date = p_pic->date;
953             }
954         }
955
956         if( p_pic )
957         {
958 #ifdef STATS
959             /* Computes FPS rate */
960             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
961 #endif
962 /* XXX?? */
963 i_trash_count++;
964 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
965 last_display_date = display_date;
966 #if 1
967             if( display_date < current_date && i_trash_count > 4 )
968             {
969                 /* Picture is late: it will be destroyed and the thread will sleep and
970                  * go to next picture */
971
972                 vlc_mutex_lock( &p_vout->picture_lock );
973                 if( p_pic->i_refcount )
974                 {
975                     p_pic->i_status = DISPLAYED_PICTURE;
976                 }
977                 else
978                 {
979                     p_pic->i_status = DESTROYED_PICTURE;
980                     p_vout->i_pictures--;
981                 }
982                 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
983                 vlc_mutex_unlock( &p_vout->picture_lock );
984
985                 /* Update synchronization information as if display delay
986                  * was 0 */
987                 Synchronize( p_vout, display_date - current_date );
988
989                 p_pic =         NULL;
990                 display_date =  0;
991                 i_trash_count = 0;
992             }
993             else
994 #endif
995                 if( display_date > current_date + VOUT_DISPLAY_DELAY )
996             {
997                 /* A picture is ready to be rendered, but its rendering date is
998                  * far from the current one so the thread will perform an empty loop
999                  * as if no picture were found. The picture state is unchanged */
1000                 p_pic =         NULL;
1001                 display_date =  0;
1002             }
1003             else
1004             {
1005                 /* Picture will be displayed, update synchronization
1006                  * information */
1007                 Synchronize( p_vout, display_date - current_date );
1008             }
1009         }
1010         /*
1011          * Find the subpicture to display - this operation does not need lock, since
1012          * only READY_SUBPICTURES are handled. If no picture has been selected,
1013          * display_date will depend on the subpicture
1014          */
1015         /* XXX?? */
1016
1017         /*
1018          * Perform rendering, sleep and display rendered picture
1019          */
1020         if( p_pic )                        /* picture and perhaps subpicture */
1021         {
1022             b_display = p_vout->b_active;
1023             p_vout->last_display_date = display_date;
1024
1025             if( b_display )
1026             {
1027                 /* Set picture dimensions and clear buffer */
1028                 SetBufferPicture( p_vout, p_pic );
1029
1030                 /* Render picture and informations */
1031                 RenderPicture( p_vout, p_pic );
1032                 if( p_vout->b_info )
1033                 {
1034                     RenderPictureInfo( p_vout, p_pic );
1035                     RenderInfo( p_vout );
1036                 }
1037             }
1038
1039             /* Remove picture from heap */
1040             vlc_mutex_lock( &p_vout->picture_lock );
1041             if( p_pic->i_refcount )
1042             {
1043                 p_pic->i_status = DISPLAYED_PICTURE;
1044             }
1045             else
1046             {
1047                 p_pic->i_status = DESTROYED_PICTURE;
1048                 p_vout->i_pictures--;
1049             }
1050             vlc_mutex_unlock( &p_vout->picture_lock );
1051
1052             /* Render interface and subpicture */
1053             if( b_display && p_vout->b_interface )
1054             {
1055                 RenderInterface( p_vout );
1056             }
1057             if( p_subpic )
1058             {
1059                 if( b_display )
1060                 {
1061                     RenderSubPicture( p_vout, p_subpic );
1062                 }
1063
1064                 /* Remove subpicture from heap */
1065                 vlc_mutex_lock( &p_vout->subpicture_lock );
1066                 p_subpic->i_status = DESTROYED_SUBPICTURE;
1067                 vlc_mutex_unlock( &p_vout->subpicture_lock );
1068             }
1069
1070         }
1071         else if( p_subpic )                              /* subpicture alone */
1072         {
1073             b_display = p_vout->b_active;
1074             p_vout->last_display_date = display_date;
1075
1076             if( b_display )
1077             {
1078                 /* Clear buffer */
1079                 SetBufferPicture( p_vout, NULL );
1080
1081                 /* Render informations, interface and subpicture */
1082                 if( p_vout->b_info )
1083                 {
1084                     RenderInfo( p_vout );
1085                 }
1086                 if( p_vout->b_interface )
1087                 {
1088                     RenderInterface( p_vout );
1089                 }
1090                 RenderSubPicture( p_vout, p_subpic );
1091             }
1092
1093             /* Remove subpicture from heap */
1094             vlc_mutex_lock( &p_vout->subpicture_lock );
1095             p_subpic->i_status = DESTROYED_SUBPICTURE;
1096             vlc_mutex_unlock( &p_vout->subpicture_lock );
1097         }
1098         else if( p_vout->b_active )        /* idle or interface screen alone */
1099         {
1100             if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1101             {
1102                 /* Interface has changed, so a new rendering is required - force
1103                  * it by setting last idle date to 0 */
1104                 p_vout->last_idle_date = 0;
1105             }
1106
1107             /* Render idle screen and update idle date, then render interface if
1108              * required */
1109             b_display = RenderIdle( p_vout );
1110             if( b_display )
1111             {
1112                 p_vout->last_idle_date = current_date;
1113                 if( p_vout->b_interface )
1114                 {
1115                     RenderInterface( p_vout );
1116                 }
1117             }
1118
1119         }
1120         else
1121         {
1122             b_display = 0;
1123         }
1124
1125         /*
1126          * Sleep, wake up and display rendered picture
1127          */
1128
1129 #ifdef STATS
1130         /* Store render time */
1131         p_vout->render_time = mdate() - current_date;
1132 #endif
1133
1134         /* Give back change lock */
1135         vlc_mutex_unlock( &p_vout->change_lock );
1136
1137         /* Sleep a while or until a given date */
1138         if( display_date != 0 )
1139         {
1140             mwait( display_date );
1141         }
1142         else
1143         {
1144             msleep( VOUT_IDLE_SLEEP );
1145         }
1146
1147         /* On awakening, take back lock and send immediately picture to display,
1148          * then swap buffers */
1149         vlc_mutex_lock( &p_vout->change_lock );
1150 #ifdef DEBUG_VIDEO
1151         intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1152                      p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1153 #endif
1154         if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1155         {
1156             p_vout->p_sys_display( p_vout );
1157             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
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 sucessfull
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         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1226         {
1227             free( p_vout->p_subpicture[i_index].p_data );
1228         }
1229     }
1230
1231     /* Destroy translation tables */
1232     vout_EndYUV( p_vout );
1233     p_vout->p_sys_end( p_vout );
1234 }
1235
1236 /*****************************************************************************
1237  * DestroyThread: thread destruction
1238  *****************************************************************************
1239  * This function is called when the thread ends. It frees all ressources
1240  * allocated by CreateThread. Status is available at this stage.
1241  *****************************************************************************/
1242 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1243 {
1244     int *pi_status;                                         /* status adress */
1245
1246     /* Store status adress */
1247     intf_DbgMsg("\n");
1248     pi_status = p_vout->pi_status;
1249
1250     /* Destroy thread structures allocated by Create and InitThread */
1251     vout_UnloadFont( p_vout->p_default_font );
1252     vout_UnloadFont( p_vout->p_large_font );
1253     p_vout->p_sys_destroy( p_vout );
1254
1255     /* Close plugin */
1256     TrashPlugin( p_vout->vout_plugin );
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 is 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     switch( p_subpic->i_type )
1805     {
1806     case TEXT_SUBPICTURE:                                /* single line text */
1807         /* Select default font if not specified */
1808         p_font = p_subpic->type.text.p_font;
1809         if( p_font == NULL )
1810         {
1811             p_font = p_vout->p_default_font;
1812         }
1813
1814         /* Computes text size (width and height fields are ignored) and print it */
1815         vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1816         if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1817                     p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1818         {
1819             vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1820                         p_subpic->i_x * p_vout->i_bytes_per_pixel +
1821                         p_subpic->i_y * p_vout->i_bytes_per_line,
1822                         p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1823                         p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1824                         p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1825                         p_subpic->p_data );
1826             SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1827         }
1828         break;
1829
1830 #ifdef DEBUG
1831     default:
1832         intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1833 #endif
1834     }
1835 }
1836
1837 /*****************************************************************************
1838  * RenderInterface: render the interface
1839  *****************************************************************************
1840  * This function render the interface, if any.
1841  *****************************************************************************/
1842 static void RenderInterface( vout_thread_t *p_vout )
1843 {
1844     int         i_height, i_text_height;            /* total and text height */
1845     int         i_width_1, i_width_2;                          /* text width */
1846     int         i_byte;                                        /* byte index */
1847     const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
1848     const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";
1849
1850     /* Get text size */
1851     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1852     vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1853     i_height += i_text_height;
1854
1855     /* Render background */
1856     for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1857          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1858          i_byte++ )
1859     {
1860         /* XXX?? noooo ! */
1861         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1862     }
1863
1864     /* Render text, if not larger than screen */
1865     if( i_width_1 < p_vout->i_width )
1866     {
1867         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1868                     (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1869                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1870                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1871                     OUTLINED_TEXT, psz_text_1 );
1872     }
1873     if( i_width_2 < p_vout->i_width )
1874     {
1875         vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1876                     (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1877                     p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1878                     p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1879                     OUTLINED_TEXT, psz_text_2 );
1880     }
1881
1882     /* Activate modified area */
1883     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1884 }
1885
1886 /*****************************************************************************
1887  * Synchronize: update synchro level depending of heap state
1888  *****************************************************************************
1889  * This function is called during the main vout loop.
1890  *****************************************************************************/
1891 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1892 {
1893     int i_synchro_inc = 0;
1894     /* XXX?? gore following */
1895     static int i_panic_count = 0;
1896     static int i_last_synchro_inc = 0;
1897     static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1898     static int i_truc = 10;
1899
1900     if( i_delay < 0 )
1901     {
1902         //fprintf( stderr, "PANIC %d\n", i_panic_count );
1903         i_panic_count++;
1904     }
1905
1906     i_truc *= 2;
1907
1908     if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1909     {
1910         i_truc = 40;
1911         i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1912
1913     }
1914     else
1915     {
1916         if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1917         {
1918             i_truc = 32;
1919             i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1920         }
1921     }
1922
1923     if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1924         i_synchro_inc*i_last_synchro_inc < 0 )
1925     {
1926         i_truc = 32;
1927     }
1928
1929     if( i_delay < 6000 )
1930     {
1931         i_truc = 16;
1932         i_synchro_inc -= 2;
1933     }
1934     else if( i_delay < 70000 )
1935     {
1936         i_truc = 24+(24*i_delay)/70000;
1937         if( i_truc < 16 )
1938             i_truc = 16;
1939         i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1940     }
1941     else if( i_delay > 100000 )
1942     {
1943         r_synchro_level += 1;
1944         if( i_delay > 130000 )
1945             r_synchro_level += 1;
1946     }
1947
1948     r_synchro_level += (float)i_synchro_inc / i_truc;
1949     p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1950
1951     if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1952     {
1953         r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1954     }
1955
1956     //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1957     //        p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1958     i_last_synchro_inc = i_synchro_inc;
1959 }
1960
1961 /*****************************************************************************
1962  * Manage: manage thread
1963  *****************************************************************************
1964  * This function will handle changes in thread configuration.
1965  *****************************************************************************/
1966 static int Manage( vout_thread_t *p_vout )
1967 {
1968 #ifdef DEBUG_VIDEO
1969     if( p_vout->i_changes )
1970     {
1971         intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1972                     p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1973     }
1974 #endif
1975
1976     /* On gamma or grayscale change, rebuild tables */
1977     if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1978                              VOUT_YUV_CHANGE) )
1979     {
1980         if( vout_ResetYUV( p_vout ) )
1981         {
1982             intf_ErrMsg("error: can't rebuild convertion tables\n");
1983             return( 1 );
1984         }
1985     }
1986
1987     /* Clear changes flags which does not need management or have been
1988      * handled */
1989     p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1990                            VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE |
1991                            VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
1992
1993     /* Detect unauthorized changes */
1994     if( p_vout->i_changes )
1995     {
1996         /* Some changes were not acknowledged by p_vout->p_sys_manage or this
1997          * function, it means they should not be authorized */
1998         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1999         return( 1 );
2000     }
2001
2002     return( 0 );
2003 }
2004
2005 /*****************************************************************************
2006  * Align: align a subpicture in the screen
2007  *****************************************************************************
2008  * This function is used for rendering text or subpictures. It returns non 0
2009  * it the final aera is not fully included in display area. Return coordinates
2010  * are absolute.
2011  *****************************************************************************/
2012 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2013                    int i_width, int i_height, int i_h_align, int i_v_align )
2014 {
2015     /* Align horizontally */
2016     switch( i_h_align )
2017     {
2018     case CENTER_ALIGN:
2019         *pi_x -= i_width / 2;
2020         break;
2021     case CENTER_RALIGN:
2022         *pi_x += (p_vout->i_width - i_width) / 2;
2023         break;
2024     case RIGHT_ALIGN:
2025         *pi_x -= i_width;
2026         break;
2027     case RIGHT_RALIGN:
2028         *pi_x += p_vout->i_width - i_width;
2029         break;
2030     }
2031
2032     /* Align vertically */
2033     switch( i_v_align )
2034     {
2035     case CENTER_ALIGN:
2036         *pi_y -= i_height / 2;
2037         break;
2038     case CENTER_RALIGN:
2039         *pi_y += (p_vout->i_height - i_height) / 2;
2040         break;
2041     case BOTTOM_ALIGN:
2042         *pi_y -= i_height;
2043         break;
2044     case BOTTOM_RALIGN:
2045         *pi_y += p_vout->i_height - i_height;
2046         break;
2047     case SUBTITLE_RALIGN:
2048         *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2049                   p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2050         break;
2051     }
2052
2053     /* Return non 0 if clipping failed */
2054     return( (*pi_x < 0) || (*pi_y < 0) ||
2055             (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2056 }
2057
2058 /*****************************************************************************
2059  * SetPalette: sets an 8 bpp palette
2060  *****************************************************************************
2061  * This function is just a prototype that does nothing. Architectures that
2062  * support palette allocation should override it.
2063  *****************************************************************************/
2064 static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
2065                                     u16 *green, u16 *blue, u16 *transp )
2066 {
2067     intf_ErrMsg( "SetPalette: method does not support palette changing\n" );
2068 }
2069