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