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