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