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