1 /*****************************************************************************
2 * video_output.c : video output thread
4 * This module describes the programming interface for video output threads.
5 * It includes functions allowing to open a new thread, send pictures to a
6 * thread, and destroy a previously oppened video output thread.
7 *****************************************************************************
8 * Copyright (C) 2000-2004 VideoLAN
11 * Authors: Vincent Seguin <seguin@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <stdlib.h> /* free() */
35 #ifdef HAVE_SYS_TIMES_H
36 # include <sys/times.h>
39 #include "vlc_video.h"
40 #include "video_output.h"
42 #include <vlc/input.h> /* for input_thread_t and i_pts_delay */
44 #if defined( SYS_DARWIN )
45 #include "darwin_specific.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int InitThread ( vout_thread_t * );
52 static void RunThread ( vout_thread_t * );
53 static void ErrorThread ( vout_thread_t * );
54 static void EndThread ( vout_thread_t * );
55 static void DestroyThread ( vout_thread_t * );
57 static void AspectRatio ( int, int *, int * );
58 static int BinaryLog ( uint32_t );
59 static void MaskToShift ( int *, int *, uint32_t );
60 static void InitWindowSize ( vout_thread_t *, int *, int * );
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 * );
68 /*****************************************************************************
69 * vout_Request: find a video output thread, create one, or destroy one.
70 *****************************************************************************
71 * This function looks for a video output thread matching the current
72 * properties. If not found, it spawns a new one.
73 *****************************************************************************/
74 vout_thread_t * __vout_Request ( vlc_object_t *p_this, vout_thread_t *p_vout,
75 unsigned int i_width, unsigned int i_height,
76 vlc_fourcc_t i_chroma, unsigned int i_aspect )
78 if( !i_width || !i_height || !i_chroma )
80 /* Reattach video output to input before bailing out */
83 vlc_object_t *p_playlist;
85 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
90 spu_Attach( p_vout->p_spu, p_this, VLC_FALSE );
91 vlc_object_detach( p_vout );
92 vlc_object_attach( p_vout, p_playlist );
94 vlc_object_release( p_playlist );
98 msg_Dbg( p_this, "cannot find playlist, destroying vout" );
99 vlc_object_detach( p_vout );
100 vout_Destroy( p_vout );
106 /* If a video output was provided, lock it, otherwise look for one. */
109 vlc_object_yield( p_vout );
113 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
117 vlc_object_t *p_playlist;
119 p_playlist = vlc_object_find( p_this,
120 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
123 p_vout = vlc_object_find( p_playlist,
124 VLC_OBJECT_VOUT, FIND_CHILD );
125 /* only first children of p_input for unused vout */
126 if( p_vout && p_vout->p_parent != p_playlist )
128 vlc_object_release( p_vout );
131 vlc_object_release( p_playlist );
136 /* If we now have a video output, check it has the right properties */
139 char *psz_filter_chain;
142 /* We don't directly check for the "filter" variable for obvious
143 * performance reasons. */
144 if( p_vout->b_filter_change )
146 var_Get( p_vout, "filter", &val );
147 psz_filter_chain = val.psz_string;
149 if( psz_filter_chain && !*psz_filter_chain )
151 free( psz_filter_chain );
152 psz_filter_chain = NULL;
154 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
156 free( p_vout->psz_filter_chain );
157 p_vout->psz_filter_chain = NULL;
160 if( !psz_filter_chain && !p_vout->psz_filter_chain )
162 p_vout->b_filter_change = VLC_FALSE;
165 if( psz_filter_chain ) free( psz_filter_chain );
168 if( ( p_vout->render.i_width != i_width ) ||
169 ( p_vout->render.i_height != i_height ) ||
170 ( p_vout->render.i_chroma != i_chroma ) ||
171 ( p_vout->render.i_aspect != i_aspect
172 && !p_vout->b_override_aspect ) ||
173 p_vout->b_filter_change )
175 /* We are not interested in this format, close this vout */
176 vlc_object_detach( p_vout );
177 vlc_object_release( p_vout );
178 vout_Destroy( p_vout );
183 /* This video output is cool! Hijack it. */
184 vlc_object_detach( p_vout );
185 spu_Attach( p_vout->p_spu, p_this, VLC_TRUE );
186 vlc_object_attach( p_vout, p_this );
187 vlc_object_release( p_vout );
193 msg_Dbg( p_this, "no usable vout present, spawning one" );
195 p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
201 /*****************************************************************************
202 * vout_Create: creates a new video output thread
203 *****************************************************************************
204 * This function creates a new video output thread, and returns a pointer
205 * to its description. On error, it returns NULL.
206 *****************************************************************************/
207 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
208 unsigned int i_width, unsigned int i_height,
209 vlc_fourcc_t i_chroma, unsigned int i_aspect )
211 vout_thread_t * p_vout; /* thread descriptor */
212 input_thread_t * p_input_thread;
213 int i_index; /* loop variable */
215 vlc_value_t val, text;
217 /* Allocate descriptor */
218 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
221 msg_Err( p_parent, "out of memory" );
225 /* Initialize pictures - translation tables and functions
226 * will be initialized later in InitThread */
227 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
229 p_vout->p_picture[i_index].pf_lock = NULL;
230 p_vout->p_picture[i_index].pf_unlock = NULL;
231 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
232 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
233 p_vout->p_picture[i_index].b_slow = 0;
236 /* No images in the heap */
237 p_vout->i_heap_size = 0;
239 /* Initialize the rendering heap */
240 I_RENDERPICTURES = 0;
241 p_vout->render.i_width = i_width;
242 p_vout->render.i_height = i_height;
243 p_vout->render.i_chroma = i_chroma;
244 p_vout->render.i_aspect = i_aspect;
246 p_vout->render.i_rmask = 0;
247 p_vout->render.i_gmask = 0;
248 p_vout->render.i_bmask = 0;
250 p_vout->render.i_last_used_pic = -1;
251 p_vout->render.b_allow_modify_pics = 1;
253 /* Zero the output heap */
254 I_OUTPUTPICTURES = 0;
255 p_vout->output.i_width = 0;
256 p_vout->output.i_height = 0;
257 p_vout->output.i_chroma = 0;
258 p_vout->output.i_aspect = 0;
260 p_vout->output.i_rmask = 0;
261 p_vout->output.i_gmask = 0;
262 p_vout->output.i_bmask = 0;
264 /* Initialize misc stuff */
265 p_vout->i_changes = 0;
267 p_vout->b_grayscale = 0;
269 p_vout->b_interface = 0;
271 p_vout->b_fullscreen = 0;
272 p_vout->i_alignment = 0;
273 p_vout->render_time = 10;
274 p_vout->c_fps_samples = 0;
275 p_vout->b_filter_change = 0;
276 p_vout->pf_control = 0;
277 p_vout->p_parent_intf = 0;
279 /* Initialize locks */
280 vlc_mutex_init( p_vout, &p_vout->picture_lock );
281 vlc_mutex_init( p_vout, &p_vout->change_lock );
283 /* Mouse coordinates */
284 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
285 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
286 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
287 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
288 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
290 /* Initialize subpicture unit */
291 p_vout->p_spu = spu_Create( p_vout );
292 spu_Attach( p_vout->p_spu, p_parent, VLC_TRUE );
294 /* Attach the new object now so we can use var inheritance below */
295 vlc_object_attach( p_vout, p_parent );
297 spu_Init( p_vout->p_spu );
299 /* Take care of some "interface/control" related initialisations */
300 vout_IntfInit( p_vout );
302 p_vout->b_override_aspect = VLC_FALSE;
304 /* If the parent is not a VOUT object, that means we are at the start of
305 * the video output pipe */
306 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
308 var_Get( p_vout, "aspect-ratio", &val );
310 /* Check whether the user tried to override aspect ratio */
313 unsigned int i_new_aspect = i_aspect;
314 char *psz_parser = strchr( val.psz_string, ':' );
318 *psz_parser++ = '\0';
319 i_new_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
320 / atoi( psz_parser );
324 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
325 * atof( val.psz_string )
329 free( val.psz_string );
331 if( i_new_aspect && i_new_aspect != i_aspect )
333 int i_aspect_x, i_aspect_y;
335 AspectRatio( i_new_aspect, &i_aspect_x, &i_aspect_y );
337 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
338 i_aspect_x, i_aspect_y );
340 p_vout->render.i_aspect = i_new_aspect;
342 p_vout->b_override_aspect = VLC_TRUE;
346 /* Look for the default filter configuration */
347 var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
348 var_Get( p_vout, "filter", &val );
349 p_vout->psz_filter_chain = val.psz_string;
353 /* continue the parent's filter chain */
356 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ',' );
357 if( psz_end && *(psz_end+1) )
358 p_vout->psz_filter_chain = strdup( psz_end+1 );
359 else p_vout->psz_filter_chain = NULL;
362 /* Choose the video output module */
363 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
365 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
366 var_Get( p_vout, "vout", &val );
367 psz_plugin = val.psz_string;
371 /* the filter chain is a string list of filters separated by double
375 psz_end = strchr( p_vout->psz_filter_chain, ',' );
377 psz_plugin = strndup( p_vout->psz_filter_chain,
378 psz_end - p_vout->psz_filter_chain );
379 else psz_plugin = strdup( p_vout->psz_filter_chain );
382 /* Initialize the dimensions of the video window */
383 InitWindowSize( p_vout, &p_vout->i_window_width,
384 &p_vout->i_window_height );
386 /* Create the vout thread */
387 p_vout->p_module = module_Need( p_vout,
388 ( p_vout->psz_filter_chain && *p_vout->psz_filter_chain ) ?
389 "video filter" : "video output", psz_plugin, 0 );
391 if( psz_plugin ) free( psz_plugin );
392 if( p_vout->p_module == NULL )
394 msg_Err( p_vout, "no suitable vout module" );
395 vlc_object_detach( p_vout );
396 vlc_object_destroy( p_vout );
400 /* Create a few object variables for interface interaction */
401 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
402 text.psz_string = _("Deinterlace");
403 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
404 val.psz_string = ""; text.psz_string = _("Disable");
405 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
406 val.psz_string = "discard"; text.psz_string = _("Discard");
407 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
408 val.psz_string = "blend"; text.psz_string = _("Blend");
409 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
410 val.psz_string = "mean"; text.psz_string = _("Mean");
411 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
412 val.psz_string = "bob"; text.psz_string = _("Bob");
413 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
414 val.psz_string = "linear"; text.psz_string = _("Linear");
415 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
416 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
418 var_Set( p_vout, "deinterlace", val );
419 if( val.psz_string ) free( val.psz_string );
421 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
424 var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
425 text.psz_string = _("Filters");
426 var_Change( p_vout, "filter", VLC_VAR_SETTEXT, &text, NULL );
427 var_AddCallback( p_vout, "filter", FilterCallback, NULL );
429 /* Calculate delay created by internal caching */
430 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
431 VLC_OBJECT_INPUT, FIND_ANYWHERE );
434 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
435 vlc_object_release( p_input_thread );
439 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
442 if( vlc_thread_create( p_vout, "video output", RunThread,
443 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
445 msg_Err( p_vout, "out of memory" );
446 module_Unneed( p_vout, p_vout->p_module );
447 vlc_object_detach( p_vout );
448 vlc_object_destroy( p_vout );
452 if( p_vout->b_error )
454 msg_Err( p_vout, "video output creation failed" );
456 /* Make sure the thread is destroyed */
457 p_vout->b_die = VLC_TRUE;
459 vlc_thread_join( p_vout );
461 vlc_object_detach( p_vout );
462 vlc_object_destroy( p_vout );
469 /*****************************************************************************
470 * vout_Destroy: destroys a previously created video output
471 *****************************************************************************
472 * Destroy a terminated thread.
473 * The function will request a destruction of the specified thread. If pi_error
474 * is NULL, it will return once the thread is destroyed. Else, it will be
475 * update using one of the THREAD_* constants.
476 *****************************************************************************/
477 void vout_Destroy( vout_thread_t *p_vout )
479 vlc_object_t *p_playlist;
481 /* Request thread destruction */
482 p_vout->b_die = VLC_TRUE;
483 vlc_thread_join( p_vout );
485 var_Destroy( p_vout, "intf-change" );
487 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
490 if( p_vout->psz_filter_chain ) free( p_vout->psz_filter_chain );
493 vlc_object_destroy( p_vout );
495 /* If it was the last vout, tell the interface to show up */
496 if( p_playlist != NULL )
498 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
499 VLC_OBJECT_VOUT, FIND_ANYWHERE );
500 if( p_another_vout == NULL )
503 val.b_bool = VLC_TRUE;
504 var_Set( p_playlist, "intf-show", val );
508 vlc_object_release( p_another_vout );
510 vlc_object_release( p_playlist );
514 /*****************************************************************************
515 * InitThread: initialize video output thread
516 *****************************************************************************
517 * This function is called from RunThread and performs the second step of the
518 * initialization. It returns 0 on success. Note that the thread's flag are not
519 * modified inside this function.
520 *****************************************************************************/
521 static int InitThread( vout_thread_t *p_vout )
523 int i, i_aspect_x, i_aspect_y;
525 vlc_mutex_lock( &p_vout->change_lock );
531 /* Initialize output method, it allocates direct buffers for us */
532 if( p_vout->pf_init( p_vout ) )
534 vlc_mutex_unlock( &p_vout->change_lock );
538 if( !I_OUTPUTPICTURES )
540 msg_Err( p_vout, "plugin was unable to allocate at least "
541 "one direct buffer" );
542 p_vout->pf_end( p_vout );
543 vlc_mutex_unlock( &p_vout->change_lock );
547 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
549 msg_Err( p_vout, "plugin allocated too many direct buffers, "
550 "our internal buffers must have overflown." );
551 p_vout->pf_end( p_vout );
552 vlc_mutex_unlock( &p_vout->change_lock );
556 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
558 AspectRatio( p_vout->render.i_aspect, &i_aspect_x, &i_aspect_y );
560 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
561 p_vout->render.i_width, p_vout->render.i_height,
562 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
563 i_aspect_x, i_aspect_y );
565 AspectRatio( p_vout->output.i_aspect, &i_aspect_x, &i_aspect_y );
567 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
568 p_vout->output.i_width, p_vout->output.i_height,
569 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
570 i_aspect_x, i_aspect_y );
572 /* Calculate shifts from system-updated masks */
573 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
574 p_vout->output.i_rmask );
575 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
576 p_vout->output.i_gmask );
577 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
578 p_vout->output.i_bmask );
580 /* Check whether we managed to create direct buffers similar to
581 * the render buffers, ie same size and chroma */
582 if( ( p_vout->output.i_width == p_vout->render.i_width )
583 && ( p_vout->output.i_height == p_vout->render.i_height )
584 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
586 /* Cool ! We have direct buffers, we can ask the decoder to
587 * directly decode into them ! Map the first render buffers to
588 * the first direct buffers, but keep the first direct buffer
589 * for memcpy operations */
590 p_vout->b_direct = 1;
592 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
594 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
595 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
596 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
598 /* We have enough direct buffers so there's no need to
599 * try to use system memory buffers. */
602 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
606 msg_Dbg( p_vout, "direct render, mapping "
607 "render pictures 0-%i to system pictures 1-%i",
608 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
612 /* Rats... Something is wrong here, we could not find an output
613 * plugin able to directly render what we decode. See if we can
614 * find a chroma plugin to do the conversion */
615 p_vout->b_direct = 0;
617 /* Choose the best module */
618 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
620 if( p_vout->chroma.p_module == NULL )
622 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
623 (char*)&p_vout->render.i_chroma,
624 (char*)&p_vout->output.i_chroma );
625 p_vout->pf_end( p_vout );
626 vlc_mutex_unlock( &p_vout->change_lock );
630 msg_Dbg( p_vout, "indirect render, mapping "
631 "render pictures 0-%i to system pictures %i-%i",
632 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
633 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
635 /* Append render buffers after the direct buffers */
636 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
638 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
641 /* Check if we have enough render pictures */
642 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
647 /* Link pictures back to their heap */
648 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
650 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
653 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
655 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
658 /* XXX XXX mark thread ready */
662 /*****************************************************************************
663 * RunThread: video output thread
664 *****************************************************************************
665 * Video output thread. This function does only returns when the thread is
666 * terminated. It handles the pictures arriving in the video heap and the
667 * display device events.
668 *****************************************************************************/
669 static void RunThread( vout_thread_t *p_vout)
671 int i_index; /* index in heap */
672 int i_idle_loops = 0; /* loops without displaying a picture */
673 mtime_t current_date; /* current date */
674 mtime_t display_date; /* display date */
676 picture_t * p_picture; /* picture pointer */
677 picture_t * p_last_picture = NULL; /* last picture */
678 picture_t * p_directbuffer; /* direct buffer to display */
680 subpicture_t * p_subpic; /* subpicture pointer */
685 p_vout->b_error = InitThread( p_vout );
687 /* signal the creation of the vout */
688 vlc_thread_ready( p_vout );
690 if( p_vout->b_error )
692 /* Destroy thread structures allocated by Create and InitThread */
693 DestroyThread( p_vout );
698 * Main loop - it is not executed if an error occurred during
701 while( (!p_vout->b_die) && (!p_vout->b_error) )
703 /* Initialize loop variables */
706 current_date = mdate();
710 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
712 msg_Dbg( p_vout, "picture heap: %d/%d",
713 I_RENDERPICTURES, p_vout->i_heap_size );
718 * Find the picture to display (the one with the earliest date).
719 * This operation does not need lock, since only READY_PICTUREs
721 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
723 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
724 && ( (p_picture == NULL) ||
725 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
727 p_picture = PP_RENDERPICTURE[i_index];
728 display_date = p_picture->date;
734 /* If we met the last picture, parse again to see whether there is
735 * a more appropriate one. */
736 if( p_picture == p_last_picture )
738 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
740 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
741 && (PP_RENDERPICTURE[i_index] != p_last_picture)
742 && ((p_picture == p_last_picture) ||
743 (PP_RENDERPICTURE[i_index]->date < display_date)) )
745 p_picture = PP_RENDERPICTURE[i_index];
746 display_date = p_picture->date;
751 /* If we found better than the last picture, destroy it */
752 if( p_last_picture && p_picture != p_last_picture )
754 vlc_mutex_lock( &p_vout->picture_lock );
755 if( p_last_picture->i_refcount )
757 p_last_picture->i_status = DISPLAYED_PICTURE;
761 p_last_picture->i_status = DESTROYED_PICTURE;
762 p_vout->i_heap_size--;
764 vlc_mutex_unlock( &p_vout->picture_lock );
765 p_last_picture = NULL;
768 /* Compute FPS rate */
769 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
772 if( !p_picture->b_force &&
773 p_picture != p_last_picture &&
774 display_date < current_date + p_vout->render_time )
776 /* Picture is late: it will be destroyed and the thread
777 * will directly choose the next picture */
778 vlc_mutex_lock( &p_vout->picture_lock );
779 if( p_picture->i_refcount )
781 /* Pretend we displayed the picture, but don't destroy
782 * it since the decoder might still need it. */
783 p_picture->i_status = DISPLAYED_PICTURE;
787 /* Destroy the picture without displaying it */
788 p_picture->i_status = DESTROYED_PICTURE;
789 p_vout->i_heap_size--;
791 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
792 current_date - display_date );
793 vlc_mutex_unlock( &p_vout->picture_lock );
799 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
801 /* Picture is waaay too early: it will be destroyed */
802 vlc_mutex_lock( &p_vout->picture_lock );
803 if( p_picture->i_refcount )
805 /* Pretend we displayed the picture, but don't destroy
806 * it since the decoder might still need it. */
807 p_picture->i_status = DISPLAYED_PICTURE;
811 /* Destroy the picture without displaying it */
812 p_picture->i_status = DESTROYED_PICTURE;
813 p_vout->i_heap_size--;
815 msg_Warn( p_vout, "vout warning: early picture skipped "
816 "("I64Fd")", display_date - current_date
817 - p_vout->i_pts_delay );
818 vlc_mutex_unlock( &p_vout->picture_lock );
823 if( display_date > current_date + VOUT_DISPLAY_DELAY )
825 /* A picture is ready to be rendered, but its rendering date
826 * is far from the current one so the thread will perform an
827 * empty loop as if no picture were found. The picture state
832 else if( p_picture == p_last_picture )
834 /* We are asked to repeat the previous picture, but we first
835 * wait for a couple of idle loops */
836 if( i_idle_loops < 4 )
843 /* We set the display date to something high, otherwise
844 * we'll have lots of problems with late pictures */
845 display_date = current_date + p_vout->render_time;
850 if( p_picture == NULL )
856 * Check for subpictures to display
858 p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date );
863 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
866 * Call the plugin-specific rendering method if there is one
868 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
870 /* Render the direct buffer returned by vout_RenderPicture */
871 p_vout->pf_render( p_vout, p_directbuffer );
877 if( display_date != 0 && p_directbuffer != NULL )
879 mtime_t current_render_time = mdate() - current_date;
880 /* if render time is very large we don't include it in the mean */
881 if( current_render_time < p_vout->render_time +
884 /* Store render time using a sliding mean weighting to
885 * current value in a 3 to 1 ratio*/
886 p_vout->render_time *= 3;
887 p_vout->render_time += current_render_time;
888 p_vout->render_time >>= 2;
892 /* Give back change lock */
893 vlc_mutex_unlock( &p_vout->change_lock );
895 /* Sleep a while or until a given date */
896 if( display_date != 0 )
898 /* If there are filters in the chain, better give them the picture
900 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
902 mwait( display_date - VOUT_MWAIT_TOLERANCE );
907 msleep( VOUT_IDLE_SLEEP );
910 /* On awakening, take back lock and send immediately picture
912 vlc_mutex_lock( &p_vout->change_lock );
915 * Display the previously rendered picture
917 if( p_picture != NULL && p_directbuffer != NULL )
919 /* Display the direct buffer returned by vout_RenderPicture */
920 if( p_vout->pf_display )
922 p_vout->pf_display( p_vout, p_directbuffer );
925 /* Tell the vout this was the last picture and that it does not
926 * need to be forced anymore. */
927 p_last_picture = p_picture;
928 p_last_picture->b_force = 0;
931 if( p_picture != NULL )
933 /* Reinitialize idle loop count */
938 * Check events and manage thread
940 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
942 /* A fatal error occurred, and the thread must terminate
943 * immediately, without displaying anything - setting b_error to 1
944 * causes the immediate end of the main while() loop. */
948 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
950 /* this must only happen when the vout plugin is incapable of
951 * rescaling the picture itself. In this case we need to destroy
952 * the current picture buffers and recreate new ones with the right
956 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
958 p_vout->pf_end( p_vout );
959 for( i = 0; i < I_OUTPUTPICTURES; i++ )
960 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
962 I_OUTPUTPICTURES = 0;
963 if( p_vout->pf_init( p_vout ) )
965 msg_Err( p_vout, "cannot resize display" );
966 /* FIXME: pf_end will be called again in EndThread() */
970 /* Need to reinitialise the chroma plugin */
971 if( p_vout->chroma.p_module )
973 if( p_vout->chroma.p_module->pf_deactivate )
974 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
975 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
979 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
981 /* This happens when the picture buffers need to be recreated.
982 * This is useful on multimonitor displays for instance.
984 * Warning: This only works when the vout creates only 1 picture
986 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
988 if( !p_vout->b_direct )
990 module_Unneed( p_vout, p_vout->chroma.p_module );
993 vlc_mutex_lock( &p_vout->picture_lock );
995 p_vout->pf_end( p_vout );
997 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
999 p_vout->b_error = InitThread( p_vout );
1001 vlc_mutex_unlock( &p_vout->picture_lock );
1006 * Error loop - wait until the thread destruction is requested
1008 if( p_vout->b_error )
1010 ErrorThread( p_vout );
1014 EndThread( p_vout );
1016 /* Destroy thread structures allocated by CreateThread */
1017 DestroyThread( p_vout );
1020 /*****************************************************************************
1021 * ErrorThread: RunThread() error loop
1022 *****************************************************************************
1023 * This function is called when an error occurred during thread main's loop.
1024 * The thread can still receive feed, but must be ready to terminate as soon
1026 *****************************************************************************/
1027 static void ErrorThread( vout_thread_t *p_vout )
1029 /* Wait until a `die' order */
1030 while( !p_vout->b_die )
1033 msleep( VOUT_IDLE_SLEEP );
1037 /*****************************************************************************
1038 * EndThread: thread destruction
1039 *****************************************************************************
1040 * This function is called when the thread ends after a sucessful
1041 * initialization. It frees all resources allocated by InitThread.
1042 *****************************************************************************/
1043 static void EndThread( vout_thread_t *p_vout )
1045 int i_index; /* index in heap */
1049 struct tms cpu_usage;
1050 times( &cpu_usage );
1052 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1053 cpu_usage.tms_utime, cpu_usage.tms_stime );
1057 if( !p_vout->b_direct )
1059 module_Unneed( p_vout, p_vout->chroma.p_module );
1062 /* Destroy all remaining pictures */
1063 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1065 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1067 free( p_vout->p_picture[i_index].p_data_orig );
1071 /* Destroy subpicture unit */
1072 spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), VLC_FALSE );
1073 spu_Destroy( p_vout->p_spu );
1075 /* Destroy translation tables */
1076 p_vout->pf_end( p_vout );
1078 /* Release the change lock */
1079 vlc_mutex_unlock( &p_vout->change_lock );
1082 /*****************************************************************************
1083 * DestroyThread: thread destruction
1084 *****************************************************************************
1085 * This function is called when the thread ends. It frees all ressources
1086 * allocated by CreateThread. Status is available at this stage.
1087 *****************************************************************************/
1088 static void DestroyThread( vout_thread_t *p_vout )
1090 /* Destroy the locks */
1091 vlc_mutex_destroy( &p_vout->picture_lock );
1092 vlc_mutex_destroy( &p_vout->change_lock );
1094 /* Release the module */
1095 if( p_vout && p_vout->p_module )
1097 module_Unneed( p_vout, p_vout->p_module );
1101 /* following functions are local */
1103 static int ReduceHeight( int i_ratio )
1105 int i_dummy = VOUT_ASPECT_FACTOR;
1113 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1114 while( !(i_ratio & 1) && !(i_dummy & 1) )
1121 while( !(i_ratio % 3) && !(i_dummy % 3) )
1128 while( !(i_ratio % 5) && !(i_dummy % 5) )
1138 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1140 unsigned int i_pgcd = ReduceHeight( i_aspect );
1141 *i_aspect_x = i_aspect / i_pgcd;
1142 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1145 /*****************************************************************************
1146 * BinaryLog: computes the base 2 log of a binary value
1147 *****************************************************************************
1148 * This functions is used by MaskToShift, to get a bit index from a binary
1150 *****************************************************************************/
1151 static int BinaryLog( uint32_t i )
1155 if( i == 0 ) return -31337;
1157 if( i & 0xffff0000 ) i_log += 16;
1158 if( i & 0xff00ff00 ) i_log += 8;
1159 if( i & 0xf0f0f0f0 ) i_log += 4;
1160 if( i & 0xcccccccc ) i_log += 2;
1161 if( i & 0xaaaaaaaa ) i_log += 1;
1166 /*****************************************************************************
1167 * MaskToShift: transform a color mask into right and left shifts
1168 *****************************************************************************
1169 * This function is used for obtaining color shifts from masks.
1170 *****************************************************************************/
1171 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1173 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1177 *pi_left = *pi_right = 0;
1182 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1183 i_high = i_mask + i_low; /* higher bit of the mask */
1185 /* Transform bits into an index */
1186 i_low = BinaryLog (i_low);
1187 i_high = BinaryLog (i_high);
1189 /* Update pointers and return */
1191 *pi_right = (8 - i_high + i_low);
1194 /*****************************************************************************
1195 * InitWindowSize: find the initial dimensions the video window should have.
1196 *****************************************************************************
1197 * This function will check the "width", "height" and "zoom" config options and
1198 * will calculate the size that the video window should have.
1199 *****************************************************************************/
1200 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1204 int i_width, i_height;
1207 #define FP_FACTOR 1000 /* our fixed point factor */
1209 var_Get( p_vout, "align", &val );
1210 p_vout->i_alignment = val.i_int;
1212 var_Get( p_vout, "width", &val );
1213 i_width = val.i_int;
1214 var_Get( p_vout, "height", &val );
1215 i_height = val.i_int;
1216 var_Get( p_vout, "zoom", &val );
1217 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1219 if( i_width > 0 && i_height > 0)
1221 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1222 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1225 else if( i_width > 0 )
1227 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1228 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1229 p_vout->render.i_aspect / FP_FACTOR );
1232 else if( i_height > 0 )
1234 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1235 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1236 VOUT_ASPECT_FACTOR / FP_FACTOR );
1240 if( p_vout->render.i_height * p_vout->render.i_aspect
1241 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1243 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1244 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1245 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1249 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1250 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1251 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1257 /*****************************************************************************
1258 * vout_VarCallback: generic callback for intf variables
1259 *****************************************************************************/
1260 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1261 vlc_value_t old_value, vlc_value_t new_value,
1264 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1266 val.b_bool = VLC_TRUE;
1267 var_Set( p_vout, "intf-change", val );
1271 /*****************************************************************************
1272 * Helper thread for object variables callbacks.
1273 * Only used to avoid deadlocks when using the video embedded mode.
1274 *****************************************************************************/
1275 typedef struct suxor_thread_t
1278 input_thread_t *p_input;
1282 static void SuxorRestartVideoES( suxor_thread_t *p_this )
1286 /* Now restart current video stream */
1287 var_Get( p_this->p_input, "video-es", &val );
1288 if( val.i_int >= 0 )
1291 val_es.i_int = -VIDEO_ES;
1292 var_Set( p_this->p_input, "video-es", val_es );
1293 var_Set( p_this->p_input, "video-es", val );
1296 vlc_object_release( p_this->p_input );
1299 CloseHandle( p_this->thread_id );
1302 vlc_object_destroy( p_this );
1305 /*****************************************************************************
1306 * object variables callbacks: a bunch of object variables are used by the
1307 * interfaces to interact with the vout.
1308 *****************************************************************************/
1309 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1310 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1312 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1313 input_thread_t *p_input;
1316 char *psz_mode = newval.psz_string;
1317 char *psz_filter, *psz_deinterlace = NULL;
1319 var_Get( p_vout, "filter", &val );
1320 psz_filter = val.psz_string;
1321 if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1323 if( !psz_mode || !*psz_mode )
1325 if( psz_deinterlace )
1327 char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1328 if( psz_src[0] == ',' ) psz_src++;
1329 memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1332 else if( !psz_deinterlace )
1334 psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1335 sizeof(",deinterlace") );
1336 if( psz_filter && *psz_filter ) strcat( psz_filter, "," );
1337 strcat( psz_filter, "deinterlace" );
1340 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1342 if( !p_input ) return VLC_EGENERIC;
1344 if( psz_mode && *psz_mode )
1346 /* Modify input as well because the vout might have to be restarted */
1347 val.psz_string = psz_mode;
1348 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1349 var_Set( p_input, "deinterlace-mode", val );
1351 vlc_object_release( p_input );
1353 val.b_bool = VLC_TRUE;
1354 var_Set( p_vout, "intf-change", val );
1356 val.psz_string = psz_filter;
1357 var_Set( p_vout, "filter", val );
1358 if( psz_filter ) free( psz_filter );
1363 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1364 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1366 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1367 input_thread_t *p_input;
1370 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1374 msg_Err( p_vout, "Input not found" );
1375 return( VLC_EGENERIC );
1378 val.b_bool = VLC_TRUE;
1379 var_Set( p_vout, "intf-change", val );
1381 /* Modify input as well because the vout might have to be restarted */
1382 val.psz_string = newval.psz_string;
1383 var_Create( p_input, "filter", VLC_VAR_STRING );
1384 var_Set( p_input, "filter", val );
1386 /* Now restart current video stream */
1387 var_Get( p_input, "video-es", &val );
1388 if( val.i_int >= 0 )
1390 suxor_thread_t *p_suxor =
1391 vlc_object_create( p_vout, sizeof(suxor_thread_t) );
1392 p_suxor->p_input = p_input;
1393 p_vout->b_filter_change = VLC_TRUE;
1394 vlc_object_yield( p_input );
1395 vlc_thread_create( p_suxor, "suxor", SuxorRestartVideoES,
1396 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1399 vlc_object_release( p_input );