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