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 the VideoLAN team
11 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12 * Gildas Bazin <gbazin@videolan.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
32 #include <stdlib.h> /* free() */
36 #ifdef HAVE_SYS_TIMES_H
37 # include <sys/times.h>
40 #include "vlc_video.h"
41 #include "video_output.h"
43 #include <vlc/input.h> /* for input_thread_t and i_pts_delay */
44 #include "vlc_playlist.h"
46 #if defined( SYS_DARWIN )
47 #include "darwin_specific.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 static int InitThread ( vout_thread_t * );
54 static void RunThread ( vout_thread_t * );
55 static void ErrorThread ( vout_thread_t * );
56 static void EndThread ( vout_thread_t * );
57 static void DestroyThread ( vout_thread_t * );
59 static void AspectRatio ( int, int *, int * );
60 static int BinaryLog ( uint32_t );
61 static void MaskToShift ( int *, int *, uint32_t );
63 /* Object variables callbacks */
64 static int DeinterlaceCallback( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
66 static int FilterCallback( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
69 /* From vout_intf.c */
70 int vout_Snapshot( vout_thread_t *, picture_t * );
72 /*****************************************************************************
73 * vout_Request: find a video output thread, create one, or destroy one.
74 *****************************************************************************
75 * This function looks for a video output thread matching the current
76 * properties. If not found, it spawns a new one.
77 *****************************************************************************/
78 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
79 video_format_t *p_fmt )
83 /* Reattach video output to input before bailing out */
86 vlc_object_t *p_playlist;
88 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
93 spu_Attach( p_vout->p_spu, p_this, VLC_FALSE );
94 vlc_object_detach( p_vout );
95 vlc_object_attach( p_vout, p_playlist );
97 vlc_object_release( p_playlist );
101 msg_Dbg( p_this, "cannot find playlist, destroying vout" );
102 vlc_object_detach( p_vout );
103 vout_Destroy( p_vout );
109 /* If a video output was provided, lock it, otherwise look for one. */
112 vlc_object_yield( p_vout );
116 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
120 playlist_t *p_playlist;
122 p_playlist = vlc_object_find( p_this,
123 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
126 vlc_mutex_lock( &p_playlist->gc_lock );
127 p_vout = vlc_object_find( p_playlist,
128 VLC_OBJECT_VOUT, FIND_CHILD );
129 /* only first children of p_input for unused vout */
130 if( p_vout && p_vout->p_parent != (vlc_object_t *)p_playlist )
132 vlc_object_release( p_vout );
135 vlc_mutex_unlock( &p_playlist->gc_lock );
136 vlc_object_release( p_playlist );
141 /* If we now have a video output, check it has the right properties */
144 char *psz_filter_chain;
147 /* We don't directly check for the "vout-filter" variable for obvious
148 * performance reasons. */
149 if( p_vout->b_filter_change )
151 var_Get( p_vout, "vout-filter", &val );
152 psz_filter_chain = val.psz_string;
154 if( psz_filter_chain && !*psz_filter_chain )
156 free( psz_filter_chain );
157 psz_filter_chain = NULL;
159 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
161 free( p_vout->psz_filter_chain );
162 p_vout->psz_filter_chain = NULL;
165 if( !psz_filter_chain && !p_vout->psz_filter_chain )
167 p_vout->b_filter_change = VLC_FALSE;
170 if( psz_filter_chain ) free( psz_filter_chain );
173 if( ( p_vout->fmt_render.i_width != p_fmt->i_width ) ||
174 ( p_vout->fmt_render.i_height != p_fmt->i_height ) ||
175 ( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ) ||
176 ( p_vout->fmt_render.i_aspect != p_fmt->i_aspect ) ||
177 p_vout->b_filter_change )
179 /* We are not interested in this format, close this vout */
180 vlc_object_detach( p_vout );
181 vlc_object_release( p_vout );
182 vout_Destroy( p_vout );
187 /* This video output is cool! Hijack it. */
188 vlc_object_detach( p_vout );
189 spu_Attach( p_vout->p_spu, p_this, VLC_TRUE );
190 vlc_object_attach( p_vout, p_this );
191 vlc_object_release( p_vout );
197 msg_Dbg( p_this, "no usable vout present, spawning one" );
199 p_vout = vout_Create( p_this, p_fmt );
205 /*****************************************************************************
206 * vout_Create: creates a new video output thread
207 *****************************************************************************
208 * This function creates a new video output thread, and returns a pointer
209 * to its description. On error, it returns NULL.
210 *****************************************************************************/
211 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
213 vout_thread_t * p_vout; /* thread descriptor */
214 input_thread_t * p_input_thread;
215 int i_index; /* loop variable */
217 vlc_value_t val, text;
219 unsigned int i_width = p_fmt->i_width;
220 unsigned int i_height = p_fmt->i_height;
221 vlc_fourcc_t i_chroma = p_fmt->i_chroma;
222 unsigned int i_aspect = p_fmt->i_aspect;
224 /* Allocate descriptor */
225 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
228 msg_Err( p_parent, "out of memory" );
232 stats_Create( p_vout, "displayed_pictures", VLC_VAR_INTEGER,
234 stats_Create( p_vout, "lost_pictures", VLC_VAR_INTEGER, STATS_COUNTER );
236 /* Initialize pictures - translation tables and functions
237 * will be initialized later in InitThread */
238 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
240 p_vout->p_picture[i_index].pf_lock = NULL;
241 p_vout->p_picture[i_index].pf_unlock = NULL;
242 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
243 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
244 p_vout->p_picture[i_index].b_slow = 0;
247 /* No images in the heap */
248 p_vout->i_heap_size = 0;
250 /* Initialize the rendering heap */
251 I_RENDERPICTURES = 0;
253 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
254 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
255 p_vout->fmt_render = *p_fmt; /* FIXME palette */
256 p_vout->fmt_in = *p_fmt; /* FIXME palette */
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;
294 p_vout->p_parent_intf = 0;
295 p_vout->i_par_num = p_vout->i_par_den = 1;
297 /* Initialize locks */
298 vlc_mutex_init( p_vout, &p_vout->picture_lock );
299 vlc_mutex_init( p_vout, &p_vout->change_lock );
301 /* Mouse coordinates */
302 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
303 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
304 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
305 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
306 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
308 /* Initialize subpicture unit */
309 p_vout->p_spu = spu_Create( p_vout );
310 spu_Attach( p_vout->p_spu, p_parent, VLC_TRUE );
312 /* Attach the new object now so we can use var inheritance below */
313 vlc_object_attach( p_vout, p_parent );
315 spu_Init( p_vout->p_spu );
317 /* Take care of some "interface/control" related initialisations */
318 vout_IntfInit( p_vout );
320 /* If the parent is not a VOUT object, that means we are at the start of
321 * the video output pipe */
322 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
324 /* Look for the default filter configuration */
325 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
326 var_Get( p_vout, "vout-filter", &val );
327 p_vout->psz_filter_chain = val.psz_string;
331 /* continue the parent's filter chain */
334 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
335 if( psz_end && *(psz_end+1) )
336 p_vout->psz_filter_chain = strdup( psz_end+1 );
337 else p_vout->psz_filter_chain = NULL;
340 /* Choose the video output module */
341 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
343 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
344 var_Get( p_vout, "vout", &val );
345 psz_plugin = val.psz_string;
349 /* the filter chain is a string list of filters separated by double
353 psz_end = strchr( p_vout->psz_filter_chain, ':' );
355 psz_plugin = strndup( p_vout->psz_filter_chain,
356 psz_end - p_vout->psz_filter_chain );
357 else psz_plugin = strdup( p_vout->psz_filter_chain );
360 /* Create the vout thread */
361 p_vout->p_module = module_Need( p_vout,
362 ( p_vout->psz_filter_chain && *p_vout->psz_filter_chain ) ?
363 "video filter" : "video output", psz_plugin, 0 );
365 if( psz_plugin ) free( psz_plugin );
366 if( p_vout->p_module == NULL )
368 msg_Err( p_vout, "no suitable vout module" );
369 vlc_object_detach( p_vout );
370 vlc_object_destroy( p_vout );
374 /* Create a few object variables for interface interaction */
375 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
376 text.psz_string = _("Deinterlace");
377 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
378 val.psz_string = ""; text.psz_string = _("Disable");
379 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
380 val.psz_string = "discard"; text.psz_string = _("Discard");
381 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
382 val.psz_string = "blend"; text.psz_string = _("Blend");
383 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
384 val.psz_string = "mean"; text.psz_string = _("Mean");
385 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
386 val.psz_string = "bob"; text.psz_string = _("Bob");
387 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
388 val.psz_string = "linear"; text.psz_string = _("Linear");
389 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
390 val.psz_string = "x"; text.psz_string = "X";
391 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
393 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
395 var_Set( p_vout, "deinterlace", val );
396 if( val.psz_string ) free( val.psz_string );
398 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
401 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
402 text.psz_string = _("Filters");
403 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
404 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
406 /* Calculate delay created by internal caching */
407 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
408 VLC_OBJECT_INPUT, FIND_ANYWHERE );
411 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
412 vlc_object_release( p_input_thread );
416 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
419 if( vlc_thread_create( p_vout, "video output", RunThread,
420 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
422 msg_Err( p_vout, "out of memory" );
423 module_Unneed( p_vout, p_vout->p_module );
424 vlc_object_detach( p_vout );
425 vlc_object_destroy( p_vout );
429 if( p_vout->b_error )
431 msg_Err( p_vout, "video output creation failed" );
433 /* Make sure the thread is destroyed */
434 p_vout->b_die = VLC_TRUE;
436 vlc_thread_join( p_vout );
438 vlc_object_detach( p_vout );
439 vlc_object_destroy( p_vout );
446 /*****************************************************************************
447 * vout_Destroy: destroys a previously created video output
448 *****************************************************************************
449 * Destroy a terminated thread.
450 * The function will request a destruction of the specified thread. If pi_error
451 * is NULL, it will return once the thread is destroyed. Else, it will be
452 * update using one of the THREAD_* constants.
453 *****************************************************************************/
454 void vout_Destroy( vout_thread_t *p_vout )
456 vlc_object_t *p_playlist;
458 /* Request thread destruction */
459 p_vout->b_die = VLC_TRUE;
460 vlc_thread_join( p_vout );
462 var_Destroy( p_vout, "intf-change" );
464 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
467 if( p_vout->psz_filter_chain ) free( p_vout->psz_filter_chain );
470 vlc_object_destroy( p_vout );
472 /* If it was the last vout, tell the interface to show up */
473 if( p_playlist != NULL )
475 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
476 VLC_OBJECT_VOUT, FIND_ANYWHERE );
477 if( p_another_vout == NULL )
480 val.b_bool = VLC_TRUE;
481 var_Set( p_playlist, "intf-show", val );
485 vlc_object_release( p_another_vout );
487 vlc_object_release( p_playlist );
491 /*****************************************************************************
492 * InitThread: initialize video output thread
493 *****************************************************************************
494 * This function is called from RunThread and performs the second step of the
495 * initialization. It returns 0 on success. Note that the thread's flag are not
496 * modified inside this function.
497 *****************************************************************************/
498 static int InitThread( vout_thread_t *p_vout )
500 int i, i_aspect_x, i_aspect_y;
502 vlc_mutex_lock( &p_vout->change_lock );
508 /* Initialize output method, it allocates direct buffers for us */
509 if( p_vout->pf_init( p_vout ) )
511 vlc_mutex_unlock( &p_vout->change_lock );
515 if( !I_OUTPUTPICTURES )
517 msg_Err( p_vout, "plugin was unable to allocate at least "
518 "one direct buffer" );
519 p_vout->pf_end( p_vout );
520 vlc_mutex_unlock( &p_vout->change_lock );
524 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
526 msg_Err( p_vout, "plugin allocated too many direct buffers, "
527 "our internal buffers must have overflown." );
528 p_vout->pf_end( p_vout );
529 vlc_mutex_unlock( &p_vout->change_lock );
533 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
535 AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
537 msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
538 "chroma %4.4s, ar %i:%i, sar %i:%i",
539 p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
540 p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
541 p_vout->fmt_render.i_visible_width,
542 p_vout->fmt_render.i_visible_height,
543 (char*)&p_vout->fmt_render.i_chroma,
544 i_aspect_x, i_aspect_y,
545 p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
547 AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
549 msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
550 "chroma %4.4s, ar %i:%i, sar %i:%i",
551 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
552 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
553 p_vout->fmt_in.i_visible_width,
554 p_vout->fmt_in.i_visible_height,
555 (char*)&p_vout->fmt_in.i_chroma,
556 i_aspect_x, i_aspect_y,
557 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
559 if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
561 p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
562 p_vout->output.i_width;
563 p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
564 p_vout->output.i_height;
565 p_vout->fmt_out.i_x_offset = p_vout->fmt_out.i_y_offset = 0;
567 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
568 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
570 if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
572 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
573 p_vout->fmt_out.i_height;
574 p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
575 p_vout->fmt_out.i_width;
578 vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
579 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
581 AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
583 msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
584 "chroma %4.4s, ar %i:%i, sar %i:%i",
585 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
586 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
587 p_vout->fmt_out.i_visible_width,
588 p_vout->fmt_out.i_visible_height,
589 (char*)&p_vout->fmt_out.i_chroma,
590 i_aspect_x, i_aspect_y,
591 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
593 /* Calculate shifts from system-updated masks */
594 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
595 p_vout->output.i_rmask );
596 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
597 p_vout->output.i_gmask );
598 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
599 p_vout->output.i_bmask );
601 /* Check whether we managed to create direct buffers similar to
602 * the render buffers, ie same size and chroma */
603 if( ( p_vout->output.i_width == p_vout->render.i_width )
604 && ( p_vout->output.i_height == p_vout->render.i_height )
605 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
607 /* Cool ! We have direct buffers, we can ask the decoder to
608 * directly decode into them ! Map the first render buffers to
609 * the first direct buffers, but keep the first direct buffer
610 * for memcpy operations */
611 p_vout->b_direct = 1;
613 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
615 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
616 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
617 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
619 /* We have enough direct buffers so there's no need to
620 * try to use system memory buffers. */
623 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
627 msg_Dbg( p_vout, "direct render, mapping "
628 "render pictures 0-%i to system pictures 1-%i",
629 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
633 /* Rats... Something is wrong here, we could not find an output
634 * plugin able to directly render what we decode. See if we can
635 * find a chroma plugin to do the conversion */
636 p_vout->b_direct = 0;
638 /* Choose the best module */
639 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
641 if( p_vout->chroma.p_module == NULL )
643 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
644 (char*)&p_vout->render.i_chroma,
645 (char*)&p_vout->output.i_chroma );
646 p_vout->pf_end( p_vout );
647 vlc_mutex_unlock( &p_vout->change_lock );
651 msg_Dbg( p_vout, "indirect render, mapping "
652 "render pictures 0-%i to system pictures %i-%i",
653 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
654 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
656 /* Append render buffers after the direct buffers */
657 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
659 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
662 /* Check if we have enough render pictures */
663 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
668 /* Link pictures back to their heap */
669 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
671 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
674 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
676 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
679 /* XXX XXX mark thread ready */
683 /*****************************************************************************
684 * RunThread: video output thread
685 *****************************************************************************
686 * Video output thread. This function does only returns when the thread is
687 * terminated. It handles the pictures arriving in the video heap and the
688 * display device events.
689 *****************************************************************************/
690 static void RunThread( vout_thread_t *p_vout)
692 int i_index; /* index in heap */
693 int i_idle_loops = 0; /* loops without displaying a picture */
694 mtime_t current_date; /* current date */
695 mtime_t display_date; /* display date */
697 picture_t * p_picture; /* picture pointer */
698 picture_t * p_last_picture = NULL; /* last picture */
699 picture_t * p_directbuffer; /* direct buffer to display */
701 subpicture_t * p_subpic = NULL; /* subpicture pointer */
706 p_vout->b_error = InitThread( p_vout );
708 /* signal the creation of the vout */
709 vlc_thread_ready( p_vout );
711 if( p_vout->b_error )
713 /* Destroy thread structures allocated by Create and InitThread */
714 DestroyThread( p_vout );
719 * Main loop - it is not executed if an error occurred during
722 while( (!p_vout->b_die) && (!p_vout->b_error) )
724 /* Initialize loop variables */
727 current_date = mdate();
731 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
733 msg_Dbg( p_vout, "picture heap: %d/%d",
734 I_RENDERPICTURES, p_vout->i_heap_size );
739 * Find the picture to display (the one with the earliest date).
740 * This operation does not need lock, since only READY_PICTUREs
742 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
744 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
745 && ( (p_picture == NULL) ||
746 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
748 p_picture = PP_RENDERPICTURE[i_index];
749 display_date = p_picture->date;
755 /* If we met the last picture, parse again to see whether there is
756 * a more appropriate one. */
757 if( p_picture == p_last_picture )
759 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
761 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
762 && (PP_RENDERPICTURE[i_index] != p_last_picture)
763 && ((p_picture == p_last_picture) ||
764 (PP_RENDERPICTURE[i_index]->date < display_date)) )
766 p_picture = PP_RENDERPICTURE[i_index];
767 display_date = p_picture->date;
772 /* If we found better than the last picture, destroy it */
773 if( p_last_picture && p_picture != p_last_picture )
775 vlc_mutex_lock( &p_vout->picture_lock );
776 if( p_last_picture->i_refcount )
778 p_last_picture->i_status = DISPLAYED_PICTURE;
782 p_last_picture->i_status = DESTROYED_PICTURE;
783 p_vout->i_heap_size--;
785 vlc_mutex_unlock( &p_vout->picture_lock );
786 p_last_picture = NULL;
789 /* Compute FPS rate */
790 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
793 /* XXX: config_GetInt is slow, but this kind of frame dropping
794 * should not happen that often. */
795 if( !p_picture->b_force &&
796 p_picture != p_last_picture &&
797 display_date < current_date + p_vout->render_time &&
798 config_GetInt( p_vout, "skip-frames" ) )
800 /* Picture is late: it will be destroyed and the thread
801 * will directly choose the next picture */
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, "late picture skipped ("I64Fd")",
816 current_date - display_date );
817 stats_UpdateInteger( p_vout, "lost_pictures", 1 );
818 vlc_mutex_unlock( &p_vout->picture_lock );
824 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
826 /* Picture is waaay too early: it will be destroyed */
827 vlc_mutex_lock( &p_vout->picture_lock );
828 if( p_picture->i_refcount )
830 /* Pretend we displayed the picture, but don't destroy
831 * it since the decoder might still need it. */
832 p_picture->i_status = DISPLAYED_PICTURE;
836 /* Destroy the picture without displaying it */
837 p_picture->i_status = DESTROYED_PICTURE;
838 p_vout->i_heap_size--;
840 stats_UpdateInteger( p_vout, "lost_pictures", 1 );
841 msg_Warn( p_vout, "vout warning: early picture skipped "
842 "("I64Fd")", display_date - current_date
843 - p_vout->i_pts_delay );
844 vlc_mutex_unlock( &p_vout->picture_lock );
849 if( display_date > current_date + VOUT_DISPLAY_DELAY )
851 /* A picture is ready to be rendered, but its rendering date
852 * is far from the current one so the thread will perform an
853 * empty loop as if no picture were found. The picture state
858 else if( p_picture == p_last_picture )
860 /* We are asked to repeat the previous picture, but we first
861 * wait for a couple of idle loops */
862 if( i_idle_loops < 4 )
869 /* We set the display date to something high, otherwise
870 * we'll have lots of problems with late pictures */
871 display_date = current_date + p_vout->render_time;
876 if( p_picture == NULL )
881 if( p_picture && p_vout->b_snapshot )
883 p_vout->b_snapshot = VLC_FALSE;
884 vout_Snapshot( p_vout, p_picture );
888 * Check for subpictures to display
890 if( display_date > 0 )
892 p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date );
898 stats_UpdateInteger( p_vout, "displayed_pictures", 1 );
899 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
902 * Call the plugin-specific rendering method if there is one
904 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
906 /* Render the direct buffer returned by vout_RenderPicture */
907 p_vout->pf_render( p_vout, p_directbuffer );
913 if( display_date != 0 && p_directbuffer != NULL )
915 mtime_t current_render_time = mdate() - current_date;
916 /* if render time is very large we don't include it in the mean */
917 if( current_render_time < p_vout->render_time +
920 /* Store render time using a sliding mean weighting to
921 * current value in a 3 to 1 ratio*/
922 p_vout->render_time *= 3;
923 p_vout->render_time += current_render_time;
924 p_vout->render_time >>= 2;
928 /* Give back change lock */
929 vlc_mutex_unlock( &p_vout->change_lock );
931 /* Sleep a while or until a given date */
932 if( display_date != 0 )
934 /* If there are filters in the chain, better give them the picture
936 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
938 mwait( display_date - VOUT_MWAIT_TOLERANCE );
943 msleep( VOUT_IDLE_SLEEP );
946 /* On awakening, take back lock and send immediately picture
948 vlc_mutex_lock( &p_vout->change_lock );
951 * Display the previously rendered picture
953 if( p_picture != NULL && p_directbuffer != NULL )
955 /* Display the direct buffer returned by vout_RenderPicture */
956 if( p_vout->pf_display )
958 p_vout->pf_display( p_vout, p_directbuffer );
961 /* Tell the vout this was the last picture and that it does not
962 * need to be forced anymore. */
963 p_last_picture = p_picture;
964 p_last_picture->b_force = 0;
967 if( p_picture != NULL )
969 /* Reinitialize idle loop count */
974 * Check events and manage thread
976 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
978 /* A fatal error occurred, and the thread must terminate
979 * immediately, without displaying anything - setting b_error to 1
980 * causes the immediate end of the main while() loop. */
984 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
986 /* this must only happen when the vout plugin is incapable of
987 * rescaling the picture itself. In this case we need to destroy
988 * the current picture buffers and recreate new ones with the right
992 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
994 p_vout->pf_end( p_vout );
995 for( i = 0; i < I_OUTPUTPICTURES; i++ )
996 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
998 I_OUTPUTPICTURES = 0;
999 if( p_vout->pf_init( p_vout ) )
1001 msg_Err( p_vout, "cannot resize display" );
1002 /* FIXME: pf_end will be called again in EndThread() */
1003 p_vout->b_error = 1;
1006 /* Need to reinitialise the chroma plugin */
1007 if( p_vout->chroma.p_module )
1009 if( p_vout->chroma.p_module->pf_deactivate )
1010 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
1011 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
1015 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1017 /* This happens when the picture buffers need to be recreated.
1018 * This is useful on multimonitor displays for instance.
1020 * Warning: This only works when the vout creates only 1 picture
1022 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1024 if( !p_vout->b_direct )
1026 module_Unneed( p_vout, p_vout->chroma.p_module );
1029 vlc_mutex_lock( &p_vout->picture_lock );
1031 p_vout->pf_end( p_vout );
1033 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1035 p_vout->b_error = InitThread( p_vout );
1037 vlc_mutex_unlock( &p_vout->picture_lock );
1042 * Error loop - wait until the thread destruction is requested
1044 if( p_vout->b_error )
1046 ErrorThread( p_vout );
1050 EndThread( p_vout );
1052 /* Destroy thread structures allocated by CreateThread */
1053 DestroyThread( p_vout );
1056 /*****************************************************************************
1057 * ErrorThread: RunThread() error loop
1058 *****************************************************************************
1059 * This function is called when an error occurred during thread main's loop.
1060 * The thread can still receive feed, but must be ready to terminate as soon
1062 *****************************************************************************/
1063 static void ErrorThread( vout_thread_t *p_vout )
1065 /* Wait until a `die' order */
1066 while( !p_vout->b_die )
1069 msleep( VOUT_IDLE_SLEEP );
1073 /*****************************************************************************
1074 * EndThread: thread destruction
1075 *****************************************************************************
1076 * This function is called when the thread ends after a sucessful
1077 * initialization. It frees all resources allocated by InitThread.
1078 *****************************************************************************/
1079 static void EndThread( vout_thread_t *p_vout )
1081 int i_index; /* index in heap */
1085 struct tms cpu_usage;
1086 times( &cpu_usage );
1088 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1089 cpu_usage.tms_utime, cpu_usage.tms_stime );
1093 if( !p_vout->b_direct )
1095 module_Unneed( p_vout, p_vout->chroma.p_module );
1098 /* Destroy all remaining pictures */
1099 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1101 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1103 free( p_vout->p_picture[i_index].p_data_orig );
1107 /* Destroy subpicture unit */
1108 spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), VLC_FALSE );
1109 spu_Destroy( p_vout->p_spu );
1111 /* Destroy translation tables */
1112 p_vout->pf_end( p_vout );
1114 /* Release the change lock */
1115 vlc_mutex_unlock( &p_vout->change_lock );
1118 /*****************************************************************************
1119 * DestroyThread: thread destruction
1120 *****************************************************************************
1121 * This function is called when the thread ends. It frees all ressources
1122 * allocated by CreateThread. Status is available at this stage.
1123 *****************************************************************************/
1124 static void DestroyThread( vout_thread_t *p_vout )
1126 /* Destroy the locks */
1127 vlc_mutex_destroy( &p_vout->picture_lock );
1128 vlc_mutex_destroy( &p_vout->change_lock );
1130 /* Release the module */
1131 if( p_vout && p_vout->p_module )
1133 module_Unneed( p_vout, p_vout->p_module );
1137 /* following functions are local */
1139 static int ReduceHeight( int i_ratio )
1141 int i_dummy = VOUT_ASPECT_FACTOR;
1149 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1150 while( !(i_ratio & 1) && !(i_dummy & 1) )
1157 while( !(i_ratio % 3) && !(i_dummy % 3) )
1164 while( !(i_ratio % 5) && !(i_dummy % 5) )
1174 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1176 unsigned int i_pgcd = ReduceHeight( i_aspect );
1177 *i_aspect_x = i_aspect / i_pgcd;
1178 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1181 /*****************************************************************************
1182 * BinaryLog: computes the base 2 log of a binary value
1183 *****************************************************************************
1184 * This functions is used by MaskToShift, to get a bit index from a binary
1186 *****************************************************************************/
1187 static int BinaryLog( uint32_t i )
1191 if( i == 0 ) return -31337;
1193 if( i & 0xffff0000 ) i_log += 16;
1194 if( i & 0xff00ff00 ) i_log += 8;
1195 if( i & 0xf0f0f0f0 ) i_log += 4;
1196 if( i & 0xcccccccc ) i_log += 2;
1197 if( i & 0xaaaaaaaa ) i_log += 1;
1202 /*****************************************************************************
1203 * MaskToShift: transform a color mask into right and left shifts
1204 *****************************************************************************
1205 * This function is used for obtaining color shifts from masks.
1206 *****************************************************************************/
1207 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1209 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1213 *pi_left = *pi_right = 0;
1218 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1219 i_high = i_mask + i_low; /* higher bit of the mask */
1221 /* Transform bits into an index */
1222 i_low = BinaryLog (i_low);
1223 i_high = BinaryLog (i_high);
1225 /* Update pointers and return */
1227 *pi_right = (8 - i_high + i_low);
1230 /*****************************************************************************
1231 * vout_VarCallback: generic callback for intf variables
1232 *****************************************************************************/
1233 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1234 vlc_value_t old_value, vlc_value_t new_value,
1237 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1239 val.b_bool = VLC_TRUE;
1240 var_Set( p_vout, "intf-change", val );
1244 /*****************************************************************************
1245 * Helper thread for object variables callbacks.
1246 * Only used to avoid deadlocks when using the video embedded mode.
1247 *****************************************************************************/
1248 typedef struct suxor_thread_t
1251 input_thread_t *p_input;
1255 static void SuxorRestartVideoES( suxor_thread_t *p_this )
1259 vlc_thread_ready( p_this );
1261 /* Now restart current video stream */
1262 var_Get( p_this->p_input, "video-es", &val );
1263 if( val.i_int >= 0 )
1266 val_es.i_int = -VIDEO_ES;
1267 var_Set( p_this->p_input, "video-es", val_es );
1268 var_Set( p_this->p_input, "video-es", val );
1271 vlc_object_release( p_this->p_input );
1274 CloseHandle( p_this->thread_id );
1277 vlc_object_destroy( p_this );
1280 /*****************************************************************************
1281 * object variables callbacks: a bunch of object variables are used by the
1282 * interfaces to interact with the vout.
1283 *****************************************************************************/
1284 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1285 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1287 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1288 input_thread_t *p_input;
1291 char *psz_mode = newval.psz_string;
1292 char *psz_filter, *psz_deinterlace = NULL;
1294 var_Get( p_vout, "vout-filter", &val );
1295 psz_filter = val.psz_string;
1296 if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1298 if( !psz_mode || !*psz_mode )
1300 if( psz_deinterlace )
1302 char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1303 if( psz_src[0] == ':' ) psz_src++;
1304 memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1307 else if( !psz_deinterlace )
1309 psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1310 sizeof(":deinterlace") );
1311 if( psz_filter && *psz_filter ) strcat( psz_filter, ":" );
1312 strcat( psz_filter, "deinterlace" );
1315 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1317 if( !p_input ) return VLC_EGENERIC;
1319 if( psz_mode && *psz_mode )
1321 /* Modify input as well because the vout might have to be restarted */
1322 val.psz_string = psz_mode;
1323 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1324 var_Set( p_input, "deinterlace-mode", val );
1326 vlc_object_release( p_input );
1328 val.b_bool = VLC_TRUE;
1329 var_Set( p_vout, "intf-change", val );
1331 val.psz_string = psz_filter;
1332 var_Set( p_vout, "vout-filter", val );
1333 if( psz_filter ) free( psz_filter );
1338 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1339 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1341 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1342 input_thread_t *p_input;
1345 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1349 msg_Err( p_vout, "Input not found" );
1350 return( VLC_EGENERIC );
1353 val.b_bool = VLC_TRUE;
1354 var_Set( p_vout, "intf-change", val );
1356 /* Modify input as well because the vout might have to be restarted */
1357 val.psz_string = newval.psz_string;
1358 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1360 var_Set( p_input, "vout-filter", val );
1362 /* Now restart current video stream */
1363 var_Get( p_input, "video-es", &val );
1364 if( val.i_int >= 0 )
1366 suxor_thread_t *p_suxor =
1367 vlc_object_create( p_vout, sizeof(suxor_thread_t) );
1368 p_suxor->p_input = p_input;
1369 p_vout->b_filter_change = VLC_TRUE;
1370 vlc_object_yield( p_input );
1371 vlc_thread_create( p_suxor, "suxor", SuxorRestartVideoES,
1372 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1375 vlc_object_release( p_input );