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