]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
* added config_GetFloatVariable() and config_PutFloatVariable() to the config
[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.172 2002/04/21 11:23:03 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 <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
257     /* If status is NULL, wait until thread has been destroyed */
258     if( pi_status == NULL )
259     {
260         do
261         {
262             msleep( THREAD_SLEEP );
263         } while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
264                  && (i_status != THREAD_FATAL) );
265     }
266 }
267
268 /*****************************************************************************
269  * InitThread: initialize video output thread
270  *****************************************************************************
271  * This function is called from RunThread and performs the second step of the
272  * initialization. It returns 0 on success. Note that the thread's flag are not
273  * modified inside this function.
274  *****************************************************************************/
275 static int InitThread( vout_thread_t *p_vout )
276 {
277     int i, i_pgcd;
278
279     /* Update status */
280     *p_vout->pi_status = THREAD_START;
281
282     vlc_mutex_lock( &p_vout->change_lock );
283
284 #ifdef STATS
285     p_vout->c_loops = 0;
286 #endif
287
288     /* Initialize output method, it allocates direct buffers for us */
289     if( p_vout->pf_init( p_vout ) )
290     {
291         vlc_mutex_unlock( &p_vout->change_lock );
292         return( 1 );
293     }
294
295     if( !I_OUTPUTPICTURES )
296     {
297         intf_ErrMsg( "vout error: plugin was unable to allocate at least "
298                      "one direct buffer" );
299         p_vout->pf_end( p_vout );
300         vlc_mutex_unlock( &p_vout->change_lock );
301         return( 1 );
302     }
303
304     intf_WarnMsg( 1, "vout info: got %i direct buffer(s)", I_OUTPUTPICTURES );
305
306     i_pgcd = ReduceHeight( p_vout->render.i_aspect );
307     intf_WarnMsg( 1, "vout info: picture in %ix%i, chroma 0x%.8x (%4.4s), "
308                      "aspect ratio %i:%i",
309                   p_vout->render.i_width, p_vout->render.i_height,
310                   p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
311                   p_vout->render.i_aspect / i_pgcd,
312                   VOUT_ASPECT_FACTOR / i_pgcd );
313
314     i_pgcd = ReduceHeight( p_vout->output.i_aspect );
315     intf_WarnMsg( 1, "vout info: picture out %ix%i, chroma 0x%.8x (%4.4s), "
316                      "aspect ratio %i:%i",
317                   p_vout->output.i_width, p_vout->output.i_height,
318                   p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
319                   p_vout->output.i_aspect / i_pgcd,
320                   VOUT_ASPECT_FACTOR / i_pgcd );
321
322     /* Calculate shifts from system-updated masks */
323     MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
324                  p_vout->output.i_rmask );
325     MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
326                  p_vout->output.i_gmask );
327     MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
328                  p_vout->output.i_bmask );
329
330     /* Check whether we managed to create direct buffers similar to
331      * the render buffers, ie same size, chroma and aspect ratio */
332     if( ( p_vout->output.i_width == p_vout->render.i_width )
333      && ( p_vout->output.i_height == p_vout->render.i_height )
334      && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) )
335      && ( p_vout->output.i_aspect == p_vout->render.i_aspect ) )
336     {
337         /* Cool ! We have direct buffers, we can ask the decoder to
338          * directly decode into them ! Map the first render buffers to
339          * the first direct buffers, but keep the first direct buffer
340          * for memcpy operations */
341         p_vout->b_direct = 1;
342
343         intf_WarnMsg( 2, "vout info: direct render, mapping "
344                          "render pictures 0-%i to system pictures 1-%i",
345                          VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
346
347         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
348         {
349             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
350             I_RENDERPICTURES++;
351         }
352     }
353     else
354     {
355         /* Rats... Something is wrong here, we could not find an output
356          * plugin able to directly render what we decode. See if we can
357          * find a chroma plugin to do the conversion */
358         p_vout->b_direct = 0;
359
360         /* Choose the best module */
361         p_vout->chroma.p_module
362             = module_Need( MODULE_CAPABILITY_CHROMA, NULL, (void *)p_vout );
363
364         if( p_vout->chroma.p_module == NULL )
365         {
366             intf_ErrMsg( "vout error: no chroma module for %4.4s to %4.4s",
367                          &p_vout->render.i_chroma, &p_vout->output.i_chroma );
368             p_vout->pf_end( p_vout );
369             vlc_mutex_unlock( &p_vout->change_lock );
370             return( 1 );
371         }
372
373 #define f p_vout->chroma.p_module->p_functions->chroma.functions.chroma
374         p_vout->chroma.pf_end        = f.pf_end;
375 #undef f
376
377         if( I_OUTPUTPICTURES < 2 * VOUT_MAX_PICTURES )
378         {
379             intf_WarnMsg( 2, "vout info: indirect render, mapping "
380                              "render pictures %i-%i to system pictures %i-%i",
381                              I_OUTPUTPICTURES - 1, 2 * VOUT_MAX_PICTURES - 2,
382                              I_OUTPUTPICTURES, 2 * VOUT_MAX_PICTURES - 1 );
383         }
384         else
385         {
386             /* FIXME: if this happens, we don't have any render picture left */
387             intf_WarnMsg( 2, "vout info: indirect render, no system "
388                              "pictures needed, we have %i directbuffers",
389                              I_OUTPUTPICTURES );
390             intf_ErrMsg( "vout: this is a bug!\n");
391         }
392
393         /* Append render buffers after the direct buffers */
394         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
395         {
396             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
397             I_RENDERPICTURES++;
398         }
399     }
400
401     /* Link pictures back to their heap */
402     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
403     {
404         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
405     }
406
407     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
408     {
409         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
410     }
411
412     /* Mark thread as running and return */
413     p_vout->b_active = 1;
414     *p_vout->pi_status = THREAD_READY;
415
416     return( 0 );
417 }
418
419 /*****************************************************************************
420  * RunThread: video output thread
421  *****************************************************************************
422  * Video output thread. This function does only returns when the thread is
423  * terminated. It handles the pictures arriving in the video heap and the
424  * display device events.
425  *****************************************************************************/
426 static void RunThread( vout_thread_t *p_vout)
427 {
428     int             i_index;                                /* index in heap */
429     mtime_t         current_date;                            /* current date */
430     mtime_t         display_date;                            /* display date */
431
432     picture_t *     p_picture;                            /* picture pointer */
433     picture_t *     p_directbuffer;              /* direct buffer to display */
434
435     subpicture_t *  p_subpic;                          /* subpicture pointer */
436
437     /*
438      * Initialize thread
439      */
440     p_vout->b_error = InitThread( p_vout );
441     if( p_vout->b_error )
442     {
443         /* Destroy thread structures allocated by Create and InitThread */
444         p_vout->pf_destroy( p_vout );
445
446         DestroyThread( p_vout, THREAD_ERROR );
447         return;
448     }
449
450     /*
451      * Main loop - it is not executed if an error occured during
452      * initialization
453      */
454     while( (!p_vout->b_die) && (!p_vout->b_error) )
455     {
456         /* Initialize loop variables */
457         display_date = 0;
458         current_date = mdate();
459
460 #ifdef STATS
461         p_vout->c_loops++;
462         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
463         {
464             intf_Msg( "vout stats: picture heap: %d/%d",
465                       I_RENDERPICTURES, p_vout->i_heap_size );
466         }
467 #endif
468
469         /*
470          * Find the picture to display - this operation does not need lock,
471          * since only READY_PICTUREs are handled
472          */
473         p_picture = NULL;
474
475         for( i_index = 0;
476              i_index < I_RENDERPICTURES;
477              i_index++ )
478         {
479             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
480                 && ( (p_picture == NULL) ||
481                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
482             {
483                 p_picture = PP_RENDERPICTURE[i_index];
484                 display_date = p_picture->date;
485             }
486         }
487
488         if( p_picture != NULL )
489         {
490             /* Compute FPS rate */
491             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
492                 = display_date;
493
494             if( display_date < current_date + p_vout->render_time )
495             {
496                 /* Picture is late: it will be destroyed and the thread
497                  * will directly choose the next picture */
498                 vlc_mutex_lock( &p_vout->picture_lock );
499                 if( p_picture->i_refcount )
500                 {
501                     /* Pretend we displayed the picture, but don't destroy
502                      * it since the decoder might still need it. */
503                     p_picture->i_status = DISPLAYED_PICTURE;
504                 }
505                 else
506                 {
507                     /* Destroy the picture without displaying it */
508                     p_picture->i_status = DESTROYED_PICTURE;
509                     p_vout->i_heap_size--;
510                 }
511                 intf_WarnMsg( 1, "vout warning: late picture skipped (%p)",
512                               p_picture );
513                 vlc_mutex_unlock( &p_vout->picture_lock );
514
515                 continue;
516             }
517             else if( display_date > current_date + VOUT_DISPLAY_DELAY )
518             {
519                 /* A picture is ready to be rendered, but its rendering date
520                  * is far from the current one so the thread will perform an
521                  * empty loop as if no picture were found. The picture state
522                  * is unchanged */
523                 p_picture    = NULL;
524                 display_date = 0;
525             }
526         }
527
528         /*
529          * Check for subpictures to display
530          */
531         p_subpic = vout_SortSubPictures( p_vout, display_date );
532
533         /*
534          * Perform rendering
535          */
536         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
537
538         /*
539          * Call the plugin-specific rendering method
540          */
541         if( p_picture != NULL )
542         {
543             /* Render the direct buffer returned by vout_RenderPicture */
544             p_vout->pf_render( p_vout, p_directbuffer );
545         }
546
547         /*
548          * Sleep, wake up
549          */
550         if( display_date != 0 )
551         {
552             /* Store render time using Bresenham algorithm */
553             p_vout->render_time += mdate() - current_date;
554             p_vout->render_time >>= 1;
555         }
556
557         /* Give back change lock */
558         vlc_mutex_unlock( &p_vout->change_lock );
559
560         /* Sleep a while or until a given date */
561         if( display_date != 0 )
562         {
563             mwait( display_date - VOUT_MWAIT_TOLERANCE );
564         }
565         else
566         {
567             msleep( VOUT_IDLE_SLEEP );
568         }
569
570         /* On awakening, take back lock and send immediately picture
571          * to display. */
572         vlc_mutex_lock( &p_vout->change_lock );
573
574         /*
575          * Display the previously rendered picture
576          */
577         if( p_picture != NULL )
578         {
579             /* Display the direct buffer returned by vout_RenderPicture */
580             p_vout->pf_display( p_vout, p_directbuffer );
581
582             /* Remove picture from heap */
583             vlc_mutex_lock( &p_vout->picture_lock );
584             if( p_picture->i_refcount )
585             {
586                 p_picture->i_status = DISPLAYED_PICTURE;
587             }
588             else
589             {
590                 p_picture->i_status = DESTROYED_PICTURE;
591                 p_vout->i_heap_size--;
592             }
593             vlc_mutex_unlock( &p_vout->picture_lock );
594         }
595
596         /*
597          * Check events and manage thread
598          */
599         if( p_vout->pf_manage( p_vout ) )
600         {
601             /* A fatal error occured, and the thread must terminate immediately,
602              * without displaying anything - setting b_error to 1 causes the
603              * immediate end of the main while() loop. */
604             p_vout->b_error = 1;
605         }
606     }
607
608     /*
609      * Error loop - wait until the thread destruction is requested
610      */
611     if( p_vout->b_error )
612     {
613         ErrorThread( p_vout );
614     }
615
616     /* End of thread */
617     EndThread( p_vout );
618
619     /* Destroy method-dependant resources */
620     p_vout->pf_destroy( p_vout );
621
622     /* Destroy thread structures allocated by CreateThread */
623     DestroyThread( p_vout, THREAD_OVER );
624 }
625
626 /*****************************************************************************
627  * ErrorThread: RunThread() error loop
628  *****************************************************************************
629  * This function is called when an error occured during thread main's loop. The
630  * thread can still receive feed, but must be ready to terminate as soon as
631  * possible.
632  *****************************************************************************/
633 static void ErrorThread( vout_thread_t *p_vout )
634 {
635     /* Wait until a `die' order */
636     while( !p_vout->b_die )
637     {
638         /* Sleep a while */
639         msleep( VOUT_IDLE_SLEEP );
640     }
641 }
642
643 /*****************************************************************************
644  * EndThread: thread destruction
645  *****************************************************************************
646  * This function is called when the thread ends after a sucessful
647  * initialization. It frees all ressources allocated by InitThread.
648  *****************************************************************************/
649 static void EndThread( vout_thread_t *p_vout )
650 {
651     int     i_index;                                        /* index in heap */
652
653     /* Store status */
654     *p_vout->pi_status = THREAD_END;
655
656 #ifdef STATS
657     {
658         struct tms cpu_usage;
659         times( &cpu_usage );
660
661         intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
662                   cpu_usage.tms_utime, cpu_usage.tms_stime );
663     }
664 #endif
665
666     if( !p_vout->b_direct )
667     {
668         p_vout->chroma.pf_end( p_vout );
669         module_Unneed( p_vout->chroma.p_module );
670     }
671
672     /* Destroy all remaining pictures */
673     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
674     {
675         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
676         {
677             free( p_vout->p_picture[i_index].p_data_orig );
678         }
679     }
680
681     /* Destroy all remaining subpictures */
682     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
683     {
684         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
685         {
686             free( p_vout->p_subpicture[i_index].p_sys );
687         }
688     }
689
690     /* Destroy translation tables */
691     p_vout->pf_end( p_vout );
692
693     /* Release the change lock */
694     vlc_mutex_unlock( &p_vout->change_lock );
695 }
696
697 /*****************************************************************************
698  * DestroyThread: thread destruction
699  *****************************************************************************
700  * This function is called when the thread ends. It frees all ressources
701  * allocated by CreateThread. Status is available at this stage.
702  *****************************************************************************/
703 static void DestroyThread( vout_thread_t *p_vout, int i_status )
704 {
705     int *pi_status;                                         /* status adress */
706
707     /* Store status adress */
708     pi_status = p_vout->pi_status;
709
710     /* Destroy the locks */
711     vlc_mutex_destroy( &p_vout->picture_lock );
712     vlc_mutex_destroy( &p_vout->subpicture_lock );
713     vlc_mutex_destroy( &p_vout->change_lock );
714
715     /* Release the module */
716     module_Unneed( p_vout->p_module );
717
718     /* Free structure */
719     free( p_vout );
720     *pi_status = i_status;
721 }
722
723 /* following functions are local */
724
725 static int ReduceHeight( int i_ratio )
726 {
727     int i_dummy = VOUT_ASPECT_FACTOR;
728     int i_pgcd  = 1;
729  
730     if( !i_ratio )
731     {
732         return i_pgcd;
733     }
734
735     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
736     while( !(i_ratio & 1) && !(i_dummy & 1) )
737     {
738         i_ratio >>= 1;
739         i_dummy >>= 1;
740         i_pgcd  <<= 1;
741     }
742
743     while( !(i_ratio % 3) && !(i_dummy % 3) )
744     {
745         i_ratio /= 3;
746         i_dummy /= 3;
747         i_pgcd  *= 3;
748     }
749
750     while( !(i_ratio % 5) && !(i_dummy % 5) )
751     {
752         i_ratio /= 5;
753         i_dummy /= 5;
754         i_pgcd  *= 5;
755     }
756
757     return i_pgcd;
758 }
759
760 /*****************************************************************************
761  * BinaryLog: computes the base 2 log of a binary value
762  *****************************************************************************
763  * This functions is used by MaskToShift, to get a bit index from a binary
764  * value.
765  *****************************************************************************/
766 static int BinaryLog(u32 i)
767 {
768     int i_log = 0;
769
770     if(i & 0xffff0000) i_log += 16;
771     if(i & 0xff00ff00) i_log += 8;
772     if(i & 0xf0f0f0f0) i_log += 4;
773     if(i & 0xcccccccc) i_log += 2;
774     if(i & 0xaaaaaaaa) i_log += 1;
775
776     if (i != ((u32)1 << i_log))
777     {
778         intf_ErrMsg( "vout error: binary log overflow for %i", i );
779     }
780
781     return( i_log );
782 }
783
784 /*****************************************************************************
785  * MaskToShift: transform a color mask into right and left shifts
786  *****************************************************************************
787  * This function is used for obtaining color shifts from masks.
788  *****************************************************************************/
789 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
790 {
791     u32 i_low, i_high;                 /* lower hand higher bits of the mask */
792
793     if( !i_mask )
794     {
795         *pi_left = *pi_right = 0;
796         return;
797     }
798
799     /* Get bits */
800     i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
801     i_high = i_mask + i_low;                       /* higher bit of the mask */
802
803     /* Transform bits into an index */
804     i_low =  BinaryLog (i_low);
805     i_high = BinaryLog (i_high);
806
807     /* Update pointers and return */
808     *pi_left =   i_low;
809     *pi_right = (8 - i_high + i_low);
810 }
811
812 /*****************************************************************************
813  * InitWindowSize: find the initial dimensions the video window should have.
814  *****************************************************************************
815  * This function will check the "width", "height" and "zoom" config options and
816  * will calculate the size that the video window should have.
817  *****************************************************************************/
818 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
819                             int *pi_height )
820 {
821     int i_width, i_height;
822     double f_zoom;
823
824     i_width = config_GetIntVariable( "width" );
825     i_height = config_GetIntVariable( "height" );
826     f_zoom = config_GetFloatVariable( "zoom" );
827
828     if( (i_width >= 0) && (i_height >= 0))
829     {
830         *pi_width = i_width * f_zoom;
831         *pi_height = i_height * f_zoom;
832         return;
833     }
834     else if( i_width >= 0 )
835     {
836         *pi_width = i_width * f_zoom;
837         *pi_height = i_width * f_zoom * VOUT_ASPECT_FACTOR /
838                         p_vout->render.i_aspect;
839         return;
840     }
841     else if( i_height >= 0 )
842     {
843         *pi_height = i_height * f_zoom;
844         *pi_width = i_height * f_zoom * p_vout->render.i_aspect /
845                        VOUT_ASPECT_FACTOR;
846         return;
847     }
848
849     if( p_vout->render.i_height * p_vout->render.i_aspect
850         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
851     {
852         *pi_width = p_vout->render.i_height * f_zoom
853           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
854         *pi_height = p_vout->render.i_height * f_zoom;
855     }
856     else
857     {
858         *pi_width = p_vout->render.i_width * f_zoom;
859         *pi_height = p_vout->render.i_width * f_zoom
860           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
861     }
862 }