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-2004 VideoLAN
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>
38 #include "vlc_video.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 void vout_IntfInit( vout_thread_t * );
62 /* Object variables callbacks */
63 static int FullscreenCallback( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
65 static int DeinterlaceCallback( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
67 static int FilterCallback( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
73 * Set the i_aspect_x and i_aspect_y from i_aspect.
75 void vout_AspectRatio( unsigned int i_aspect,
76 /*out*/ unsigned int *i_aspect_x,
77 /*out*/ unsigned int *i_aspect_y )
79 unsigned int i_pgcd = ReduceHeight( i_aspect );
80 *i_aspect_x = i_aspect / i_pgcd;
81 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
84 /*****************************************************************************
85 * vout_Request: find a video output thread, create one, or destroy one.
86 *****************************************************************************
87 * This function looks for a video output thread matching the current
88 * properties. If not found, it spawns a new one.
89 *****************************************************************************/
90 vout_thread_t * __vout_Request ( vlc_object_t *p_this, vout_thread_t *p_vout,
91 unsigned int i_width, unsigned int i_height,
92 vlc_fourcc_t i_chroma, unsigned int i_aspect )
94 if( !i_width || !i_height || !i_chroma )
96 /* Reattach video output to input before bailing out */
99 vlc_object_t *p_playlist;
101 p_playlist = vlc_object_find( p_this,
102 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
106 vlc_object_detach( p_vout );
107 vlc_object_attach( p_vout, p_playlist );
109 vlc_object_release( p_playlist );
113 msg_Dbg( p_this, "cannot find playlist, destroying vout" );
114 vlc_object_detach( p_vout );
115 vout_Destroy( p_vout );
121 /* If a video output was provided, lock it, otherwise look for one. */
124 vlc_object_yield( p_vout );
128 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
132 vlc_object_t *p_playlist;
134 p_playlist = vlc_object_find( p_this,
135 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
138 p_vout = vlc_object_find( p_playlist,
139 VLC_OBJECT_VOUT, FIND_CHILD );
140 /* only first children of p_input for unused vout */
141 if( p_vout && p_vout->p_parent != p_playlist )
143 vlc_object_release( p_vout );
146 vlc_object_release( p_playlist );
151 /* If we now have a video output, check it has the right properties */
154 char *psz_filter_chain;
156 /* We don't directly check for the "filter" variable for obvious
157 * performance reasons. */
158 if( p_vout->b_filter_change )
160 psz_filter_chain = config_GetPsz( p_this, "filter" );
162 if( psz_filter_chain && !*psz_filter_chain )
164 free( psz_filter_chain );
165 psz_filter_chain = NULL;
167 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
169 free( p_vout->psz_filter_chain );
170 p_vout->psz_filter_chain = NULL;
173 if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
174 ( psz_filter_chain && p_vout->psz_filter_chain &&
175 !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
177 p_vout->b_filter_change = VLC_FALSE;
180 if( psz_filter_chain ) free( psz_filter_chain );
183 if( ( p_vout->render.i_width != i_width ) ||
184 ( p_vout->render.i_height != i_height ) ||
185 ( p_vout->render.i_chroma != i_chroma ) ||
186 ( p_vout->render.i_aspect != i_aspect
187 && !p_vout->b_override_aspect ) ||
188 p_vout->b_filter_change )
190 /* We are not interested in this format, close this vout */
191 vlc_object_detach( p_vout );
192 vlc_object_release( p_vout );
193 vout_Destroy( p_vout );
198 /* This video output is cool! Hijack it. */
199 vlc_object_detach( p_vout );
200 vlc_object_attach( p_vout, p_this );
201 vlc_object_release( p_vout );
207 msg_Dbg( p_this, "no usable vout present, spawning one" );
209 p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
215 /*****************************************************************************
216 * vout_Create: creates a new video output thread
217 *****************************************************************************
218 * This function creates a new video output thread, and returns a pointer
219 * to its description. On error, it returns NULL.
220 *****************************************************************************/
221 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
222 unsigned int i_width, unsigned int i_height,
223 vlc_fourcc_t i_chroma, unsigned int i_aspect )
225 vout_thread_t * p_vout; /* thread descriptor */
226 input_thread_t * p_input_thread;
227 int i_index; /* loop variable */
229 vlc_value_t val, text;
231 /* Allocate descriptor */
232 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
235 msg_Err( p_parent, "out of memory" );
239 /* Initialize pictures and subpictures - translation tables and functions
240 * will be initialized later in InitThread */
241 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
243 p_vout->p_picture[i_index].pf_lock = NULL;
244 p_vout->p_picture[i_index].pf_unlock = NULL;
245 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
246 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
249 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
251 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
252 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
255 /* No images in the heap */
256 p_vout->i_heap_size = 0;
258 /* Initialize the rendering heap */
259 I_RENDERPICTURES = 0;
260 p_vout->render.i_width = i_width;
261 p_vout->render.i_height = i_height;
262 p_vout->render.i_chroma = i_chroma;
263 p_vout->render.i_aspect = i_aspect;
265 p_vout->render.i_rmask = 0;
266 p_vout->render.i_gmask = 0;
267 p_vout->render.i_bmask = 0;
269 p_vout->render.i_last_used_pic = -1;
270 p_vout->render.b_allow_modify_pics = 1;
272 /* Zero the output heap */
273 I_OUTPUTPICTURES = 0;
274 p_vout->output.i_width = 0;
275 p_vout->output.i_height = 0;
276 p_vout->output.i_chroma = 0;
277 p_vout->output.i_aspect = 0;
279 p_vout->output.i_rmask = 0;
280 p_vout->output.i_gmask = 0;
281 p_vout->output.i_bmask = 0;
283 /* Initialize misc stuff */
284 p_vout->i_changes = 0;
286 p_vout->b_grayscale = 0;
288 p_vout->b_interface = 0;
290 p_vout->b_fullscreen = 0;
291 p_vout->i_alignment = 0;
292 p_vout->render_time = 10;
293 p_vout->c_fps_samples = 0;
294 p_vout->b_filter_change = 0;
295 p_vout->pf_control = 0;
297 /* Mouse coordinates */
298 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
299 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
300 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
301 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
302 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
304 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
305 val.b_bool = VLC_TRUE;
306 var_Set( p_vout, "intf-change", val );
308 /* Initialize locks */
309 vlc_mutex_init( p_vout, &p_vout->picture_lock );
310 vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
311 vlc_mutex_init( p_vout, &p_vout->change_lock );
313 /* Attach the new object now so we can use var inheritance below */
314 vlc_object_attach( p_vout, p_parent );
316 /* Take care of some "interface/control" related initialisations */
317 vout_IntfInit( p_vout );
319 p_vout->b_override_aspect = VLC_FALSE;
321 /* If the parent is not a VOUT object, that means we are at the start of
322 * the video output pipe */
323 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
325 var_Get( p_vout, "aspect-ratio", &val );
327 /* Check whether the user tried to override aspect ratio */
330 unsigned int i_new_aspect = i_aspect;
331 char *psz_parser = strchr( val.psz_string, ':' );
335 *psz_parser++ = '\0';
336 i_new_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
337 / atoi( psz_parser );
341 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
342 * atof( val.psz_string )
346 free( val.psz_string );
348 if( i_new_aspect && i_new_aspect != i_aspect )
350 unsigned int i_aspect_x, i_aspect_y;
352 vout_AspectRatio( i_new_aspect, &i_aspect_x, &i_aspect_y );
354 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
355 i_aspect_x, i_aspect_y );
357 p_vout->render.i_aspect = i_new_aspect;
359 p_vout->b_override_aspect = VLC_TRUE;
363 /* Look for the default filter configuration */
364 var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
365 var_Get( p_vout, "filter", &val );
366 p_vout->psz_filter_chain = val.psz_string;
370 /* continue the parent's filter chain */
373 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
374 if( psz_end && *(psz_end+1) )
375 p_vout->psz_filter_chain = strdup( psz_end+1 );
376 else p_vout->psz_filter_chain = NULL;
379 /* Choose the video output module */
380 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
382 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
383 var_Get( p_vout, "vout", &val );
384 psz_plugin = val.psz_string;
388 /* the filter chain is a string list of filters separated by double
392 psz_end = strchr( p_vout->psz_filter_chain, ':' );
394 psz_plugin = strndup( p_vout->psz_filter_chain,
395 psz_end - p_vout->psz_filter_chain );
396 else psz_plugin = strdup( p_vout->psz_filter_chain );
399 /* Initialize the dimensions of the video window */
400 InitWindowSize( p_vout, &p_vout->i_window_width,
401 &p_vout->i_window_height );
403 /* Create the vout thread */
404 p_vout->p_module = module_Need( p_vout,
405 ( p_vout->psz_filter_chain && *p_vout->psz_filter_chain ) ?
406 "video filter" : "video output", psz_plugin, 0 );
408 if( psz_plugin ) free( psz_plugin );
409 if( p_vout->p_module == NULL )
411 msg_Err( p_vout, "no suitable vout module" );
412 vlc_object_destroy( p_vout );
416 p_vout->p_text_renderer_module =
417 module_Need( p_vout, "text renderer", NULL, 0 );
418 if( p_vout->p_text_renderer_module == NULL )
420 msg_Warn( p_vout, "no suitable text renderer module" );
421 p_vout->pf_add_string = NULL;
424 /* Create a few object variables for interface interaction */
425 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
426 text.psz_string = _("Fullscreen");
427 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
428 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
431 /* user requested fullscreen */
432 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
434 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
436 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
437 text.psz_string = _("Deinterlace");
438 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
439 val.psz_string = ""; text.psz_string = _("Disable");
440 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
441 val.psz_string = "discard"; text.psz_string = _("Discard");
442 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
443 val.psz_string = "blend"; text.psz_string = _("Blend");
444 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
445 val.psz_string = "mean"; text.psz_string = _("Mean");
446 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
447 val.psz_string = "bob"; text.psz_string = _("Bob");
448 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
449 val.psz_string = "linear"; text.psz_string = _("Linear");
450 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
451 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
453 var_Set( p_vout, "deinterlace", val );
454 if( val.psz_string ) free( val.psz_string );
456 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
459 var_Create( p_vout, "filter", VLC_VAR_STRING );
460 text.psz_string = _("Filters");
461 var_Change( p_vout, "filter", VLC_VAR_SETTEXT, &text, NULL );
462 var_Change( p_vout, "filter", VLC_VAR_INHERITVALUE, &val, NULL );
465 var_Set( p_vout, "filter", val );
466 free( val.psz_string );
468 var_AddCallback( p_vout, "filter", FilterCallback, NULL );
470 /* Calculate delay created by internal caching */
471 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
472 VLC_OBJECT_INPUT, FIND_ANYWHERE );
475 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
476 vlc_object_release( p_input_thread );
480 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
483 if( vlc_thread_create( p_vout, "video output", RunThread,
484 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
486 msg_Err( p_vout, "out of memory" );
487 module_Unneed( p_vout, p_vout->p_module );
488 vlc_object_destroy( p_vout );
495 /*****************************************************************************
496 * vout_Destroy: destroys a previously created video output
497 *****************************************************************************
498 * Destroy a terminated thread.
499 * The function will request a destruction of the specified thread. If pi_error
500 * is NULL, it will return once the thread is destroyed. Else, it will be
501 * update using one of the THREAD_* constants.
502 *****************************************************************************/
503 void vout_Destroy( vout_thread_t *p_vout )
505 vlc_object_t *p_playlist;
507 /* Request thread destruction */
508 p_vout->b_die = VLC_TRUE;
509 vlc_thread_join( p_vout );
511 var_Destroy( p_vout, "intf-change" );
513 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
516 if( p_vout->psz_filter_chain ) free( p_vout->psz_filter_chain );
519 vlc_object_destroy( p_vout );
521 /* If it was the last vout, tell the interface to show up */
522 if( p_playlist != NULL )
524 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
525 VLC_OBJECT_VOUT, FIND_ANYWHERE );
526 if( p_another_vout == NULL )
529 val.b_bool = VLC_TRUE;
530 var_Set( p_playlist, "intf-show", val );
534 vlc_object_release( p_another_vout );
536 vlc_object_release( p_playlist );
540 /*****************************************************************************
541 * InitThread: initialize video output thread
542 *****************************************************************************
543 * This function is called from RunThread and performs the second step of the
544 * initialization. It returns 0 on success. Note that the thread's flag are not
545 * modified inside this function.
546 *****************************************************************************/
547 static int InitThread( vout_thread_t *p_vout )
550 unsigned int i_aspect_x, i_aspect_y;
552 vlc_mutex_lock( &p_vout->change_lock );
558 /* Initialize output method, it allocates direct buffers for us */
559 if( p_vout->pf_init( p_vout ) )
561 vlc_mutex_unlock( &p_vout->change_lock );
565 if( !I_OUTPUTPICTURES )
567 msg_Err( p_vout, "plugin was unable to allocate at least "
568 "one direct buffer" );
569 p_vout->pf_end( p_vout );
570 vlc_mutex_unlock( &p_vout->change_lock );
574 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
576 msg_Err( p_vout, "plugin allocated too many direct buffers, "
577 "our internal buffers must have overflown." );
578 p_vout->pf_end( p_vout );
579 vlc_mutex_unlock( &p_vout->change_lock );
583 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
586 if( !p_vout->psz_filter_chain )
588 char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
592 int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
594 / p_vout->output.i_height;
597 if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
599 vout_AspectRatio( i_new_aspect, &i_aspect_x, &i_aspect_y );
601 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
602 i_aspect_x, i_aspect_y );
603 p_vout->output.i_aspect = i_new_aspect;
609 vout_AspectRatio( p_vout->render.i_aspect, &i_aspect_x, &i_aspect_y );
611 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
612 p_vout->render.i_width, p_vout->render.i_height,
613 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
614 i_aspect_x, i_aspect_y );
616 vout_AspectRatio( p_vout->output.i_aspect, &i_aspect_x, &i_aspect_y );
618 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
619 p_vout->output.i_width, p_vout->output.i_height,
620 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
621 i_aspect_x, i_aspect_y );
623 /* Calculate shifts from system-updated masks */
624 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
625 p_vout->output.i_rmask );
626 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
627 p_vout->output.i_gmask );
628 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
629 p_vout->output.i_bmask );
631 /* Check whether we managed to create direct buffers similar to
632 * the render buffers, ie same size and chroma */
633 if( ( p_vout->output.i_width == p_vout->render.i_width )
634 && ( p_vout->output.i_height == p_vout->render.i_height )
635 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
637 /* Cool ! We have direct buffers, we can ask the decoder to
638 * directly decode into them ! Map the first render buffers to
639 * the first direct buffers, but keep the first direct buffer
640 * for memcpy operations */
641 p_vout->b_direct = 1;
643 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
645 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
646 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
647 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
649 /* We have enough direct buffers so there's no need to
650 * try to use system memory buffers. */
653 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
657 msg_Dbg( p_vout, "direct render, mapping "
658 "render pictures 0-%i to system pictures 1-%i",
659 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
663 /* Rats... Something is wrong here, we could not find an output
664 * plugin able to directly render what we decode. See if we can
665 * find a chroma plugin to do the conversion */
666 p_vout->b_direct = 0;
668 /* Choose the best module */
669 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
671 if( p_vout->chroma.p_module == NULL )
673 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
674 (char*)&p_vout->render.i_chroma,
675 (char*)&p_vout->output.i_chroma );
676 p_vout->pf_end( p_vout );
677 vlc_mutex_unlock( &p_vout->change_lock );
681 msg_Dbg( p_vout, "indirect render, mapping "
682 "render pictures 0-%i to system pictures %i-%i",
683 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
684 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
686 /* Append render buffers after the direct buffers */
687 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
689 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
692 /* Check if we have enough render pictures */
693 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
698 /* Link pictures back to their heap */
699 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
701 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
704 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
706 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
709 /* XXX XXX mark thread ready */
713 /*****************************************************************************
714 * RunThread: video output thread
715 *****************************************************************************
716 * Video output thread. This function does only returns when the thread is
717 * terminated. It handles the pictures arriving in the video heap and the
718 * display device events.
719 *****************************************************************************/
720 static void RunThread( vout_thread_t *p_vout)
722 int i_index; /* index in heap */
723 int i_idle_loops = 0; /* loops without displaying a picture */
724 mtime_t current_date; /* current date */
725 mtime_t display_date; /* display date */
727 picture_t * p_picture; /* picture pointer */
728 picture_t * p_last_picture = NULL; /* last picture */
729 picture_t * p_directbuffer; /* direct buffer to display */
731 subpicture_t * p_subpic; /* subpicture pointer */
736 p_vout->b_error = InitThread( p_vout );
738 /* signal the creation of the vout */
739 vlc_thread_ready( p_vout );
741 if( p_vout->b_error )
743 /* Destroy thread structures allocated by Create and InitThread */
744 DestroyThread( p_vout );
749 * Main loop - it is not executed if an error occured during
752 while( (!p_vout->b_die) && (!p_vout->b_error) )
754 /* Initialize loop variables */
757 current_date = mdate();
761 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
763 msg_Dbg( p_vout, "picture heap: %d/%d",
764 I_RENDERPICTURES, p_vout->i_heap_size );
769 * Find the picture to display (the one with the earliest date).
770 * This operation does not need lock, since only READY_PICTUREs
772 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
774 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
775 && ( (p_picture == NULL) ||
776 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
778 p_picture = PP_RENDERPICTURE[i_index];
779 display_date = p_picture->date;
785 /* If we met the last picture, parse again to see whether there is
786 * a more appropriate one. */
787 if( p_picture == p_last_picture )
789 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
791 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
792 && (PP_RENDERPICTURE[i_index] != p_last_picture)
793 && ((p_picture == p_last_picture) ||
794 (PP_RENDERPICTURE[i_index]->date < display_date)) )
796 p_picture = PP_RENDERPICTURE[i_index];
797 display_date = p_picture->date;
802 /* If we found better than the last picture, destroy it */
803 if( p_last_picture && p_picture != p_last_picture )
805 vlc_mutex_lock( &p_vout->picture_lock );
806 if( p_last_picture->i_refcount )
808 p_last_picture->i_status = DISPLAYED_PICTURE;
812 p_last_picture->i_status = DESTROYED_PICTURE;
813 p_vout->i_heap_size--;
815 vlc_mutex_unlock( &p_vout->picture_lock );
816 p_last_picture = NULL;
819 /* Compute FPS rate */
820 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
823 if( !p_picture->b_force &&
824 p_picture != p_last_picture &&
825 display_date < current_date + p_vout->render_time )
827 /* Picture is late: it will be destroyed and the thread
828 * will directly choose the next picture */
829 vlc_mutex_lock( &p_vout->picture_lock );
830 if( p_picture->i_refcount )
832 /* Pretend we displayed the picture, but don't destroy
833 * it since the decoder might still need it. */
834 p_picture->i_status = DISPLAYED_PICTURE;
838 /* Destroy the picture without displaying it */
839 p_picture->i_status = DESTROYED_PICTURE;
840 p_vout->i_heap_size--;
842 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
843 current_date - display_date );
844 vlc_mutex_unlock( &p_vout->picture_lock );
850 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
852 /* Picture is waaay too early: it will be destroyed */
853 vlc_mutex_lock( &p_vout->picture_lock );
854 if( p_picture->i_refcount )
856 /* Pretend we displayed the picture, but don't destroy
857 * it since the decoder might still need it. */
858 p_picture->i_status = DISPLAYED_PICTURE;
862 /* Destroy the picture without displaying it */
863 p_picture->i_status = DESTROYED_PICTURE;
864 p_vout->i_heap_size--;
866 msg_Warn( p_vout, "vout warning: early picture skipped "
867 "("I64Fd")", display_date - current_date
868 - p_vout->i_pts_delay );
869 vlc_mutex_unlock( &p_vout->picture_lock );
874 if( display_date > current_date + VOUT_DISPLAY_DELAY )
876 /* A picture is ready to be rendered, but its rendering date
877 * is far from the current one so the thread will perform an
878 * empty loop as if no picture were found. The picture state
883 else if( p_picture == p_last_picture )
885 /* We are asked to repeat the previous picture, but we first
886 * wait for a couple of idle loops */
887 if( i_idle_loops < 4 )
894 /* We set the display date to something high, otherwise
895 * we'll have lots of problems with late pictures */
896 display_date = current_date + p_vout->render_time;
901 if( p_picture == NULL )
907 * Check for subpictures to display
909 p_subpic = vout_SortSubPictures( p_vout, display_date );
914 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
917 * Call the plugin-specific rendering method if there is one
919 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
921 /* Render the direct buffer returned by vout_RenderPicture */
922 p_vout->pf_render( p_vout, p_directbuffer );
928 if( display_date != 0 && p_directbuffer != NULL )
930 mtime_t current_render_time = mdate() - current_date;
931 /* if render time is very large we don't include it in the mean */
932 if( current_render_time < p_vout->render_time +
935 /* Store render time using a sliding mean weighting to
936 * current value in a 3 to 1 ratio*/
937 p_vout->render_time *= 3;
938 p_vout->render_time += current_render_time;
939 p_vout->render_time >>= 2;
943 /* Give back change lock */
944 vlc_mutex_unlock( &p_vout->change_lock );
946 /* Sleep a while or until a given date */
947 if( display_date != 0 )
949 /* If there are filters in the chain, better give them the picture
951 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
953 mwait( display_date - VOUT_MWAIT_TOLERANCE );
958 msleep( VOUT_IDLE_SLEEP );
961 /* On awakening, take back lock and send immediately picture
963 vlc_mutex_lock( &p_vout->change_lock );
966 * Display the previously rendered picture
968 if( p_picture != NULL && p_directbuffer != NULL )
970 /* Display the direct buffer returned by vout_RenderPicture */
971 if( p_vout->pf_display )
973 p_vout->pf_display( p_vout, p_directbuffer );
976 /* Reinitialize idle loop count */
979 /* Tell the vout this was the last picture and that it does not
980 * need to be forced anymore. */
981 p_last_picture = p_picture;
982 p_last_picture->b_force = 0;
986 * Check events and manage thread
988 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
990 /* A fatal error occured, and the thread must terminate
991 * immediately, without displaying anything - setting b_error to 1
992 * causes the immediate end of the main while() loop. */
996 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
998 /* this must only happen when the vout plugin is incapable of
999 * rescaling the picture itself. In this case we need to destroy
1000 * the current picture buffers and recreate new ones with the right
1004 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1006 p_vout->pf_end( p_vout );
1007 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1008 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1010 I_OUTPUTPICTURES = 0;
1011 if( p_vout->pf_init( p_vout ) )
1013 msg_Err( p_vout, "cannot resize display" );
1014 /* FIXME: pf_end will be called again in EndThread() */
1015 p_vout->b_error = 1;
1018 /* Need to reinitialise the chroma plugin */
1019 if( p_vout->chroma.p_module )
1021 if( p_vout->chroma.p_module->pf_deactivate )
1022 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
1023 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
1027 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1029 /* This happens when the picture buffers need to be recreated.
1030 * This is useful on multimonitor displays for instance.
1032 * Warning: This only works when the vout creates only 1 picture
1034 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1036 if( !p_vout->b_direct )
1038 module_Unneed( p_vout, p_vout->chroma.p_module );
1041 vlc_mutex_lock( &p_vout->picture_lock );
1043 p_vout->pf_end( p_vout );
1045 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1047 p_vout->b_error = InitThread( p_vout );
1049 vlc_mutex_unlock( &p_vout->picture_lock );
1054 * Error loop - wait until the thread destruction is requested
1056 if( p_vout->b_error )
1058 ErrorThread( p_vout );
1062 EndThread( p_vout );
1064 /* Destroy thread structures allocated by CreateThread */
1065 DestroyThread( p_vout );
1068 /*****************************************************************************
1069 * ErrorThread: RunThread() error loop
1070 *****************************************************************************
1071 * This function is called when an error occured during thread main's loop. The
1072 * thread can still receive feed, but must be ready to terminate as soon as
1074 *****************************************************************************/
1075 static void ErrorThread( vout_thread_t *p_vout )
1077 /* Wait until a `die' order */
1078 while( !p_vout->b_die )
1081 msleep( VOUT_IDLE_SLEEP );
1085 /*****************************************************************************
1086 * EndThread: thread destruction
1087 *****************************************************************************
1088 * This function is called when the thread ends after a sucessful
1089 * initialization. It frees all ressources allocated by InitThread.
1090 *****************************************************************************/
1091 static void EndThread( vout_thread_t *p_vout )
1093 int i_index; /* index in heap */
1097 struct tms cpu_usage;
1098 times( &cpu_usage );
1100 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1101 cpu_usage.tms_utime, cpu_usage.tms_stime );
1105 if( !p_vout->b_direct )
1107 module_Unneed( p_vout, p_vout->chroma.p_module );
1110 /* Destroy all remaining pictures */
1111 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1113 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1115 free( p_vout->p_picture[i_index].p_data_orig );
1119 /* Destroy all remaining subpictures */
1120 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1122 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1124 vout_DestroySubPicture( p_vout,
1125 &p_vout->p_subpicture[i_index] );
1129 if( p_vout->p_text_renderer_module )
1130 module_Unneed( p_vout, p_vout->p_text_renderer_module );
1132 /* Destroy translation tables */
1133 p_vout->pf_end( p_vout );
1135 /* Release the change lock */
1136 vlc_mutex_unlock( &p_vout->change_lock );
1139 /*****************************************************************************
1140 * DestroyThread: thread destruction
1141 *****************************************************************************
1142 * This function is called when the thread ends. It frees all ressources
1143 * allocated by CreateThread. Status is available at this stage.
1144 *****************************************************************************/
1145 static void DestroyThread( vout_thread_t *p_vout )
1147 /* Destroy the locks */
1148 vlc_mutex_destroy( &p_vout->picture_lock );
1149 vlc_mutex_destroy( &p_vout->subpicture_lock );
1150 vlc_mutex_destroy( &p_vout->change_lock );
1152 /* Release the module */
1153 module_Unneed( p_vout, p_vout->p_module );
1156 /* following functions are local */
1158 static int ReduceHeight( int i_ratio )
1160 int i_dummy = VOUT_ASPECT_FACTOR;
1168 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1169 while( !(i_ratio & 1) && !(i_dummy & 1) )
1176 while( !(i_ratio % 3) && !(i_dummy % 3) )
1183 while( !(i_ratio % 5) && !(i_dummy % 5) )
1193 /*****************************************************************************
1194 * BinaryLog: computes the base 2 log of a binary value
1195 *****************************************************************************
1196 * This functions is used by MaskToShift, to get a bit index from a binary
1198 *****************************************************************************/
1199 static int BinaryLog( uint32_t i )
1203 if( i == 0 ) return -31337;
1205 if( i & 0xffff0000 ) i_log += 16;
1206 if( i & 0xff00ff00 ) i_log += 8;
1207 if( i & 0xf0f0f0f0 ) i_log += 4;
1208 if( i & 0xcccccccc ) i_log += 2;
1209 if( i & 0xaaaaaaaa ) i_log += 1;
1214 /*****************************************************************************
1215 * MaskToShift: transform a color mask into right and left shifts
1216 *****************************************************************************
1217 * This function is used for obtaining color shifts from masks.
1218 *****************************************************************************/
1219 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1221 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1225 *pi_left = *pi_right = 0;
1230 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1231 i_high = i_mask + i_low; /* higher bit of the mask */
1233 /* Transform bits into an index */
1234 i_low = BinaryLog (i_low);
1235 i_high = BinaryLog (i_high);
1237 /* Update pointers and return */
1239 *pi_right = (8 - i_high + i_low);
1242 /*****************************************************************************
1243 * InitWindowSize: find the initial dimensions the video window should have.
1244 *****************************************************************************
1245 * This function will check the "width", "height" and "zoom" config options and
1246 * will calculate the size that the video window should have.
1247 *****************************************************************************/
1248 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1252 int i_width, i_height;
1255 #define FP_FACTOR 1000 /* our fixed point factor */
1257 var_Get( p_vout, "align", &val );
1258 p_vout->i_alignment = val.i_int;
1260 var_Get( p_vout, "width", &val );
1261 i_width = val.i_int;
1262 var_Get( p_vout, "height", &val );
1263 i_height = val.i_int;
1264 var_Get( p_vout, "zoom", &val );
1265 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1267 if( i_width > 0 && i_height > 0)
1269 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1270 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1273 else if( i_width > 0 )
1275 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1276 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1277 p_vout->render.i_aspect / FP_FACTOR );
1280 else if( i_height > 0 )
1282 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1283 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1284 VOUT_ASPECT_FACTOR / FP_FACTOR );
1288 if( p_vout->render.i_height * p_vout->render.i_aspect
1289 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1291 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1292 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1293 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1297 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1298 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1299 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1305 /*****************************************************************************
1306 * vout_VarCallback: generic callback for intf variables
1307 *****************************************************************************/
1308 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1309 vlc_value_t old_value, vlc_value_t new_value,
1312 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1314 val.b_bool = VLC_TRUE;
1315 var_Set( p_vout, "intf-change", val );
1319 /*****************************************************************************
1320 * object variables callbacks: a bunch of object variables are used by the
1321 * interfaces to interact with the vout.
1322 *****************************************************************************/
1323 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1324 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1326 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1327 input_thread_t *p_input;
1330 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1332 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1336 /* Modify input as well because the vout might have to be restarted */
1337 var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1338 var_Set( p_input, "fullscreen", newval );
1340 vlc_object_release( p_input );
1343 val.b_bool = VLC_TRUE;
1344 var_Set( p_vout, "intf-change", val );
1348 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1349 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1351 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1352 input_thread_t *p_input;
1355 char *psz_mode = newval.psz_string;
1359 psz_filter = config_GetPsz( p_vout, "filter" );
1361 if( !psz_mode || !*psz_mode )
1363 config_PutPsz( p_vout, "filter", "" );
1367 if( !psz_filter || !*psz_filter )
1369 config_PutPsz( p_vout, "filter", "deinterlace" );
1373 if( strstr( psz_filter, "deinterlace" ) == NULL )
1375 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1376 strcat( psz_filter, ",deinterlace" );
1378 config_PutPsz( p_vout, "filter", psz_filter );
1382 if( psz_filter ) free( psz_filter );
1385 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1387 if( !p_input ) return VLC_EGENERIC;
1389 if( psz_mode && *psz_mode )
1391 val.psz_string = psz_mode;
1392 var_Set( p_vout, "deinterlace-mode", val );
1393 /* Modify input as well because the vout might have to be restarted */
1394 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1395 var_Set( p_input, "deinterlace-mode", val );
1398 /* now restart all video streams */
1399 vlc_mutex_lock( &p_input->stream.stream_lock );
1401 p_vout->b_filter_change = VLC_TRUE;
1403 #define ES p_input->stream.pp_es[i]
1405 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1407 if( ( ES->i_cat == VIDEO_ES ) && ES->p_dec != NULL )
1409 input_UnselectES( p_input, ES );
1410 input_SelectES( p_input, ES );
1414 vlc_mutex_unlock( &p_input->stream.stream_lock );
1416 vlc_object_release( p_input );
1418 val.b_bool = VLC_TRUE;
1419 var_Set( p_vout, "intf-change", val );
1423 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1424 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1426 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1427 input_thread_t *p_input;
1431 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1436 msg_Err( p_vout, "Input not found" );
1437 return( VLC_EGENERIC );
1439 /* Restart the video stream */
1440 vlc_mutex_lock( &p_input->stream.stream_lock );
1442 p_vout->b_filter_change = VLC_TRUE;
1444 #define ES p_input->stream.pp_es[i]
1446 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1448 if( ( ES->i_cat == VIDEO_ES ) && ES->p_dec != NULL )
1450 input_UnselectES( p_input, ES );
1451 input_SelectES( p_input, ES );
1455 vlc_mutex_unlock( &p_input->stream.stream_lock );
1457 vlc_object_release( p_input );
1459 val.b_bool = VLC_TRUE;
1460 var_Set( p_vout, "intf-change", val );