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