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