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