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-2007 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 *****************************************************************************/
36 #include <vlc_common.h>
38 #include <stdlib.h> /* free() */
42 #ifdef HAVE_SYS_TIMES_H
43 # include <sys/times.h>
48 #include <vlc_filter.h>
52 #if defined( __APPLE__ )
53 /* Include darwin_specific.h here if needed */
56 /** FIXME This is quite ugly but needed while we don't have counters
58 //#include "input/input_internal.h"
61 #include <vlc_input.h>
62 #include "vout_pictures.h"
63 #include "vout_internal.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int InitThread ( vout_thread_t * );
69 static void* RunThread ( void * );
70 static void ErrorThread ( vout_thread_t * );
71 static void CleanThread ( vout_thread_t * );
72 static void EndThread ( vout_thread_t * );
74 static void AspectRatio ( int, int *, int * );
76 static void VideoFormatImportRgb( video_format_t *, const picture_heap_t * );
77 static void PictureHeapFixRgb( picture_heap_t * );
79 static void vout_Destructor ( vlc_object_t * p_this );
81 /* Object variables callbacks */
82 static int DeinterlaceCallback( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
84 static int FilterCallback( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
86 static int VideoFilter2Callback( vlc_object_t *, char const *,
87 vlc_value_t, vlc_value_t, void * );
89 /* From vout_intf.c */
90 int vout_Snapshot( vout_thread_t *, picture_t * );
92 /* Display media title in OSD */
93 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
95 /* Time during which the thread will sleep if it has nothing to
96 * display (in micro-seconds) */
97 #define VOUT_IDLE_SLEEP ((int)(0.020*CLOCK_FREQ))
99 /* Maximum lap of time allowed between the beginning of rendering and
100 * display. If, compared to the current date, the next image is too
101 * late, the thread will perform an idle loop. This time should be
102 * at least VOUT_IDLE_SLEEP plus the time required to render a few
103 * images, to avoid trashing of decoded images */
104 #define VOUT_DISPLAY_DELAY ((int)(0.200*CLOCK_FREQ))
106 /* Better be in advance when awakening than late... */
107 #define VOUT_MWAIT_TOLERANCE ((mtime_t)(0.020*CLOCK_FREQ))
109 /* Minimum number of direct pictures the video output will accept without
110 * creating additional pictures in system memory */
111 #ifdef OPTIMIZE_MEMORY
112 # define VOUT_MIN_DIRECT_PICTURES (VOUT_MAX_PICTURES/2)
114 # define VOUT_MIN_DIRECT_PICTURES (3*VOUT_MAX_PICTURES/4)
117 /*****************************************************************************
118 * Video Filter2 functions
119 *****************************************************************************/
120 static picture_t *video_new_buffer_filter( filter_t *p_filter )
122 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
123 picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
125 p_picture->i_status = READY_PICTURE;
130 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
132 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
134 vlc_mutex_lock( &p_vout->picture_lock );
135 vout_UsePictureLocked( p_vout, p_pic );
136 vlc_mutex_unlock( &p_vout->picture_lock );
139 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
141 p_filter->pf_vout_buffer_new = video_new_buffer_filter;
142 p_filter->pf_vout_buffer_del = video_del_buffer_filter;
143 p_filter->p_owner = p_data; /* p_vout */
147 /*****************************************************************************
148 * vout_Request: find a video output thread, create one, or destroy one.
149 *****************************************************************************
150 * This function looks for a video output thread matching the current
151 * properties. If not found, it spawns a new one.
152 *****************************************************************************/
153 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
154 video_format_t *p_fmt )
158 /* Video output is no longer used.
159 * TODO: support for reusing video outputs with proper _thread-safe_
160 * reference handling. */
162 vout_CloseAndRelease( p_vout );
166 /* If a video output was provided, lock it, otherwise look for one. */
169 vlc_object_hold( p_vout );
172 /* TODO: find a suitable unused video output */
174 /* If we now have a video output, check it has the right properties */
177 char *psz_filter_chain;
180 vlc_mutex_lock( &p_vout->change_lock );
182 /* We don't directly check for the "vout-filter" variable for obvious
183 * performance reasons. */
184 if( p_vout->p->b_filter_change )
186 var_Get( p_vout, "vout-filter", &val );
187 psz_filter_chain = val.psz_string;
189 if( psz_filter_chain && !*psz_filter_chain )
191 free( psz_filter_chain );
192 psz_filter_chain = NULL;
194 if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
196 free( p_vout->p->psz_filter_chain );
197 p_vout->p->psz_filter_chain = NULL;
200 if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
202 p_vout->p->b_filter_change = false;
205 free( psz_filter_chain );
208 if( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ||
209 p_vout->fmt_render.i_width != p_fmt->i_width ||
210 p_vout->fmt_render.i_height != p_fmt->i_height ||
211 p_vout->p->b_filter_change )
213 vlc_mutex_unlock( &p_vout->change_lock );
215 /* We are not interested in this format, close this vout */
216 vout_CloseAndRelease( p_vout );
217 vlc_object_release( p_vout );
222 /* This video output is cool! Hijack it. */
223 if( p_vout->fmt_render.i_aspect != p_fmt->i_aspect )
225 /* Correct aspect ratio on change
226 * FIXME factorize this code with other aspect ration related code */
227 unsigned int i_sar_num;
228 unsigned int i_sar_den;
229 unsigned int i_aspect;
231 i_aspect = p_fmt->i_aspect;
232 vlc_ureduce( &i_sar_num, &i_sar_den,
233 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
235 /* What's that, it does not seems to be used correcly everywhere
236 * beside the previous p_vout->fmt_render.i_aspect != p_fmt->i_aspect
237 * should be fixed to use it too then */
238 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
240 i_sar_num *= p_vout->i_par_den;
241 i_sar_den *= p_vout->i_par_num;
242 i_aspect = i_aspect * p_vout->i_par_den / p_vout->i_par_num;
246 if( i_sar_num > 0 && i_sar_den > 0 && i_aspect > 0 )
248 p_vout->fmt_in.i_sar_num = i_sar_num;
249 p_vout->fmt_in.i_sar_den = i_sar_den;
250 p_vout->fmt_in.i_aspect = i_aspect;
252 p_vout->fmt_render.i_sar_num = i_sar_num;
253 p_vout->fmt_render.i_sar_den = i_sar_den;
254 p_vout->fmt_render.i_aspect = i_aspect;
256 p_vout->render.i_aspect = i_aspect;
258 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
262 vlc_mutex_unlock( &p_vout->change_lock );
264 vlc_object_release( p_vout );
269 msg_Dbg( p_this, "reusing provided vout" );
271 spu_Attach( p_vout->p_spu, p_this, true );
273 vlc_object_detach( p_vout );
274 vlc_object_attach( p_vout, p_this );
280 msg_Dbg( p_this, "no usable vout present, spawning one" );
282 p_vout = vout_Create( p_this, p_fmt );
288 /*****************************************************************************
289 * vout_Create: creates a new video output thread
290 *****************************************************************************
291 * This function creates a new video output thread, and returns a pointer
292 * to its description. On error, it returns NULL.
293 *****************************************************************************/
294 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
296 vout_thread_t * p_vout; /* thread descriptor */
297 int i_index; /* loop variable */
298 vlc_value_t val, text;
300 unsigned int i_width = p_fmt->i_width;
301 unsigned int i_height = p_fmt->i_height;
302 vlc_fourcc_t i_chroma = p_fmt->i_chroma;
303 unsigned int i_aspect = p_fmt->i_aspect;
305 config_chain_t *p_cfg;
309 if( i_width <= 0 || i_height <= 0 || i_aspect <= 0 )
312 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
313 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
314 if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
317 /* Allocate descriptor */
318 static const char typename[] = "video output";
319 p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
325 p_vout->p = calloc( 1, sizeof(*p_vout->p) );
328 vlc_object_release( p_vout );
332 /* Initialize pictures - translation tables and functions
333 * will be initialized later in InitThread */
334 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
336 p_vout->p_picture[i_index].pf_lock = NULL;
337 p_vout->p_picture[i_index].pf_unlock = NULL;
338 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
339 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
340 p_vout->p_picture[i_index].b_slow = 0;
343 /* No images in the heap */
344 p_vout->i_heap_size = 0;
346 /* Initialize the rendering heap */
347 I_RENDERPICTURES = 0;
349 p_vout->fmt_render = *p_fmt; /* FIXME palette */
350 p_vout->fmt_in = *p_fmt; /* FIXME palette */
352 p_vout->render.i_width = i_width;
353 p_vout->render.i_height = i_height;
354 p_vout->render.i_chroma = i_chroma;
355 p_vout->render.i_aspect = i_aspect;
357 p_vout->render.i_rmask = p_fmt->i_rmask;
358 p_vout->render.i_gmask = p_fmt->i_gmask;
359 p_vout->render.i_bmask = p_fmt->i_bmask;
361 p_vout->render.i_last_used_pic = -1;
362 p_vout->render.b_allow_modify_pics = 1;
364 /* Zero the output heap */
365 I_OUTPUTPICTURES = 0;
366 p_vout->output.i_width = 0;
367 p_vout->output.i_height = 0;
368 p_vout->output.i_chroma = 0;
369 p_vout->output.i_aspect = 0;
371 p_vout->output.i_rmask = 0;
372 p_vout->output.i_gmask = 0;
373 p_vout->output.i_bmask = 0;
375 /* Initialize misc stuff */
376 p_vout->i_changes = 0;
378 p_vout->b_fullscreen = 0;
379 p_vout->i_alignment = 0;
380 p_vout->p->render_time = 10;
381 p_vout->p->c_fps_samples = 0;
382 p_vout->p->i_picture_lost = 0;
383 p_vout->p->i_picture_displayed = 0;
384 p_vout->p->b_filter_change = 0;
385 p_vout->p->b_paused = false;
386 p_vout->p->i_pause_date = 0;
387 p_vout->pf_control = NULL;
388 p_vout->p_window = NULL;
389 p_vout->p->i_par_num =
390 p_vout->p->i_par_den = 1;
391 p_vout->p->p_picture_displayed = NULL;
392 p_vout->p->i_picture_displayed_date = 0;
393 p_vout->p->b_picture_displayed = false;
394 p_vout->p->b_picture_empty = false;
396 /* Initialize locks */
397 vlc_mutex_init( &p_vout->picture_lock );
398 vlc_cond_init( &p_vout->p->picture_wait );
399 vlc_mutex_init( &p_vout->change_lock );
400 vlc_mutex_init( &p_vout->p->vfilter_lock );
402 /* Mouse coordinates */
403 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
404 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
405 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
406 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
407 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
409 /* Initialize subpicture unit */
410 p_vout->p_spu = spu_Create( p_vout );
411 spu_Attach( p_vout->p_spu, p_parent, true );
413 /* Attach the new object now so we can use var inheritance below */
414 vlc_object_attach( p_vout, p_parent );
416 spu_Init( p_vout->p_spu );
418 /* Take care of some "interface/control" related initialisations */
419 vout_IntfInit( p_vout );
421 /* If the parent is not a VOUT object, that means we are at the start of
422 * the video output pipe */
423 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
425 /* Look for the default filter configuration */
426 p_vout->p->psz_filter_chain =
427 var_CreateGetStringCommand( p_vout, "vout-filter" );
429 /* Apply video filter2 objects on the first vout */
431 var_CreateGetStringCommand( p_vout, "video-filter" );
435 /* continue the parent's filter chain */
438 /* Ugly hack to jump to our configuration chain */
439 p_vout->p->psz_filter_chain
440 = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
441 p_vout->p->psz_filter_chain
442 = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
443 config_ChainDestroy( p_cfg );
446 /* Create a video filter2 var ... but don't inherit values */
447 var_Create( p_vout, "video-filter",
448 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
449 p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
452 var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
453 p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
454 false, video_filter_buffer_allocation_init, NULL, p_vout );
456 /* Choose the video output module */
457 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
459 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
460 var_Get( p_vout, "vout", &val );
461 psz_parser = val.psz_string;
465 psz_parser = strdup( p_vout->p->psz_filter_chain );
466 p_vout->p->b_title_show = false;
469 /* Create the vout thread */
470 char* psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
473 p_vout->p_cfg = p_cfg;
474 p_vout->p_module = module_need( p_vout,
475 ( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain ) ?
476 "video filter" : "video output", psz_name, p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain );
479 vlc_object_set_destructor( p_vout, vout_Destructor );
481 if( p_vout->p_module == NULL )
483 msg_Err( p_vout, "no suitable vout module" );
484 vlc_object_release( p_vout );
488 /* Create a few object variables for interface interaction */
489 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
490 text.psz_string = _("Deinterlace");
491 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
492 val.psz_string = (char *)""; text.psz_string = _("Disable");
493 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
494 val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
495 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
496 val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
497 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
498 val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
499 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
500 val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
501 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
502 val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
503 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
504 val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
505 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
507 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
509 var_Set( p_vout, "deinterlace", val );
510 free( val.psz_string );
512 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
514 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
515 text.psz_string = _("Filters");
516 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
517 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
519 vlc_cond_init( &p_vout->p->change_wait );
520 if( vlc_clone( &p_vout->p->thread, RunThread, p_vout,
521 VLC_THREAD_PRIORITY_OUTPUT ) )
523 module_unneed( p_vout, p_vout->p_module );
524 p_vout->p_module = NULL;
525 vlc_object_release( p_vout );
529 vlc_mutex_lock( &p_vout->change_lock );
530 while( !p_vout->p->b_ready )
531 { /* We are (ab)using the same condition in opposite directions for
532 * b_ready and b_done. This works because of the strict ordering. */
533 assert( !p_vout->p->b_done );
534 vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
536 vlc_mutex_unlock( &p_vout->change_lock );
538 if( p_vout->b_error )
540 msg_Err( p_vout, "video output creation failed" );
541 vout_CloseAndRelease( p_vout );
548 /*****************************************************************************
549 * vout_Close: Close a vout created by vout_Create.
550 *****************************************************************************
551 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
552 * You should NEVER call it on vout not obtained through vout_Create
553 * (like with vout_Request or vlc_object_find.)
554 * You can use vout_CloseAndRelease() as a convenience method.
555 *****************************************************************************/
556 void vout_Close( vout_thread_t *p_vout )
560 vlc_mutex_lock( &p_vout->change_lock );
561 p_vout->p->b_done = true;
562 vlc_cond_signal( &p_vout->p->change_wait );
563 vlc_mutex_unlock( &p_vout->change_lock );
564 vlc_join( p_vout->p->thread, NULL );
565 module_unneed( p_vout, p_vout->p_module );
566 p_vout->p_module = NULL;
570 static void vout_Destructor( vlc_object_t * p_this )
572 vout_thread_t *p_vout = (vout_thread_t *)p_this;
574 /* Make sure the vout was stopped first */
575 assert( !p_vout->p_module );
578 spu_Destroy( p_vout->p_spu );
580 /* Destroy the locks */
581 vlc_cond_destroy( &p_vout->p->change_wait );
582 vlc_cond_destroy( &p_vout->p->picture_wait );
583 vlc_mutex_destroy( &p_vout->picture_lock );
584 vlc_mutex_destroy( &p_vout->change_lock );
585 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
587 free( p_vout->p->psz_filter_chain );
588 free( p_vout->p->psz_title );
590 config_ChainDestroy( p_vout->p_cfg );
595 vout_thread_t *p_another_vout;
597 /* This is a dirty hack mostly for Linux, where there is no way to get the
598 * GUI back if you closed it while playing video. This is solved in
599 * Mac OS X, where we have this novelty called menubar, that will always
600 * allow you access to the applications main functionality. They should try
601 * that on linux sometime. */
602 p_another_vout = vlc_object_find( p_this->p_libvlc,
603 VLC_OBJECT_VOUT, FIND_ANYWHERE );
604 if( p_another_vout == NULL )
605 var_SetBool( p_this->p_libvlc, "intf-show", true );
607 vlc_object_release( p_another_vout );
612 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
614 vlc_mutex_lock( &p_vout->change_lock );
616 assert( !p_vout->p->b_paused || !b_paused );
618 vlc_mutex_lock( &p_vout->picture_lock );
620 p_vout->p->i_picture_displayed_date = 0;
622 if( p_vout->p->b_paused )
624 const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
626 for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
628 picture_t *p_pic = PP_RENDERPICTURE[i_index];
630 if( p_pic->i_status == READY_PICTURE )
631 p_pic->date += i_duration;
633 vlc_cond_signal( &p_vout->p->picture_wait );
634 vlc_mutex_unlock( &p_vout->picture_lock );
636 spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
640 vlc_mutex_unlock( &p_vout->picture_lock );
642 p_vout->p->b_paused = b_paused;
643 p_vout->p->i_pause_date = i_date;
645 vlc_mutex_unlock( &p_vout->change_lock );
648 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
650 vlc_mutex_lock( &p_vout->change_lock );
652 *pi_displayed = p_vout->p->i_picture_displayed;
653 *pi_lost = p_vout->p->i_picture_lost;
655 p_vout->p->i_picture_displayed = 0;
656 p_vout->p->i_picture_lost = 0;
658 vlc_mutex_unlock( &p_vout->change_lock );
661 void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
663 vlc_mutex_lock( &p_vout->picture_lock );
664 p_vout->p->i_picture_displayed_date = 0;
665 for( int i = 0; i < p_vout->render.i_pictures; i++ )
667 picture_t *p_pic = p_vout->render.pp_picture[i];
669 if( p_pic->i_status == READY_PICTURE ||
670 p_pic->i_status == DISPLAYED_PICTURE )
672 /* We cannot change picture status if it is in READY_PICTURE state,
673 * Just make sure they won't be displayed */
674 if( p_pic->date > i_date )
675 p_pic->date = i_date;
678 vlc_cond_signal( &p_vout->p->picture_wait );
679 vlc_mutex_unlock( &p_vout->picture_lock );
682 void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
684 int i_pic, i_ready_pic;
686 vlc_mutex_lock( &p_vout->picture_lock );
688 for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
690 const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
692 if( p_pic->i_status == READY_PICTURE )
695 /* If we have at least 2 ready pictures, wait for the vout thread to
697 if( i_ready_pic >= 2 )
703 if( p_pic->i_status == DISPLAYED_PICTURE )
705 /* If at least one displayed picture is not referenced
706 * let vout free it */
707 if( p_pic->i_refcount == 0 )
711 if( i_pic < p_vout->render.i_pictures && !b_forced )
713 vlc_mutex_unlock( &p_vout->picture_lock );
717 /* Too many pictures are still referenced, there is probably a bug
718 * with the decoder */
720 msg_Err( p_vout, "pictures leaked, resetting the heap" );
722 /* Just free all the pictures */
723 for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
725 picture_t *p_pic = p_vout->render.pp_picture[i_pic];
727 msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
728 p_pic->i_refcount = 0;
730 switch( p_pic->i_status )
733 case DISPLAYED_PICTURE:
734 case RESERVED_PICTURE:
735 if( p_pic != p_vout->p->p_picture_displayed )
736 vout_UsePictureLocked( p_vout, p_pic );
740 vlc_cond_signal( &p_vout->p->picture_wait );
741 vlc_mutex_unlock( &p_vout->picture_lock );
743 void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
745 vlc_mutex_lock( &p_vout->picture_lock );
747 const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
749 p_vout->p->b_picture_displayed = false;
750 p_vout->p->b_picture_empty = false;
751 if( p_vout->p->p_picture_displayed )
753 p_vout->p->p_picture_displayed->date = 1;
754 vlc_cond_signal( &p_vout->p->picture_wait );
757 while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
758 vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->picture_lock );
760 *pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
762 /* TODO advance subpicture by the duration ... */
764 vlc_mutex_unlock( &p_vout->picture_lock );
767 void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
771 if( !config_GetInt( p_vout, "osd" ) )
774 vlc_mutex_lock( &p_vout->change_lock );
775 free( p_vout->p->psz_title );
776 p_vout->p->psz_title = strdup( psz_title );
777 vlc_mutex_unlock( &p_vout->change_lock );
780 /*****************************************************************************
781 * InitThread: initialize video output thread
782 *****************************************************************************
783 * This function is called from RunThread and performs the second step of the
784 * initialization. It returns 0 on success. Note that the thread's flag are not
785 * modified inside this function.
786 * XXX You have to enter it with change_lock taken.
787 *****************************************************************************/
788 static int ChromaCreate( vout_thread_t *p_vout );
789 static void ChromaDestroy( vout_thread_t *p_vout );
791 static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
793 if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
796 if( p_output->i_chroma != FOURCC_RV15 &&
797 p_output->i_chroma != FOURCC_RV16 &&
798 p_output->i_chroma != FOURCC_RV24 &&
799 p_output->i_chroma != FOURCC_RV32 )
802 return p_output->i_rmask == p_render->i_rmask &&
803 p_output->i_gmask == p_render->i_gmask &&
804 p_output->i_bmask == p_render->i_bmask;
807 static int InitThread( vout_thread_t *p_vout )
809 int i, i_aspect_x, i_aspect_y;
811 /* Initialize output method, it allocates direct buffers for us */
812 if( p_vout->pf_init( p_vout ) )
815 p_vout->p->p_picture_displayed = NULL;
817 if( !I_OUTPUTPICTURES )
819 msg_Err( p_vout, "plugin was unable to allocate at least "
820 "one direct buffer" );
821 p_vout->pf_end( p_vout );
825 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
827 msg_Err( p_vout, "plugin allocated too many direct buffers, "
828 "our internal buffers must have overflown." );
829 p_vout->pf_end( p_vout );
833 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
835 AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
837 msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
838 "chroma %4.4s, ar %i:%i, sar %i:%i",
839 p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
840 p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
841 p_vout->fmt_render.i_visible_width,
842 p_vout->fmt_render.i_visible_height,
843 (char*)&p_vout->fmt_render.i_chroma,
844 i_aspect_x, i_aspect_y,
845 p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
847 AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
849 msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
850 "chroma %4.4s, ar %i:%i, sar %i:%i",
851 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
852 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
853 p_vout->fmt_in.i_visible_width,
854 p_vout->fmt_in.i_visible_height,
855 (char*)&p_vout->fmt_in.i_chroma,
856 i_aspect_x, i_aspect_y,
857 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
859 if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
861 p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
862 p_vout->output.i_width;
863 p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
864 p_vout->output.i_height;
865 p_vout->fmt_out.i_x_offset = p_vout->fmt_out.i_y_offset = 0;
867 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
868 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
870 if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
872 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
873 p_vout->fmt_out.i_height;
874 p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
875 p_vout->fmt_out.i_width;
878 vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
879 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
881 AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
883 msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
884 "chroma %4.4s, ar %i:%i, sar %i:%i",
885 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
886 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
887 p_vout->fmt_out.i_visible_width,
888 p_vout->fmt_out.i_visible_height,
889 (char*)&p_vout->fmt_out.i_chroma,
890 i_aspect_x, i_aspect_y,
891 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
893 /* FIXME removed the need of both fmt_* and heap infos */
894 /* Calculate shifts from system-updated masks */
895 PictureHeapFixRgb( &p_vout->render );
896 VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
898 PictureHeapFixRgb( &p_vout->output );
899 VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
901 /* Check whether we managed to create direct buffers similar to
902 * the render buffers, ie same size and chroma */
903 if( ( p_vout->output.i_width == p_vout->render.i_width )
904 && ( p_vout->output.i_height == p_vout->render.i_height )
905 && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
907 /* Cool ! We have direct buffers, we can ask the decoder to
908 * directly decode into them ! Map the first render buffers to
909 * the first direct buffers, but keep the first direct buffer
910 * for memcpy operations */
911 p_vout->p->b_direct = true;
913 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
915 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
916 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
917 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
919 /* We have enough direct buffers so there's no need to
920 * try to use system memory buffers. */
923 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
927 msg_Dbg( p_vout, "direct render, mapping "
928 "render pictures 0-%i to system pictures 1-%i",
929 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
933 /* Rats... Something is wrong here, we could not find an output
934 * plugin able to directly render what we decode. See if we can
935 * find a chroma plugin to do the conversion */
936 p_vout->p->b_direct = false;
938 if( ChromaCreate( p_vout ) )
940 p_vout->pf_end( p_vout );
944 msg_Dbg( p_vout, "indirect render, mapping "
945 "render pictures 0-%i to system pictures %i-%i",
946 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
947 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
949 /* Append render buffers after the direct buffers */
950 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
952 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
955 /* Check if we have enough render pictures */
956 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
961 /* Link pictures back to their heap */
962 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
964 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
967 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
969 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
975 /*****************************************************************************
976 * RunThread: video output thread
977 *****************************************************************************
978 * Video output thread. This function does only returns when the thread is
979 * terminated. It handles the pictures arriving in the video heap and the
980 * display device events.
981 *****************************************************************************/
982 static void* RunThread( void *p_this )
984 vout_thread_t *p_vout = p_this;
985 int i_idle_loops = 0; /* loops without displaying a picture */
992 vlc_mutex_lock( &p_vout->change_lock );
993 p_vout->b_error = InitThread( p_vout );
995 b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
997 /* signal the creation of the vout */
998 p_vout->p->b_ready = true;
999 vlc_cond_signal( &p_vout->p->change_wait );
1001 if( p_vout->b_error )
1003 EndThread( p_vout );
1004 vlc_mutex_unlock( &p_vout->change_lock );
1009 * Main loop - it is not executed if an error occurred during
1012 while( !p_vout->p->b_done && !p_vout->b_error )
1014 /* Initialize loop variables */
1015 const mtime_t current_date = mdate();
1016 picture_t *p_picture;
1017 picture_t *p_filtered_picture;
1018 mtime_t display_date;
1019 picture_t *p_directbuffer;
1022 if( p_vout->p->b_title_show && p_vout->p->psz_title )
1023 DisplayTitleOnOSD( p_vout );
1025 vlc_mutex_lock( &p_vout->picture_lock );
1027 /* Look for the earliest picture but after the last displayed one */
1028 picture_t *p_last = p_vout->p->p_picture_displayed;;
1031 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
1033 picture_t *p_pic = PP_RENDERPICTURE[i_index];
1035 if( p_pic->i_status != READY_PICTURE )
1038 if( p_vout->p->b_paused && p_last && p_last->date > 1 )
1041 if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
1043 /* Drop old picture */
1044 vout_UsePictureLocked( p_vout, p_pic );
1046 else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
1047 p_pic->date < current_date + p_vout->p->render_time &&
1050 /* Picture is late: it will be destroyed and the thread
1051 * will directly choose the next picture */
1052 vout_UsePictureLocked( p_vout, p_pic );
1053 p_vout->p->i_picture_lost++;
1055 msg_Warn( p_vout, "late picture skipped (%"PRId64")",
1056 current_date - p_pic->date );
1058 else if( ( !p_last || p_last->date < p_pic->date ) &&
1059 ( p_picture == NULL || p_pic->date < p_picture->date ) )
1068 if( !p_vout->p->b_picture_empty )
1070 p_vout->p->b_picture_empty = true;
1071 vlc_cond_signal( &p_vout->p->picture_wait );
1078 display_date = p_picture->date;
1080 /* If we found better than the last picture, destroy it */
1081 if( p_last && p_picture != p_last )
1083 vout_UsePictureLocked( p_vout, p_last );
1084 p_vout->p->p_picture_displayed = p_last = NULL;
1087 /* Compute FPS rate */
1088 p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1090 if( !p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1092 /* A picture is ready to be rendered, but its rendering date
1093 * is far from the current one so the thread will perform an
1094 * empty loop as if no picture were found. The picture state
1099 else if( p_picture == p_last )
1101 /* We are asked to repeat the previous picture, but we first
1102 * wait for a couple of idle loops */
1103 if( i_idle_loops < 4 )
1110 /* We set the display date to something high, otherwise
1111 * we'll have lots of problems with late pictures */
1112 display_date = current_date + p_vout->p->render_time;
1115 else if( p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1117 display_date = current_date + VOUT_DISPLAY_DELAY;
1122 if( p_picture->date > 1 )
1124 p_vout->p->i_picture_displayed_date = p_picture->date;
1125 if( p_picture != p_last && !p_vout->p->b_picture_displayed )
1127 p_vout->p->b_picture_displayed = true;
1128 vlc_cond_signal( &p_vout->p->picture_wait );
1131 p_vout->p->p_picture_displayed = p_picture;
1134 vlc_mutex_unlock( &p_vout->picture_lock );
1136 if( p_picture == NULL )
1139 p_filtered_picture = NULL;
1141 p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
1144 /* FIXME it is ugly that b_snapshot is not locked but I do not
1145 * know which lock to use (here and in the snapshot callback) */
1146 const bool b_snapshot = p_vout->p->b_snapshot && p_picture != NULL;
1149 * Check for subpictures to display
1151 subpicture_t *p_subpic = NULL;
1152 if( display_date > 0 )
1153 p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
1154 p_vout->p->b_paused, b_snapshot );
1159 p_vout->p->i_picture_displayed++;
1160 p_directbuffer = vout_RenderPicture( p_vout, p_filtered_picture,
1161 p_subpic, p_vout->p->b_paused );
1164 * Take a snapshot if requested
1166 if( p_directbuffer && b_snapshot )
1168 /* FIXME lock (see b_snapshot) */
1169 p_vout->p->b_snapshot = false;
1171 vout_Snapshot( p_vout, p_directbuffer );
1175 * Call the plugin-specific rendering method if there is one
1177 if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
1179 /* Render the direct buffer returned by vout_RenderPicture */
1180 p_vout->pf_render( p_vout, p_directbuffer );
1186 if( display_date != 0 && p_directbuffer != NULL )
1188 mtime_t current_render_time = mdate() - current_date;
1189 /* if render time is very large we don't include it in the mean */
1190 if( current_render_time < p_vout->p->render_time +
1191 VOUT_DISPLAY_DELAY )
1193 /* Store render time using a sliding mean weighting to
1194 * current value in a 3 to 1 ratio*/
1195 p_vout->p->render_time *= 3;
1196 p_vout->p->render_time += current_render_time;
1197 p_vout->p->render_time >>= 2;
1201 /* Give back change lock */
1202 vlc_mutex_unlock( &p_vout->change_lock );
1204 /* Sleep a while or until a given date */
1205 if( display_date != 0 )
1207 /* If there are *vout* filters in the chain, better give them the picture
1209 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
1211 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1216 /* Wait until a frame is being sent or a spurious wakeup (not a problem here) */
1217 vlc_mutex_lock( &p_vout->picture_lock );
1218 vlc_cond_timedwait( &p_vout->p->picture_wait, &p_vout->picture_lock, current_date + VOUT_IDLE_SLEEP );
1219 vlc_mutex_unlock( &p_vout->picture_lock );
1222 /* On awakening, take back lock and send immediately picture
1224 /* Note: p_vout->p->b_done could be true here and now */
1225 vlc_mutex_lock( &p_vout->change_lock );
1228 * Display the previously rendered picture
1230 if( p_filtered_picture != NULL && p_directbuffer != NULL )
1232 /* Display the direct buffer returned by vout_RenderPicture */
1233 if( p_vout->pf_display )
1234 p_vout->pf_display( p_vout, p_directbuffer );
1236 /* Tell the vout this was the last picture and that it does not
1237 * need to be forced anymore. */
1238 p_picture->b_force = false;
1241 /* Drop the filtered picture if created by video filters */
1242 if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
1244 vlc_mutex_lock( &p_vout->picture_lock );
1245 vout_UsePictureLocked( p_vout, p_filtered_picture );
1246 vlc_mutex_unlock( &p_vout->picture_lock );
1249 if( p_picture != NULL )
1251 /* Reinitialize idle loop count */
1256 * Check events and manage thread
1258 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1260 /* A fatal error occurred, and the thread must terminate
1261 * immediately, without displaying anything - setting b_error to 1
1262 * causes the immediate end of the main while() loop. */
1264 p_vout->b_error = 1;
1268 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1270 /* this must only happen when the vout plugin is incapable of
1271 * rescaling the picture itself. In this case we need to destroy
1272 * the current picture buffers and recreate new ones with the right
1276 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1278 assert( !p_vout->p->b_direct );
1280 ChromaDestroy( p_vout );
1282 vlc_mutex_lock( &p_vout->picture_lock );
1284 p_vout->pf_end( p_vout );
1286 p_vout->p->p_picture_displayed = NULL;
1287 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1288 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1289 vlc_cond_signal( &p_vout->p->picture_wait );
1291 I_OUTPUTPICTURES = 0;
1293 if( p_vout->pf_init( p_vout ) )
1295 msg_Err( p_vout, "cannot resize display" );
1296 /* FIXME: pf_end will be called again in EndThread() */
1297 p_vout->b_error = 1;
1300 vlc_mutex_unlock( &p_vout->picture_lock );
1302 /* Need to reinitialise the chroma plugin. Since we might need
1303 * resizing too and it's not sure that we already had it,
1304 * recreate the chroma plugin chain from scratch. */
1306 if( ChromaCreate( p_vout ) )
1308 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1309 p_vout->b_error = 1;
1311 if( p_vout->b_error )
1315 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1317 /* This happens when the picture buffers need to be recreated.
1318 * This is useful on multimonitor displays for instance.
1320 * Warning: This only works when the vout creates only 1 picture
1322 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1324 if( !p_vout->p->b_direct )
1325 ChromaDestroy( p_vout );
1327 vlc_mutex_lock( &p_vout->picture_lock );
1329 p_vout->pf_end( p_vout );
1331 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1333 p_vout->b_error = InitThread( p_vout );
1334 if( p_vout->b_error )
1335 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed\n" );
1337 vlc_cond_signal( &p_vout->p->picture_wait );
1338 vlc_mutex_unlock( &p_vout->picture_lock );
1340 if( p_vout->b_error )
1344 /* Check for "video filter2" changes */
1345 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1346 if( p_vout->p->psz_vf2 )
1350 es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1351 fmt.video = p_vout->fmt_render;
1352 filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
1354 if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
1355 p_vout->p->psz_vf2 ) < 0 )
1356 msg_Err( p_vout, "Video filter chain creation failed" );
1358 free( p_vout->p->psz_vf2 );
1359 p_vout->p->psz_vf2 = NULL;
1361 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1365 * Error loop - wait until the thread destruction is requested
1367 if( p_vout->b_error )
1368 ErrorThread( p_vout );
1371 CleanThread( p_vout );
1372 EndThread( p_vout );
1373 vlc_mutex_unlock( &p_vout->change_lock );
1378 /*****************************************************************************
1379 * ErrorThread: RunThread() error loop
1380 *****************************************************************************
1381 * This function is called when an error occurred during thread main's loop.
1382 * The thread can still receive feed, but must be ready to terminate as soon
1384 *****************************************************************************/
1385 static void ErrorThread( vout_thread_t *p_vout )
1387 /* Wait until a `close' order */
1388 while( !p_vout->p->b_done )
1389 vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
1392 /*****************************************************************************
1393 * CleanThread: clean up after InitThread
1394 *****************************************************************************
1395 * This function is called after a sucessful
1396 * initialization. It frees all resources allocated by InitThread.
1397 * XXX You have to enter it with change_lock taken.
1398 *****************************************************************************/
1399 static void CleanThread( vout_thread_t *p_vout )
1401 int i_index; /* index in heap */
1403 if( !p_vout->p->b_direct )
1404 ChromaDestroy( p_vout );
1406 /* Destroy all remaining pictures */
1407 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1409 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1411 free( p_vout->p_picture[i_index].p_data_orig );
1415 /* Destroy translation tables */
1416 if( !p_vout->b_error )
1417 p_vout->pf_end( p_vout );
1420 /*****************************************************************************
1421 * EndThread: thread destruction
1422 *****************************************************************************
1423 * This function is called when the thread ends.
1424 * It frees all resources not allocated by InitThread.
1425 * XXX You have to enter it with change_lock taken.
1426 *****************************************************************************/
1427 static void EndThread( vout_thread_t *p_vout )
1431 struct tms cpu_usage;
1432 times( &cpu_usage );
1434 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1435 cpu_usage.tms_utime, cpu_usage.tms_stime );
1439 /* FIXME does that function *really* need to be called inside the thread ? */
1441 /* Detach subpicture unit from both input and vout */
1442 spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1443 vlc_object_detach( p_vout->p_spu );
1445 /* Destroy the video filters2 */
1446 filter_chain_Delete( p_vout->p->p_vf2_chain );
1449 /* Thread helpers */
1450 static picture_t *ChromaGetPicture( filter_t *p_filter )
1452 picture_t *p_pic = (picture_t *)p_filter->p_owner;
1453 p_filter->p_owner = NULL;
1457 static int ChromaCreate( vout_thread_t *p_vout )
1459 static const char typename[] = "chroma";
1462 /* Choose the best module */
1463 p_chroma = p_vout->p->p_chroma =
1464 vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
1467 vlc_object_attach( p_chroma, p_vout );
1469 /* TODO: Set the fmt_in and fmt_out stuff here */
1470 p_chroma->fmt_in.video = p_vout->fmt_render;
1471 p_chroma->fmt_out.video = p_vout->fmt_out;
1472 VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
1473 VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
1475 p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, false );
1477 if( p_chroma->p_module == NULL )
1479 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1480 (char*)&p_vout->render.i_chroma,
1481 (char*)&p_vout->output.i_chroma,
1482 p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1483 p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1486 vlc_object_release( p_vout->p->p_chroma );
1487 p_vout->p->p_chroma = NULL;
1489 return VLC_EGENERIC;
1491 p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1495 static void ChromaDestroy( vout_thread_t *p_vout )
1497 assert( !p_vout->p->b_direct );
1499 if( !p_vout->p->p_chroma )
1502 module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
1503 vlc_object_release( p_vout->p->p_chroma );
1504 p_vout->p->p_chroma = NULL;
1507 /* following functions are local */
1508 static int ReduceHeight( int i_ratio )
1510 int i_dummy = VOUT_ASPECT_FACTOR;
1518 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1519 while( !(i_ratio & 1) && !(i_dummy & 1) )
1526 while( !(i_ratio % 3) && !(i_dummy % 3) )
1533 while( !(i_ratio % 5) && !(i_dummy % 5) )
1543 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1545 unsigned int i_pgcd = ReduceHeight( i_aspect );
1546 *i_aspect_x = i_aspect / i_pgcd;
1547 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1551 * This function copies all RGB informations from a picture_heap_t into
1554 static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
1556 p_fmt->i_rmask = p_heap->i_rmask;
1557 p_fmt->i_gmask = p_heap->i_gmask;
1558 p_fmt->i_bmask = p_heap->i_bmask;
1559 p_fmt->i_rrshift = p_heap->i_rrshift;
1560 p_fmt->i_lrshift = p_heap->i_lrshift;
1561 p_fmt->i_rgshift = p_heap->i_rgshift;
1562 p_fmt->i_lgshift = p_heap->i_lgshift;
1563 p_fmt->i_rbshift = p_heap->i_rbshift;
1564 p_fmt->i_lbshift = p_heap->i_lbshift;
1568 * This funtion copes all RGB informations from a video_format_t into
1571 static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
1573 p_heap->i_rmask = p_fmt->i_rmask;
1574 p_heap->i_gmask = p_fmt->i_gmask;
1575 p_heap->i_bmask = p_fmt->i_bmask;
1576 p_heap->i_rrshift = p_fmt->i_rrshift;
1577 p_heap->i_lrshift = p_fmt->i_lrshift;
1578 p_heap->i_rgshift = p_fmt->i_rgshift;
1579 p_heap->i_lgshift = p_fmt->i_lgshift;
1580 p_heap->i_rbshift = p_fmt->i_rbshift;
1581 p_heap->i_lbshift = p_fmt->i_lbshift;
1585 * This function computes rgb shifts from masks
1587 static void PictureHeapFixRgb( picture_heap_t *p_heap )
1592 fmt.i_chroma = p_heap->i_chroma;
1593 VideoFormatImportRgb( &fmt, p_heap );
1596 video_format_FixRgb( &fmt );
1598 VideoFormatExportRgb( &fmt, p_heap );
1601 /*****************************************************************************
1602 * object variables callbacks: a bunch of object variables are used by the
1603 * interfaces to interact with the vout.
1604 *****************************************************************************/
1605 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1606 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1608 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1609 input_thread_t *p_input;
1612 char *psz_mode = newval.psz_string;
1613 char *psz_filter, *psz_deinterlace = NULL;
1614 (void)psz_cmd; (void)oldval; (void)p_data;
1616 var_Get( p_vout, "vout-filter", &val );
1617 psz_filter = val.psz_string;
1618 if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1620 if( !psz_mode || !*psz_mode )
1622 if( psz_deinterlace )
1624 char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1625 if( psz_src[0] == ':' ) psz_src++;
1626 memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1629 else if( !psz_deinterlace )
1631 psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1632 sizeof(":deinterlace") );
1636 strcat( psz_filter, ":" );
1637 strcat( psz_filter, "deinterlace" );
1641 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1646 return VLC_EGENERIC;
1649 if( psz_mode && *psz_mode )
1651 /* Modify input as well because the vout might have to be restarted */
1652 val.psz_string = psz_mode;
1653 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1654 var_Set( p_input, "deinterlace-mode", val );
1656 vlc_object_release( p_input );
1659 var_Set( p_vout, "intf-change", val );
1661 val.psz_string = psz_filter;
1662 var_Set( p_vout, "vout-filter", val );
1668 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1669 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1671 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1672 input_thread_t *p_input;
1674 (void)psz_cmd; (void)oldval; (void)p_data;
1676 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1680 msg_Err( p_vout, "Input not found" );
1681 return( VLC_EGENERIC );
1685 var_Set( p_vout, "intf-change", val );
1687 /* Modify input as well because the vout might have to be restarted */
1688 val.psz_string = newval.psz_string;
1689 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1691 var_Set( p_input, "vout-filter", val );
1693 /* Now restart current video stream */
1694 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1695 vlc_object_release( p_input );
1700 /*****************************************************************************
1701 * Video Filter2 stuff
1702 *****************************************************************************/
1703 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1704 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1706 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1707 (void)psz_cmd; (void)oldval; (void)p_data;
1709 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1710 p_vout->p->psz_vf2 = strdup( newval.psz_string );
1711 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1716 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1718 const mtime_t i_start = mdate();
1719 const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->i_title_timeout;
1721 vlc_assert_locked( &p_vout->change_lock );
1723 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1724 p_vout->p->psz_title, NULL,
1725 p_vout->p->i_title_position,
1726 30 + p_vout->fmt_in.i_width
1727 - p_vout->fmt_in.i_visible_width
1728 - p_vout->fmt_in.i_x_offset,
1729 20 + p_vout->fmt_in.i_y_offset,
1732 free( p_vout->p->psz_title );
1734 p_vout->p->psz_title = NULL;