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