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