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.237 2003/10/05 23:03:35 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_playlist;
83 p_playlist = vlc_object_find( p_this,
84 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
88 vlc_object_detach( p_vout );
89 vlc_object_attach( p_vout, p_playlist );
91 vlc_object_release( p_playlist );
95 msg_Dbg( p_this, "cannot find playlist destroying vout" );
96 vlc_object_detach( p_vout );
97 vout_Destroy( p_vout );
103 /* If a video output was provided, lock it, otherwise look for one. */
106 vlc_object_yield( p_vout );
110 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
114 vlc_object_t *p_playlist;
116 p_playlist = vlc_object_find( p_this,
117 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
120 p_vout = vlc_object_find( p_playlist,
121 VLC_OBJECT_VOUT, FIND_CHILD );
122 /* only first children of p_input for unused vout */
123 if( p_vout && p_vout->p_parent != p_playlist )
125 vlc_object_release( p_vout );
128 vlc_object_release( p_playlist );
133 /* If we now have a video output, check it has the right properties */
136 char *psz_filter_chain;
138 /* We don't directly check for the "filter" variable for obvious
139 * performance reasons. */
140 if( p_vout->b_filter_change )
142 psz_filter_chain = config_GetPsz( p_this, "filter" );
144 if( psz_filter_chain && !*psz_filter_chain )
146 free( psz_filter_chain );
147 psz_filter_chain = NULL;
149 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
151 free( p_vout->psz_filter_chain );
152 p_vout->psz_filter_chain = NULL;
155 if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
156 ( psz_filter_chain && p_vout->psz_filter_chain &&
157 !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
159 p_vout->b_filter_change = VLC_FALSE;
162 if( psz_filter_chain ) free( psz_filter_chain );
165 if( ( p_vout->render.i_width != i_width ) ||
166 ( p_vout->render.i_height != i_height ) ||
167 ( p_vout->render.i_chroma != i_chroma ) ||
168 ( p_vout->render.i_aspect != i_aspect
169 && !p_vout->b_override_aspect ) ||
170 p_vout->b_filter_change )
172 /* We are not interested in this format, close this vout */
173 vlc_object_detach( p_vout );
174 vlc_object_release( p_vout );
175 vout_Destroy( p_vout );
180 /* This video output is cool! Hijack it. */
181 vlc_object_detach( p_vout );
182 vlc_object_attach( p_vout, p_this );
183 vlc_object_release( p_vout );
189 msg_Dbg( p_this, "no usable vout present, spawning one" );
191 p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
197 /*****************************************************************************
198 * vout_Create: creates a new video output thread
199 *****************************************************************************
200 * This function creates a new video output thread, and returns a pointer
201 * to its description. On error, it returns NULL.
202 *****************************************************************************/
203 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
204 unsigned int i_width, unsigned int i_height,
205 vlc_fourcc_t i_chroma, unsigned int i_aspect )
207 vout_thread_t * p_vout; /* thread descriptor */
208 input_thread_t * p_input_thread;
209 int i_index; /* loop variable */
211 vlc_value_t val, text;
213 /* Allocate descriptor */
214 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
217 msg_Err( p_parent, "out of memory" );
221 /* Initialize pictures and subpictures - translation tables and functions
222 * will be initialized later in InitThread */
223 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
225 p_vout->p_picture[i_index].pf_lock = NULL;
226 p_vout->p_picture[i_index].pf_unlock = NULL;
227 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
228 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
231 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
233 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
234 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
237 /* No images in the heap */
238 p_vout->i_heap_size = 0;
240 /* Initialize the rendering heap */
241 I_RENDERPICTURES = 0;
242 p_vout->render.i_width = i_width;
243 p_vout->render.i_height = i_height;
244 p_vout->render.i_chroma = i_chroma;
245 p_vout->render.i_aspect = i_aspect;
247 p_vout->render.i_rmask = 0;
248 p_vout->render.i_gmask = 0;
249 p_vout->render.i_bmask = 0;
251 p_vout->render.i_last_used_pic = -1;
252 p_vout->render.b_allow_modify_pics = 1;
254 /* Zero the output heap */
255 I_OUTPUTPICTURES = 0;
256 p_vout->output.i_width = 0;
257 p_vout->output.i_height = 0;
258 p_vout->output.i_chroma = 0;
259 p_vout->output.i_aspect = 0;
261 p_vout->output.i_rmask = 0;
262 p_vout->output.i_gmask = 0;
263 p_vout->output.i_bmask = 0;
265 /* Initialize misc stuff */
266 p_vout->i_changes = 0;
268 p_vout->b_grayscale = 0;
270 p_vout->b_interface = 0;
272 p_vout->b_fullscreen = 0;
273 p_vout->i_alignment = 0;
274 p_vout->render_time = 10;
275 p_vout->c_fps_samples = 0;
276 p_vout->b_filter_change = 0;
278 /* Mouse coordinates */
279 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
280 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
281 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
282 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
283 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
284 var_Create( p_vout, "key-pressed", VLC_VAR_STRING );
286 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
287 val.b_bool = VLC_TRUE;
288 var_Set( p_vout, "intf-change", val );
290 /* Initialize locks */
291 vlc_mutex_init( p_vout, &p_vout->picture_lock );
292 vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
293 vlc_mutex_init( p_vout, &p_vout->change_lock );
295 /* Attach the new object now so we can use var inheritance below */
296 vlc_object_attach( p_vout, p_parent );
298 /* Create a few object variables we'll need later on */
299 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
300 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
301 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
302 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
303 var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
305 p_vout->b_override_aspect = VLC_FALSE;
307 /* If the parent is not a VOUT object, that means we are at the start of
308 * the video output pipe */
309 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
311 var_Get( p_vout, "aspect-ratio", &val );
313 /* Check whether the user tried to override aspect ratio */
316 unsigned int i_new_aspect = i_aspect;
317 char *psz_parser = strchr( val.psz_string, ':' );
321 *psz_parser++ = '\0';
322 i_new_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
323 / atoi( psz_parser );
327 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
328 * atof( val.psz_string )
332 free( val.psz_string );
334 if( i_new_aspect && i_new_aspect != i_aspect )
336 int i_pgcd = ReduceHeight( i_new_aspect );
338 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
339 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
341 p_vout->render.i_aspect = i_new_aspect;
343 p_vout->b_override_aspect = VLC_TRUE;
347 /* Look for the default filter configuration */
348 var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
349 var_Get( p_vout, "filter", &val );
350 p_vout->psz_filter_chain = val.psz_string;
354 /* continue the parent's filter chain */
357 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
358 if( psz_end && *(psz_end+1) )
359 p_vout->psz_filter_chain = strdup( psz_end+1 );
360 else p_vout->psz_filter_chain = NULL;
363 /* Choose the video output module */
364 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
366 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
367 var_Get( p_vout, "vout", &val );
368 psz_plugin = val.psz_string;
372 /* the filter chain is a string list of filters separated by double
376 psz_end = strchr( p_vout->psz_filter_chain, ':' );
378 psz_plugin = strndup( p_vout->psz_filter_chain,
379 psz_end - p_vout->psz_filter_chain );
380 else psz_plugin = strdup( p_vout->psz_filter_chain );
383 /* Initialize the dimensions of the video window */
384 InitWindowSize( p_vout, &p_vout->i_window_width,
385 &p_vout->i_window_height );
387 /* Create the vout thread */
388 p_vout->p_module = module_Need( p_vout,
389 ( p_vout->psz_filter_chain &&
390 *p_vout->psz_filter_chain ) ?
391 "video filter" : "video output",
394 if( psz_plugin ) free( psz_plugin );
395 if( p_vout->p_module == NULL )
397 msg_Err( p_vout, "no suitable vout module" );
398 vlc_object_destroy( p_vout );
402 p_vout->p_text_renderer_module = module_Need( p_vout, "text renderer",
404 if( p_vout->p_text_renderer_module == NULL )
406 msg_Warn( p_vout, "no suitable text renderer module" );
407 p_vout->pf_add_string = NULL;
410 /* Create a few object variables for interface interaction */
411 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
412 text.psz_string = _("Fullscreen");
413 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
414 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
417 /* user requested fullscreen */
418 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
420 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
422 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
423 text.psz_string = _("Deinterlace");
424 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
425 val.psz_string = ""; text.psz_string = _("Disable");
426 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
427 val.psz_string = "discard"; text.psz_string = _("Discard");
428 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
429 val.psz_string = "blend"; text.psz_string = _("Blend");
430 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
431 val.psz_string = "mean"; text.psz_string = _("Mean");
432 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
433 val.psz_string = "bob"; text.psz_string = _("Bob");
434 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
435 val.psz_string = "linear"; text.psz_string = _("Linear");
436 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
437 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
439 var_Set( p_vout, "deinterlace", val );
441 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
443 /* Calculate delay created by internal caching */
444 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
445 VLC_OBJECT_INPUT, FIND_ANYWHERE );
448 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
449 vlc_object_release( p_input_thread );
453 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
456 if( vlc_thread_create( p_vout, "video output", RunThread,
457 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
459 msg_Err( p_vout, "out of memory" );
460 module_Unneed( p_vout, p_vout->p_module );
461 vlc_object_destroy( p_vout );
468 /*****************************************************************************
469 * vout_Destroy: destroys a previously created video output
470 *****************************************************************************
471 * Destroy a terminated thread.
472 * The function will request a destruction of the specified thread. If pi_error
473 * is NULL, it will return once the thread is destroyed. Else, it will be
474 * update using one of the THREAD_* constants.
475 *****************************************************************************/
476 void vout_Destroy( vout_thread_t *p_vout )
478 vlc_object_t *p_playlist;
480 /* Request thread destruction */
481 p_vout->b_die = VLC_TRUE;
482 vlc_thread_join( p_vout );
484 var_Destroy( p_vout, "intf-change" );
486 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
490 vlc_object_destroy( p_vout );
492 /* If it was the last vout, tell the interface to show up */
493 if( p_playlist != NULL )
495 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
496 VLC_OBJECT_VOUT, FIND_ANYWHERE );
497 if( p_another_vout == NULL )
500 val.b_bool = VLC_TRUE;
501 var_Set( p_playlist, "intf-show", val );
505 vlc_object_release( p_another_vout );
507 vlc_object_release( p_playlist );
511 /*****************************************************************************
512 * InitThread: initialize video output thread
513 *****************************************************************************
514 * This function is called from RunThread and performs the second step of the
515 * initialization. It returns 0 on success. Note that the thread's flag are not
516 * modified inside this function.
517 *****************************************************************************/
518 static int InitThread( vout_thread_t *p_vout )
522 vlc_mutex_lock( &p_vout->change_lock );
528 /* Initialize output method, it allocates direct buffers for us */
529 if( p_vout->pf_init( p_vout ) )
531 vlc_mutex_unlock( &p_vout->change_lock );
535 if( !I_OUTPUTPICTURES )
537 msg_Err( p_vout, "plugin was unable to allocate at least "
538 "one direct buffer" );
539 p_vout->pf_end( p_vout );
540 vlc_mutex_unlock( &p_vout->change_lock );
544 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
546 msg_Err( p_vout, "plugin allocated too many direct buffers, "
547 "our internal buffers must have overflown." );
548 p_vout->pf_end( p_vout );
549 vlc_mutex_unlock( &p_vout->change_lock );
553 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
556 if( !p_vout->psz_filter_chain )
558 char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
562 int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
564 / p_vout->output.i_height;
567 if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
569 int i_pgcd = ReduceHeight( i_new_aspect );
570 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
571 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
572 p_vout->output.i_aspect = i_new_aspect;
578 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
580 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
581 p_vout->render.i_width, p_vout->render.i_height,
582 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
583 p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
585 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
587 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
588 p_vout->output.i_width, p_vout->output.i_height,
589 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
590 p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
592 /* Calculate shifts from system-updated masks */
593 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
594 p_vout->output.i_rmask );
595 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
596 p_vout->output.i_gmask );
597 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
598 p_vout->output.i_bmask );
600 /* Check whether we managed to create direct buffers similar to
601 * the render buffers, ie same size and chroma */
602 if( ( p_vout->output.i_width == p_vout->render.i_width )
603 && ( p_vout->output.i_height == p_vout->render.i_height )
604 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
606 /* Cool ! We have direct buffers, we can ask the decoder to
607 * directly decode into them ! Map the first render buffers to
608 * the first direct buffers, but keep the first direct buffer
609 * for memcpy operations */
610 p_vout->b_direct = 1;
612 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
614 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
615 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
616 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
618 /* We have enough direct buffers so there's no need to
619 * try to use system memory buffers. */
622 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
626 msg_Dbg( p_vout, "direct render, mapping "
627 "render pictures 0-%i to system pictures 1-%i",
628 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
632 /* Rats... Something is wrong here, we could not find an output
633 * plugin able to directly render what we decode. See if we can
634 * find a chroma plugin to do the conversion */
635 p_vout->b_direct = 0;
637 /* Choose the best module */
638 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
640 if( p_vout->chroma.p_module == NULL )
642 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
643 (char*)&p_vout->render.i_chroma,
644 (char*)&p_vout->output.i_chroma );
645 p_vout->pf_end( p_vout );
646 vlc_mutex_unlock( &p_vout->change_lock );
650 msg_Dbg( p_vout, "indirect render, mapping "
651 "render pictures 0-%i to system pictures %i-%i",
652 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
653 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
655 /* Append render buffers after the direct buffers */
656 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
658 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
661 /* Check if we have enough render pictures */
662 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
667 /* Link pictures back to their heap */
668 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
670 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
673 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
675 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
678 /* XXX XXX mark thread ready */
682 /*****************************************************************************
683 * RunThread: video output thread
684 *****************************************************************************
685 * Video output thread. This function does only returns when the thread is
686 * terminated. It handles the pictures arriving in the video heap and the
687 * display device events.
688 *****************************************************************************/
689 static void RunThread( vout_thread_t *p_vout)
691 int i_index; /* index in heap */
692 int i_idle_loops = 0; /* loops without displaying a picture */
693 mtime_t current_date; /* current date */
694 mtime_t display_date; /* display date */
696 picture_t * p_picture; /* picture pointer */
697 picture_t * p_last_picture = NULL; /* last picture */
698 picture_t * p_directbuffer; /* direct buffer to display */
700 subpicture_t * p_subpic; /* subpicture pointer */
705 p_vout->b_error = InitThread( p_vout );
707 /* signal the creation of the vout */
708 vlc_thread_ready( p_vout );
710 if( p_vout->b_error )
712 /* Destroy thread structures allocated by Create and InitThread */
713 DestroyThread( p_vout );
718 * Main loop - it is not executed if an error occured during
721 while( (!p_vout->b_die) && (!p_vout->b_error) )
723 /* Initialize loop variables */
726 current_date = mdate();
730 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
732 msg_Dbg( p_vout, "picture heap: %d/%d",
733 I_RENDERPICTURES, p_vout->i_heap_size );
738 * Find the picture to display (the one with the earliest date).
739 * This operation does not need lock, since only READY_PICTUREs
741 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
743 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
744 && ( (p_picture == NULL) ||
745 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
747 p_picture = PP_RENDERPICTURE[i_index];
748 display_date = p_picture->date;
754 /* If we met the last picture, parse again to see whether there is
755 * a more appropriate one. */
756 if( p_picture == p_last_picture )
758 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
760 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
761 && (PP_RENDERPICTURE[i_index] != p_last_picture)
762 && ((p_picture == p_last_picture) ||
763 (PP_RENDERPICTURE[i_index]->date < display_date)) )
765 p_picture = PP_RENDERPICTURE[i_index];
766 display_date = p_picture->date;
771 /* If we found better than the last picture, destroy it */
772 if( p_last_picture && p_picture != p_last_picture )
774 vlc_mutex_lock( &p_vout->picture_lock );
775 if( p_last_picture->i_refcount )
777 p_last_picture->i_status = DISPLAYED_PICTURE;
781 p_last_picture->i_status = DESTROYED_PICTURE;
782 p_vout->i_heap_size--;
784 vlc_mutex_unlock( &p_vout->picture_lock );
785 p_last_picture = NULL;
788 /* Compute FPS rate */
789 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
792 if( !p_picture->b_force &&
793 p_picture != p_last_picture &&
794 display_date < current_date + p_vout->render_time )
796 /* Picture is late: it will be destroyed and the thread
797 * will directly choose the next picture */
798 vlc_mutex_lock( &p_vout->picture_lock );
799 if( p_picture->i_refcount )
801 /* Pretend we displayed the picture, but don't destroy
802 * it since the decoder might still need it. */
803 p_picture->i_status = DISPLAYED_PICTURE;
807 /* Destroy the picture without displaying it */
808 p_picture->i_status = DESTROYED_PICTURE;
809 p_vout->i_heap_size--;
811 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
812 current_date - display_date );
813 vlc_mutex_unlock( &p_vout->picture_lock );
819 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
821 /* Picture is waaay too early: it will be destroyed */
822 vlc_mutex_lock( &p_vout->picture_lock );
823 if( p_picture->i_refcount )
825 /* Pretend we displayed the picture, but don't destroy
826 * it since the decoder might still need it. */
827 p_picture->i_status = DISPLAYED_PICTURE;
831 /* Destroy the picture without displaying it */
832 p_picture->i_status = DESTROYED_PICTURE;
833 p_vout->i_heap_size--;
835 msg_Warn( p_vout, "vout warning: early picture skipped "
836 "("I64Fd")", display_date - current_date
837 - p_vout->i_pts_delay );
838 vlc_mutex_unlock( &p_vout->picture_lock );
843 if( display_date > current_date + VOUT_DISPLAY_DELAY )
845 /* A picture is ready to be rendered, but its rendering date
846 * is far from the current one so the thread will perform an
847 * empty loop as if no picture were found. The picture state
852 else if( p_picture == p_last_picture )
854 /* We are asked to repeat the previous picture, but we first
855 * wait for a couple of idle loops */
856 if( i_idle_loops < 4 )
863 /* We set the display date to something high, otherwise
864 * we'll have lots of problems with late pictures */
865 display_date = current_date + p_vout->render_time;
870 if( p_picture == NULL )
876 * Check for subpictures to display
878 p_subpic = vout_SortSubPictures( p_vout, display_date );
883 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
886 * Call the plugin-specific rendering method if there is one
888 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
890 /* Render the direct buffer returned by vout_RenderPicture */
891 p_vout->pf_render( p_vout, p_directbuffer );
897 if( display_date != 0 && p_directbuffer != NULL )
899 mtime_t current_render_time = mdate() - current_date;
900 /* if render time is very large we don't include it in the mean */
901 if( current_render_time < p_vout->render_time +
904 /* Store render time using a sliding mean weighting to
905 * current value in a 3 to 1 ratio*/
906 p_vout->render_time *= 3;
907 p_vout->render_time += current_render_time;
908 p_vout->render_time >>= 2;
912 /* Give back change lock */
913 vlc_mutex_unlock( &p_vout->change_lock );
915 /* Sleep a while or until a given date */
916 if( display_date != 0 )
918 /* If there are filters in the chain, better give them the picture
920 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
922 mwait( display_date - VOUT_MWAIT_TOLERANCE );
927 msleep( VOUT_IDLE_SLEEP );
930 /* On awakening, take back lock and send immediately picture
932 vlc_mutex_lock( &p_vout->change_lock );
935 * Display the previously rendered picture
937 if( p_picture != NULL && p_directbuffer != NULL )
939 /* Display the direct buffer returned by vout_RenderPicture */
940 if( p_vout->pf_display )
942 p_vout->pf_display( p_vout, p_directbuffer );
945 /* Reinitialize idle loop count */
948 /* Tell the vout this was the last picture and that it does not
949 * need to be forced anymore. */
950 p_last_picture = p_picture;
951 p_last_picture->b_force = 0;
955 * Check events and manage thread
957 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
959 /* A fatal error occured, and the thread must terminate
960 * immediately, without displaying anything - setting b_error to 1
961 * causes the immediate end of the main while() loop. */
965 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
967 /* this must only happen when the vout plugin is incapable of
968 * rescaling the picture itself. In this case we need to destroy
969 * the current picture buffers and recreate new ones with the right
973 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
975 p_vout->pf_end( p_vout );
976 for( i = 0; i < I_OUTPUTPICTURES; i++ )
977 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
979 I_OUTPUTPICTURES = 0;
980 if( p_vout->pf_init( p_vout ) )
982 msg_Err( p_vout, "cannot resize display" );
983 /* FIXME: pf_end will be called again in EndThread() */
987 /* Need to reinitialise the chroma plugin */
988 if( p_vout->chroma.p_module )
990 if( p_vout->chroma.p_module->pf_deactivate )
991 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
992 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
998 * Error loop - wait until the thread destruction is requested
1000 if( p_vout->b_error )
1002 ErrorThread( p_vout );
1006 EndThread( p_vout );
1008 /* Destroy thread structures allocated by CreateThread */
1009 DestroyThread( p_vout );
1012 /*****************************************************************************
1013 * ErrorThread: RunThread() error loop
1014 *****************************************************************************
1015 * This function is called when an error occured during thread main's loop. The
1016 * thread can still receive feed, but must be ready to terminate as soon as
1018 *****************************************************************************/
1019 static void ErrorThread( vout_thread_t *p_vout )
1021 /* Wait until a `die' order */
1022 while( !p_vout->b_die )
1025 msleep( VOUT_IDLE_SLEEP );
1029 /*****************************************************************************
1030 * EndThread: thread destruction
1031 *****************************************************************************
1032 * This function is called when the thread ends after a sucessful
1033 * initialization. It frees all ressources allocated by InitThread.
1034 *****************************************************************************/
1035 static void EndThread( vout_thread_t *p_vout )
1037 int i_index; /* index in heap */
1041 struct tms cpu_usage;
1042 times( &cpu_usage );
1044 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1045 cpu_usage.tms_utime, cpu_usage.tms_stime );
1049 if( !p_vout->b_direct )
1051 module_Unneed( p_vout, p_vout->chroma.p_module );
1054 /* Destroy all remaining pictures */
1055 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1057 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1059 free( p_vout->p_picture[i_index].p_data_orig );
1063 /* Destroy all remaining subpictures */
1064 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1066 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1068 vout_DestroySubPicture( p_vout,
1069 &p_vout->p_subpicture[i_index] );
1073 if( p_vout->p_text_renderer_module )
1074 module_Unneed( p_vout, p_vout->p_text_renderer_module );
1076 /* Destroy translation tables */
1077 p_vout->pf_end( p_vout );
1079 /* Release the change lock */
1080 vlc_mutex_unlock( &p_vout->change_lock );
1083 /*****************************************************************************
1084 * DestroyThread: thread destruction
1085 *****************************************************************************
1086 * This function is called when the thread ends. It frees all ressources
1087 * allocated by CreateThread. Status is available at this stage.
1088 *****************************************************************************/
1089 static void DestroyThread( vout_thread_t *p_vout )
1091 /* Destroy the locks */
1092 vlc_mutex_destroy( &p_vout->picture_lock );
1093 vlc_mutex_destroy( &p_vout->subpicture_lock );
1094 vlc_mutex_destroy( &p_vout->change_lock );
1096 /* Release the module */
1097 module_Unneed( p_vout, p_vout->p_module );
1100 /* following functions are local */
1102 static int ReduceHeight( int i_ratio )
1104 int i_dummy = VOUT_ASPECT_FACTOR;
1112 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1113 while( !(i_ratio & 1) && !(i_dummy & 1) )
1120 while( !(i_ratio % 3) && !(i_dummy % 3) )
1127 while( !(i_ratio % 5) && !(i_dummy % 5) )
1137 /*****************************************************************************
1138 * BinaryLog: computes the base 2 log of a binary value
1139 *****************************************************************************
1140 * This functions is used by MaskToShift, to get a bit index from a binary
1142 *****************************************************************************/
1143 static int BinaryLog( uint32_t i )
1147 if( i == 0 ) return -31337;
1149 if( i & 0xffff0000 ) i_log += 16;
1150 if( i & 0xff00ff00 ) i_log += 8;
1151 if( i & 0xf0f0f0f0 ) i_log += 4;
1152 if( i & 0xcccccccc ) i_log += 2;
1153 if( i & 0xaaaaaaaa ) i_log += 1;
1158 /*****************************************************************************
1159 * MaskToShift: transform a color mask into right and left shifts
1160 *****************************************************************************
1161 * This function is used for obtaining color shifts from masks.
1162 *****************************************************************************/
1163 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1165 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1169 *pi_left = *pi_right = 0;
1174 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1175 i_high = i_mask + i_low; /* higher bit of the mask */
1177 /* Transform bits into an index */
1178 i_low = BinaryLog (i_low);
1179 i_high = BinaryLog (i_high);
1181 /* Update pointers and return */
1183 *pi_right = (8 - i_high + i_low);
1186 /*****************************************************************************
1187 * InitWindowSize: find the initial dimensions the video window should have.
1188 *****************************************************************************
1189 * This function will check the "width", "height" and "zoom" config options and
1190 * will calculate the size that the video window should have.
1191 *****************************************************************************/
1192 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1196 int i_width, i_height;
1199 #define FP_FACTOR 1000 /* our fixed point factor */
1201 var_Get( p_vout, "align", &val );
1202 p_vout->i_alignment = val.i_int;
1204 var_Get( p_vout, "width", &val );
1205 i_width = val.i_int;
1206 var_Get( p_vout, "height", &val );
1207 i_height = val.i_int;
1208 var_Get( p_vout, "zoom", &val );
1209 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1211 if( i_width > 0 && i_height > 0)
1213 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1214 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1217 else if( i_width > 0 )
1219 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1220 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1221 p_vout->render.i_aspect / FP_FACTOR );
1224 else if( i_height > 0 )
1226 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1227 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1228 VOUT_ASPECT_FACTOR / FP_FACTOR );
1232 if( p_vout->render.i_height * p_vout->render.i_aspect
1233 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1235 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1236 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1237 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1241 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1242 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1243 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1249 /*****************************************************************************
1250 * vout_VarCallback: generic callback for intf variables
1251 *****************************************************************************/
1252 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1253 vlc_value_t old_value, vlc_value_t new_value,
1256 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1258 val.b_bool = VLC_TRUE;
1259 var_Set( p_vout, "intf-change", val );
1263 /*****************************************************************************
1264 * object variables callbacks: a bunch of object variables are used by the
1265 * interfaces to interact with the vout.
1266 *****************************************************************************/
1267 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1268 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1270 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1271 input_thread_t *p_input;
1274 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1276 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1280 /* Modify input as well because the vout might have to be restarted */
1281 var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1282 var_Set( p_input, "fullscreen", newval );
1284 vlc_object_release( p_input );
1287 val.b_bool = VLC_TRUE;
1288 var_Set( p_vout, "intf-change", val );
1292 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1293 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1295 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1296 input_thread_t *p_input;
1299 char *psz_mode = newval.psz_string;
1303 psz_filter = config_GetPsz( p_vout, "filter" );
1305 if( !psz_mode || !*psz_mode )
1307 config_PutPsz( p_vout, "filter", "" );
1311 if( !psz_filter || !*psz_filter )
1313 config_PutPsz( p_vout, "filter", "deinterlace" );
1317 if( strstr( psz_filter, "deinterlace" ) == NULL )
1319 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1320 strcat( psz_filter, ",deinterlace" );
1322 config_PutPsz( p_vout, "filter", psz_filter );
1326 if( psz_filter ) free( psz_filter );
1329 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1331 if( !p_input ) return VLC_EGENERIC;
1333 if( psz_mode && *psz_mode )
1335 val.psz_string = psz_mode;
1336 var_Set( p_vout, "deinterlace-mode", val );
1337 /* Modify input as well because the vout might have to be restarted */
1338 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1339 var_Set( p_input, "deinterlace-mode", val );
1342 /* now restart all video streams */
1343 vlc_mutex_lock( &p_input->stream.stream_lock );
1345 p_vout->b_filter_change = VLC_TRUE;
1347 #define ES p_input->stream.pp_es[i]
1349 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1351 if( ( ES->i_cat == VIDEO_ES ) && ES->p_decoder_fifo != NULL )
1353 input_UnselectES( p_input, ES );
1354 input_SelectES( p_input, ES );
1358 vlc_mutex_unlock( &p_input->stream.stream_lock );
1360 vlc_object_release( p_input );
1362 val.b_bool = VLC_TRUE;
1363 var_Set( p_vout, "intf-change", val );