]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
85ada8f23e2c04cd59c895a2cdbcaf3869e78ce3
[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.180 2002/05/29 18:39: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     int             i_idle_loops = 0;  /* loops without displaying a picture */
488     mtime_t         current_date;                            /* current date */
489     mtime_t         display_date;                            /* display date */
490
491     picture_t *     p_picture;                            /* picture pointer */
492     picture_t *     p_last_picture = NULL;                   /* last picture */
493     picture_t *     p_directbuffer;              /* direct buffer to display */
494
495     subpicture_t *  p_subpic;                          /* subpicture pointer */
496
497     /*
498      * Initialize thread
499      */
500     p_vout->b_error = InitThread( p_vout );
501     if( p_vout->b_error )
502     {
503         /* Destroy thread structures allocated by Create and InitThread */
504         p_vout->pf_destroy( p_vout );
505
506         DestroyThread( p_vout, THREAD_ERROR );
507         return;
508     }
509
510     /*
511      * Main loop - it is not executed if an error occured during
512      * initialization
513      */
514     while( (!p_vout->b_die) && (!p_vout->b_error) )
515     {
516         /* Initialize loop variables */
517         display_date = 0;
518         current_date = mdate();
519
520 #ifdef STATS
521         p_vout->c_loops++;
522         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
523         {
524             intf_Msg( "vout stats: picture heap: %d/%d",
525                       I_RENDERPICTURES, p_vout->i_heap_size );
526         }
527 #endif
528
529         /*
530          * Find the picture to display - this operation does not need lock,
531          * since only READY_PICTUREs are handled
532          */
533         p_picture = NULL;
534
535         for( i_index = 0; i_index < I_RENDERPICTURES; 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             /* If we met the last picture, parse again to see whether there is
549              * a more appropriate one. */
550             if( p_picture == p_last_picture )
551             {
552                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
553                 {
554                     if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
555                         && (PP_RENDERPICTURE[i_index] != p_last_picture)
556                         && ((p_picture == p_last_picture) ||
557                             (PP_RENDERPICTURE[i_index]->date < display_date)) )
558                     {
559                         p_picture = PP_RENDERPICTURE[i_index];
560                         display_date = p_picture->date;
561                     }
562                 }
563             }
564     
565             /* If we found better than the last picture, destroy it */
566             if( p_last_picture && p_picture != p_last_picture )
567             {
568                 vlc_mutex_lock( &p_vout->picture_lock );
569                 if( p_last_picture->i_refcount )
570                 {
571                     p_last_picture->i_status = DISPLAYED_PICTURE;
572                 }
573                 else
574                 {
575                     p_last_picture->i_status = DESTROYED_PICTURE;
576                     p_vout->i_heap_size--;
577                 }
578                 vlc_mutex_unlock( &p_vout->picture_lock );
579             }
580
581             /* Compute FPS rate */
582             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
583                 = display_date;
584
585             if( !p_picture->b_force &&
586                 p_picture != p_last_picture &&
587                 display_date < current_date + p_vout->render_time )
588             {
589                 /* Picture is late: it will be destroyed and the thread
590                  * will directly choose the next picture */
591                 vlc_mutex_lock( &p_vout->picture_lock );
592                 if( p_picture->i_refcount )
593                 {
594                     /* Pretend we displayed the picture, but don't destroy
595                      * it since the decoder might still need it. */
596                     p_picture->i_status = DISPLAYED_PICTURE;
597                 }
598                 else
599                 {
600                     /* Destroy the picture without displaying it */
601                     p_picture->i_status = DESTROYED_PICTURE;
602                     p_vout->i_heap_size--;
603                 }
604                 intf_WarnMsg( 1, "vout warning: late picture skipped (%lld)",
605                               current_date - display_date );
606                 vlc_mutex_unlock( &p_vout->picture_lock );
607
608                 continue;
609             }
610 #if 0
611             /* Removed because it causes problems for some people --Meuuh */
612             if( display_date > current_date + VOUT_BOGUS_DELAY )
613             {
614                 /* Picture is waaay too early: it will be destroyed */
615                 vlc_mutex_lock( &p_vout->picture_lock );
616                 if( p_picture->i_refcount )
617                 {
618                     /* Pretend we displayed the picture, but don't destroy
619                      * it since the decoder might still need it. */
620                     p_picture->i_status = DISPLAYED_PICTURE;
621                 }
622                 else
623                 {
624                     /* Destroy the picture without displaying it */
625                     p_picture->i_status = DESTROYED_PICTURE;
626                     p_vout->i_heap_size--;
627                 }
628                 intf_WarnMsg( 1, "vout warning: early picture skipped (%lld)",
629                               display_date - current_date );
630                 vlc_mutex_unlock( &p_vout->picture_lock );
631
632                 continue;
633             }
634 #endif
635             if( display_date > current_date + VOUT_DISPLAY_DELAY )
636             {
637                 /* A picture is ready to be rendered, but its rendering date
638                  * is far from the current one so the thread will perform an
639                  * empty loop as if no picture were found. The picture state
640                  * is unchanged */
641                 p_picture    = NULL;
642                 display_date = 0;
643             }
644             else if( p_picture == p_last_picture )
645             {
646                 if( i_idle_loops < 4 )
647                 {
648                     /* We are asked to repeat the previous picture, but we first
649                      * wait for a couple of idle loops */
650                     p_picture    = NULL;
651                     display_date = 0;
652                 }
653                 else
654                 {
655                     //intf_WarnMsg( 6, "vout info: duplicating picture" );
656                 }
657             }
658         }
659
660         if( p_picture == NULL )
661         {
662             i_idle_loops++;
663         }
664
665         /*
666          * Check for subpictures to display
667          */
668         p_subpic = vout_SortSubPictures( p_vout, display_date );
669
670         /*
671          * Perform rendering
672          */
673         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
674
675         /*
676          * Call the plugin-specific rendering method
677          */
678         if( p_picture != NULL )
679         {
680             /* Render the direct buffer returned by vout_RenderPicture */
681             p_vout->pf_render( p_vout, p_directbuffer );
682         }
683
684         /*
685          * Sleep, wake up
686          */
687         if( display_date != 0 )
688         {
689             /* Store render time using Bresenham algorithm */
690             p_vout->render_time += mdate() - current_date;
691             p_vout->render_time >>= 1;
692         }
693
694         /* Give back change lock */
695         vlc_mutex_unlock( &p_vout->change_lock );
696
697         /* Sleep a while or until a given date */
698         if( display_date != 0 )
699         {
700             mwait( display_date - VOUT_MWAIT_TOLERANCE );
701         }
702         else
703         {
704             msleep( VOUT_IDLE_SLEEP );
705         }
706
707         /* On awakening, take back lock and send immediately picture
708          * to display. */
709         vlc_mutex_lock( &p_vout->change_lock );
710
711         /*
712          * Display the previously rendered picture
713          */
714         if( p_picture != NULL )
715         {
716             /* Display the direct buffer returned by vout_RenderPicture */
717             p_vout->pf_display( p_vout, p_directbuffer );
718
719             /* Reinitialize idle loop count */
720             i_idle_loops = 0;
721
722             /* Tell the vout this was the last picture and that it does not
723              * need to be forced anymore. */
724             p_last_picture = p_picture;
725             p_last_picture->b_force = 0;
726         }
727
728         /*
729          * Check events and manage thread
730          */
731         if( p_vout->pf_manage( p_vout ) )
732         {
733             /* A fatal error occured, and the thread must terminate
734              * immediately, without displaying anything - setting b_error to 1
735              * causes the immediate end of the main while() loop. */
736             p_vout->b_error = 1;
737         }
738
739         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
740         {
741             /* this must only happen when the vout plugin is incapable of
742              * rescaling the picture itself. In this case we need to destroy
743              * the current picture buffers and recreate new ones with the right
744              * dimensions */
745             int i;
746
747             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
748
749             p_vout->pf_end( p_vout );
750             for( i = 0; i < I_OUTPUTPICTURES; i++ )
751                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
752
753             I_OUTPUTPICTURES = 0;
754             if( p_vout->pf_init( p_vout ) )
755             {
756                 intf_ErrMsg( "vout error: cannot resize display" );
757                 /* FixMe: p_vout->pf_end will be called again in EndThread() */
758                 p_vout->b_error = 1;
759             }
760
761             /* Need to reinitialise the chroma plugin */
762             p_vout->chroma.pf_end( p_vout );
763             p_vout->chroma.pf_init( p_vout );
764         }
765
766     }
767
768     /*
769      * Error loop - wait until the thread destruction is requested
770      */
771     if( p_vout->b_error )
772     {
773         ErrorThread( p_vout );
774     }
775
776     /* End of thread */
777     EndThread( p_vout );
778
779     /* Destroy method-dependant resources */
780     p_vout->pf_destroy( p_vout );
781
782     /* Destroy thread structures allocated by CreateThread */
783     DestroyThread( p_vout, THREAD_OVER );
784 }
785
786 /*****************************************************************************
787  * ErrorThread: RunThread() error loop
788  *****************************************************************************
789  * This function is called when an error occured during thread main's loop. The
790  * thread can still receive feed, but must be ready to terminate as soon as
791  * possible.
792  *****************************************************************************/
793 static void ErrorThread( vout_thread_t *p_vout )
794 {
795     /* Wait until a `die' order */
796     while( !p_vout->b_die )
797     {
798         /* Sleep a while */
799         msleep( VOUT_IDLE_SLEEP );
800     }
801 }
802
803 /*****************************************************************************
804  * EndThread: thread destruction
805  *****************************************************************************
806  * This function is called when the thread ends after a sucessful
807  * initialization. It frees all ressources allocated by InitThread.
808  *****************************************************************************/
809 static void EndThread( vout_thread_t *p_vout )
810 {
811     int     i_index;                                        /* index in heap */
812
813     /* Store status */
814     *p_vout->pi_status = THREAD_END;
815
816 #ifdef STATS
817     {
818         struct tms cpu_usage;
819         times( &cpu_usage );
820
821         intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
822                   cpu_usage.tms_utime, cpu_usage.tms_stime );
823     }
824 #endif
825
826     if( !p_vout->b_direct )
827     {
828         p_vout->chroma.pf_end( p_vout );
829         module_Unneed( p_vout->chroma.p_module );
830     }
831
832     /* Destroy all remaining pictures */
833     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
834     {
835         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
836         {
837             free( p_vout->p_picture[i_index].p_data_orig );
838         }
839     }
840
841     /* Destroy all remaining subpictures */
842     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
843     {
844         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
845         {
846             free( p_vout->p_subpicture[i_index].p_sys );
847         }
848     }
849
850     /* Destroy translation tables */
851     p_vout->pf_end( p_vout );
852
853     /* Release the change lock */
854     vlc_mutex_unlock( &p_vout->change_lock );
855 }
856
857 /*****************************************************************************
858  * DestroyThread: thread destruction
859  *****************************************************************************
860  * This function is called when the thread ends. It frees all ressources
861  * allocated by CreateThread. Status is available at this stage.
862  *****************************************************************************/
863 static void DestroyThread( vout_thread_t *p_vout, int i_status )
864 {
865     int *pi_status;                                         /* status adress */
866
867     /* Store status adress */
868     pi_status = p_vout->pi_status;
869
870     /* Destroy the locks */
871     vlc_mutex_destroy( &p_vout->picture_lock );
872     vlc_mutex_destroy( &p_vout->subpicture_lock );
873     vlc_mutex_destroy( &p_vout->change_lock );
874
875     /* Release the module */
876     module_Unneed( p_vout->p_module );
877
878     *pi_status = i_status;
879 }
880
881 /* following functions are local */
882
883 static int ReduceHeight( int i_ratio )
884 {
885     int i_dummy = VOUT_ASPECT_FACTOR;
886     int i_pgcd  = 1;
887  
888     if( !i_ratio )
889     {
890         return i_pgcd;
891     }
892
893     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
894     while( !(i_ratio & 1) && !(i_dummy & 1) )
895     {
896         i_ratio >>= 1;
897         i_dummy >>= 1;
898         i_pgcd  <<= 1;
899     }
900
901     while( !(i_ratio % 3) && !(i_dummy % 3) )
902     {
903         i_ratio /= 3;
904         i_dummy /= 3;
905         i_pgcd  *= 3;
906     }
907
908     while( !(i_ratio % 5) && !(i_dummy % 5) )
909     {
910         i_ratio /= 5;
911         i_dummy /= 5;
912         i_pgcd  *= 5;
913     }
914
915     return i_pgcd;
916 }
917
918 /*****************************************************************************
919  * BinaryLog: computes the base 2 log of a binary value
920  *****************************************************************************
921  * This functions is used by MaskToShift, to get a bit index from a binary
922  * value.
923  *****************************************************************************/
924 static int BinaryLog(u32 i)
925 {
926     int i_log = 0;
927
928     if(i & 0xffff0000) i_log += 16;
929     if(i & 0xff00ff00) i_log += 8;
930     if(i & 0xf0f0f0f0) i_log += 4;
931     if(i & 0xcccccccc) i_log += 2;
932     if(i & 0xaaaaaaaa) i_log += 1;
933
934     if (i != ((u32)1 << i_log))
935     {
936         intf_ErrMsg( "vout error: binary log overflow for %i", i );
937     }
938
939     return( i_log );
940 }
941
942 /*****************************************************************************
943  * MaskToShift: transform a color mask into right and left shifts
944  *****************************************************************************
945  * This function is used for obtaining color shifts from masks.
946  *****************************************************************************/
947 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
948 {
949     u32 i_low, i_high;                 /* lower hand higher bits of the mask */
950
951     if( !i_mask )
952     {
953         *pi_left = *pi_right = 0;
954         return;
955     }
956
957     /* Get bits */
958     i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
959     i_high = i_mask + i_low;                       /* higher bit of the mask */
960
961     /* Transform bits into an index */
962     i_low =  BinaryLog (i_low);
963     i_high = BinaryLog (i_high);
964
965     /* Update pointers and return */
966     *pi_left =   i_low;
967     *pi_right = (8 - i_high + i_low);
968 }
969
970 /*****************************************************************************
971  * InitWindowSize: find the initial dimensions the video window should have.
972  *****************************************************************************
973  * This function will check the "width", "height" and "zoom" config options and
974  * will calculate the size that the video window should have.
975  *****************************************************************************/
976 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
977                             int *pi_height )
978 {
979     int i_width, i_height;
980     double f_zoom;
981
982     i_width = config_GetIntVariable( "width" );
983     i_height = config_GetIntVariable( "height" );
984     f_zoom = config_GetFloatVariable( "zoom" );
985
986     if( (i_width >= 0) && (i_height >= 0))
987     {
988         *pi_width = i_width * f_zoom;
989         *pi_height = i_height * f_zoom;
990         return;
991     }
992     else if( i_width >= 0 )
993     {
994         *pi_width = i_width * f_zoom;
995         *pi_height = i_width * f_zoom * VOUT_ASPECT_FACTOR /
996                         p_vout->render.i_aspect;
997         return;
998     }
999     else if( i_height >= 0 )
1000     {
1001         *pi_height = i_height * f_zoom;
1002         *pi_width = i_height * f_zoom * p_vout->render.i_aspect /
1003                        VOUT_ASPECT_FACTOR;
1004         return;
1005     }
1006
1007     if( p_vout->render.i_height * p_vout->render.i_aspect
1008         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1009     {
1010         *pi_width = p_vout->render.i_height * f_zoom
1011           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
1012         *pi_height = p_vout->render.i_height * f_zoom;
1013     }
1014     else
1015     {
1016         *pi_width = p_vout->render.i_width * f_zoom;
1017         *pi_height = p_vout->render.i_width * f_zoom
1018           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
1019     }
1020 }