]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
* ./po/*: polish translation, courtesy of Arkadiusz Lipiec.
[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-2001 VideoLAN
8  * $Id: video_output.c,v 1.186 2002/07/02 22:07:02 jlj Exp $
9  *
10  * Authors: Vincent Seguin <seguin@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <string.h>                                            /* strerror() */
34
35 #include <vlc/vlc.h>
36
37 #ifdef HAVE_SYS_TIMES_H
38 #   include <sys/times.h>
39 #endif
40
41 #include "video.h"
42 #include "video_output.h"
43
44 #if defined( SYS_DARWIN )
45 #include "darwin_specific.h"
46 #endif
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int      InitThread        ( vout_thread_t * );
52 static void     RunThread         ( vout_thread_t * );
53 static void     ErrorThread       ( vout_thread_t * );
54 static void     EndThread         ( vout_thread_t * );
55 static void     DestroyThread     ( vout_thread_t * );
56
57 static int      ReduceHeight      ( int );
58 static int      BinaryLog         ( u32 );
59 static void     MaskToShift       ( int *, int *, u32 );
60 static void     InitWindowSize    ( vout_thread_t *, int *, int * );
61
62 /*****************************************************************************
63  * vout_CreateThread: creates a new video output thread
64  *****************************************************************************
65  * This function creates a new video output thread, and returns a pointer
66  * to its description. On error, it returns NULL.
67  *****************************************************************************/
68 vout_thread_t * __vout_CreateThread ( vlc_object_t *p_parent,
69                                       int i_width, int i_height,
70                                       u32 i_chroma, int i_aspect )
71 {
72     vout_thread_t * p_vout;                             /* thread descriptor */
73     int             i_index;                                /* loop variable */
74     char          * psz_plugin;
75
76     /* Allocate descriptor */
77     p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
78     if( p_vout == NULL )
79     {
80         msg_Err( p_parent, "out of memory" );
81         return( NULL );
82     }
83
84     /* If the parent is not a VOUT object, that means we are at the start of
85      * the video output pipe */
86     if( p_parent->i_object_type != VLC_OBJECT_VOUT )
87     {
88         /* look for the default filter configuration */
89         p_vout->psz_filter_chain = config_GetPsz( p_parent, "filter" );
90     }
91     else
92     {
93         /* continue the parent's filter chain */
94         char *psz_end;
95
96         psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
97         if( psz_end && *(psz_end+1) )
98             p_vout->psz_filter_chain = strdup( psz_end+1 );
99         else p_vout->psz_filter_chain = NULL;
100     }
101
102     /* Choose the video output module */
103     if( !p_vout->psz_filter_chain )
104     {
105         psz_plugin = config_GetPsz( p_parent, "vout" );
106     }
107     else
108     {
109         /* the filter chain is a string list of filters separated by double
110          * colons */
111         char *psz_end;
112
113         psz_end = strchr( p_vout->psz_filter_chain, ':' );
114         if( psz_end )
115             psz_plugin = strndup( p_vout->psz_filter_chain,
116                                   psz_end - p_vout->psz_filter_chain );
117         else psz_plugin = strdup( p_vout->psz_filter_chain );
118     }
119
120     /* Initialize pictures and subpictures - translation tables and functions
121      * will be initialized later in InitThread */
122     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
123     {
124         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
125         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
126     }
127
128     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
129     {
130         p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
131         p_vout->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
132     }
133
134     /* No images in the heap */
135     p_vout->i_heap_size = 0;
136
137     /* Initialize the rendering heap */
138     I_RENDERPICTURES = 0;
139     p_vout->render.i_width    = i_width;
140     p_vout->render.i_height   = i_height;
141     p_vout->render.i_chroma   = i_chroma;
142     p_vout->render.i_aspect   = i_aspect;
143
144     p_vout->render.i_rmask    = 0;
145     p_vout->render.i_gmask    = 0;
146     p_vout->render.i_bmask    = 0;
147
148     /* Zero the output heap */
149     I_OUTPUTPICTURES = 0;
150     p_vout->output.i_width    = 0;
151     p_vout->output.i_height   = 0;
152     p_vout->output.i_chroma   = 0;
153     p_vout->output.i_aspect   = 0;
154
155     p_vout->output.i_rmask    = 0;
156     p_vout->output.i_gmask    = 0;
157     p_vout->output.i_bmask    = 0;
158
159     /* Initialize misc stuff */
160     p_vout->i_changes    = 0;
161     p_vout->f_gamma      = 0;
162     p_vout->b_grayscale  = 0;
163     p_vout->b_info       = 0;
164     p_vout->b_interface  = 0;
165     p_vout->b_scale      = 1;
166     p_vout->b_fullscreen = 0;
167     p_vout->render_time  = 10;
168     p_vout->c_fps_samples= 0;
169
170     p_vout->i_mouse_x = 0;
171     p_vout->i_mouse_y = 0;
172     p_vout->i_mouse_button = 0;
173
174     /* user requested fullscreen? */
175     if( config_GetInt( p_vout, "fullscreen" ) )
176     {
177         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
178     }
179
180     /* Initialize the dimensions of the video window */
181     InitWindowSize( p_vout, &p_vout->i_window_width,
182                     &p_vout->i_window_height );
183
184
185     p_vout->p_module = module_Need( p_vout,
186                            ( p_vout->psz_filter_chain ) ?
187                            MODULE_CAPABILITY_VOUT_FILTER :
188                            MODULE_CAPABILITY_VOUT,
189                            psz_plugin, (void *)p_vout );
190
191     if( psz_plugin ) free( psz_plugin );
192     if( p_vout->p_module == NULL )
193     {
194         msg_Err( p_vout, "no suitable vout module" );
195         vlc_object_destroy( p_vout );
196         return( NULL );
197     }
198
199 #define f p_vout->p_module->p_functions->vout.functions.vout
200     p_vout->pf_create     = f.pf_create;
201     p_vout->pf_init       = f.pf_init;
202     p_vout->pf_end        = f.pf_end;
203     p_vout->pf_destroy    = f.pf_destroy;
204     p_vout->pf_manage     = f.pf_manage;
205     p_vout->pf_render     = f.pf_render;
206     p_vout->pf_display    = f.pf_display;
207 #undef f
208
209     /* Create thread and set locks */
210     vlc_mutex_init( p_vout, &p_vout->picture_lock );
211     vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
212     vlc_mutex_init( p_vout, &p_vout->change_lock );
213
214     vlc_object_attach( p_vout, p_parent );
215
216     if( vlc_thread_create( p_vout, "video output", RunThread, 0 ) )
217     {
218         msg_Err( p_vout, "%s", strerror(ENOMEM) );
219         p_vout->pf_destroy( p_vout );
220         module_Unneed( p_vout->p_module );
221         vlc_object_destroy( p_vout );
222         return NULL;
223     }
224
225     return p_vout;
226 }
227
228 /*****************************************************************************
229  * vout_DestroyThread: destroys a previously created thread
230  *****************************************************************************
231  * Destroy a terminated thread.
232  * The function will request a destruction of the specified thread. If pi_error
233  * is NULL, it will return once the thread is destroyed. Else, it will be
234  * update using one of the THREAD_* constants.
235  *****************************************************************************/
236 void vout_DestroyThread( vout_thread_t *p_vout )
237 {
238     /* Unlink object */
239     vlc_object_detach_all( p_vout );
240
241     /* Request thread destruction */
242     p_vout->b_die = 1;
243     vlc_thread_join( p_vout );
244
245     /* Free structure */
246     vlc_object_destroy( p_vout );
247 }
248
249 /*****************************************************************************
250  * vout_ChromaCmp: compare two chroma values
251  *****************************************************************************
252  * This function returns 1 if the two fourcc values given as argument are
253  * the same format (eg. UYVY / UYNV) or almost the same format (eg. I420/YV12)
254  *****************************************************************************/
255 int vout_ChromaCmp( u32 i_chroma, u32 i_amorhc )
256 {
257     /* If they are the same, they are the same ! */
258     if( i_chroma == i_amorhc )
259     {
260         return 1;
261     }
262
263     /* Check for equivalence classes */
264     switch( i_chroma )
265     {
266         case FOURCC_I420:
267         case FOURCC_IYUV:
268         case FOURCC_YV12:
269             switch( i_amorhc )
270             {
271                 case FOURCC_I420:
272                 case FOURCC_IYUV:
273                 case FOURCC_YV12:
274                     return 1;
275
276                 default:
277                     return 0;
278             }
279
280         case FOURCC_UYVY:
281         case FOURCC_UYNV:
282         case FOURCC_Y422:
283             switch( i_amorhc )
284             {
285                 case FOURCC_UYVY:
286                 case FOURCC_UYNV:
287                 case FOURCC_Y422:
288                     return 1;
289
290                 default:
291                     return 0;
292             }
293
294         case FOURCC_YUY2:
295         case FOURCC_YUNV:
296             switch( i_amorhc )
297             {
298                 case FOURCC_YUY2:
299                 case FOURCC_YUNV:
300                     return 1;
301
302                 default:
303                     return 0;
304             }
305
306         default:
307             return 0;
308     }
309 }
310
311 /*****************************************************************************
312  * InitThread: initialize video output thread
313  *****************************************************************************
314  * This function is called from RunThread and performs the second step of the
315  * initialization. It returns 0 on success. Note that the thread's flag are not
316  * modified inside this function.
317  *****************************************************************************/
318 static int InitThread( vout_thread_t *p_vout )
319 {
320     int i, i_pgcd;
321
322     vlc_mutex_lock( &p_vout->change_lock );
323
324 #ifdef STATS
325     p_vout->c_loops = 0;
326 #endif
327
328     /* Initialize output method, it allocates direct buffers for us */
329     if( p_vout->pf_init( p_vout ) )
330     {
331         vlc_mutex_unlock( &p_vout->change_lock );
332         return( 1 );
333     }
334
335     if( !I_OUTPUTPICTURES )
336     {
337         msg_Err( p_vout, "plugin was unable to allocate at least "
338                          "one direct buffer" );
339         p_vout->pf_end( p_vout );
340         vlc_mutex_unlock( &p_vout->change_lock );
341         return( 1 );
342     }
343
344     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
345
346     i_pgcd = ReduceHeight( p_vout->render.i_aspect );
347     msg_Dbg( p_vout,
348              "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
349              p_vout->render.i_width, p_vout->render.i_height,
350              p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
351              p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
352
353     i_pgcd = ReduceHeight( p_vout->output.i_aspect );
354     msg_Dbg( p_vout,
355              "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
356              p_vout->output.i_width, p_vout->output.i_height,
357              p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
358              p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
359
360     /* Calculate shifts from system-updated masks */
361     MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
362                  p_vout->output.i_rmask );
363     MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
364                  p_vout->output.i_gmask );
365     MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
366                  p_vout->output.i_bmask );
367
368     /* Check whether we managed to create direct buffers similar to
369      * the render buffers, ie same size, chroma and aspect ratio */
370     if( ( p_vout->output.i_width == p_vout->render.i_width )
371      && ( p_vout->output.i_height == p_vout->render.i_height )
372      && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) )
373      && ( p_vout->output.i_aspect == p_vout->render.i_aspect ) )
374     {
375         /* Cool ! We have direct buffers, we can ask the decoder to
376          * directly decode into them ! Map the first render buffers to
377          * the first direct buffers, but keep the first direct buffer
378          * for memcpy operations */
379         p_vout->b_direct = 1;
380
381         msg_Dbg( p_vout, "direct render, mapping "
382                  "render pictures 0-%i to system pictures 1-%i",
383                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
384
385         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
386         {
387             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
388             I_RENDERPICTURES++;
389         }
390     }
391     else
392     {
393         /* Rats... Something is wrong here, we could not find an output
394          * plugin able to directly render what we decode. See if we can
395          * find a chroma plugin to do the conversion */
396         p_vout->b_direct = 0;
397
398         /* Choose the best module */
399         p_vout->chroma.p_module =
400             module_Need( p_vout, MODULE_CAPABILITY_CHROMA,
401                          NULL, (void *)p_vout );
402
403         if( p_vout->chroma.p_module == NULL )
404         {
405             msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
406                      &p_vout->render.i_chroma, &p_vout->output.i_chroma );
407             p_vout->pf_end( p_vout );
408             vlc_mutex_unlock( &p_vout->change_lock );
409             return( 1 );
410         }
411
412 #define f p_vout->chroma.p_module->p_functions->chroma.functions.chroma
413         p_vout->chroma.pf_init       = f.pf_init;
414         p_vout->chroma.pf_end        = f.pf_end;
415 #undef f
416
417         if( I_OUTPUTPICTURES < 2 * VOUT_MAX_PICTURES )
418         {
419             msg_Dbg( p_vout, "indirect render, mapping "
420                      "render pictures %i-%i to system pictures %i-%i",
421                      I_OUTPUTPICTURES - 1, 2 * VOUT_MAX_PICTURES - 2,
422                      I_OUTPUTPICTURES, 2 * VOUT_MAX_PICTURES - 1 );
423         }
424         else
425         {
426             /* FIXME: if this happens, we don't have any render pictures left */
427             msg_Dbg( p_vout, "indirect render, no system pictures needed,"
428                      " we have %i directbuffers", I_OUTPUTPICTURES );
429             msg_Err( p_vout, "this is a bug!" );
430         }
431
432         /* Append render buffers after the direct buffers */
433         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
434         {
435             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
436             I_RENDERPICTURES++;
437         }
438     }
439
440     /* Link pictures back to their heap */
441     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
442     {
443         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
444     }
445
446     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
447     {
448         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
449     }
450
451 /* XXX XXX mark thread ready */
452     return( 0 );
453 }
454
455 /*****************************************************************************
456  * RunThread: video output thread
457  *****************************************************************************
458  * Video output thread. This function does only returns when the thread is
459  * terminated. It handles the pictures arriving in the video heap and the
460  * display device events.
461  *****************************************************************************/
462 static void RunThread( vout_thread_t *p_vout)
463 {
464     int             i_index;                                /* index in heap */
465     int             i_idle_loops = 0;  /* loops without displaying a picture */
466     mtime_t         current_date;                            /* current date */
467     mtime_t         display_date;                            /* display date */
468
469     picture_t *     p_picture;                            /* picture pointer */
470     picture_t *     p_last_picture = NULL;                   /* last picture */
471     picture_t *     p_directbuffer;              /* direct buffer to display */
472
473     subpicture_t *  p_subpic;                          /* subpicture pointer */
474
475     /*
476      * Initialize thread
477      */
478     p_vout->b_error = InitThread( p_vout );
479     if( p_vout->b_error )
480     {
481         /* Destroy thread structures allocated by Create and InitThread */
482         p_vout->pf_destroy( p_vout );
483
484         DestroyThread( p_vout );
485         return;
486     }
487
488     /*
489      * Main loop - it is not executed if an error occured during
490      * initialization
491      */
492     while( (!p_vout->b_die) && (!p_vout->b_error) )
493     {
494         /* Initialize loop variables */
495         display_date = 0;
496         current_date = mdate();
497
498 #ifdef STATS
499         p_vout->c_loops++;
500         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
501         {
502             msg_Dbg( p_vout, "picture heap: %d/%d",
503                      I_RENDERPICTURES, p_vout->i_heap_size );
504         }
505 #endif
506
507         /*
508          * Find the picture to display - this operation does not need lock,
509          * since only READY_PICTUREs are handled
510          */
511         p_picture = NULL;
512
513         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
514         {
515             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
516                 && ( (p_picture == NULL) ||
517                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
518             {
519                 p_picture = PP_RENDERPICTURE[i_index];
520                 display_date = p_picture->date;
521             }
522         }
523
524         if( p_picture != NULL )
525         {
526             /* If we met the last picture, parse again to see whether there is
527              * a more appropriate one. */
528             if( p_picture == p_last_picture )
529             {
530                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
531                 {
532                     if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
533                         && (PP_RENDERPICTURE[i_index] != p_last_picture)
534                         && ((p_picture == p_last_picture) ||
535                             (PP_RENDERPICTURE[i_index]->date < display_date)) )
536                     {
537                         p_picture = PP_RENDERPICTURE[i_index];
538                         display_date = p_picture->date;
539                     }
540                 }
541             }
542     
543             /* If we found better than the last picture, destroy it */
544             if( p_last_picture && p_picture != p_last_picture )
545             {
546                 vlc_mutex_lock( &p_vout->picture_lock );
547                 if( p_last_picture->i_refcount )
548                 {
549                     p_last_picture->i_status = DISPLAYED_PICTURE;
550                 }
551                 else
552                 {
553                     p_last_picture->i_status = DESTROYED_PICTURE;
554                     p_vout->i_heap_size--;
555                 }
556                 vlc_mutex_unlock( &p_vout->picture_lock );
557                 p_last_picture = NULL;
558             }
559
560             /* Compute FPS rate */
561             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
562                 = display_date;
563
564             if( !p_picture->b_force &&
565                 p_picture != p_last_picture &&
566                 display_date < current_date + p_vout->render_time )
567             {
568                 /* Picture is late: it will be destroyed and the thread
569                  * will directly choose the next picture */
570                 vlc_mutex_lock( &p_vout->picture_lock );
571                 if( p_picture->i_refcount )
572                 {
573                     /* Pretend we displayed the picture, but don't destroy
574                      * it since the decoder might still need it. */
575                     p_picture->i_status = DISPLAYED_PICTURE;
576                 }
577                 else
578                 {
579                     /* Destroy the picture without displaying it */
580                     p_picture->i_status = DESTROYED_PICTURE;
581                     p_vout->i_heap_size--;
582                 }
583                 msg_Warn( p_vout, "late picture skipped (%lld)",
584                                   current_date - display_date );
585                 vlc_mutex_unlock( &p_vout->picture_lock );
586
587                 continue;
588             }
589 #if 0
590             /* Removed because it causes problems for some people --Meuuh */
591             if( display_date > current_date + VOUT_BOGUS_DELAY )
592             {
593                 /* Picture is waaay too early: it will be destroyed */
594                 vlc_mutex_lock( &p_vout->picture_lock );
595                 if( p_picture->i_refcount )
596                 {
597                     /* Pretend we displayed the picture, but don't destroy
598                      * it since the decoder might still need it. */
599                     p_picture->i_status = DISPLAYED_PICTURE;
600                 }
601                 else
602                 {
603                     /* Destroy the picture without displaying it */
604                     p_picture->i_status = DESTROYED_PICTURE;
605                     p_vout->i_heap_size--;
606                 }
607                 intf_WarnMsg( 1, "vout warning: early picture skipped (%lld)",
608                               display_date - current_date );
609                 vlc_mutex_unlock( &p_vout->picture_lock );
610
611                 continue;
612             }
613 #endif
614             if( display_date > current_date + VOUT_DISPLAY_DELAY )
615             {
616                 /* A picture is ready to be rendered, but its rendering date
617                  * is far from the current one so the thread will perform an
618                  * empty loop as if no picture were found. The picture state
619                  * is unchanged */
620                 p_picture    = NULL;
621                 display_date = 0;
622             }
623             else if( p_picture == p_last_picture )
624             {
625                 /* We are asked to repeat the previous picture, but we first
626                  * wait for a couple of idle loops */
627                 if( i_idle_loops < 4 )
628                 {
629                     p_picture    = NULL;
630                     display_date = 0;
631                 }
632                 else
633                 {
634                     //intf_WarnMsg( 6, "vout info: duplicating picture" );
635                 }
636             }
637         }
638
639         if( p_picture == NULL )
640         {
641             i_idle_loops++;
642         }
643
644         /*
645          * Check for subpictures to display
646          */
647         p_subpic = vout_SortSubPictures( p_vout, display_date );
648
649         /*
650          * Perform rendering
651          */
652         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
653
654         /*
655          * Call the plugin-specific rendering method
656          */
657         if( p_picture != NULL )
658         {
659             /* Render the direct buffer returned by vout_RenderPicture */
660             p_vout->pf_render( p_vout, p_directbuffer );
661         }
662
663         /*
664          * Sleep, wake up
665          */
666         if( display_date != 0 )
667         {
668             /* Store render time using Bresenham algorithm */
669             p_vout->render_time += mdate() - current_date;
670             p_vout->render_time >>= 1;
671         }
672
673         /* Give back change lock */
674         vlc_mutex_unlock( &p_vout->change_lock );
675
676         /* Sleep a while or until a given date */
677         if( display_date != 0 )
678         {
679             mwait( display_date - VOUT_MWAIT_TOLERANCE );
680         }
681         else
682         {
683             msleep( VOUT_IDLE_SLEEP );
684         }
685
686         /* On awakening, take back lock and send immediately picture
687          * to display. */
688         vlc_mutex_lock( &p_vout->change_lock );
689
690         /*
691          * Display the previously rendered picture
692          */
693         if( p_picture != NULL )
694         {
695             /* Display the direct buffer returned by vout_RenderPicture */
696             p_vout->pf_display( p_vout, p_directbuffer );
697
698             /* Reinitialize idle loop count */
699             i_idle_loops = 0;
700
701             /* Tell the vout this was the last picture and that it does not
702              * need to be forced anymore. */
703             p_last_picture = p_picture;
704             p_last_picture->b_force = 0;
705         }
706
707         /*
708          * Check events and manage thread
709          */
710         if( p_vout->pf_manage( p_vout ) )
711         {
712             /* A fatal error occured, and the thread must terminate
713              * immediately, without displaying anything - setting b_error to 1
714              * causes the immediate end of the main while() loop. */
715             p_vout->b_error = 1;
716         }
717
718         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
719         {
720             /* this must only happen when the vout plugin is incapable of
721              * rescaling the picture itself. In this case we need to destroy
722              * the current picture buffers and recreate new ones with the right
723              * dimensions */
724             int i;
725
726             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
727
728             p_vout->pf_end( p_vout );
729             for( i = 0; i < I_OUTPUTPICTURES; i++ )
730                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
731
732             I_OUTPUTPICTURES = 0;
733             if( p_vout->pf_init( p_vout ) )
734             {
735                 msg_Err( p_vout, "cannot resize display" );
736                 /* FixMe: p_vout->pf_end will be called again in EndThread() */
737                 p_vout->b_error = 1;
738             }
739
740             /* Need to reinitialise the chroma plugin */
741             p_vout->chroma.pf_end( p_vout );
742             p_vout->chroma.pf_init( p_vout );
743         }
744
745     }
746
747     /*
748      * Error loop - wait until the thread destruction is requested
749      */
750     if( p_vout->b_error )
751     {
752         ErrorThread( p_vout );
753     }
754
755     /* End of thread */
756     EndThread( p_vout );
757
758     /* Destroy method-dependant resources */
759     p_vout->pf_destroy( p_vout );
760
761     /* Destroy thread structures allocated by CreateThread */
762     DestroyThread( p_vout );
763 }
764
765 /*****************************************************************************
766  * ErrorThread: RunThread() error loop
767  *****************************************************************************
768  * This function is called when an error occured during thread main's loop. The
769  * thread can still receive feed, but must be ready to terminate as soon as
770  * possible.
771  *****************************************************************************/
772 static void ErrorThread( vout_thread_t *p_vout )
773 {
774     /* Wait until a `die' order */
775     while( !p_vout->b_die )
776     {
777         /* Sleep a while */
778         msleep( VOUT_IDLE_SLEEP );
779     }
780 }
781
782 /*****************************************************************************
783  * EndThread: thread destruction
784  *****************************************************************************
785  * This function is called when the thread ends after a sucessful
786  * initialization. It frees all ressources allocated by InitThread.
787  *****************************************************************************/
788 static void EndThread( vout_thread_t *p_vout )
789 {
790     int     i_index;                                        /* index in heap */
791
792 #ifdef STATS
793     {
794         struct tms cpu_usage;
795         times( &cpu_usage );
796
797         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
798                  cpu_usage.tms_utime, cpu_usage.tms_stime );
799     }
800 #endif
801
802     if( !p_vout->b_direct )
803     {
804         p_vout->chroma.pf_end( p_vout );
805         module_Unneed( p_vout->chroma.p_module );
806     }
807
808     /* Destroy all remaining pictures */
809     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
810     {
811         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
812         {
813             free( p_vout->p_picture[i_index].p_data_orig );
814         }
815     }
816
817     /* Destroy all remaining subpictures */
818     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
819     {
820         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
821         {
822             free( p_vout->p_subpicture[i_index].p_sys );
823         }
824     }
825
826     /* Destroy translation tables */
827     p_vout->pf_end( p_vout );
828
829     /* Release the change lock */
830     vlc_mutex_unlock( &p_vout->change_lock );
831 }
832
833 /*****************************************************************************
834  * DestroyThread: thread destruction
835  *****************************************************************************
836  * This function is called when the thread ends. It frees all ressources
837  * allocated by CreateThread. Status is available at this stage.
838  *****************************************************************************/
839 static void DestroyThread( vout_thread_t *p_vout )
840 {
841     /* Destroy the locks */
842     vlc_mutex_destroy( &p_vout->picture_lock );
843     vlc_mutex_destroy( &p_vout->subpicture_lock );
844     vlc_mutex_destroy( &p_vout->change_lock );
845
846     /* Release the module */
847     module_Unneed( p_vout->p_module );
848 }
849
850 /* following functions are local */
851
852 static int ReduceHeight( int i_ratio )
853 {
854     int i_dummy = VOUT_ASPECT_FACTOR;
855     int i_pgcd  = 1;
856  
857     if( !i_ratio )
858     {
859         return i_pgcd;
860     }
861
862     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
863     while( !(i_ratio & 1) && !(i_dummy & 1) )
864     {
865         i_ratio >>= 1;
866         i_dummy >>= 1;
867         i_pgcd  <<= 1;
868     }
869
870     while( !(i_ratio % 3) && !(i_dummy % 3) )
871     {
872         i_ratio /= 3;
873         i_dummy /= 3;
874         i_pgcd  *= 3;
875     }
876
877     while( !(i_ratio % 5) && !(i_dummy % 5) )
878     {
879         i_ratio /= 5;
880         i_dummy /= 5;
881         i_pgcd  *= 5;
882     }
883
884     return i_pgcd;
885 }
886
887 /*****************************************************************************
888  * BinaryLog: computes the base 2 log of a binary value
889  *****************************************************************************
890  * This functions is used by MaskToShift, to get a bit index from a binary
891  * value.
892  *****************************************************************************/
893 static int BinaryLog(u32 i)
894 {
895     int i_log = 0;
896
897     if( i == 0 ) return -31337;
898
899     if( i & 0xffff0000 ) i_log += 16;
900     if( i & 0xff00ff00 ) i_log += 8;
901     if( i & 0xf0f0f0f0 ) i_log += 4;
902     if( i & 0xcccccccc ) i_log += 2;
903     if( i & 0xaaaaaaaa ) i_log += 1;
904
905     return( i_log );
906 }
907
908 /*****************************************************************************
909  * MaskToShift: transform a color mask into right and left shifts
910  *****************************************************************************
911  * This function is used for obtaining color shifts from masks.
912  *****************************************************************************/
913 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
914 {
915     u32 i_low, i_high;                 /* lower hand higher bits of the mask */
916
917     if( !i_mask )
918     {
919         *pi_left = *pi_right = 0;
920         return;
921     }
922
923     /* Get bits */
924     i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
925     i_high = i_mask + i_low;                       /* higher bit of the mask */
926
927     /* Transform bits into an index */
928     i_low =  BinaryLog (i_low);
929     i_high = BinaryLog (i_high);
930
931     /* Update pointers and return */
932     *pi_left =   i_low;
933     *pi_right = (8 - i_high + i_low);
934 }
935
936 /*****************************************************************************
937  * InitWindowSize: find the initial dimensions the video window should have.
938  *****************************************************************************
939  * This function will check the "width", "height" and "zoom" config options and
940  * will calculate the size that the video window should have.
941  *****************************************************************************/
942 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
943                             int *pi_height )
944 {
945     int i_width, i_height;
946     double f_zoom;
947
948     i_width = config_GetInt( p_vout, "width" );
949     i_height = config_GetInt( p_vout, "height" );
950     f_zoom = config_GetFloat( p_vout, "zoom" );
951
952     if( (i_width >= 0) && (i_height >= 0))
953     {
954         *pi_width = i_width * f_zoom;
955         *pi_height = i_height * f_zoom;
956         return;
957     }
958     else if( i_width >= 0 )
959     {
960         *pi_width = i_width * f_zoom;
961         *pi_height = i_width * f_zoom * VOUT_ASPECT_FACTOR /
962                         p_vout->render.i_aspect;
963         return;
964     }
965     else if( i_height >= 0 )
966     {
967         *pi_height = i_height * f_zoom;
968         *pi_width = i_height * f_zoom * p_vout->render.i_aspect /
969                        VOUT_ASPECT_FACTOR;
970         return;
971     }
972
973     if( p_vout->render.i_height * p_vout->render.i_aspect
974         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
975     {
976         *pi_width = p_vout->render.i_height * f_zoom
977           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
978         *pi_height = p_vout->render.i_height * f_zoom;
979     }
980     else
981     {
982         *pi_width = p_vout->render.i_width * f_zoom;
983         *pi_height = p_vout->render.i_width * f_zoom
984           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
985     }
986 }