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.230 2003/07/23 22:01:25 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>
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 /* 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;
83 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
87 vlc_object_detach( p_vout );
88 vlc_object_attach( p_vout, p_input );
90 vlc_object_release( p_input );
94 vlc_object_detach( p_vout );
95 /* vlc_object_release( p_vout ); */
96 vout_Destroy( p_vout );
102 /* If a video output was provided, lock it, otherwise look for one. */
105 vlc_object_yield( p_vout );
109 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
113 vlc_object_t *p_input;
115 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
118 p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT,
120 /* only first children of p_input for unused vout */
121 if( p_vout && p_vout->p_parent != p_input )
123 vlc_object_release( p_vout );
126 vlc_object_release( p_input );
131 /* If we now have a video output, check it has the right properties */
134 char *psz_filter_chain;
136 /* We don't directly check for the "filter" variable for obvious
137 * performance reasons. */
138 if( p_vout->b_filter_change )
140 psz_filter_chain = config_GetPsz( p_this, "filter" );
142 if( psz_filter_chain && !*psz_filter_chain )
144 free( psz_filter_chain );
145 psz_filter_chain = NULL;
147 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
149 free( p_vout->psz_filter_chain );
150 p_vout->psz_filter_chain = NULL;
153 if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
154 ( psz_filter_chain && p_vout->psz_filter_chain &&
155 !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
157 p_vout->b_filter_change = VLC_FALSE;
160 if( psz_filter_chain ) free( psz_filter_chain );
163 if( ( p_vout->render.i_width != i_width ) ||
164 ( p_vout->render.i_height != i_height ) ||
165 ( p_vout->render.i_chroma != i_chroma ) ||
166 ( p_vout->render.i_aspect != i_aspect
167 && !p_vout->b_override_aspect ) ||
168 p_vout->b_filter_change )
170 /* We are not interested in this format, close this vout */
171 vlc_object_detach( p_vout );
172 vlc_object_release( p_vout );
173 vout_Destroy( p_vout );
178 /* This video output is cool! Hijack it. */
179 vlc_object_detach( p_vout );
180 vlc_object_attach( p_vout, p_this );
181 vlc_object_release( p_vout );
187 msg_Dbg( p_this, "no usable vout present, spawning one" );
189 p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
195 /*****************************************************************************
196 * vout_Create: creates a new video output thread
197 *****************************************************************************
198 * This function creates a new video output thread, and returns a pointer
199 * to its description. On error, it returns NULL.
200 *****************************************************************************/
201 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
202 unsigned int i_width, unsigned int i_height,
203 vlc_fourcc_t i_chroma, unsigned int i_aspect )
205 vout_thread_t * p_vout; /* thread descriptor */
206 input_thread_t * p_input_thread;
207 int i_index; /* loop variable */
209 vlc_value_t val, text;
211 /* Allocate descriptor */
212 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
215 msg_Err( p_parent, "out of memory" );
219 /* Initialize pictures and subpictures - translation tables and functions
220 * will be initialized later in InitThread */
221 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
223 p_vout->p_picture[i_index].pf_lock = NULL;
224 p_vout->p_picture[i_index].pf_unlock = NULL;
225 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
226 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
229 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
231 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
232 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
235 /* No images in the heap */
236 p_vout->i_heap_size = 0;
238 /* Initialize the rendering heap */
239 I_RENDERPICTURES = 0;
240 p_vout->render.i_width = i_width;
241 p_vout->render.i_height = i_height;
242 p_vout->render.i_chroma = i_chroma;
243 p_vout->render.i_aspect = i_aspect;
245 p_vout->render.i_rmask = 0;
246 p_vout->render.i_gmask = 0;
247 p_vout->render.i_bmask = 0;
249 p_vout->render.i_last_used_pic = -1;
250 p_vout->render.b_allow_modify_pics = 1;
252 /* Zero the output heap */
253 I_OUTPUTPICTURES = 0;
254 p_vout->output.i_width = 0;
255 p_vout->output.i_height = 0;
256 p_vout->output.i_chroma = 0;
257 p_vout->output.i_aspect = 0;
259 p_vout->output.i_rmask = 0;
260 p_vout->output.i_gmask = 0;
261 p_vout->output.i_bmask = 0;
263 /* Initialize misc stuff */
264 p_vout->i_changes = 0;
266 p_vout->b_grayscale = 0;
268 p_vout->b_interface = 0;
270 p_vout->b_fullscreen = 0;
271 p_vout->render_time = 10;
272 p_vout->c_fps_samples = 0;
273 p_vout->b_filter_change = 0;
275 /* Mouse coordinates */
276 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
277 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
278 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
279 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
280 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
281 var_Create( p_vout, "key-pressed", VLC_VAR_STRING );
283 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
284 val.b_bool = VLC_TRUE;
285 var_Set( p_vout, "intf-change", val );
287 /* Initialize locks */
288 vlc_mutex_init( p_vout, &p_vout->picture_lock );
289 vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
290 vlc_mutex_init( p_vout, &p_vout->change_lock );
292 /* Attach the new object now so we can use var inheritance below */
293 vlc_object_attach( p_vout, p_parent );
295 /* Create a few object variables we'll need later on */
296 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
297 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
298 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
299 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
301 p_vout->b_override_aspect = VLC_FALSE;
303 /* If the parent is not a VOUT object, that means we are at the start of
304 * the video output pipe */
305 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
307 var_Get( p_vout, "aspect-ratio", &val );
309 /* Check whether the user tried to override aspect ratio */
312 unsigned int i_new_aspect = i_aspect;
313 char *psz_parser = strchr( val.psz_string, ':' );
317 *psz_parser++ = '\0';
318 i_new_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
319 / atoi( psz_parser );
323 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
324 * atof( val.psz_string )
328 free( val.psz_string );
330 if( i_new_aspect && i_new_aspect != i_aspect )
332 int i_pgcd = ReduceHeight( i_new_aspect );
334 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
335 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
337 p_vout->render.i_aspect = i_new_aspect;
339 p_vout->b_override_aspect = VLC_TRUE;
343 /* Look for the default filter configuration */
344 var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
345 var_Get( p_vout, "filter", &val );
346 p_vout->psz_filter_chain = val.psz_string;
350 /* continue the parent's filter chain */
353 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
354 if( psz_end && *(psz_end+1) )
355 p_vout->psz_filter_chain = strdup( psz_end+1 );
356 else p_vout->psz_filter_chain = NULL;
359 /* Choose the video output module */
360 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
362 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
363 var_Get( p_vout, "vout", &val );
364 psz_plugin = val.psz_string;
368 /* the filter chain is a string list of filters separated by double
372 psz_end = strchr( p_vout->psz_filter_chain, ':' );
374 psz_plugin = strndup( p_vout->psz_filter_chain,
375 psz_end - p_vout->psz_filter_chain );
376 else psz_plugin = strdup( p_vout->psz_filter_chain );
379 /* Initialize the dimensions of the video window */
380 InitWindowSize( p_vout, &p_vout->i_window_width,
381 &p_vout->i_window_height );
383 /* Create the vout thread */
384 p_vout->p_module = module_Need( p_vout,
385 ( p_vout->psz_filter_chain &&
386 *p_vout->psz_filter_chain ) ?
387 "video filter" : "video output",
390 if( psz_plugin ) free( psz_plugin );
391 if( p_vout->p_module == NULL )
393 msg_Err( p_vout, "no suitable vout module" );
394 vlc_object_destroy( p_vout );
397 p_vout->p_text_renderer_module = module_Need( p_vout, "text renderer",
399 if( p_vout->p_text_renderer_module == NULL )
401 msg_Warn( p_vout, "no suitable text renderer module" );
402 p_vout->pf_add_string = NULL;
404 /* Create a few object variables for interface interaction */
405 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
406 text.psz_string = _("Fullscreen");
407 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
408 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
411 /* user requested fullscreen */
412 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
414 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
416 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
417 text.psz_string = _("Deinterlace");
418 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
419 val.psz_string = ""; text.psz_string = _("Disable");
420 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
421 val.psz_string = "discard"; text.psz_string = _("Discard");
422 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
423 val.psz_string = "blend"; text.psz_string = _("Blend");
424 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
425 val.psz_string = "mean"; text.psz_string = _("Mean");
426 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
427 val.psz_string = "bob"; text.psz_string = _("Bob");
428 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
429 val.psz_string = "linear"; text.psz_string = _("Linear");
430 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
431 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
433 var_Set( p_vout, "deinterlace", val );
435 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
437 /* Calculate delay created by internal caching */
438 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
439 VLC_OBJECT_INPUT, FIND_ANYWHERE );
442 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
443 vlc_object_release( p_input_thread );
447 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
450 if( vlc_thread_create( p_vout, "video output", RunThread,
451 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
453 msg_Err( p_vout, "out of memory" );
454 module_Unneed( p_vout, p_vout->p_module );
455 vlc_object_destroy( p_vout );
462 /*****************************************************************************
463 * vout_Destroy: destroys a previously created video output
464 *****************************************************************************
465 * Destroy a terminated thread.
466 * The function will request a destruction of the specified thread. If pi_error
467 * is NULL, it will return once the thread is destroyed. Else, it will be
468 * update using one of the THREAD_* constants.
469 *****************************************************************************/
470 void vout_Destroy( vout_thread_t *p_vout )
472 vlc_object_t *p_playlist;
474 /* Request thread destruction */
475 p_vout->b_die = VLC_TRUE;
476 vlc_thread_join( p_vout );
478 var_Destroy( p_vout, "intf-change" );
480 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
484 vlc_object_destroy( p_vout );
486 /* If it was the last vout, tell the interface to show up */
487 if( p_playlist != NULL )
489 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
490 VLC_OBJECT_VOUT, FIND_ANYWHERE );
491 if( p_another_vout == NULL )
494 val.b_bool = VLC_TRUE;
495 var_Set( p_playlist, "intf-show", val );
499 vlc_object_release( p_another_vout );
501 vlc_object_release( p_playlist );
505 /*****************************************************************************
506 * InitThread: initialize video output thread
507 *****************************************************************************
508 * This function is called from RunThread and performs the second step of the
509 * initialization. It returns 0 on success. Note that the thread's flag are not
510 * modified inside this function.
511 *****************************************************************************/
512 static int InitThread( vout_thread_t *p_vout )
516 vlc_mutex_lock( &p_vout->change_lock );
522 /* Initialize output method, it allocates direct buffers for us */
523 if( p_vout->pf_init( p_vout ) )
525 vlc_mutex_unlock( &p_vout->change_lock );
529 if( !I_OUTPUTPICTURES )
531 msg_Err( p_vout, "plugin was unable to allocate at least "
532 "one direct buffer" );
533 p_vout->pf_end( p_vout );
534 vlc_mutex_unlock( &p_vout->change_lock );
538 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
540 msg_Err( p_vout, "plugin allocated too many direct buffers, "
541 "our internal buffers must have overflown." );
542 p_vout->pf_end( p_vout );
543 vlc_mutex_unlock( &p_vout->change_lock );
547 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
550 if( !p_vout->psz_filter_chain )
552 char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
556 int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
558 / p_vout->output.i_height;
561 if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
563 int i_pgcd = ReduceHeight( i_new_aspect );
564 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
565 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
566 p_vout->output.i_aspect = i_new_aspect;
572 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
574 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
575 p_vout->render.i_width, p_vout->render.i_height,
576 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
577 p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
579 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
581 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
582 p_vout->output.i_width, p_vout->output.i_height,
583 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
584 p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
586 /* Calculate shifts from system-updated masks */
587 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
588 p_vout->output.i_rmask );
589 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
590 p_vout->output.i_gmask );
591 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
592 p_vout->output.i_bmask );
594 /* Check whether we managed to create direct buffers similar to
595 * the render buffers, ie same size and chroma */
596 if( ( p_vout->output.i_width == p_vout->render.i_width )
597 && ( p_vout->output.i_height == p_vout->render.i_height )
598 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
600 /* Cool ! We have direct buffers, we can ask the decoder to
601 * directly decode into them ! Map the first render buffers to
602 * the first direct buffers, but keep the first direct buffer
603 * for memcpy operations */
604 p_vout->b_direct = 1;
606 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
608 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
609 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
610 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
612 /* We have enough direct buffers so there's no need to
613 * try to use system memory buffers. */
616 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
620 msg_Dbg( p_vout, "direct render, mapping "
621 "render pictures 0-%i to system pictures 1-%i",
622 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
626 /* Rats... Something is wrong here, we could not find an output
627 * plugin able to directly render what we decode. See if we can
628 * find a chroma plugin to do the conversion */
629 p_vout->b_direct = 0;
631 /* Choose the best module */
632 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
634 if( p_vout->chroma.p_module == NULL )
636 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
637 (char*)&p_vout->render.i_chroma,
638 (char*)&p_vout->output.i_chroma );
639 p_vout->pf_end( p_vout );
640 vlc_mutex_unlock( &p_vout->change_lock );
644 msg_Dbg( p_vout, "indirect render, mapping "
645 "render pictures 0-%i to system pictures %i-%i",
646 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
647 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
649 /* Append render buffers after the direct buffers */
650 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
652 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
655 /* Check if we have enough render pictures */
656 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
661 /* Link pictures back to their heap */
662 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
664 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
667 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
669 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
672 /* XXX XXX mark thread ready */
676 /*****************************************************************************
677 * RunThread: video output thread
678 *****************************************************************************
679 * Video output thread. This function does only returns when the thread is
680 * terminated. It handles the pictures arriving in the video heap and the
681 * display device events.
682 *****************************************************************************/
683 static void RunThread( vout_thread_t *p_vout)
685 int i_index; /* index in heap */
686 int i_idle_loops = 0; /* loops without displaying a picture */
687 mtime_t current_date; /* current date */
688 mtime_t display_date; /* display date */
690 picture_t * p_picture; /* picture pointer */
691 picture_t * p_last_picture = NULL; /* last picture */
692 picture_t * p_directbuffer; /* direct buffer to display */
694 subpicture_t * p_subpic; /* subpicture pointer */
699 p_vout->b_error = InitThread( p_vout );
701 /* signal the creation of the vout */
702 vlc_thread_ready( p_vout );
704 if( p_vout->b_error )
706 /* Destroy thread structures allocated by Create and InitThread */
707 DestroyThread( p_vout );
712 * Main loop - it is not executed if an error occured during
715 while( (!p_vout->b_die) && (!p_vout->b_error) )
717 /* Initialize loop variables */
720 current_date = mdate();
724 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
726 msg_Dbg( p_vout, "picture heap: %d/%d",
727 I_RENDERPICTURES, p_vout->i_heap_size );
732 * Find the picture to display (the one with the earliest date).
733 * This operation does not need lock, since only READY_PICTUREs
735 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
737 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
738 && ( (p_picture == NULL) ||
739 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
741 p_picture = PP_RENDERPICTURE[i_index];
742 display_date = p_picture->date;
748 /* If we met the last picture, parse again to see whether there is
749 * a more appropriate one. */
750 if( p_picture == p_last_picture )
752 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
754 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
755 && (PP_RENDERPICTURE[i_index] != p_last_picture)
756 && ((p_picture == p_last_picture) ||
757 (PP_RENDERPICTURE[i_index]->date < display_date)) )
759 p_picture = PP_RENDERPICTURE[i_index];
760 display_date = p_picture->date;
765 /* If we found better than the last picture, destroy it */
766 if( p_last_picture && p_picture != p_last_picture )
768 vlc_mutex_lock( &p_vout->picture_lock );
769 if( p_last_picture->i_refcount )
771 p_last_picture->i_status = DISPLAYED_PICTURE;
775 p_last_picture->i_status = DESTROYED_PICTURE;
776 p_vout->i_heap_size--;
778 vlc_mutex_unlock( &p_vout->picture_lock );
779 p_last_picture = NULL;
782 /* Compute FPS rate */
783 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
786 if( !p_picture->b_force &&
787 p_picture != p_last_picture &&
788 display_date < current_date + p_vout->render_time )
790 /* Picture is late: it will be destroyed and the thread
791 * will directly choose the next picture */
792 vlc_mutex_lock( &p_vout->picture_lock );
793 if( p_picture->i_refcount )
795 /* Pretend we displayed the picture, but don't destroy
796 * it since the decoder might still need it. */
797 p_picture->i_status = DISPLAYED_PICTURE;
801 /* Destroy the picture without displaying it */
802 p_picture->i_status = DESTROYED_PICTURE;
803 p_vout->i_heap_size--;
805 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
806 current_date - display_date );
807 vlc_mutex_unlock( &p_vout->picture_lock );
813 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
815 /* Picture is waaay too early: it will be destroyed */
816 vlc_mutex_lock( &p_vout->picture_lock );
817 if( p_picture->i_refcount )
819 /* Pretend we displayed the picture, but don't destroy
820 * it since the decoder might still need it. */
821 p_picture->i_status = DISPLAYED_PICTURE;
825 /* Destroy the picture without displaying it */
826 p_picture->i_status = DESTROYED_PICTURE;
827 p_vout->i_heap_size--;
829 msg_Warn( p_vout, "vout warning: early picture skipped "
830 "("I64Fd")", display_date - current_date
831 - p_vout->i_pts_delay );
832 vlc_mutex_unlock( &p_vout->picture_lock );
837 if( display_date > current_date + VOUT_DISPLAY_DELAY )
839 /* A picture is ready to be rendered, but its rendering date
840 * is far from the current one so the thread will perform an
841 * empty loop as if no picture were found. The picture state
846 else if( p_picture == p_last_picture )
848 /* We are asked to repeat the previous picture, but we first
849 * wait for a couple of idle loops */
850 if( i_idle_loops < 4 )
857 /* We set the display date to something high, otherwise
858 * we'll have lots of problems with late pictures */
859 display_date = current_date + p_vout->render_time;
864 if( p_picture == NULL )
870 * Check for subpictures to display
872 p_subpic = vout_SortSubPictures( p_vout, display_date );
877 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
880 * Call the plugin-specific rendering method if there is one
882 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
884 /* Render the direct buffer returned by vout_RenderPicture */
885 p_vout->pf_render( p_vout, p_directbuffer );
891 if( display_date != 0 && p_directbuffer != NULL )
893 /* Store render time using a sliding mean */
894 p_vout->render_time += mdate() - current_date;
895 p_vout->render_time >>= 1;
898 /* Give back change lock */
899 vlc_mutex_unlock( &p_vout->change_lock );
901 /* Sleep a while or until a given date */
902 if( display_date != 0 )
904 mwait( display_date - VOUT_MWAIT_TOLERANCE );
908 msleep( VOUT_IDLE_SLEEP );
911 /* On awakening, take back lock and send immediately picture
913 vlc_mutex_lock( &p_vout->change_lock );
916 * Display the previously rendered picture
918 if( p_picture != NULL && p_directbuffer != NULL )
920 /* Display the direct buffer returned by vout_RenderPicture */
921 if( p_vout->pf_display )
923 p_vout->pf_display( p_vout, p_directbuffer );
926 /* Reinitialize idle loop count */
929 /* Tell the vout this was the last picture and that it does not
930 * need to be forced anymore. */
931 p_last_picture = p_picture;
932 p_last_picture->b_force = 0;
936 * Check events and manage thread
938 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
940 /* A fatal error occured, and the thread must terminate
941 * immediately, without displaying anything - setting b_error to 1
942 * causes the immediate end of the main while() loop. */
946 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
948 /* this must only happen when the vout plugin is incapable of
949 * rescaling the picture itself. In this case we need to destroy
950 * the current picture buffers and recreate new ones with the right
954 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
956 p_vout->pf_end( p_vout );
957 for( i = 0; i < I_OUTPUTPICTURES; i++ )
958 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
960 I_OUTPUTPICTURES = 0;
961 if( p_vout->pf_init( p_vout ) )
963 msg_Err( p_vout, "cannot resize display" );
964 /* FIXME: pf_end will be called again in EndThread() */
968 /* Need to reinitialise the chroma plugin */
969 if( p_vout->chroma.p_module->pf_deactivate )
970 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
971 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
976 * Error loop - wait until the thread destruction is requested
978 if( p_vout->b_error )
980 ErrorThread( p_vout );
986 /* Destroy thread structures allocated by CreateThread */
987 DestroyThread( p_vout );
990 /*****************************************************************************
991 * ErrorThread: RunThread() error loop
992 *****************************************************************************
993 * This function is called when an error occured during thread main's loop. The
994 * thread can still receive feed, but must be ready to terminate as soon as
996 *****************************************************************************/
997 static void ErrorThread( vout_thread_t *p_vout )
999 /* Wait until a `die' order */
1000 while( !p_vout->b_die )
1003 msleep( VOUT_IDLE_SLEEP );
1007 /*****************************************************************************
1008 * EndThread: thread destruction
1009 *****************************************************************************
1010 * This function is called when the thread ends after a sucessful
1011 * initialization. It frees all ressources allocated by InitThread.
1012 *****************************************************************************/
1013 static void EndThread( vout_thread_t *p_vout )
1015 int i_index; /* index in heap */
1019 struct tms cpu_usage;
1020 times( &cpu_usage );
1022 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1023 cpu_usage.tms_utime, cpu_usage.tms_stime );
1027 if( !p_vout->b_direct )
1029 module_Unneed( p_vout, p_vout->chroma.p_module );
1032 /* Destroy all remaining pictures */
1033 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1035 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1037 free( p_vout->p_picture[i_index].p_data_orig );
1041 /* Destroy all remaining subpictures */
1042 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1044 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1046 vout_DestroySubPicture( p_vout,
1047 &p_vout->p_subpicture[i_index] );
1051 module_Unneed( p_vout, p_vout->p_text_renderer_module );
1053 /* Destroy translation tables */
1054 p_vout->pf_end( p_vout );
1056 /* Release the change lock */
1057 vlc_mutex_unlock( &p_vout->change_lock );
1060 /*****************************************************************************
1061 * DestroyThread: thread destruction
1062 *****************************************************************************
1063 * This function is called when the thread ends. It frees all ressources
1064 * allocated by CreateThread. Status is available at this stage.
1065 *****************************************************************************/
1066 static void DestroyThread( vout_thread_t *p_vout )
1068 /* Destroy the locks */
1069 vlc_mutex_destroy( &p_vout->picture_lock );
1070 vlc_mutex_destroy( &p_vout->subpicture_lock );
1071 vlc_mutex_destroy( &p_vout->change_lock );
1073 /* Release the module */
1074 module_Unneed( p_vout, p_vout->p_module );
1077 /* following functions are local */
1079 static int ReduceHeight( int i_ratio )
1081 int i_dummy = VOUT_ASPECT_FACTOR;
1089 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1090 while( !(i_ratio & 1) && !(i_dummy & 1) )
1097 while( !(i_ratio % 3) && !(i_dummy % 3) )
1104 while( !(i_ratio % 5) && !(i_dummy % 5) )
1114 /*****************************************************************************
1115 * BinaryLog: computes the base 2 log of a binary value
1116 *****************************************************************************
1117 * This functions is used by MaskToShift, to get a bit index from a binary
1119 *****************************************************************************/
1120 static int BinaryLog( uint32_t i )
1124 if( i == 0 ) return -31337;
1126 if( i & 0xffff0000 ) i_log += 16;
1127 if( i & 0xff00ff00 ) i_log += 8;
1128 if( i & 0xf0f0f0f0 ) i_log += 4;
1129 if( i & 0xcccccccc ) i_log += 2;
1130 if( i & 0xaaaaaaaa ) i_log += 1;
1135 /*****************************************************************************
1136 * MaskToShift: transform a color mask into right and left shifts
1137 *****************************************************************************
1138 * This function is used for obtaining color shifts from masks.
1139 *****************************************************************************/
1140 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1142 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1146 *pi_left = *pi_right = 0;
1151 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1152 i_high = i_mask + i_low; /* higher bit of the mask */
1154 /* Transform bits into an index */
1155 i_low = BinaryLog (i_low);
1156 i_high = BinaryLog (i_high);
1158 /* Update pointers and return */
1160 *pi_right = (8 - i_high + i_low);
1163 /*****************************************************************************
1164 * InitWindowSize: find the initial dimensions the video window should have.
1165 *****************************************************************************
1166 * This function will check the "width", "height" and "zoom" config options and
1167 * will calculate the size that the video window should have.
1168 *****************************************************************************/
1169 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1173 int i_width, i_height;
1176 #define FP_FACTOR 1000 /* our fixed point factor */
1178 var_Get( p_vout, "width", &val );
1179 i_width = val.i_int;
1180 var_Get( p_vout, "height", &val );
1181 i_height = val.i_int;
1182 var_Get( p_vout, "zoom", &val );
1183 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1185 if( i_width > 0 && i_height > 0)
1187 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1188 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1191 else if( i_width > 0 )
1193 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1194 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1195 p_vout->render.i_aspect / FP_FACTOR );
1198 else if( i_height > 0 )
1200 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1201 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1202 VOUT_ASPECT_FACTOR / FP_FACTOR );
1206 if( p_vout->render.i_height * p_vout->render.i_aspect
1207 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1209 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1210 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1211 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1215 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1216 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1217 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1223 /*****************************************************************************
1224 * vout_VarCallback: generic callback for intf variables
1225 *****************************************************************************/
1226 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1227 vlc_value_t old_value, vlc_value_t new_value,
1230 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1232 val.b_bool = VLC_TRUE;
1233 var_Set( p_vout, "intf-change", val );
1237 /*****************************************************************************
1238 * object variables callbacks: a bunch of object variables are used by the
1239 * interfaces to interact with the vout.
1240 *****************************************************************************/
1241 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1242 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1244 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1245 input_thread_t *p_input;
1248 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1250 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1254 /* Modify input as well because the vout might have to be restarted */
1255 var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1256 var_Set( p_input, "fullscreen", newval );
1258 vlc_object_release( p_input );
1261 val.b_bool = VLC_TRUE;
1262 var_Set( p_vout, "intf-change", val );
1266 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1267 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1269 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1270 input_thread_t *p_input;
1273 char *psz_mode = newval.psz_string;
1277 psz_filter = config_GetPsz( p_vout, "filter" );
1279 if( !psz_mode || !*psz_mode )
1281 config_PutPsz( p_vout, "filter", "" );
1285 if( !psz_filter || !*psz_filter )
1287 config_PutPsz( p_vout, "filter", "deinterlace" );
1291 if( strstr( psz_filter, "deinterlace" ) == NULL )
1293 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1294 strcat( psz_filter, ",deinterlace" );
1296 config_PutPsz( p_vout, "filter", psz_filter );
1300 if( psz_filter ) free( psz_filter );
1303 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1305 if( !p_input ) return VLC_EGENERIC;
1307 if( psz_mode && *psz_mode )
1309 val.psz_string = psz_mode;
1310 var_Set( p_vout, "deinterlace-mode", val );
1311 /* Modify input as well because the vout might have to be restarted */
1312 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1313 var_Set( p_input, "deinterlace-mode", val );
1316 /* now restart all video streams */
1317 vlc_mutex_lock( &p_input->stream.stream_lock );
1319 p_vout->b_filter_change = VLC_TRUE;
1321 #define ES p_input->stream.pp_es[i]
1323 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1325 if( ( ES->i_cat == VIDEO_ES ) && ES->p_decoder_fifo != NULL )
1327 input_UnselectES( p_input, ES );
1328 input_SelectES( p_input, ES );
1332 vlc_mutex_unlock( &p_input->stream.stream_lock );
1334 vlc_object_release( p_input );
1336 val.b_bool = VLC_TRUE;
1337 var_Set( p_vout, "intf-change", val );