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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 );
62 static void InitWindowSize ( vout_thread_t *, unsigned *, unsigned * );
64 /* Object variables callbacks */
65 static int DeinterlaceCallback( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
67 static int FilterCallback( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
70 /* From vout_intf.c */
71 int vout_Snapshot( vout_thread_t *, picture_t * );
73 /*****************************************************************************
74 * vout_Request: find a video output thread, create one, or destroy one.
75 *****************************************************************************
76 * This function looks for a video output thread matching the current
77 * properties. If not found, it spawns a new one.
78 *****************************************************************************/
79 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
80 video_format_t *p_fmt )
84 /* Reattach video output to input before bailing out */
87 vlc_object_t *p_playlist;
89 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
94 spu_Attach( p_vout->p_spu, p_this, VLC_FALSE );
95 vlc_object_detach( p_vout );
96 vlc_object_attach( p_vout, p_playlist );
98 vlc_object_release( p_playlist );
102 msg_Dbg( p_this, "cannot find playlist, destroying vout" );
103 vlc_object_detach( p_vout );
104 vout_Destroy( p_vout );
110 /* If a video output was provided, lock it, otherwise look for one. */
113 vlc_object_yield( p_vout );
117 p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
121 playlist_t *p_playlist;
123 p_playlist = vlc_object_find( p_this,
124 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
127 vlc_mutex_lock( &p_playlist->gc_lock );
128 p_vout = vlc_object_find( p_playlist,
129 VLC_OBJECT_VOUT, FIND_CHILD );
130 /* only first children of p_input for unused vout */
131 if( p_vout && p_vout->p_parent != (vlc_object_t *)p_playlist )
133 vlc_object_release( p_vout );
136 vlc_mutex_unlock( &p_playlist->gc_lock );
137 vlc_object_release( p_playlist );
142 /* If we now have a video output, check it has the right properties */
145 char *psz_filter_chain;
148 /* We don't directly check for the "vout-filter" variable for obvious
149 * performance reasons. */
150 if( p_vout->b_filter_change )
152 var_Get( p_vout, "vout-filter", &val );
153 psz_filter_chain = val.psz_string;
155 if( psz_filter_chain && !*psz_filter_chain )
157 free( psz_filter_chain );
158 psz_filter_chain = NULL;
160 if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
162 free( p_vout->psz_filter_chain );
163 p_vout->psz_filter_chain = NULL;
166 if( !psz_filter_chain && !p_vout->psz_filter_chain )
168 p_vout->b_filter_change = VLC_FALSE;
171 if( psz_filter_chain ) free( psz_filter_chain );
174 if( ( p_vout->fmt_render.i_width != p_fmt->i_width ) ||
175 ( p_vout->fmt_render.i_height != p_fmt->i_height ) ||
176 ( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ) ||
177 ( p_vout->fmt_render.i_aspect != p_fmt->i_aspect
178 && !p_vout->b_override_aspect ) ||
179 p_vout->b_filter_change )
181 /* We are not interested in this format, close this vout */
182 vlc_object_detach( p_vout );
183 vlc_object_release( p_vout );
184 vout_Destroy( p_vout );
189 /* This video output is cool! Hijack it. */
190 vlc_object_detach( p_vout );
191 spu_Attach( p_vout->p_spu, p_this, VLC_TRUE );
192 vlc_object_attach( p_vout, p_this );
193 vlc_object_release( p_vout );
199 msg_Dbg( p_this, "no usable vout present, spawning one" );
201 p_vout = vout_Create( p_this, p_fmt );
207 /*****************************************************************************
208 * vout_Create: creates a new video output thread
209 *****************************************************************************
210 * This function creates a new video output thread, and returns a pointer
211 * to its description. On error, it returns NULL.
212 *****************************************************************************/
213 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
215 vout_thread_t * p_vout; /* thread descriptor */
216 input_thread_t * p_input_thread;
217 int i_index; /* loop variable */
219 vlc_value_t val, val2, text;
221 unsigned int i_width = p_fmt->i_visible_width;
222 unsigned int i_height = p_fmt->i_visible_height;
223 vlc_fourcc_t i_chroma = p_fmt->i_chroma;
224 unsigned int i_aspect = p_fmt->i_aspect;
226 /* Allocate descriptor */
227 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
230 msg_Err( p_parent, "out of memory" );
234 /* Initialize pictures - translation tables and functions
235 * will be initialized later in InitThread */
236 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
238 p_vout->p_picture[i_index].pf_lock = NULL;
239 p_vout->p_picture[i_index].pf_unlock = NULL;
240 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
241 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
242 p_vout->p_picture[i_index].b_slow = 0;
245 /* No images in the heap */
246 p_vout->i_heap_size = 0;
248 /* Initialize the rendering heap */
249 I_RENDERPICTURES = 0;
250 p_vout->fmt_render = *p_fmt; /* FIXME palette */
251 p_vout->fmt_in = *p_fmt; /* FIXME palette */
252 p_vout->render.i_width = i_width;
253 p_vout->render.i_height = i_height;
254 p_vout->render.i_chroma = i_chroma;
255 p_vout->render.i_aspect = i_aspect;
257 p_vout->render.i_rmask = 0;
258 p_vout->render.i_gmask = 0;
259 p_vout->render.i_bmask = 0;
261 p_vout->render.i_last_used_pic = -1;
262 p_vout->render.b_allow_modify_pics = 1;
264 /* Zero the output heap */
265 I_OUTPUTPICTURES = 0;
266 p_vout->output.i_width = 0;
267 p_vout->output.i_height = 0;
268 p_vout->output.i_chroma = 0;
269 p_vout->output.i_aspect = 0;
271 p_vout->output.i_rmask = 0;
272 p_vout->output.i_gmask = 0;
273 p_vout->output.i_bmask = 0;
275 /* Initialize misc stuff */
276 p_vout->i_changes = 0;
278 p_vout->b_grayscale = 0;
280 p_vout->b_interface = 0;
282 p_vout->b_fullscreen = 0;
283 p_vout->i_alignment = 0;
284 p_vout->render_time = 10;
285 p_vout->c_fps_samples = 0;
286 p_vout->b_filter_change = 0;
287 p_vout->pf_control = 0;
288 p_vout->p_parent_intf = 0;
290 /* Initialize locks */
291 vlc_mutex_init( p_vout, &p_vout->picture_lock );
292 vlc_mutex_init( p_vout, &p_vout->change_lock );
294 /* Mouse coordinates */
295 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
296 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
297 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
298 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
299 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
301 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
302 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
304 /* Initialize subpicture unit */
305 p_vout->p_spu = spu_Create( p_vout );
306 spu_Attach( p_vout->p_spu, p_parent, VLC_TRUE );
308 /* Attach the new object now so we can use var inheritance below */
309 vlc_object_attach( p_vout, p_parent );
311 spu_Init( p_vout->p_spu );
313 /* Take care of some "interface/control" related initialisations */
314 vout_IntfInit( p_vout );
316 p_vout->b_override_aspect = VLC_FALSE;
318 /* If the parent is not a VOUT object, that means we are at the start of
319 * the video output pipe */
320 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
322 int i_monitor_aspect_x = 4 , i_monitor_aspect_y = 3;
323 var_Get( p_vout, "aspect-ratio", &val );
324 var_Get( p_vout, "monitor-aspect-ratio", &val2 );
326 if( val2.psz_string )
328 char *psz_parser = strchr( val2.psz_string, ':' );
331 *psz_parser++ = '\0';
332 i_monitor_aspect_x = atoi( val2.psz_string );
333 i_monitor_aspect_y = atoi( psz_parser );
335 AspectRatio( VOUT_ASPECT_FACTOR * atof( val2.psz_string ),
336 &i_monitor_aspect_x, &i_monitor_aspect_y );
339 free( val2.psz_string );
342 /* Check whether the user tried to override aspect ratio */
345 unsigned int i_new_aspect = i_aspect;
346 char *psz_parser = strchr( val.psz_string, ':' );
347 int i_aspect_x, i_aspect_y;
348 AspectRatio( i_aspect, &i_aspect_x, &i_aspect_y );
352 *psz_parser++ = '\0';
353 i_new_aspect = atoi( val.psz_string )
354 * VOUT_ASPECT_FACTOR / atoi( psz_parser );
358 if( atof( val.psz_string ) != 0 )
360 i_new_aspect = VOUT_ASPECT_FACTOR * atof( val.psz_string );
363 i_new_aspect = (int)((float)i_new_aspect
364 * (float)i_monitor_aspect_y*4.0/((float)i_monitor_aspect_x*3.0));
366 free( val.psz_string );
368 if( i_new_aspect && i_new_aspect != i_aspect )
370 int i_aspect_x, i_aspect_y;
372 AspectRatio( i_new_aspect, &i_aspect_x, &i_aspect_y );
374 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
375 i_aspect_x, i_aspect_y );
377 p_vout->render.i_aspect = i_new_aspect;
379 p_vout->b_override_aspect = VLC_TRUE;
383 /* Look for the default filter configuration */
384 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
385 var_Get( p_vout, "vout-filter", &val );
386 p_vout->psz_filter_chain = val.psz_string;
390 /* continue the parent's filter chain */
393 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
394 if( psz_end && *(psz_end+1) )
395 p_vout->psz_filter_chain = strdup( psz_end+1 );
396 else p_vout->psz_filter_chain = NULL;
399 /* Choose the video output module */
400 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
402 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
403 var_Get( p_vout, "vout", &val );
404 psz_plugin = val.psz_string;
408 /* the filter chain is a string list of filters separated by double
412 psz_end = strchr( p_vout->psz_filter_chain, ':' );
414 psz_plugin = strndup( p_vout->psz_filter_chain,
415 psz_end - p_vout->psz_filter_chain );
416 else psz_plugin = strdup( p_vout->psz_filter_chain );
419 /* Initialize the dimensions of the video window */
420 InitWindowSize( p_vout, &p_vout->i_window_width,
421 &p_vout->i_window_height );
423 /* Create the vout thread */
424 p_vout->p_module = module_Need( p_vout,
425 ( p_vout->psz_filter_chain && *p_vout->psz_filter_chain ) ?
426 "video filter" : "video output", psz_plugin, 0 );
428 if( psz_plugin ) free( psz_plugin );
429 if( p_vout->p_module == NULL )
431 msg_Err( p_vout, "no suitable vout module" );
432 vlc_object_detach( p_vout );
433 vlc_object_destroy( p_vout );
437 /* Create a few object variables for interface interaction */
438 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
439 text.psz_string = _("Deinterlace");
440 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
441 val.psz_string = ""; text.psz_string = _("Disable");
442 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
443 val.psz_string = "discard"; text.psz_string = _("Discard");
444 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
445 val.psz_string = "blend"; text.psz_string = _("Blend");
446 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
447 val.psz_string = "mean"; text.psz_string = _("Mean");
448 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
449 val.psz_string = "bob"; text.psz_string = _("Bob");
450 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
451 val.psz_string = "linear"; text.psz_string = _("Linear");
452 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
453 val.psz_string = "x"; text.psz_string = "X";
454 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
456 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
458 var_Set( p_vout, "deinterlace", val );
459 if( val.psz_string ) free( val.psz_string );
461 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
464 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
465 text.psz_string = _("Filters");
466 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
467 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
469 /* Calculate delay created by internal caching */
470 p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
471 VLC_OBJECT_INPUT, FIND_ANYWHERE );
474 p_vout->i_pts_delay = p_input_thread->i_pts_delay;
475 vlc_object_release( p_input_thread );
479 p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
482 if( vlc_thread_create( p_vout, "video output", RunThread,
483 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
485 msg_Err( p_vout, "out of memory" );
486 module_Unneed( p_vout, p_vout->p_module );
487 vlc_object_detach( p_vout );
488 vlc_object_destroy( p_vout );
492 if( p_vout->b_error )
494 msg_Err( p_vout, "video output creation failed" );
496 /* Make sure the thread is destroyed */
497 p_vout->b_die = VLC_TRUE;
499 vlc_thread_join( p_vout );
501 vlc_object_detach( p_vout );
502 vlc_object_destroy( p_vout );
509 /*****************************************************************************
510 * vout_Destroy: destroys a previously created video output
511 *****************************************************************************
512 * Destroy a terminated thread.
513 * The function will request a destruction of the specified thread. If pi_error
514 * is NULL, it will return once the thread is destroyed. Else, it will be
515 * update using one of the THREAD_* constants.
516 *****************************************************************************/
517 void vout_Destroy( vout_thread_t *p_vout )
519 vlc_object_t *p_playlist;
521 /* Request thread destruction */
522 p_vout->b_die = VLC_TRUE;
523 vlc_thread_join( p_vout );
525 var_Destroy( p_vout, "intf-change" );
527 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
530 if( p_vout->psz_filter_chain ) free( p_vout->psz_filter_chain );
533 vlc_object_destroy( p_vout );
535 /* If it was the last vout, tell the interface to show up */
536 if( p_playlist != NULL )
538 vout_thread_t *p_another_vout = vlc_object_find( p_playlist,
539 VLC_OBJECT_VOUT, FIND_ANYWHERE );
540 if( p_another_vout == NULL )
543 val.b_bool = VLC_TRUE;
544 var_Set( p_playlist, "intf-show", val );
548 vlc_object_release( p_another_vout );
550 vlc_object_release( p_playlist );
554 /*****************************************************************************
555 * InitThread: initialize video output thread
556 *****************************************************************************
557 * This function is called from RunThread and performs the second step of the
558 * initialization. It returns 0 on success. Note that the thread's flag are not
559 * modified inside this function.
560 *****************************************************************************/
561 static int InitThread( vout_thread_t *p_vout )
563 int i, i_aspect_x, i_aspect_y;
565 vlc_mutex_lock( &p_vout->change_lock );
571 /* Initialize output method, it allocates direct buffers for us */
572 if( p_vout->pf_init( p_vout ) )
574 vlc_mutex_unlock( &p_vout->change_lock );
578 if( !I_OUTPUTPICTURES )
580 msg_Err( p_vout, "plugin was unable to allocate at least "
581 "one direct buffer" );
582 p_vout->pf_end( p_vout );
583 vlc_mutex_unlock( &p_vout->change_lock );
587 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
589 msg_Err( p_vout, "plugin allocated too many direct buffers, "
590 "our internal buffers must have overflown." );
591 p_vout->pf_end( p_vout );
592 vlc_mutex_unlock( &p_vout->change_lock );
596 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
598 AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
600 msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
601 "chroma %4.4s, ar %i:%i, sar %i:%i",
602 p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
603 p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
604 p_vout->fmt_render.i_visible_width,
605 p_vout->fmt_render.i_visible_height,
606 (char*)&p_vout->fmt_render.i_chroma,
607 i_aspect_x, i_aspect_y,
608 p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
610 AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
612 msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
613 "chroma %4.4s, ar %i:%i, sar %i:%i",
614 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
615 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
616 p_vout->fmt_in.i_visible_width,
617 p_vout->fmt_in.i_visible_height,
618 (char*)&p_vout->fmt_in.i_chroma,
619 i_aspect_x, i_aspect_y,
620 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
622 if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
624 p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
625 p_vout->output.i_width;
626 p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
627 p_vout->output.i_height;
628 p_vout->fmt_out.i_x_offset = p_vout->fmt_out.i_y_offset = 0;
630 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
631 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
633 if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
635 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
636 p_vout->fmt_out.i_height;
637 p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
638 p_vout->fmt_out.i_width;
641 vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
642 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
644 AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
646 msg_Dbg( p_vout, "picture out %ix%i, chroma %4.4s, ar %i:%i, sar %i:%i",
647 p_vout->output.i_width, p_vout->output.i_height,
648 (char*)&p_vout->output.i_chroma,
649 i_aspect_x, i_aspect_y,
650 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
652 /* Calculate shifts from system-updated masks */
653 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
654 p_vout->output.i_rmask );
655 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
656 p_vout->output.i_gmask );
657 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
658 p_vout->output.i_bmask );
660 /* Check whether we managed to create direct buffers similar to
661 * the render buffers, ie same size and chroma */
662 if( ( p_vout->output.i_width == p_vout->render.i_width )
663 && ( p_vout->output.i_height == p_vout->render.i_height )
664 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
666 /* Cool ! We have direct buffers, we can ask the decoder to
667 * directly decode into them ! Map the first render buffers to
668 * the first direct buffers, but keep the first direct buffer
669 * for memcpy operations */
670 p_vout->b_direct = 1;
672 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
674 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
675 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
676 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
678 /* We have enough direct buffers so there's no need to
679 * try to use system memory buffers. */
682 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
686 msg_Dbg( p_vout, "direct render, mapping "
687 "render pictures 0-%i to system pictures 1-%i",
688 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
692 /* Rats... Something is wrong here, we could not find an output
693 * plugin able to directly render what we decode. See if we can
694 * find a chroma plugin to do the conversion */
695 p_vout->b_direct = 0;
697 /* Choose the best module */
698 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
700 if( p_vout->chroma.p_module == NULL )
702 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
703 (char*)&p_vout->render.i_chroma,
704 (char*)&p_vout->output.i_chroma );
705 p_vout->pf_end( p_vout );
706 vlc_mutex_unlock( &p_vout->change_lock );
710 msg_Dbg( p_vout, "indirect render, mapping "
711 "render pictures 0-%i to system pictures %i-%i",
712 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
713 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
715 /* Append render buffers after the direct buffers */
716 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
718 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
721 /* Check if we have enough render pictures */
722 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
727 /* Link pictures back to their heap */
728 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
730 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
733 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
735 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
738 /* XXX XXX mark thread ready */
742 /*****************************************************************************
743 * RunThread: video output thread
744 *****************************************************************************
745 * Video output thread. This function does only returns when the thread is
746 * terminated. It handles the pictures arriving in the video heap and the
747 * display device events.
748 *****************************************************************************/
749 static void RunThread( vout_thread_t *p_vout)
751 int i_index; /* index in heap */
752 int i_idle_loops = 0; /* loops without displaying a picture */
753 mtime_t current_date; /* current date */
754 mtime_t display_date; /* display date */
756 picture_t * p_picture; /* picture pointer */
757 picture_t * p_last_picture = NULL; /* last picture */
758 picture_t * p_directbuffer; /* direct buffer to display */
760 subpicture_t * p_subpic = NULL; /* subpicture pointer */
765 p_vout->b_error = InitThread( p_vout );
767 /* signal the creation of the vout */
768 vlc_thread_ready( p_vout );
770 if( p_vout->b_error )
772 /* Destroy thread structures allocated by Create and InitThread */
773 DestroyThread( p_vout );
778 * Main loop - it is not executed if an error occurred during
781 while( (!p_vout->b_die) && (!p_vout->b_error) )
783 /* Initialize loop variables */
786 current_date = mdate();
790 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
792 msg_Dbg( p_vout, "picture heap: %d/%d",
793 I_RENDERPICTURES, p_vout->i_heap_size );
798 * Find the picture to display (the one with the earliest date).
799 * This operation does not need lock, since only READY_PICTUREs
801 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
803 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
804 && ( (p_picture == NULL) ||
805 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
807 p_picture = PP_RENDERPICTURE[i_index];
808 display_date = p_picture->date;
814 /* If we met the last picture, parse again to see whether there is
815 * a more appropriate one. */
816 if( p_picture == p_last_picture )
818 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
820 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
821 && (PP_RENDERPICTURE[i_index] != p_last_picture)
822 && ((p_picture == p_last_picture) ||
823 (PP_RENDERPICTURE[i_index]->date < display_date)) )
825 p_picture = PP_RENDERPICTURE[i_index];
826 display_date = p_picture->date;
831 /* If we found better than the last picture, destroy it */
832 if( p_last_picture && p_picture != p_last_picture )
834 vlc_mutex_lock( &p_vout->picture_lock );
835 if( p_last_picture->i_refcount )
837 p_last_picture->i_status = DISPLAYED_PICTURE;
841 p_last_picture->i_status = DESTROYED_PICTURE;
842 p_vout->i_heap_size--;
844 vlc_mutex_unlock( &p_vout->picture_lock );
845 p_last_picture = NULL;
848 /* Compute FPS rate */
849 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
852 if( !p_picture->b_force &&
853 p_picture != p_last_picture &&
854 display_date < current_date + p_vout->render_time )
856 /* Picture is late: it will be destroyed and the thread
857 * will directly choose the next picture */
858 vlc_mutex_lock( &p_vout->picture_lock );
859 if( p_picture->i_refcount )
861 /* Pretend we displayed the picture, but don't destroy
862 * it since the decoder might still need it. */
863 p_picture->i_status = DISPLAYED_PICTURE;
867 /* Destroy the picture without displaying it */
868 p_picture->i_status = DESTROYED_PICTURE;
869 p_vout->i_heap_size--;
871 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
872 current_date - display_date );
873 vlc_mutex_unlock( &p_vout->picture_lock );
879 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
881 /* Picture is waaay too early: it will be destroyed */
882 vlc_mutex_lock( &p_vout->picture_lock );
883 if( p_picture->i_refcount )
885 /* Pretend we displayed the picture, but don't destroy
886 * it since the decoder might still need it. */
887 p_picture->i_status = DISPLAYED_PICTURE;
891 /* Destroy the picture without displaying it */
892 p_picture->i_status = DESTROYED_PICTURE;
893 p_vout->i_heap_size--;
895 msg_Warn( p_vout, "vout warning: early picture skipped "
896 "("I64Fd")", display_date - current_date
897 - p_vout->i_pts_delay );
898 vlc_mutex_unlock( &p_vout->picture_lock );
903 if( display_date > current_date + VOUT_DISPLAY_DELAY )
905 /* A picture is ready to be rendered, but its rendering date
906 * is far from the current one so the thread will perform an
907 * empty loop as if no picture were found. The picture state
912 else if( p_picture == p_last_picture )
914 /* We are asked to repeat the previous picture, but we first
915 * wait for a couple of idle loops */
916 if( i_idle_loops < 4 )
923 /* We set the display date to something high, otherwise
924 * we'll have lots of problems with late pictures */
925 display_date = current_date + p_vout->render_time;
930 if( p_picture == NULL )
935 if( p_picture && p_vout->b_snapshot )
937 p_vout->b_snapshot = VLC_FALSE;
938 vout_Snapshot( p_vout, p_picture );
942 * Check for subpictures to display
944 if( display_date > 0 )
946 p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date );
952 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
955 * Call the plugin-specific rendering method if there is one
957 if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
959 /* Render the direct buffer returned by vout_RenderPicture */
960 p_vout->pf_render( p_vout, p_directbuffer );
966 if( display_date != 0 && p_directbuffer != NULL )
968 mtime_t current_render_time = mdate() - current_date;
969 /* if render time is very large we don't include it in the mean */
970 if( current_render_time < p_vout->render_time +
973 /* Store render time using a sliding mean weighting to
974 * current value in a 3 to 1 ratio*/
975 p_vout->render_time *= 3;
976 p_vout->render_time += current_render_time;
977 p_vout->render_time >>= 2;
981 /* Give back change lock */
982 vlc_mutex_unlock( &p_vout->change_lock );
984 /* Sleep a while or until a given date */
985 if( display_date != 0 )
987 /* If there are filters in the chain, better give them the picture
989 if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
991 mwait( display_date - VOUT_MWAIT_TOLERANCE );
996 msleep( VOUT_IDLE_SLEEP );
999 /* On awakening, take back lock and send immediately picture
1001 vlc_mutex_lock( &p_vout->change_lock );
1004 * Display the previously rendered picture
1006 if( p_picture != NULL && p_directbuffer != NULL )
1008 /* Display the direct buffer returned by vout_RenderPicture */
1009 if( p_vout->pf_display )
1011 p_vout->pf_display( p_vout, p_directbuffer );
1014 /* Tell the vout this was the last picture and that it does not
1015 * need to be forced anymore. */
1016 p_last_picture = p_picture;
1017 p_last_picture->b_force = 0;
1020 if( p_picture != NULL )
1022 /* Reinitialize idle loop count */
1027 * Check events and manage thread
1029 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1031 /* A fatal error occurred, and the thread must terminate
1032 * immediately, without displaying anything - setting b_error to 1
1033 * causes the immediate end of the main while() loop. */
1034 p_vout->b_error = 1;
1037 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1039 /* this must only happen when the vout plugin is incapable of
1040 * rescaling the picture itself. In this case we need to destroy
1041 * the current picture buffers and recreate new ones with the right
1045 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1047 p_vout->pf_end( p_vout );
1048 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1049 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1051 I_OUTPUTPICTURES = 0;
1052 if( p_vout->pf_init( p_vout ) )
1054 msg_Err( p_vout, "cannot resize display" );
1055 /* FIXME: pf_end will be called again in EndThread() */
1056 p_vout->b_error = 1;
1059 /* Need to reinitialise the chroma plugin */
1060 if( p_vout->chroma.p_module )
1062 if( p_vout->chroma.p_module->pf_deactivate )
1063 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
1064 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
1068 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1070 /* This happens when the picture buffers need to be recreated.
1071 * This is useful on multimonitor displays for instance.
1073 * Warning: This only works when the vout creates only 1 picture
1075 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1077 if( !p_vout->b_direct )
1079 module_Unneed( p_vout, p_vout->chroma.p_module );
1082 vlc_mutex_lock( &p_vout->picture_lock );
1084 p_vout->pf_end( p_vout );
1086 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1088 p_vout->b_error = InitThread( p_vout );
1090 vlc_mutex_unlock( &p_vout->picture_lock );
1095 * Error loop - wait until the thread destruction is requested
1097 if( p_vout->b_error )
1099 ErrorThread( p_vout );
1103 EndThread( p_vout );
1105 /* Destroy thread structures allocated by CreateThread */
1106 DestroyThread( p_vout );
1109 /*****************************************************************************
1110 * ErrorThread: RunThread() error loop
1111 *****************************************************************************
1112 * This function is called when an error occurred during thread main's loop.
1113 * The thread can still receive feed, but must be ready to terminate as soon
1115 *****************************************************************************/
1116 static void ErrorThread( vout_thread_t *p_vout )
1118 /* Wait until a `die' order */
1119 while( !p_vout->b_die )
1122 msleep( VOUT_IDLE_SLEEP );
1126 /*****************************************************************************
1127 * EndThread: thread destruction
1128 *****************************************************************************
1129 * This function is called when the thread ends after a sucessful
1130 * initialization. It frees all resources allocated by InitThread.
1131 *****************************************************************************/
1132 static void EndThread( vout_thread_t *p_vout )
1134 int i_index; /* index in heap */
1138 struct tms cpu_usage;
1139 times( &cpu_usage );
1141 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1142 cpu_usage.tms_utime, cpu_usage.tms_stime );
1146 if( !p_vout->b_direct )
1148 module_Unneed( p_vout, p_vout->chroma.p_module );
1151 /* Destroy all remaining pictures */
1152 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1154 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1156 free( p_vout->p_picture[i_index].p_data_orig );
1160 /* Destroy subpicture unit */
1161 spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), VLC_FALSE );
1162 spu_Destroy( p_vout->p_spu );
1164 /* Destroy translation tables */
1165 p_vout->pf_end( p_vout );
1167 /* Release the change lock */
1168 vlc_mutex_unlock( &p_vout->change_lock );
1171 /*****************************************************************************
1172 * DestroyThread: thread destruction
1173 *****************************************************************************
1174 * This function is called when the thread ends. It frees all ressources
1175 * allocated by CreateThread. Status is available at this stage.
1176 *****************************************************************************/
1177 static void DestroyThread( vout_thread_t *p_vout )
1179 /* Destroy the locks */
1180 vlc_mutex_destroy( &p_vout->picture_lock );
1181 vlc_mutex_destroy( &p_vout->change_lock );
1183 /* Release the module */
1184 if( p_vout && p_vout->p_module )
1186 module_Unneed( p_vout, p_vout->p_module );
1190 /* following functions are local */
1192 static int ReduceHeight( int i_ratio )
1194 int i_dummy = VOUT_ASPECT_FACTOR;
1202 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1203 while( !(i_ratio & 1) && !(i_dummy & 1) )
1210 while( !(i_ratio % 3) && !(i_dummy % 3) )
1217 while( !(i_ratio % 5) && !(i_dummy % 5) )
1227 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1229 unsigned int i_pgcd = ReduceHeight( i_aspect );
1230 *i_aspect_x = i_aspect / i_pgcd;
1231 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1234 /*****************************************************************************
1235 * BinaryLog: computes the base 2 log of a binary value
1236 *****************************************************************************
1237 * This functions is used by MaskToShift, to get a bit index from a binary
1239 *****************************************************************************/
1240 static int BinaryLog( uint32_t i )
1244 if( i == 0 ) return -31337;
1246 if( i & 0xffff0000 ) i_log += 16;
1247 if( i & 0xff00ff00 ) i_log += 8;
1248 if( i & 0xf0f0f0f0 ) i_log += 4;
1249 if( i & 0xcccccccc ) i_log += 2;
1250 if( i & 0xaaaaaaaa ) i_log += 1;
1255 /*****************************************************************************
1256 * MaskToShift: transform a color mask into right and left shifts
1257 *****************************************************************************
1258 * This function is used for obtaining color shifts from masks.
1259 *****************************************************************************/
1260 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1262 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
1266 *pi_left = *pi_right = 0;
1271 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
1272 i_high = i_mask + i_low; /* higher bit of the mask */
1274 /* Transform bits into an index */
1275 i_low = BinaryLog (i_low);
1276 i_high = BinaryLog (i_high);
1278 /* Update pointers and return */
1280 *pi_right = (8 - i_high + i_low);
1283 /*****************************************************************************
1284 * InitWindowSize: find the initial dimensions the video window should have.
1285 *****************************************************************************
1286 * This function will check the "width", "height" and "zoom" config options and
1287 * will calculate the size that the video window should have.
1288 *****************************************************************************/
1289 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
1290 unsigned *pi_height )
1293 int i_width, i_height;
1296 #define FP_FACTOR 1000 /* our fixed point factor */
1298 var_Get( p_vout, "align", &val );
1299 p_vout->i_alignment = val.i_int;
1301 var_Get( p_vout, "width", &val );
1302 i_width = val.i_int;
1303 var_Get( p_vout, "height", &val );
1304 i_height = val.i_int;
1305 var_Get( p_vout, "zoom", &val );
1306 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1308 if( i_width > 0 && i_height > 0)
1310 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1311 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1314 else if( i_width > 0 )
1316 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1317 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1318 p_vout->render.i_aspect / FP_FACTOR );
1321 else if( i_height > 0 )
1323 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1324 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1325 VOUT_ASPECT_FACTOR / FP_FACTOR );
1329 if( p_vout->render.i_height * p_vout->render.i_aspect
1330 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1332 *pi_width = (int)( p_vout->render.i_height * ll_zoom
1333 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1334 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1338 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1339 *pi_height = (int)( p_vout->render.i_width * ll_zoom
1340 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1346 /*****************************************************************************
1347 * vout_VarCallback: generic callback for intf variables
1348 *****************************************************************************/
1349 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1350 vlc_value_t old_value, vlc_value_t new_value,
1353 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1355 val.b_bool = VLC_TRUE;
1356 var_Set( p_vout, "intf-change", val );
1360 /*****************************************************************************
1361 * Helper thread for object variables callbacks.
1362 * Only used to avoid deadlocks when using the video embedded mode.
1363 *****************************************************************************/
1364 typedef struct suxor_thread_t
1367 input_thread_t *p_input;
1371 static void SuxorRestartVideoES( suxor_thread_t *p_this )
1375 vlc_thread_ready( p_this );
1377 /* Now restart current video stream */
1378 var_Get( p_this->p_input, "video-es", &val );
1379 if( val.i_int >= 0 )
1382 val_es.i_int = -VIDEO_ES;
1383 var_Set( p_this->p_input, "video-es", val_es );
1384 var_Set( p_this->p_input, "video-es", val );
1387 vlc_object_release( p_this->p_input );
1390 CloseHandle( p_this->thread_id );
1393 vlc_object_destroy( p_this );
1396 /*****************************************************************************
1397 * object variables callbacks: a bunch of object variables are used by the
1398 * interfaces to interact with the vout.
1399 *****************************************************************************/
1400 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1401 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1403 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1404 input_thread_t *p_input;
1407 char *psz_mode = newval.psz_string;
1408 char *psz_filter, *psz_deinterlace = NULL;
1410 var_Get( p_vout, "vout-filter", &val );
1411 psz_filter = val.psz_string;
1412 if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1414 if( !psz_mode || !*psz_mode )
1416 if( psz_deinterlace )
1418 char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1419 if( psz_src[0] == ':' ) psz_src++;
1420 memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1423 else if( !psz_deinterlace )
1425 psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1426 sizeof(":deinterlace") );
1427 if( psz_filter && *psz_filter ) strcat( psz_filter, ":" );
1428 strcat( psz_filter, "deinterlace" );
1431 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1433 if( !p_input ) return VLC_EGENERIC;
1435 if( psz_mode && *psz_mode )
1437 /* Modify input as well because the vout might have to be restarted */
1438 val.psz_string = psz_mode;
1439 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1440 var_Set( p_input, "deinterlace-mode", val );
1442 vlc_object_release( p_input );
1444 val.b_bool = VLC_TRUE;
1445 var_Set( p_vout, "intf-change", val );
1447 val.psz_string = psz_filter;
1448 var_Set( p_vout, "vout-filter", val );
1449 if( psz_filter ) free( psz_filter );
1454 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1455 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1457 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1458 input_thread_t *p_input;
1461 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1465 msg_Err( p_vout, "Input not found" );
1466 return( VLC_EGENERIC );
1469 val.b_bool = VLC_TRUE;
1470 var_Set( p_vout, "intf-change", val );
1472 /* Modify input as well because the vout might have to be restarted */
1473 val.psz_string = newval.psz_string;
1474 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1476 var_Set( p_input, "vout-filter", val );
1478 /* Now restart current video stream */
1479 var_Get( p_input, "video-es", &val );
1480 if( val.i_int >= 0 )
1482 suxor_thread_t *p_suxor =
1483 vlc_object_create( p_vout, sizeof(suxor_thread_t) );
1484 p_suxor->p_input = p_input;
1485 p_vout->b_filter_change = VLC_TRUE;
1486 vlc_object_yield( p_input );
1487 vlc_thread_create( p_suxor, "suxor", SuxorRestartVideoES,
1488 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1491 vlc_object_release( p_input );