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