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 DeinterlaceCallback( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
65 static int FilterCallback( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
71 * Set the i_aspect_x and i_aspect_y from i_aspect.
73 void vout_AspectRatio( unsigned int i_aspect,
74 /*out*/ unsigned int *i_aspect_x,
75 /*out*/ unsigned int *i_aspect_y )
77 unsigned int i_pgcd = ReduceHeight( i_aspect );
78 *i_aspect_x = i_aspect / i_pgcd;
79 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
82 /*****************************************************************************
83 * vout_Request: find a video output thread, create one, or destroy one.
84 *****************************************************************************
85 * This function looks for a video output thread matching the current
86 * properties. If not found, it spawns a new one.
87 *****************************************************************************/
88 vout_thread_t * __vout_Request ( vlc_object_t *p_this, vout_thread_t *p_vout,
89 unsigned int i_width, unsigned int i_height,
90 vlc_fourcc_t i_chroma, unsigned int i_aspect )
92 if( !i_width || !i_height || !i_chroma )
94 /* Reattach video output to input before bailing out */
97 vlc_object_t *p_playlist;
99 p_playlist = vlc_object_find( p_this,
100 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
104 vlc_object_detach( p_vout );
105 vlc_object_attach( p_vout, p_playlist );
107 vlc_object_release( p_playlist );
111 msg_Dbg( p_this, "cannot find playlist, destroying vout" );
112 vlc_object_detach( p_vout );
113 vout_Destroy( p_vout );
119 /* If a video output was provided, lock it, otherwise look for one. */
122 vlc_object_yield( p_vout );
126 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
130 vlc_object_t *p_playlist;
132 p_playlist = vlc_object_find( p_this,
133 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
136 p_vout = vlc_object_find( p_playlist,
137 VLC_OBJECT_VOUT, FIND_CHILD );
138 /* only first children of p_input for unused vout */
139 if( p_vout && p_vout->p_parent != p_playlist )
141 vlc_object_release( p_vout );
144 vlc_object_release( p_playlist );
149 /* If we now have a video output, check it has the right properties */
152 char *psz_filter_chain;
154 /* We don't directly check for the "filter" variable for obvious
155 * performance reasons. */
156 if( p_vout->b_filter_change )
158 psz_filter_chain = config_GetPsz( p_this, "filter" );
160 if( psz_filter_chain && !*psz_filter_chain )
162 free( psz_filter_chain );
163 psz_filter_chain = NULL;
165 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
167 free( p_vout->psz_filter_chain );
168 p_vout->psz_filter_chain = NULL;
171 if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
172 ( psz_filter_chain && p_vout->psz_filter_chain &&
173 !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
175 p_vout->b_filter_change = VLC_FALSE;
178 if( psz_filter_chain ) free( psz_filter_chain );
181 if( ( p_vout->render.i_width != i_width ) ||
182 ( p_vout->render.i_height != i_height ) ||
183 ( p_vout->render.i_chroma != i_chroma ) ||
184 ( p_vout->render.i_aspect != i_aspect
185 && !p_vout->b_override_aspect ) ||
186 p_vout->b_filter_change )
188 /* We are not interested in this format, close this vout */
189 vlc_object_detach( p_vout );
190 vlc_object_release( p_vout );
191 vout_Destroy( p_vout );
196 /* This video output is cool! Hijack it. */
197 vlc_object_detach( p_vout );
198 vlc_object_attach( p_vout, p_this );
199 vlc_object_release( p_vout );
205 msg_Dbg( p_this, "no usable vout present, spawning one" );
207 p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
213 /*****************************************************************************
214 * vout_Create: creates a new video output thread
215 *****************************************************************************
216 * This function creates a new video output thread, and returns a pointer
217 * to its description. On error, it returns NULL.
218 *****************************************************************************/
219 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
220 unsigned int i_width, unsigned int i_height,
221 vlc_fourcc_t i_chroma, unsigned int i_aspect )
223 vout_thread_t * p_vout; /* thread descriptor */
224 input_thread_t * p_input_thread;
225 int i_index; /* loop variable */
227 vlc_value_t val, text;
229 /* Allocate descriptor */
230 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
233 msg_Err( p_parent, "out of memory" );
237 /* Initialize pictures and subpictures - translation tables and functions
238 * will be initialized later in InitThread */
239 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
241 p_vout->p_picture[i_index].pf_lock = NULL;
242 p_vout->p_picture[i_index].pf_unlock = NULL;
243 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
244 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
247 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
249 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
250 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
253 /* No images in the heap */
254 p_vout->i_heap_size = 0;
256 /* Initialize the rendering heap */
257 I_RENDERPICTURES = 0;
258 p_vout->render.i_width = i_width;
259 p_vout->render.i_height = i_height;
260 p_vout->render.i_chroma = i_chroma;
261 p_vout->render.i_aspect = i_aspect;
263 p_vout->render.i_rmask = 0;
264 p_vout->render.i_gmask = 0;
265 p_vout->render.i_bmask = 0;
267 p_vout->render.i_last_used_pic = -1;
268 p_vout->render.b_allow_modify_pics = 1;
270 /* Zero the output heap */
271 I_OUTPUTPICTURES = 0;
272 p_vout->output.i_width = 0;
273 p_vout->output.i_height = 0;
274 p_vout->output.i_chroma = 0;
275 p_vout->output.i_aspect = 0;
277 p_vout->output.i_rmask = 0;
278 p_vout->output.i_gmask = 0;
279 p_vout->output.i_bmask = 0;
281 /* Initialize misc stuff */
282 p_vout->i_changes = 0;
284 p_vout->b_grayscale = 0;
286 p_vout->b_interface = 0;
288 p_vout->b_fullscreen = 0;
289 p_vout->i_alignment = 0;
290 p_vout->render_time = 10;
291 p_vout->c_fps_samples = 0;
292 p_vout->b_filter_change = 0;
293 p_vout->pf_control = 0;
295 /* Mouse coordinates */
296 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
297 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
298 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
299 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
300 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
302 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
303 val.b_bool = VLC_TRUE;
304 var_Set( p_vout, "intf-change", val );
306 /* Initialize locks */
307 vlc_mutex_init( p_vout, &p_vout->picture_lock );
308 vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
309 vlc_mutex_init( p_vout, &p_vout->change_lock );
311 /* Attach the new object now so we can use var inheritance below */
312 vlc_object_attach( p_vout, p_parent );
314 /* Take care of some "interface/control" related initialisations */
315 vout_IntfInit( p_vout );
317 p_vout->b_override_aspect = VLC_FALSE;
319 /* If the parent is not a VOUT object, that means we are at the start of
320 * the video output pipe */
321 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
323 var_Get( p_vout, "aspect-ratio", &val );
325 /* Check whether the user tried to override aspect ratio */
328 unsigned int i_new_aspect = i_aspect;
329 char *psz_parser = strchr( val.psz_string, ':' );
333 *psz_parser++ = '\0';
334 i_new_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
335 / atoi( psz_parser );
339 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
340 * atof( val.psz_string )
344 free( val.psz_string );
346 if( i_new_aspect && i_new_aspect != i_aspect )
348 unsigned int i_aspect_x, i_aspect_y;
350 vout_AspectRatio( i_new_aspect, &i_aspect_x, &i_aspect_y );
352 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
353 i_aspect_x, i_aspect_y );
355 p_vout->render.i_aspect = i_new_aspect;
357 p_vout->b_override_aspect = VLC_TRUE;
361 /* Look for the default filter configuration */
362 var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
363 var_Get( p_vout, "filter", &val );
364 p_vout->psz_filter_chain = val.psz_string;
368 /* continue the parent's filter chain */
371 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
372 if( psz_end && *(psz_end+1) )
373 p_vout->psz_filter_chain = strdup( psz_end+1 );
374 else p_vout->psz_filter_chain = NULL;
377 /* Choose the video output module */
378 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
380 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
381 var_Get( p_vout, "vout", &val );
382 psz_plugin = val.psz_string;
386 /* the filter chain is a string list of filters separated by double
390 psz_end = strchr( p_vout->psz_filter_chain, ':' );
392 psz_plugin = strndup( p_vout->psz_filter_chain,
393 psz_end - p_vout->psz_filter_chain );
394 else psz_plugin = strdup( p_vout->psz_filter_chain );
397 /* Initialize the dimensions of the video window */
398 InitWindowSize( p_vout, &p_vout->i_window_width,
399 &p_vout->i_window_height );
401 /* Create the vout thread */
402 p_vout->p_module = module_Need( p_vout,
403 ( p_vout->psz_filter_chain && *p_vout->psz_filter_chain ) ?
404 "video filter" : "video output", psz_plugin, 0 );
406 if( psz_plugin ) free( psz_plugin );
407 if( p_vout->p_module == NULL )
409 msg_Err( p_vout, "no suitable vout module" );
410 vlc_object_destroy( p_vout );
414 p_vout->p_text_renderer_module =
415 module_Need( p_vout, "text renderer", NULL, 0 );
416 if( p_vout->p_text_renderer_module == NULL )
418 msg_Warn( p_vout, "no suitable text renderer module" );
419 p_vout->pf_add_string = NULL;
422 /* Create a few object variables for interface interaction */
423 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
424 text.psz_string = _("Deinterlace");
425 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
426 val.psz_string = ""; text.psz_string = _("Disable");
427 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
428 val.psz_string = "discard"; text.psz_string = _("Discard");
429 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
430 val.psz_string = "blend"; text.psz_string = _("Blend");
431 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
432 val.psz_string = "mean"; text.psz_string = _("Mean");
433 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
434 val.psz_string = "bob"; text.psz_string = _("Bob");
435 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
436 val.psz_string = "linear"; text.psz_string = _("Linear");
437 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
438 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
440 var_Set( p_vout, "deinterlace", val );
441 if( val.psz_string ) free( val.psz_string );
443 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
446 var_Create( p_vout, "filter", VLC_VAR_STRING );
447 text.psz_string = _("Filters");
448 var_Change( p_vout, "filter", VLC_VAR_SETTEXT, &text, NULL );
449 var_Change( p_vout, "filter", VLC_VAR_INHERITVALUE, &val, NULL );
452 var_Set( p_vout, "filter", val );
453 free( val.psz_string );
455 var_AddCallback( p_vout, "filter", FilterCallback, NULL );
457 /* Calculate delay created by internal caching */
458 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
459 VLC_OBJECT_INPUT, FIND_ANYWHERE );
462 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
463 vlc_object_release( p_input_thread );
467 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
470 if( vlc_thread_create( p_vout, "video output", RunThread,
471 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
473 msg_Err( p_vout, "out of memory" );
474 module_Unneed( p_vout, p_vout->p_module );
475 vlc_object_detach( p_vout );
476 vlc_object_destroy( p_vout );
480 if( p_vout->b_error )
482 msg_Err( p_vout, "video output creation failed" );
484 /* Make sure the thread is destroyed */
485 p_vout->b_die = VLC_TRUE;
486 vlc_thread_join( p_vout );
488 vlc_object_detach( p_vout );
489 vlc_object_destroy( p_vout );
496 /*****************************************************************************
497 * vout_Destroy: destroys a previously created video output
498 *****************************************************************************
499 * Destroy a terminated thread.
500 * The function will request a destruction of the specified thread. If pi_error
501 * is NULL, it will return once the thread is destroyed. Else, it will be
502 * update using one of the THREAD_* constants.
503 *****************************************************************************/
504 void vout_Destroy( vout_thread_t *p_vout )
506 vlc_object_t *p_playlist;
508 /* Request thread destruction */
509 p_vout->b_die = VLC_TRUE;
510 vlc_thread_join( p_vout );
512 var_Destroy( p_vout, "intf-change" );
514 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
517 if( p_vout->psz_filter_chain ) free( p_vout->psz_filter_chain );
520 vlc_object_destroy( p_vout );
522 /* If it was the last vout, tell the interface to show up */
523 if( p_playlist != NULL )
525 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
526 VLC_OBJECT_VOUT, FIND_ANYWHERE );
527 if( p_another_vout == NULL )
530 val.b_bool = VLC_TRUE;
531 var_Set( p_playlist, "intf-show", val );
535 vlc_object_release( p_another_vout );
537 vlc_object_release( p_playlist );
541 /*****************************************************************************
542 * InitThread: initialize video output thread
543 *****************************************************************************
544 * This function is called from RunThread and performs the second step of the
545 * initialization. It returns 0 on success. Note that the thread's flag are not
546 * modified inside this function.
547 *****************************************************************************/
548 static int InitThread( vout_thread_t *p_vout )
551 unsigned int i_aspect_x, i_aspect_y;
553 vlc_mutex_lock( &p_vout->change_lock );
559 /* Initialize output method, it allocates direct buffers for us */
560 if( p_vout->pf_init( p_vout ) )
562 vlc_mutex_unlock( &p_vout->change_lock );
566 if( !I_OUTPUTPICTURES )
568 msg_Err( p_vout, "plugin was unable to allocate at least "
569 "one direct buffer" );
570 p_vout->pf_end( p_vout );
571 vlc_mutex_unlock( &p_vout->change_lock );
575 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
577 msg_Err( p_vout, "plugin allocated too many direct buffers, "
578 "our internal buffers must have overflown." );
579 p_vout->pf_end( p_vout );
580 vlc_mutex_unlock( &p_vout->change_lock );
584 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
587 if( !p_vout->psz_filter_chain )
589 char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
593 int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
595 / p_vout->output.i_height;
598 if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
600 vout_AspectRatio( i_new_aspect, &i_aspect_x, &i_aspect_y );
602 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
603 i_aspect_x, i_aspect_y );
604 p_vout->output.i_aspect = i_new_aspect;
610 vout_AspectRatio( p_vout->render.i_aspect, &i_aspect_x, &i_aspect_y );
612 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
613 p_vout->render.i_width, p_vout->render.i_height,
614 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
615 i_aspect_x, i_aspect_y );
617 vout_AspectRatio( p_vout->output.i_aspect, &i_aspect_x, &i_aspect_y );
619 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
620 p_vout->output.i_width, p_vout->output.i_height,
621 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
622 i_aspect_x, i_aspect_y );
624 /* Calculate shifts from system-updated masks */
625 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
626 p_vout->output.i_rmask );
627 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
628 p_vout->output.i_gmask );
629 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
630 p_vout->output.i_bmask );
632 /* Check whether we managed to create direct buffers similar to
633 * the render buffers, ie same size and chroma */
634 if( ( p_vout->output.i_width == p_vout->render.i_width )
635 && ( p_vout->output.i_height == p_vout->render.i_height )
636 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
638 /* Cool ! We have direct buffers, we can ask the decoder to
639 * directly decode into them ! Map the first render buffers to
640 * the first direct buffers, but keep the first direct buffer
641 * for memcpy operations */
642 p_vout->b_direct = 1;
644 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
646 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
647 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
648 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
650 /* We have enough direct buffers so there's no need to
651 * try to use system memory buffers. */
654 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
658 msg_Dbg( p_vout, "direct render, mapping "
659 "render pictures 0-%i to system pictures 1-%i",
660 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
664 /* Rats... Something is wrong here, we could not find an output
665 * plugin able to directly render what we decode. See if we can
666 * find a chroma plugin to do the conversion */
667 p_vout->b_direct = 0;
669 /* Choose the best module */
670 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
672 if( p_vout->chroma.p_module == NULL )
674 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
675 (char*)&p_vout->render.i_chroma,
676 (char*)&p_vout->output.i_chroma );
677 p_vout->pf_end( p_vout );
678 vlc_mutex_unlock( &p_vout->change_lock );
682 msg_Dbg( p_vout, "indirect render, mapping "
683 "render pictures 0-%i to system pictures %i-%i",
684 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
685 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
687 /* Append render buffers after the direct buffers */
688 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
690 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
693 /* Check if we have enough render pictures */
694 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
699 /* Link pictures back to their heap */
700 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
702 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
705 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
707 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
710 /* XXX XXX mark thread ready */
714 /*****************************************************************************
715 * RunThread: video output thread
716 *****************************************************************************
717 * Video output thread. This function does only returns when the thread is
718 * terminated. It handles the pictures arriving in the video heap and the
719 * display device events.
720 *****************************************************************************/
721 static void RunThread( vout_thread_t *p_vout)
723 int i_index; /* index in heap */
724 int i_idle_loops = 0; /* loops without displaying a picture */
725 mtime_t current_date; /* current date */
726 mtime_t display_date; /* display date */
728 picture_t * p_picture; /* picture pointer */
729 picture_t * p_last_picture = NULL; /* last picture */
730 picture_t * p_directbuffer; /* direct buffer to display */
732 subpicture_t * p_subpic; /* subpicture pointer */
737 p_vout->b_error = InitThread( p_vout );
739 /* signal the creation of the vout */
740 vlc_thread_ready( p_vout );
742 if( p_vout->b_error )
744 /* Destroy thread structures allocated by Create and InitThread */
745 DestroyThread( p_vout );
750 * Main loop - it is not executed if an error occured during
753 while( (!p_vout->b_die) && (!p_vout->b_error) )
755 /* Initialize loop variables */
758 current_date = mdate();
762 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
764 msg_Dbg( p_vout, "picture heap: %d/%d",
765 I_RENDERPICTURES, p_vout->i_heap_size );
770 * Find the picture to display (the one with the earliest date).
771 * This operation does not need lock, since only READY_PICTUREs
773 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
775 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
776 && ( (p_picture == NULL) ||
777 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
779 p_picture = PP_RENDERPICTURE[i_index];
780 display_date = p_picture->date;
786 /* If we met the last picture, parse again to see whether there is
787 * a more appropriate one. */
788 if( p_picture == p_last_picture )
790 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
792 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
793 && (PP_RENDERPICTURE[i_index] != p_last_picture)
794 && ((p_picture == p_last_picture) ||
795 (PP_RENDERPICTURE[i_index]->date < display_date)) )
797 p_picture = PP_RENDERPICTURE[i_index];
798 display_date = p_picture->date;
803 /* If we found better than the last picture, destroy it */
804 if( p_last_picture && p_picture != p_last_picture )
806 vlc_mutex_lock( &p_vout->picture_lock );
807 if( p_last_picture->i_refcount )
809 p_last_picture->i_status = DISPLAYED_PICTURE;
813 p_last_picture->i_status = DESTROYED_PICTURE;
814 p_vout->i_heap_size--;
816 vlc_mutex_unlock( &p_vout->picture_lock );
817 p_last_picture = NULL;
820 /* Compute FPS rate */
821 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
824 if( !p_picture->b_force &&
825 p_picture != p_last_picture &&
826 display_date < current_date + p_vout->render_time )
828 /* Picture is late: it will be destroyed and the thread
829 * will directly choose the next picture */
830 vlc_mutex_lock( &p_vout->picture_lock );
831 if( p_picture->i_refcount )
833 /* Pretend we displayed the picture, but don't destroy
834 * it since the decoder might still need it. */
835 p_picture->i_status = DISPLAYED_PICTURE;
839 /* Destroy the picture without displaying it */
840 p_picture->i_status = DESTROYED_PICTURE;
841 p_vout->i_heap_size--;
843 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
844 current_date - display_date );
845 vlc_mutex_unlock( &p_vout->picture_lock );
851 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
853 /* Picture is waaay too early: it will be destroyed */
854 vlc_mutex_lock( &p_vout->picture_lock );
855 if( p_picture->i_refcount )
857 /* Pretend we displayed the picture, but don't destroy
858 * it since the decoder might still need it. */
859 p_picture->i_status = DISPLAYED_PICTURE;
863 /* Destroy the picture without displaying it */
864 p_picture->i_status = DESTROYED_PICTURE;
865 p_vout->i_heap_size--;
867 msg_Warn( p_vout, "vout warning: early picture skipped "
868 "("I64Fd")", display_date - current_date
869 - p_vout->i_pts_delay );
870 vlc_mutex_unlock( &p_vout->picture_lock );
875 if( display_date > current_date + VOUT_DISPLAY_DELAY )
877 /* A picture is ready to be rendered, but its rendering date
878 * is far from the current one so the thread will perform an
879 * empty loop as if no picture were found. The picture state
884 else if( p_picture == p_last_picture )
886 /* We are asked to repeat the previous picture, but we first
887 * wait for a couple of idle loops */
888 if( i_idle_loops < 4 )
895 /* We set the display date to something high, otherwise
896 * we'll have lots of problems with late pictures */
897 display_date = current_date + p_vout->render_time;
902 if( p_picture == NULL )
908 * Check for subpictures to display
910 p_subpic = vout_SortSubPictures( p_vout, display_date );
915 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
918 * Call the plugin-specific rendering method if there is one
920 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
922 /* Render the direct buffer returned by vout_RenderPicture */
923 p_vout->pf_render( p_vout, p_directbuffer );
929 if( display_date != 0 && p_directbuffer != NULL )
931 mtime_t current_render_time = mdate() - current_date;
932 /* if render time is very large we don't include it in the mean */
933 if( current_render_time < p_vout->render_time +
936 /* Store render time using a sliding mean weighting to
937 * current value in a 3 to 1 ratio*/
938 p_vout->render_time *= 3;
939 p_vout->render_time += current_render_time;
940 p_vout->render_time >>= 2;
944 /* Give back change lock */
945 vlc_mutex_unlock( &p_vout->change_lock );
947 /* Sleep a while or until a given date */
948 if( display_date != 0 )
950 /* If there are filters in the chain, better give them the picture
952 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
954 mwait( display_date - VOUT_MWAIT_TOLERANCE );
959 msleep( VOUT_IDLE_SLEEP );
962 /* On awakening, take back lock and send immediately picture
964 vlc_mutex_lock( &p_vout->change_lock );
967 * Display the previously rendered picture
969 if( p_picture != NULL && p_directbuffer != NULL )
971 /* Display the direct buffer returned by vout_RenderPicture */
972 if( p_vout->pf_display )
974 p_vout->pf_display( p_vout, p_directbuffer );
977 /* Reinitialize idle loop count */
980 /* Tell the vout this was the last picture and that it does not
981 * need to be forced anymore. */
982 p_last_picture = p_picture;
983 p_last_picture->b_force = 0;
987 * Check events and manage thread
989 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
991 /* A fatal error occured, and the thread must terminate
992 * immediately, without displaying anything - setting b_error to 1
993 * causes the immediate end of the main while() loop. */
997 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
999 /* this must only happen when the vout plugin is incapable of
1000 * rescaling the picture itself. In this case we need to destroy
1001 * the current picture buffers and recreate new ones with the right
1005 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1007 p_vout->pf_end( p_vout );
1008 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1009 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1011 I_OUTPUTPICTURES = 0;
1012 if( p_vout->pf_init( p_vout ) )
1014 msg_Err( p_vout, "cannot resize display" );
1015 /* FIXME: pf_end will be called again in EndThread() */
1016 p_vout->b_error = 1;
1019 /* Need to reinitialise the chroma plugin */
1020 if( p_vout->chroma.p_module )
1022 if( p_vout->chroma.p_module->pf_deactivate )
1023 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
1024 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
1028 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1030 /* This happens when the picture buffers need to be recreated.
1031 * This is useful on multimonitor displays for instance.
1033 * Warning: This only works when the vout creates only 1 picture
1035 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1037 if( !p_vout->b_direct )
1039 module_Unneed( p_vout, p_vout->chroma.p_module );
1042 vlc_mutex_lock( &p_vout->picture_lock );
1044 p_vout->pf_end( p_vout );
1046 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1048 p_vout->b_error = InitThread( p_vout );
1050 vlc_mutex_unlock( &p_vout->picture_lock );
1055 * Error loop - wait until the thread destruction is requested
1057 if( p_vout->b_error )
1059 ErrorThread( p_vout );
1063 EndThread( p_vout );
1065 /* Destroy thread structures allocated by CreateThread */
1066 DestroyThread( p_vout );
1069 /*****************************************************************************
1070 * ErrorThread: RunThread() error loop
1071 *****************************************************************************
1072 * This function is called when an error occured during thread main's loop. The
1073 * thread can still receive feed, but must be ready to terminate as soon as
1075 *****************************************************************************/
1076 static void ErrorThread( vout_thread_t *p_vout )
1078 /* Wait until a `die' order */
1079 while( !p_vout->b_die )
1082 msleep( VOUT_IDLE_SLEEP );
1086 /*****************************************************************************
1087 * EndThread: thread destruction
1088 *****************************************************************************
1089 * This function is called when the thread ends after a sucessful
1090 * initialization. It frees all ressources allocated by InitThread.
1091 *****************************************************************************/
1092 static void EndThread( vout_thread_t *p_vout )
1094 int i_index; /* index in heap */
1098 struct tms cpu_usage;
1099 times( &cpu_usage );
1101 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1102 cpu_usage.tms_utime, cpu_usage.tms_stime );
1106 if( !p_vout->b_direct )
1108 module_Unneed( p_vout, p_vout->chroma.p_module );
1111 /* Destroy all remaining pictures */
1112 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1114 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1116 free( p_vout->p_picture[i_index].p_data_orig );
1120 /* Destroy all remaining subpictures */
1121 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1123 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1125 vout_DestroySubPicture( p_vout,
1126 &p_vout->p_subpicture[i_index] );
1130 if( p_vout->p_text_renderer_module )
1131 module_Unneed( p_vout, p_vout->p_text_renderer_module );
1133 /* Destroy translation tables */
1134 p_vout->pf_end( p_vout );
1136 /* Release the change lock */
1137 vlc_mutex_unlock( &p_vout->change_lock );
1140 /*****************************************************************************
1141 * DestroyThread: thread destruction
1142 *****************************************************************************
1143 * This function is called when the thread ends. It frees all ressources
1144 * allocated by CreateThread. Status is available at this stage.
1145 *****************************************************************************/
1146 static void DestroyThread( vout_thread_t *p_vout )
1148 /* Destroy the locks */
1149 vlc_mutex_destroy( &p_vout->picture_lock );
1150 vlc_mutex_destroy( &p_vout->subpicture_lock );
1151 vlc_mutex_destroy( &p_vout->change_lock );
1153 /* Release the module */
1154 module_Unneed( p_vout, p_vout->p_module );
1157 /* following functions are local */
1159 static int ReduceHeight( int i_ratio )
1161 int i_dummy = VOUT_ASPECT_FACTOR;
1169 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1170 while( !(i_ratio & 1) && !(i_dummy & 1) )
1177 while( !(i_ratio % 3) && !(i_dummy % 3) )
1184 while( !(i_ratio % 5) && !(i_dummy % 5) )
1194 /*****************************************************************************
1195 * BinaryLog: computes the base 2 log of a binary value
1196 *****************************************************************************
1197 * This functions is used by MaskToShift, to get a bit index from a binary
1199 *****************************************************************************/
1200 static int BinaryLog( uint32_t i )
1204 if( i == 0 ) return -31337;
1206 if( i & 0xffff0000 ) i_log += 16;
1207 if( i & 0xff00ff00 ) i_log += 8;
1208 if( i & 0xf0f0f0f0 ) i_log += 4;
1209 if( i & 0xcccccccc ) i_log += 2;
1210 if( i & 0xaaaaaaaa ) i_log += 1;
1215 /*****************************************************************************
1216 * MaskToShift: transform a color mask into right and left shifts
1217 *****************************************************************************
1218 * This function is used for obtaining color shifts from masks.
1219 *****************************************************************************/
1220 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1222 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1226 *pi_left = *pi_right = 0;
1231 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1232 i_high = i_mask + i_low; /* higher bit of the mask */
1234 /* Transform bits into an index */
1235 i_low = BinaryLog (i_low);
1236 i_high = BinaryLog (i_high);
1238 /* Update pointers and return */
1240 *pi_right = (8 - i_high + i_low);
1243 /*****************************************************************************
1244 * InitWindowSize: find the initial dimensions the video window should have.
1245 *****************************************************************************
1246 * This function will check the "width", "height" and "zoom" config options and
1247 * will calculate the size that the video window should have.
1248 *****************************************************************************/
1249 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1253 int i_width, i_height;
1256 #define FP_FACTOR 1000 /* our fixed point factor */
1258 var_Get( p_vout, "align", &val );
1259 p_vout->i_alignment = val.i_int;
1261 var_Get( p_vout, "width", &val );
1262 i_width = val.i_int;
1263 var_Get( p_vout, "height", &val );
1264 i_height = val.i_int;
1265 var_Get( p_vout, "zoom", &val );
1266 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1268 if( i_width > 0 && i_height > 0)
1270 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1271 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1274 else if( i_width > 0 )
1276 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1277 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1278 p_vout->render.i_aspect / FP_FACTOR );
1281 else if( i_height > 0 )
1283 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1284 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1285 VOUT_ASPECT_FACTOR / FP_FACTOR );
1289 if( p_vout->render.i_height * p_vout->render.i_aspect
1290 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1292 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1293 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1294 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1298 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1299 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1300 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1306 /*****************************************************************************
1307 * vout_VarCallback: generic callback for intf variables
1308 *****************************************************************************/
1309 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1310 vlc_value_t old_value, vlc_value_t new_value,
1313 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1315 val.b_bool = VLC_TRUE;
1316 var_Set( p_vout, "intf-change", val );
1320 /*****************************************************************************
1321 * object variables callbacks: a bunch of object variables are used by the
1322 * interfaces to interact with the vout.
1323 *****************************************************************************/
1324 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1325 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1327 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1328 input_thread_t *p_input;
1331 char *psz_mode = newval.psz_string;
1335 psz_filter = config_GetPsz( p_vout, "filter" );
1337 if( !psz_mode || !*psz_mode )
1339 config_PutPsz( p_vout, "filter", "" );
1343 if( !psz_filter || !*psz_filter )
1345 config_PutPsz( p_vout, "filter", "deinterlace" );
1349 if( strstr( psz_filter, "deinterlace" ) == NULL )
1351 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1352 strcat( psz_filter, ",deinterlace" );
1354 config_PutPsz( p_vout, "filter", psz_filter );
1358 if( psz_filter ) free( psz_filter );
1361 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1363 if( !p_input ) return VLC_EGENERIC;
1365 if( psz_mode && *psz_mode )
1367 val.psz_string = psz_mode;
1368 var_Set( p_vout, "deinterlace-mode", val );
1369 /* Modify input as well because the vout might have to be restarted */
1370 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1371 var_Set( p_input, "deinterlace-mode", val );
1374 /* now restart all video streams */
1375 vlc_mutex_lock( &p_input->stream.stream_lock );
1377 p_vout->b_filter_change = VLC_TRUE;
1379 #define ES p_input->stream.pp_es[i]
1381 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1383 if( ( ES->i_cat == VIDEO_ES ) && ES->p_dec != NULL )
1385 input_UnselectES( p_input, ES );
1386 input_SelectES( p_input, ES );
1390 vlc_mutex_unlock( &p_input->stream.stream_lock );
1392 vlc_object_release( p_input );
1394 val.b_bool = VLC_TRUE;
1395 var_Set( p_vout, "intf-change", val );
1399 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1400 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1402 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1403 input_thread_t *p_input;
1407 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1412 msg_Err( p_vout, "Input not found" );
1413 return( VLC_EGENERIC );
1415 /* Restart the video stream */
1416 vlc_mutex_lock( &p_input->stream.stream_lock );
1418 p_vout->b_filter_change = VLC_TRUE;
1420 #define ES p_input->stream.pp_es[i]
1422 for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1424 if( ( ES->i_cat == VIDEO_ES ) && ES->p_dec != NULL )
1426 input_UnselectES( p_input, ES );
1427 input_SelectES( p_input, ES );
1431 vlc_mutex_unlock( &p_input->stream.stream_lock );
1433 vlc_object_release( p_input );
1435 val.b_bool = VLC_TRUE;
1436 var_Set( p_vout, "intf-change", val );