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.225 2003/05/25 11:31:54 gbazin Exp $
10 * Authors: Vincent Seguin <seguin@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* free() */
34 #ifdef HAVE_SYS_TIMES_H
35 # include <sys/times.h>
39 #include "video_output.h"
40 #include <vlc/input.h> /* for input_thread_t and i_pts_delay */
42 #if defined( SYS_DARWIN )
43 #include "darwin_specific.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int InitThread ( vout_thread_t * );
50 static void RunThread ( vout_thread_t * );
51 static void ErrorThread ( vout_thread_t * );
52 static void EndThread ( vout_thread_t * );
53 static void DestroyThread ( vout_thread_t * );
55 static int ReduceHeight ( int );
56 static int BinaryLog ( uint32_t );
57 static void MaskToShift ( int *, int *, uint32_t );
58 static void InitWindowSize ( vout_thread_t *, int *, int * );
60 /* Object variables callbacks */
61 static int FullscreenCallback( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
63 static int DeinterlaceCallback( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
66 /*****************************************************************************
67 * vout_Request: find a video output thread, create one, or destroy one.
68 *****************************************************************************
69 * This function looks for a video output thread matching the current
70 * properties. If not found, it spawns a new one.
71 *****************************************************************************/
72 vout_thread_t * __vout_Request ( vlc_object_t *p_this, vout_thread_t *p_vout,
73 unsigned int i_width, unsigned int i_height,
74 vlc_fourcc_t i_chroma, unsigned int i_aspect )
76 if( !i_width || !i_height || !i_chroma )
78 /* Reattach video output to input before bailing out */
81 vlc_object_t *p_input;
82 char *psz_sout = config_GetPsz( p_this, "sout" );
84 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
86 if( p_input && (!psz_sout || !*psz_sout) )
88 vlc_object_detach( p_vout );
89 vlc_object_attach( p_vout, p_input );
93 vlc_object_detach( p_vout );
94 vlc_object_release( p_vout );
95 vout_Destroy( p_vout );
97 if( psz_sout ) free( psz_sout );
98 if( p_input ) vlc_object_release( p_input );
104 /* If a video output was provided, lock it, otherwise look for one. */
107 vlc_object_yield( p_vout );
111 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
115 vlc_object_t *p_input;
117 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
120 p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT,
122 vlc_object_release( p_input );
127 /* If we now have a video output, check it has the right properties */
130 char *psz_filter_chain;
132 /* We don't directly check for the "filter" variable for obvious
133 * performance reasons. */
134 if( p_vout->b_filter_change )
136 psz_filter_chain = config_GetPsz( p_this, "filter" );
138 if( psz_filter_chain && !*psz_filter_chain )
140 free( psz_filter_chain );
141 psz_filter_chain = NULL;
143 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
145 free( p_vout->psz_filter_chain );
146 p_vout->psz_filter_chain = NULL;
149 if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
150 ( psz_filter_chain && p_vout->psz_filter_chain &&
151 !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
153 p_vout->b_filter_change = VLC_FALSE;
156 if( psz_filter_chain ) free( psz_filter_chain );
159 if( ( p_vout->render.i_width != i_width ) ||
160 ( p_vout->render.i_height != i_height ) ||
161 ( p_vout->render.i_chroma != i_chroma ) ||
162 ( p_vout->render.i_aspect != i_aspect
163 && !p_vout->b_override_aspect ) ||
164 p_vout->b_filter_change )
166 /* We are not interested in this format, close this vout */
167 vlc_object_detach( p_vout );
168 vlc_object_release( p_vout );
169 vout_Destroy( p_vout );
174 /* This video output is cool! Hijack it. */
175 vlc_object_detach( p_vout );
176 vlc_object_attach( p_vout, p_this );
177 vlc_object_release( p_vout );
183 msg_Dbg( p_this, "no usable vout present, spawning one" );
185 p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
191 /*****************************************************************************
192 * vout_Create: creates a new video output thread
193 *****************************************************************************
194 * This function creates a new video output thread, and returns a pointer
195 * to its description. On error, it returns NULL.
196 *****************************************************************************/
197 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
198 unsigned int i_width, unsigned int i_height,
199 vlc_fourcc_t i_chroma, unsigned int i_aspect )
201 vout_thread_t * p_vout; /* thread descriptor */
202 input_thread_t * p_input_thread;
203 int i_index; /* loop variable */
205 vlc_value_t val, text;
207 /* Allocate descriptor */
208 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
211 msg_Err( p_parent, "out of memory" );
215 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
216 val.b_bool = VLC_TRUE;
217 var_Set( p_vout, "intf-change", val );
219 p_vout->b_override_aspect = VLC_FALSE;
221 /* If the parent is not a VOUT object, that means we are at the start of
222 * the video output pipe */
223 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
225 char *psz_aspect = config_GetPsz( p_parent, "aspect-ratio" );
227 /* Check whether the user tried to override aspect ratio */
230 unsigned int i_new_aspect = i_aspect;
231 char *psz_parser = strchr( psz_aspect, ':' );
235 *psz_parser++ = '\0';
236 i_new_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR
237 / atoi( psz_parser );
241 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
248 if( i_new_aspect && i_new_aspect != i_aspect )
250 int i_pgcd = ReduceHeight( i_new_aspect );
252 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
253 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
255 i_aspect = i_new_aspect;
257 p_vout->b_override_aspect = VLC_TRUE;
261 /* Look for the default filter configuration */
262 p_vout->psz_filter_chain = config_GetPsz( p_parent, "filter" );
266 /* continue the parent's filter chain */
269 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
270 if( psz_end && *(psz_end+1) )
271 p_vout->psz_filter_chain = strdup( psz_end+1 );
272 else p_vout->psz_filter_chain = NULL;
275 /* Choose the video output module */
276 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
278 psz_plugin = config_GetPsz( p_parent, "vout" );
282 /* the filter chain is a string list of filters separated by double
286 psz_end = strchr( p_vout->psz_filter_chain, ':' );
288 psz_plugin = strndup( p_vout->psz_filter_chain,
289 psz_end - p_vout->psz_filter_chain );
290 else psz_plugin = strdup( p_vout->psz_filter_chain );
293 /* Initialize pictures and subpictures - translation tables and functions
294 * will be initialized later in InitThread */
295 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
297 p_vout->p_picture[i_index].pf_lock = NULL;
298 p_vout->p_picture[i_index].pf_unlock = NULL;
299 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
300 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
303 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
305 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
306 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
309 /* No images in the heap */
310 p_vout->i_heap_size = 0;
312 /* Initialize the rendering heap */
313 I_RENDERPICTURES = 0;
314 p_vout->render.i_width = i_width;
315 p_vout->render.i_height = i_height;
316 p_vout->render.i_chroma = i_chroma;
317 p_vout->render.i_aspect = i_aspect;
319 p_vout->render.i_rmask = 0;
320 p_vout->render.i_gmask = 0;
321 p_vout->render.i_bmask = 0;
323 p_vout->render.i_last_used_pic = -1;
324 p_vout->render.b_allow_modify_pics = 1;
326 /* Zero the output heap */
327 I_OUTPUTPICTURES = 0;
328 p_vout->output.i_width = 0;
329 p_vout->output.i_height = 0;
330 p_vout->output.i_chroma = 0;
331 p_vout->output.i_aspect = 0;
333 p_vout->output.i_rmask = 0;
334 p_vout->output.i_gmask = 0;
335 p_vout->output.i_bmask = 0;
337 /* Initialize misc stuff */
338 p_vout->i_changes = 0;
340 p_vout->b_grayscale = 0;
342 p_vout->b_interface = 0;
344 p_vout->b_fullscreen = 0;
345 p_vout->render_time = 10;
346 p_vout->c_fps_samples = 0;
347 p_vout->b_filter_change = 0;
349 /* Mouse coordinates */
350 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
351 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
352 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
353 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
354 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
355 var_Create( p_vout, "key-pressed", VLC_VAR_STRING );
357 /* Initialize the dimensions of the video window */
358 InitWindowSize( p_vout, &p_vout->i_window_width,
359 &p_vout->i_window_height );
361 /* Create thread and set locks */
362 vlc_mutex_init( p_vout, &p_vout->picture_lock );
363 vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
364 vlc_mutex_init( p_vout, &p_vout->change_lock );
366 vlc_object_attach( p_vout, p_parent );
368 p_vout->p_module = module_Need( p_vout,
369 ( p_vout->psz_filter_chain &&
370 *p_vout->psz_filter_chain ) ?
371 "video filter" : "video output",
374 if( psz_plugin ) free( psz_plugin );
375 if( p_vout->p_module == NULL )
377 msg_Err( p_vout, "no suitable vout module" );
378 vlc_object_destroy( p_vout );
382 /* Create a few object variables for interface interaction */
383 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
384 text.psz_string = _("Fullscreen");
385 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
386 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
389 /* user requested fullscreen */
390 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
392 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
394 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
395 text.psz_string = _("Deinterlace");
396 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
397 val.psz_string = ""; text.psz_string = _("Disable");
398 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
399 val.psz_string = "discard"; text.psz_string = _("Discard");
400 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
401 val.psz_string = "blend"; text.psz_string = _("Blend");
402 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
403 val.psz_string = "mean"; text.psz_string = _("Mean");
404 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
405 val.psz_string = "bob"; text.psz_string = _("Bob");
406 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
407 val.psz_string = "linear"; text.psz_string = _("Linear");
408 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
409 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
411 var_Set( p_vout, "deinterlace", val );
413 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
415 /* Calculate delay created by internal caching */
416 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
417 VLC_OBJECT_INPUT, FIND_ANYWHERE );
420 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
421 vlc_object_release( p_input_thread );
425 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
428 if( vlc_thread_create( p_vout, "video output", RunThread,
429 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
431 msg_Err( p_vout, "out of memory" );
432 module_Unneed( p_vout, p_vout->p_module );
433 vlc_object_destroy( p_vout );
440 /*****************************************************************************
441 * vout_Destroy: destroys a previously created video output
442 *****************************************************************************
443 * Destroy a terminated thread.
444 * The function will request a destruction of the specified thread. If pi_error
445 * is NULL, it will return once the thread is destroyed. Else, it will be
446 * update using one of the THREAD_* constants.
447 *****************************************************************************/
448 void vout_Destroy( vout_thread_t *p_vout )
450 /* Request thread destruction */
451 p_vout->b_die = VLC_TRUE;
452 vlc_thread_join( p_vout );
454 var_Destroy( p_vout, "intf-change" );
457 vlc_object_destroy( p_vout );
460 /*****************************************************************************
461 * InitThread: initialize video output thread
462 *****************************************************************************
463 * This function is called from RunThread and performs the second step of the
464 * initialization. It returns 0 on success. Note that the thread's flag are not
465 * modified inside this function.
466 *****************************************************************************/
467 static int InitThread( vout_thread_t *p_vout )
471 vlc_mutex_lock( &p_vout->change_lock );
477 /* Initialize output method, it allocates direct buffers for us */
478 if( p_vout->pf_init( p_vout ) )
480 vlc_mutex_unlock( &p_vout->change_lock );
484 if( !I_OUTPUTPICTURES )
486 msg_Err( p_vout, "plugin was unable to allocate at least "
487 "one direct buffer" );
488 p_vout->pf_end( p_vout );
489 vlc_mutex_unlock( &p_vout->change_lock );
493 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
495 msg_Err( p_vout, "plugin allocated too many direct buffers, "
496 "our internal buffers must have overflown." );
497 p_vout->pf_end( p_vout );
498 vlc_mutex_unlock( &p_vout->change_lock );
502 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
505 if( !p_vout->psz_filter_chain )
507 char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
511 int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
513 / p_vout->output.i_height;
516 if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
518 int i_pgcd = ReduceHeight( i_new_aspect );
519 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
520 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
521 p_vout->output.i_aspect = i_new_aspect;
527 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
529 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
530 p_vout->render.i_width, p_vout->render.i_height,
531 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
532 p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
534 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
536 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
537 p_vout->output.i_width, p_vout->output.i_height,
538 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
539 p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
541 /* Calculate shifts from system-updated masks */
542 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
543 p_vout->output.i_rmask );
544 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
545 p_vout->output.i_gmask );
546 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
547 p_vout->output.i_bmask );
549 /* Check whether we managed to create direct buffers similar to
550 * the render buffers, ie same size and chroma */
551 if( ( p_vout->output.i_width == p_vout->render.i_width )
552 && ( p_vout->output.i_height == p_vout->render.i_height )
553 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
555 /* Cool ! We have direct buffers, we can ask the decoder to
556 * directly decode into them ! Map the first render buffers to
557 * the first direct buffers, but keep the first direct buffer
558 * for memcpy operations */
559 p_vout->b_direct = 1;
561 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
563 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
564 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
565 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
567 /* We have enough direct buffers so there's no need to
568 * try to use system memory buffers. */
571 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
575 msg_Dbg( p_vout, "direct render, mapping "
576 "render pictures 0-%i to system pictures 1-%i",
577 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
581 /* Rats... Something is wrong here, we could not find an output
582 * plugin able to directly render what we decode. See if we can
583 * find a chroma plugin to do the conversion */
584 p_vout->b_direct = 0;
586 /* Choose the best module */
587 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
589 if( p_vout->chroma.p_module == NULL )
591 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
592 (char*)&p_vout->render.i_chroma,
593 (char*)&p_vout->output.i_chroma );
594 p_vout->pf_end( p_vout );
595 vlc_mutex_unlock( &p_vout->change_lock );
599 msg_Dbg( p_vout, "indirect render, mapping "
600 "render pictures 0-%i to system pictures %i-%i",
601 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
602 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
604 /* Append render buffers after the direct buffers */
605 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
607 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
610 /* Check if we have enough render pictures */
611 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
616 /* Link pictures back to their heap */
617 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
619 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
622 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
624 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
627 /* XXX XXX mark thread ready */
631 /*****************************************************************************
632 * RunThread: video output thread
633 *****************************************************************************
634 * Video output thread. This function does only returns when the thread is
635 * terminated. It handles the pictures arriving in the video heap and the
636 * display device events.
637 *****************************************************************************/
638 static void RunThread( vout_thread_t *p_vout)
640 int i_index; /* index in heap */
641 int i_idle_loops = 0; /* loops without displaying a picture */
642 mtime_t current_date; /* current date */
643 mtime_t display_date; /* display date */
645 picture_t * p_picture; /* picture pointer */
646 picture_t * p_last_picture = NULL; /* last picture */
647 picture_t * p_directbuffer; /* direct buffer to display */
649 subpicture_t * p_subpic; /* subpicture pointer */
654 p_vout->b_error = InitThread( p_vout );
656 /* signal the creation of the vout */
657 vlc_thread_ready( p_vout );
659 if( p_vout->b_error )
661 /* Destroy thread structures allocated by Create and InitThread */
662 DestroyThread( p_vout );
667 * Main loop - it is not executed if an error occured during
670 while( (!p_vout->b_die) && (!p_vout->b_error) )
672 /* Initialize loop variables */
675 current_date = mdate();
679 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
681 msg_Dbg( p_vout, "picture heap: %d/%d",
682 I_RENDERPICTURES, p_vout->i_heap_size );
687 * Find the picture to display (the one with the earliest date).
688 * This operation does not need lock, since only READY_PICTUREs
690 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
692 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
693 && ( (p_picture == NULL) ||
694 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
696 p_picture = PP_RENDERPICTURE[i_index];
697 display_date = p_picture->date;
703 /* If we met the last picture, parse again to see whether there is
704 * a more appropriate one. */
705 if( p_picture == p_last_picture )
707 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
709 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
710 && (PP_RENDERPICTURE[i_index] != p_last_picture)
711 && ((p_picture == p_last_picture) ||
712 (PP_RENDERPICTURE[i_index]->date < display_date)) )
714 p_picture = PP_RENDERPICTURE[i_index];
715 display_date = p_picture->date;
720 /* If we found better than the last picture, destroy it */
721 if( p_last_picture && p_picture != p_last_picture )
723 vlc_mutex_lock( &p_vout->picture_lock );
724 if( p_last_picture->i_refcount )
726 p_last_picture->i_status = DISPLAYED_PICTURE;
730 p_last_picture->i_status = DESTROYED_PICTURE;
731 p_vout->i_heap_size--;
733 vlc_mutex_unlock( &p_vout->picture_lock );
734 p_last_picture = NULL;
737 /* Compute FPS rate */
738 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
741 if( !p_picture->b_force &&
742 p_picture != p_last_picture &&
743 display_date < current_date + p_vout->render_time )
745 /* Picture is late: it will be destroyed and the thread
746 * will directly choose the next picture */
747 vlc_mutex_lock( &p_vout->picture_lock );
748 if( p_picture->i_refcount )
750 /* Pretend we displayed the picture, but don't destroy
751 * it since the decoder might still need it. */
752 p_picture->i_status = DISPLAYED_PICTURE;
756 /* Destroy the picture without displaying it */
757 p_picture->i_status = DESTROYED_PICTURE;
758 p_vout->i_heap_size--;
760 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
761 current_date - display_date );
762 vlc_mutex_unlock( &p_vout->picture_lock );
768 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
770 /* Picture is waaay too early: it will be destroyed */
771 vlc_mutex_lock( &p_vout->picture_lock );
772 if( p_picture->i_refcount )
774 /* Pretend we displayed the picture, but don't destroy
775 * it since the decoder might still need it. */
776 p_picture->i_status = DISPLAYED_PICTURE;
780 /* Destroy the picture without displaying it */
781 p_picture->i_status = DESTROYED_PICTURE;
782 p_vout->i_heap_size--;
784 msg_Warn( p_vout, "vout warning: early picture skipped "
785 "("I64Fd")", display_date - current_date
786 - p_vout->i_pts_delay );
787 vlc_mutex_unlock( &p_vout->picture_lock );
792 if( display_date > current_date + VOUT_DISPLAY_DELAY )
794 /* A picture is ready to be rendered, but its rendering date
795 * is far from the current one so the thread will perform an
796 * empty loop as if no picture were found. The picture state
801 else if( p_picture == p_last_picture )
803 /* We are asked to repeat the previous picture, but we first
804 * wait for a couple of idle loops */
805 if( i_idle_loops < 4 )
812 /* We set the display date to something high, otherwise
813 * we'll have lots of problems with late pictures */
814 display_date = current_date + p_vout->render_time;
819 if( p_picture == NULL )
825 * Check for subpictures to display
827 p_subpic = vout_SortSubPictures( p_vout, display_date );
832 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
835 * Call the plugin-specific rendering method if there is one
837 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
839 /* Render the direct buffer returned by vout_RenderPicture */
840 p_vout->pf_render( p_vout, p_directbuffer );
846 if( display_date != 0 && p_directbuffer != NULL )
848 /* Store render time using a sliding mean */
849 p_vout->render_time += mdate() - current_date;
850 p_vout->render_time >>= 1;
853 /* Give back change lock */
854 vlc_mutex_unlock( &p_vout->change_lock );
856 /* Sleep a while or until a given date */
857 if( display_date != 0 )
859 mwait( display_date - VOUT_MWAIT_TOLERANCE );
863 msleep( VOUT_IDLE_SLEEP );
866 /* On awakening, take back lock and send immediately picture
868 vlc_mutex_lock( &p_vout->change_lock );
871 * Display the previously rendered picture
873 if( p_picture != NULL && p_directbuffer != NULL )
875 /* Display the direct buffer returned by vout_RenderPicture */
876 if( p_vout->pf_display )
878 p_vout->pf_display( p_vout, p_directbuffer );
881 /* Reinitialize idle loop count */
884 /* Tell the vout this was the last picture and that it does not
885 * need to be forced anymore. */
886 p_last_picture = p_picture;
887 p_last_picture->b_force = 0;
891 * Check events and manage thread
893 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
895 /* A fatal error occured, and the thread must terminate
896 * immediately, without displaying anything - setting b_error to 1
897 * causes the immediate end of the main while() loop. */
901 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
903 /* this must only happen when the vout plugin is incapable of
904 * rescaling the picture itself. In this case we need to destroy
905 * the current picture buffers and recreate new ones with the right
909 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
911 p_vout->pf_end( p_vout );
912 for( i = 0; i < I_OUTPUTPICTURES; i++ )
913 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
915 I_OUTPUTPICTURES = 0;
916 if( p_vout->pf_init( p_vout ) )
918 msg_Err( p_vout, "cannot resize display" );
919 /* FIXME: pf_end will be called again in EndThread() */
923 /* Need to reinitialise the chroma plugin */
924 if( p_vout->chroma.p_module->pf_deactivate )
925 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
926 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
931 * Error loop - wait until the thread destruction is requested
933 if( p_vout->b_error )
935 ErrorThread( p_vout );
941 /* Destroy thread structures allocated by CreateThread */
942 DestroyThread( p_vout );
945 /*****************************************************************************
946 * ErrorThread: RunThread() error loop
947 *****************************************************************************
948 * This function is called when an error occured during thread main's loop. The
949 * thread can still receive feed, but must be ready to terminate as soon as
951 *****************************************************************************/
952 static void ErrorThread( vout_thread_t *p_vout )
954 /* Wait until a `die' order */
955 while( !p_vout->b_die )
958 msleep( VOUT_IDLE_SLEEP );
962 /*****************************************************************************
963 * EndThread: thread destruction
964 *****************************************************************************
965 * This function is called when the thread ends after a sucessful
966 * initialization. It frees all ressources allocated by InitThread.
967 *****************************************************************************/
968 static void EndThread( vout_thread_t *p_vout )
970 int i_index; /* index in heap */
974 struct tms cpu_usage;
977 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
978 cpu_usage.tms_utime, cpu_usage.tms_stime );
982 if( !p_vout->b_direct )
984 module_Unneed( p_vout, p_vout->chroma.p_module );
987 /* Destroy all remaining pictures */
988 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
990 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
992 free( p_vout->p_picture[i_index].p_data_orig );
996 /* Destroy all remaining subpictures */
997 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
999 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1001 vout_DestroySubPicture( p_vout,
1002 &p_vout->p_subpicture[i_index] );
1006 /* Destroy translation tables */
1007 p_vout->pf_end( p_vout );
1009 /* Release the change lock */
1010 vlc_mutex_unlock( &p_vout->change_lock );
1013 /*****************************************************************************
1014 * DestroyThread: thread destruction
1015 *****************************************************************************
1016 * This function is called when the thread ends. It frees all ressources
1017 * allocated by CreateThread. Status is available at this stage.
1018 *****************************************************************************/
1019 static void DestroyThread( vout_thread_t *p_vout )
1021 /* Destroy the locks */
1022 vlc_mutex_destroy( &p_vout->picture_lock );
1023 vlc_mutex_destroy( &p_vout->subpicture_lock );
1024 vlc_mutex_destroy( &p_vout->change_lock );
1026 /* Release the module */
1027 module_Unneed( p_vout, p_vout->p_module );
1030 /* following functions are local */
1032 static int ReduceHeight( int i_ratio )
1034 int i_dummy = VOUT_ASPECT_FACTOR;
1042 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1043 while( !(i_ratio & 1) && !(i_dummy & 1) )
1050 while( !(i_ratio % 3) && !(i_dummy % 3) )
1057 while( !(i_ratio % 5) && !(i_dummy % 5) )
1067 /*****************************************************************************
1068 * BinaryLog: computes the base 2 log of a binary value
1069 *****************************************************************************
1070 * This functions is used by MaskToShift, to get a bit index from a binary
1072 *****************************************************************************/
1073 static int BinaryLog( uint32_t i )
1077 if( i == 0 ) return -31337;
1079 if( i & 0xffff0000 ) i_log += 16;
1080 if( i & 0xff00ff00 ) i_log += 8;
1081 if( i & 0xf0f0f0f0 ) i_log += 4;
1082 if( i & 0xcccccccc ) i_log += 2;
1083 if( i & 0xaaaaaaaa ) i_log += 1;
1088 /*****************************************************************************
1089 * MaskToShift: transform a color mask into right and left shifts
1090 *****************************************************************************
1091 * This function is used for obtaining color shifts from masks.
1092 *****************************************************************************/
1093 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1095 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1099 *pi_left = *pi_right = 0;
1104 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1105 i_high = i_mask + i_low; /* higher bit of the mask */
1107 /* Transform bits into an index */
1108 i_low = BinaryLog (i_low);
1109 i_high = BinaryLog (i_high);
1111 /* Update pointers and return */
1113 *pi_right = (8 - i_high + i_low);
1116 /*****************************************************************************
1117 * InitWindowSize: find the initial dimensions the video window should have.
1118 *****************************************************************************
1119 * This function will check the "width", "height" and "zoom" config options and
1120 * will calculate the size that the video window should have.
1121 *****************************************************************************/
1122 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1125 int i_width, i_height;
1128 #define FP_FACTOR 1000 /* our fixed point factor */
1130 i_width = config_GetInt( p_vout, "width" );
1131 i_height = config_GetInt( p_vout, "height" );
1132 ll_zoom = (uint64_t)( FP_FACTOR * config_GetFloat( p_vout, "zoom" ) );
1134 if( i_width > 0 && i_height > 0)
1136 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1137 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1140 else if( i_width > 0 )
1142 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1143 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1144 p_vout->render.i_aspect / FP_FACTOR );
1147 else if( i_height > 0 )
1149 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1150 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1151 VOUT_ASPECT_FACTOR / FP_FACTOR );
1155 if( p_vout->render.i_height * p_vout->render.i_aspect
1156 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1158 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1159 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1160 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1164 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1165 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1166 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1172 /*****************************************************************************
1173 * vout_VarCallback: generic callback for intf variables
1174 *****************************************************************************/
1175 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1176 vlc_value_t old_value, vlc_value_t new_value,
1179 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1181 val.b_bool = VLC_TRUE;
1182 var_Set( p_vout, "intf-change", val );
1186 /*****************************************************************************
1187 * object variables callbacks: a bunch of object variables are used by the
1188 * interfaces to interact with the vout.
1189 *****************************************************************************/
1190 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1191 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1193 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1194 input_thread_t *p_input;
1197 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1199 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1203 /* Modify input as well because the vout might have to be restarted */
1204 var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1205 var_Set( p_input, "fullscreen", newval );
1207 vlc_object_release( p_input );
1210 val.b_bool = VLC_TRUE;
1211 var_Set( p_vout, "intf-change", val );
1215 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1216 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1218 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1219 input_thread_t *p_input;
1222 char *psz_mode = newval.psz_string;
1226 psz_filter = config_GetPsz( p_vout, "filter" );
1228 if( !psz_mode || !*psz_mode )
1230 config_PutPsz( p_vout, "filter", "" );
1234 if( !psz_filter || !*psz_filter )
1236 config_PutPsz( p_vout, "filter", "deinterlace" );
1240 if( strstr( psz_filter, "deinterlace" ) == NULL )
1242 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1243 strcat( psz_filter, ",deinterlace" );
1245 config_PutPsz( p_vout, "filter", psz_filter );
1249 if( psz_filter ) free( psz_filter );
1252 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1254 if( !p_input ) return VLC_EGENERIC;
1256 if( psz_mode && *psz_mode )
1258 val.psz_string = psz_mode;
1259 var_Set( p_vout, "deinterlace-mode", val );
1260 /* Modify input as well because the vout might have to be restarted */
1261 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1262 var_Set( p_input, "deinterlace-mode", val );
1265 /* now restart all video streams */
1266 vlc_mutex_lock( &p_input->stream.stream_lock );
1268 p_vout->b_filter_change = VLC_TRUE;
1270 #define ES p_input->stream.pp_es[i]
1272 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1274 if( ( ES->i_cat == VIDEO_ES ) && ES->p_decoder_fifo != NULL )
1276 input_UnselectES( p_input, ES );
1277 input_SelectES( p_input, ES );
1281 vlc_mutex_unlock( &p_input->stream.stream_lock );
1283 vlc_object_release( p_input );
1285 val.b_bool = VLC_TRUE;
1286 var_Set( p_vout, "intf-change", val );