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