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