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