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