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